IoT – Arduino – NodeMCU ESP-12 ESP8266

Written by: techguru

By: @IoT_Live

A few days ago, in the midst of my searches to try to solve some problems with the Modulo ESP8266, I found some forums that mentioned the NodemCU based on the LUA programming language. I call so much attention, buy some to test its functionality. Were really cheap (USD4,73 in Aliexpress)

I take some days working with, and I've got very good answers because with the integrated WIFI and availability of 11 GPIO ports and one analog, I have managed to set very easily features that years ago we required many hours of work and expensive hardware and considerable size.

The NodeMCU ESP-12 is even possible to program from the Arduino IDE achieving use a tool that already was familiar to me. To do this , use the Arduino IDE version 1.6.7 downloaded from the official Arduino URLhttp://www.arduino.cc and add in the preferences option, manager Additional URLs for ESP8266 cards corresponding to http: // www.arduino.cc



the advantage is that if you are already working with cards from other manufacturers, can use multiple URLs.

Step 1:

restart the Arduino IDE, we will have at our disposal Modules ESP8266 to program them as if it were another module Arduino. It is important to select the other features that are activated for these modules in the image, you can see the values I am using the ESP12 plate to use the pins, it was necessary to perform some tests to obtain equivalencies pins, depending on how youare using, since physical distribution are different. pins: NODEMCU ESP12 LUA Description Arduino 20 - TX GPIO1 Digital 1 28 - D4 GPIO2 Digital 2 21 - RX GPIO3 Digital 3 30 - D2 GPIO4 Digital 4 Input 31 - D1 GPIO5 digital converter 24 - D6 GPIO12 Digital 12 23 - D7 GPIO13 Digital 13 25 - D5 GPIO14 Digital 14 22 - D8 Digital GPIO15 15 32 - D0 GPIO16 - USER - WAKE Digital 16 01 - A0 ADC0 - Analog TOUT A0 The basic examples that come with Arduino, work without any inconvenience. In the following example, use the following materials. Plate: NodeMCU ESP-12 ESP8266 Sensor DHT22Arduino IDE 1.6.7
 

Step 2: Programa arduino

 

#include “ESP8266WiFi.h”

#include “WiFiClient.h”

#include “ESP8266WebServer.h”

#include “ESP8266mDNS.h”

#include "DHT.h" 

#define DHTPIN 4 
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE); 

const char* ssid = "UNE_7586"; 
const char* password = "12345678"; 

ESP8266WebServer server(80); 

const int led = 13; 

void handleRoot() { 

String message = "ESP8266 Server Nodemcunn"; 

// Reading temperature or humidity takes about 250 milliseconds! 
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) 
float h = dht.readHumidity(); 
// Read temperature as Celsius (the default) 
float t = dht.readTemperature(); 
// Read temperature as Fahrenheit (isFahrenheit = true) 
float f = dht.readTemperature(true); 

// Check if any reads failed and exit early (to try again). 
if (isnan(h) || isnan(t) || isnan(f)) { 
Serial.println("Failed to read from DHT sensor!"); 
return; 


// Compute heat index in Fahrenheit (the default) 
float hif = dht.computeHeatIndex(f, h); 
// Compute heat index in Celsius (isFahreheit = false) 
float hic = dht.computeHeatIndex(t, h, false); 

message += "Humidity: "; 
message += h; 
message += " %t"; 
message += "Temperature: "; 
message += t; 
message += " *C "; 
message += f; 
message += " *Ft"; 
message += "Heat index: "; 
message += hic; 
message += " *C "; 
message += hif; 
message += " *F n"; 

digitalWrite(led, 1); 
server.send(200, "text/plain", message); 
digitalWrite(led, 0); 



void handleNotFound(){ 
digitalWrite(led, 1); 
String message = "File Not Foundnn"; 
message += "URI: "; 
message += server.uri(); 
message += "nMethod: "; 
message += (server.method() == HTTP_GET)?"GET":"POST"; 
message += "nArguments: "; 
message += server.args(); 
message += "n"; 
for (uint8_t i=0; i message += " " + server.argName(i) + ": " + server.arg(i) + "n"; 

server.send(404, "text/plain", message); 
digitalWrite(led, 0); 


void setup(void){ 

pinMode(led, OUTPUT); 
digitalWrite(led, 0); 
Serial.begin(115200); 
dht.begin(); 
WiFi.begin(ssid, password); 
Serial.println(""); 

// Wait for connection 
while (WiFi.status() != WL_CONNECTED) { 
delay(500); 
Serial.print("."); 

Serial.println(""); 
Serial.print("Connected to "); 
Serial.println(ssid); 
Serial.print("IP address: "); 
Serial.println(WiFi.localIP()); 

if (MDNS.begin("esp8266")) { 
Serial.println("MDNS responder started"); 


server.on("/", handleRoot); 

server.on("/inline", [](){ 
server.send(200, "text/plain", "this works as well"); 
}); 

server.onNotFound(handleNotFound); 

server.begin(); 
Serial.println("HTTP server started"); 


void loop(void){ 
server.handleClient(); 
}

Leave a Reply