Exercise 27 - WIFI module (NODECMU ESP866-ESP12)
1. INTRODUCTION
WiFi or Wi-Fi is a system of wireless connection between devices such as televisions, computers, smart phones… These devices can connect to internet through wireless access point.
Wi-Fi is a brand from Wi-Fi Alliance and it is invented by Interbrand who was hired by WECA (previous name of Wi-Fi Alliance).
WECA is the union of some companies, which were
finding a standard method of wireless communication. By year 2000, WECA could
guarantee to the final user that a device with Wi-Fi mark can works with
another one independently of the manufacturer.
Wi-Fi devices can be classified on two groups:
· Distribution devices: These devices transmit Wi-Fi signal and they allow other devices to connect to them to access WLAN (Access points, Repeaters, Routers…).
· Terminal devices: This definition is associated to a PCI, PCMCIA and USB WiFi cards, which are inside of devices like computers.
These cards allow the devices, where are assembled, connecting to Distribution devices.
On this exercise, we are going to connect nodemcu esp866-esp12 module. It works as Terminal device that is to say, it connect to a router to transmit data through it. You can use this module of three forms.
· Setting it with AT commands.
· Connect it to other microcontroller to use as a peripheral.
· Program it without other microcontroller.
On this exercise, the third form is chosen.
2. HOW TO CONNECT?.
Materials:
· NODEMCU ESP866-ESP12 board
· Protoboard
· Some wire Jumpers
· Temperature and relative humidity sensor
· 10K ohms resistor.
On this exercise Arduino board not is necessary. To simplify the connections you can attach the Wi-Fi module to a protoboard like in the schematic.
On the other hand, the temperature and humidity sensor can be connected like in
CONNECTING_A_TEMPERATURE_AND_RELATIVE_HUMIDITY_SENSOR exercise but in this
case, data terminal go connected to D1 on Wi-Fi board. This is a board
schematic where you can see the pins equivalence with the ESP866-ESP12 attached to the nodemcu:
D1 have an associated pin. This pin is GPIO5, because this in programming you can see this line: #define DHTPIN 5
WARNING
Furthermore, to use this board it is necessary to install it on Arduino IDE. To install it you have to add this link to File\Preferences on Arduino IDE.
http://arduino.esp8266.com/versions/2.3.0/package_esp8266com_index.json
On the other hand, you must to install CP2102 driver. You can download it from here.
EXERCISE_A |
3. PROGRAMMING
EXERCISE_A
/*Code based on http://www.instructables.com/id/Arduino-NodeMCU-ESP-12-ESP8266 */
First, include the necessary libraries, define pins and declare variables. You have to care for capital letters on ssid and password.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include "DHT.h"
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "AIJU_Tic2";
const char* password = "Aiju_213";
ESP8266WebServer server(80);
const int led = 13;
//This function control the server configuration.
void handleRoot() {
String message = "EDUTRONIX Nodemecu Server\n\n";
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);
delay(1000);/*This sensor is very slow. It need about 250 milliseconds
to read temperature or humidity. The delay gives to the sensor more time
to do the meassure.*/
// 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 += "\t";
message += "Humidity: ";
message += h;
message += " %\t\n\n";
message += "\t";
message += "Temperature: ";
message += t;
message += " *C ";
message += f;
message += " *F\t\n\n";
message += "\t";
message += "Heat index: ";
message += hic;
message += " *C ";
message += hif;
message += " *F \n";
digitalWrite(led, 1);
//Send the joined message to the server.
server.send(200, "text/plain", message);
digitalWrite(led, 0);
}
//If the server is not available prints an error.
void handleNotFound() {
digitalWrite(led, 1);
String message = "File Not Found\n\n";
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; 8) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
On the setup, serial communication and server are started. To start the server first tries to connect to Wi-Fi network and then print the successful connection messages with the connected IP.
void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.begin(ssid, password);
dht.begin();
Serial.println("");
// Wait for connection
//The program prints "." until the server is connected.
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 response received.
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");
}
On loop the number of clients is managed.
void loop(void) {
//Internal function from esp-8266 library Wifi
server.handleClient();
}