electroSome

IoT Data Logger using Arduino and ESP8266

IoT Data Logger using Arduino and ESP8266

IoT Data Logger using Arduino and ESP8266

Today we are going to make an IoT WiFi data logger using Arduino, ESP8266 WiFi module and DHT22 temperature humidity sensor. Arduino is reading temperature and humidity sensed by DHT22 and sending it to an internet server using the ESP8266 WiFi module. Here we are using ThingSpeak as the internet server. ThingSpeak is an open source IoT application which can be used as an Internet or Intranet Server. It is providing HTTP API for data logging, location tracking, status updates etc.

Components Required

Circuit Diagram and Explanation

IoT Data Logger using Arduino and ESP8266 – Circuit Diagram

First of all, we are going to connect ESP8266 module with the Arduino. ESP8266 runs on 3.3V, so we should power it from the 3.3V output of Arduino. Connect VCC and CH_PD of ESP8266 module to the 3.3V output of Arduino and the ground of ESP8266 to the ground of Arduino. The RX pin of the ESP8266 is not 5V tolerant, so we need to reduce the 5V TX output of Arduino to 3.3V using voltage dividing resistors. Here we are using three 1KΩ resistors connected in series for that. So we will connect pin 10 (TX) of Arduino to RX of ESP8266 module via voltage dividing resistors. We can directly connect TX pin of ESP8266 to 9th (RX) of Arduino as it will detect 3.3V as logic HIGH according to TTL voltage specifications.

Then connect the DHT22 sensor to the Arduino. The first pin of the DHT22 sensor VCC power input, connect it to the 5V output pin of the Arduino. The second pin of the DHT22 is the data output pin, connect it to the pin 13 of the Arduino. The third pin is No Connection (NC) so we can leave it. Connect last pin to ground.

Now we can connect 16X2 LCD to the Arduino. The connections of LCD to Arduino are as follows.

ThingSpeak

ThingSpeak is an open source IoT application with HTTP API which can store and retrieve data from ‘things’ via Internet or Intranet. By using this we can easily create a network of things which can be used for applications like logging, tracking, analytics etc.

First of all,open the ThingSpeak Website and create your account.

ThingSpeak – Home Page

Then go to channels and create a new channel.

ThingSpeak – Create a New Channel

Enter the name of your channel and select two fields and name these fields. Also make the channel public by checking in the box which will be below these fields.

ThingSpeak – Create a Channel Public

After creating, your channel will look like this.

ThingSpeak – Channel Private View

Then goto API keys and copy your write api key. We will use this in the Arduino program.

ThingSpeak – API Keys

Program and Explanation

Arduino Code

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal.h>
#include <stdlib.h>
#include <DHT.h>

#define DEBUG true
#define DHTPIN 13                 // DHT data pin connected to Arduino pin 2
#define DHTTYPE DHT22             // DHT 22 (or AM2302)
#define SSID "YOUR WIFI NAME"     // "SSID-WiFiname"
#define PASS "YOUR WIFI PASSWORD" // "password"
#define IP "184.106.153.149"      // thingspeak.com ip

String msg = "GET /update?key=M2QNKN73LG2X8TVH"; //change it with your api key like "GET /update?key=Your Api Key"
SoftwareSerial esp8266(9,10);
LiquidCrystal lcd(12,11,5,4,3,2);
DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor

//Variables
float temp;
int hum;
String tempC;
int error;
void setup()
{
  lcd.begin(16, 2);
  lcd.print("electrosome.com");
  delay(100);
  lcd.setCursor(0,1);
  lcd.print("Connecting...");
  Serial.begin(9600); //or use default 115200.
  esp8266.begin(9600);
  Serial.println("AT");
  esp8266.println("AT");

  delay(5000);

  if(esp8266.find("OK"))
  {
    connectWiFi();
  }
}

void loop()
{
  lcd.clear();
  //Read temperature and humidity values from DHT sensor:
  start: //label
    error=0;
    temp = dht.readTemperature();
    hum = dht.readHumidity();
    lcd.setCursor(0, 0);
    lcd.print("Temp = ");
    lcd.print(temp);
    lcd.print(" C");
    delay (100);
    lcd.setCursor(0, 1); // set the cursor to column 0, line 2
    lcd.print("Humi = ");
    lcd.print(hum);
    lcd.print(" %");
    delay(1000);
    char buffer[10];
    // there is a useful c function called dtostrf() which will convert a float to a char array
    //so it can then be printed easily.  The format is: dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
    tempC = dtostrf(temp, 4, 1, buffer);
    updateTemp();
    //Resend if transmission is not completed
    if (error==1)
    {
      goto start; //go to label "start"
    } 
  delay(3600);
}

