The Internet of Things (IoT) is a system of interrelated computing devices, mechanical and digital machines, objects, animals or people that are provided with unique identifiers and the ability to transfer data over a network without requiring human-to-human or human-to-computer interaction. Wireless Communication plays a vital role in the development of technology to an extent of data communication at a large scale. ESP8266 is a module used as a data transfer device to analyze and generate data statistics.
About Module.
ESP8266-01 is a very low-cost WiFi enabled chip. But it has very limited I/O and only has 8 pins. [VCC,GND, RST ( Reset Pin ),RX,TX,GPIO0,GPIO2].
DHT 11 is the sensor used to sense temperature and humidity over a various area. It has 3 pin configuration of VCC, GND, OUT.
Pin configuration
Here we connect DHT11 directly to Arduino and ESP 8266 ESP - 01 separately. Pins connection are made as given in schematic. We connect CHPD and VCC to 3.3V of ESP8266 to the board as it cannot hold 5V. TX as orange connected to 2 port and RX as yellow connected to 3 port.DHT output value is connected to 7 port.
Description
As when the sensor gives the output values of temperature and humidity which are displayed on the Serial console of the server. Here we will generate a channel on Thinkspeak. Sign up and generate an account on Thinkspeak and create a new channel using 2 fields as temperature and humidity. The sensor will give the output and the module will send the data
to the server of given channel and plot a chart.
Code
#include <SoftwareSerial.h>
#include <Wire.h>
#include <stdlib.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
#include<LiquidCrystal.h>
SoftwareSerial esp8266Module(2, 3);
#define DEBUG true
#define DHTPIN 7
#define DHTTYPE DHT11
String network = "*";// Network or ssid name of wifi ESP8266 module connecting to
String password = "*"; // password of network
#define IP "*" //* - Enter IP Address of your device connect to ESP8266
String GET = "GET /update?api_key=*"; // * - get your API key from your created data server
float tempc;
float tempf;
float vout;
int sec1;
DHT dht(DHTPIN, DHTTYPE); // DHT sensor pin mode declaration
float temp; //temperature
int hum; // humidity
String tempC;
int flex;
void setup()
{
Serial.begin(9600);
esp8266Module.begin(115200);
setupEsp8266();
}
void loop()
{
sec1++;
tempC = dht.readTemperature(); //read temperature value
hum = dht.readHumidity(); // read Humidity Value
flex = analogRead(A0); delay(200);
if (sec1 > 30) {
send2();
sec1 = 0;
}
}
void send2() {
updateTemp(String(tempC) , String(hum), String(flex));
}
// Following function setup the esp8266, put it in station made and
// connect to wifi access point.
void setupEsp8266()
{ // esp8266Module.flush();
esp8266Module.println(F("AT+RST"));//For Restart Module
delay(4000);
if (esp8266Module.find("OK"))
{
Serial.println("Found OK");
Serial.println("Changing espmode");
// esp8266Module.flush();
changingMode(); // call function for changing of esp8266 module
delay(2000);
// esp8266Module.flush();
connectToWiFi(); //call fucntion to connect to wifi
delay(2000);
// mux(); delay(2000);
// ser();
}
else
{
Serial.println("OK not found");
}
}
// Following function sets esp8266 to station mode
bool changingMode()
{
esp8266Module.println(F("AT+CWMODE=1"));//WIFI mode(station, AP, station + AP) if "AT+CWMODE=1 [1 = Station mode (client)]
if (esp8266Module.find("OK"))
{
Serial.println("Mode changed");
return true;
}
else if (esp8266Module.find("NO CHANGE")) {
Serial.println("Already in mode 1");
return true;
}
else
{
Serial.println("Error while changing mode");
return false;
}
}
// Following function connects esp8266 to wifi access point
bool connectToWiFi()
{
Serial.println("inside connectToWiFi");
String cmd = F("AT+CWJAP=\""); // AT+CWJAP - Connect to AP [AT+CWJAP="ssid/network","password" ]
cmd += network;
cmd += F("\",\"");
cmd += password;
cmd += F("\"");
esp8266Module.println(cmd);
delay(3000);
if (esp8266Module.find("OK"))
{
Serial.println("Connected to Access Point");
return true;
}
else
{
Serial.println("Could not connect to Access Point");
return false;
}
}
// Following function sends sensor data to thingspeak.com
void updateTemp(String voltage1, String voltage2, String voltage3)
{
String cmd = "AT+CIPSTART=\"TCP\",\""; //AT+CIPSTART - Establish TCP connection or register UDP port and start a connection [ AT+CIPSTART=type,addr,port ]
cmd += IP;
cmd += "\",80";
esp8266Module.println(cmd);
delay(3000);
if (esp8266Module.find("Error")) {
Serial.println("ERROR while SENDING");
return;
}
cmd = GET + "&field1=" + voltage1 + "&field2=" + voltage2 + "&field3=" + voltage3 + "\r\n";
esp8266Module.print("AT+CIPSEND="); //AT+CIPSEND - Send data
esp8266Module.println(cmd.length());
delay(3000);
if (esp8266Module.find(">"))
{
esp8266Module.print(cmd);
Serial.println("Data sent");
} else
{
esp8266Module.println("AT+CIPCLOSE"); // AT+CIPCLOSE - Close TCP or UDP connection
Serial.println("Connection closed");
}
}
Schematics
![](https://static.wixstatic.com/media/a27d24_fdfcfae653eb45e19e85fd9a54549df8~mv2_d_1920_1347_s_2.jpg/v1/fill/w_980,h_688,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/a27d24_fdfcfae653eb45e19e85fd9a54549df8~mv2_d_1920_1347_s_2.jpg)
![](https://static.wixstatic.com/media/a27d24_2923b83337df48f8865b0ec1fc75ee47~mv2.jpg/v1/fill/w_980,h_516,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/a27d24_2923b83337df48f8865b0ec1fc75ee47~mv2.jpg)