void updateTemp()
{
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += IP;
  cmd += "\",80";
  Serial.println(cmd);
  esp8266.println(cmd);
  delay(2000);
  if(esp8266.find("Error"))
  {
    return;
  }
  cmd = msg ;
  cmd += "&field1=";    //field 1 for temperature
  cmd += tempC;
  cmd += "&field2=";  //field 2 for humidity
  cmd += String(hum);
  cmd += "\r\n";
  Serial.print("AT+CIPSEND=");
  esp8266.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  esp8266.println(cmd.length());
  if(esp8266.find(">"))
  {
    Serial.print(cmd);
    esp8266.print(cmd);
  }
  else
  {
    Serial.println("AT+CIPCLOSE");
    esp8266.println("AT+CIPCLOSE");
    //Resend...
    error=1;
  }
}

boolean connectWiFi()
{
  Serial.println("AT+CWMODE=1");
  esp8266.println("AT+CWMODE=1");
  delay(2000);
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  Serial.println(cmd);
  esp8266.println(cmd);
  delay(5000);
  if(esp8266.find("OK"))
  {
    return true;
  }
  else
  {
    return false;
  }
}

DHT22 Library

You need to manually add DHT library to Arduino IDE as it is not included by default. You can ignore it if you already added it. Otherwise you can do following steps for that.

Code Explanation

First of all, add the libraries. The software serial library will allow you to communicate with ESP8266 via Software UART. The liquid crystal library enables us to display data in the 16×2 LCD display. DHT library is used to read temperature and humidity from the DHT22 sensor.

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include <stdlib.h>
#include <DHT.h>

Next is defining some constants. DHTPIN is the Arduino pin to which we are connecting the DHT22 sensor and DHTTYPE indicates the type of sensor, here it is DHT22. Then we you can define your WiFi SSID and password for connecting ESP8266 to your WiFi internet. Then enter your ThingSpeak API key which you generated above.

#define DEBUG true
#define DHTPIN 13                 // DHT data pin connected to Arduino pin 2
#define DHTTYPE DHT22             // DHT22 (or AM2302)
#define SSID "YOUR WIFI NAME"     // "SSID-WiFiname"
#define PASS "YOUR WIFI PASSWORD" // "password"
#define IP "184.106.153.149"      // thingspeak.com ip

String msg = "GET /update?key=M2QNKN73LG2X8TVH"; //change it with your api key like "GET /update?key=Your Api Key"

Then we can initialize the Software UART, LCD and DHT sensor.

SoftwareSerial esp8266(9,10);
LiquidCrystal lcd(12,11,5,4,3,2);
DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor

Following will display some initial data on LCD and also connect ESP8266 to your WiFi network.

  lcd.begin(16, 2);
  lcd.print("electrosome.com");
  delay(100);
  lcd.setCursor(0,1);
  lcd.print("Connecting...");
  Serial.begin(9600); //or use default 115200.
  esp8266.begin(9600);
  Serial.println("AT");
  esp8266.println("AT");

  delay(5000);

  if(esp8266.find("OK"))
  {
    connectWiFi();
  }

The following code will instruct the ESP8266 module to establish a TCP connection to the specified IP address and port number (80). Standard port of HTTP is 80.

void updateTemp()
{
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += IP;
  cmd += "\",80";
  Serial.println(cmd);
  esp8266.println(cmd);
  delay(2000);
  if(esp8266.find("Error"))
  {
    return;
  }

Following code will send the current Temperature and Humidity to the ThingSpeak server via HTTP API using above established TCP connection.

  cmd = msg ;
  cmd += "&field1=";    //field 1 for temperature
  cmd += tempC;
  cmd += "&field2=";  //field 2 for humidity
  cmd += String(hum);
  cmd += "\r\n";
  Serial.print("AT+CIPSEND=");
  esp8266.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  esp8266.println(cmd.length());
  if(esp8266.find(">"))
  {
    Serial.print(cmd);
    esp8266.print(cmd);
  }

The following function is used to make ESP8266 in station mode and connect it to your WiFi.

boolean connectWiFi()
{
  Serial.println("AT+CWMODE=1");
  esp8266.println("AT+CWMODE=1");
  delay(2000);
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  Serial.println(cmd);
  esp8266.println(cmd);
  delay(5000);
  if(esp8266.find("OK"))
  {
    return true;
  }
  else
  {
    return false;
  }
}

Video