Curious about smart parking systems engineering? Click here to learn more !

IoT: Humidity and Temperature Sensor

Posted by Momen 13/06/2017 0 Comment(s)

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

We are also an authorized Arduino distributor for the UAE, Oman and Qatar. We can provide you with any product of your choice. Please, contact us for inquires.

Our internet of things (IoT) training series:

1. Introduction to IoT using Arduino

2. IoT: Humidity and Temperature Sensor

3. ESP8266 WiFi Controlled Home Automation

4. IoT: Smart Doorbell

 

 

In this tutorial, I will try to explain how to make an IoT (Internet of Things) humidity and temperature sensor using Arduino Uno board. All the components in this tutorial have been bought from Zero Ohm Electronics which is a Dubai based company specialised in selling electronic parts. Below is a list of all hardware components used in this tutorial.

Hardware Components:

A DHT-22 sensor will basically take temperature (°C) and humidity (%) values for every fifteen minutes. The Wi-Fi serial module will be used for the internet connection and for data logging, an open data platform for IoT ‘ThingSpeak’ will be used.

Hardware Design

Step 1- Follow the pin configuration table below for hardware connections.

Arduino UNO

DHT-22

ESP8266

5V

VDD

X

Pin 2

Data

X

3.3V

X

VCC + RST + CH_PD (Parallel connection)

Pin 1 (TX)

X

RXD

Pin 0 (RX)

X

TXD

GND

GND

GND

Your hardware connection should look something like below:

arduino-uno-dht22-esp8266-wifi-module-connection

NOTE: Make sure you do not do the mistake of connecting the VCC of the Wi-fi module to the 5V of Arduino and it is recommended to connect a pull up resistor with the DATA pin of the DHT-22 sensor.

After setting up the hardware stuff, it is time to get working on the software!

Software Design

Step 1- Create a ThingSpeak account.

Step 2- Click on the ‘Channels’ button and select ‘New Channel’. You will be directed to a page shown below. Give it a name and select two fields for Temperature and Humidity. Now click the ‘Save Channel’ button at the end of the page.

thingspeak-iot-with-arduino

 

Step 3- After saving you will be directed your New Channel page. Select the ‘API Keys tab’.

temperature-and-humidity-sensor

 

Step 4- Copy your API Key, it will be needed in the next step.

thingspeak-iot-with-api

Step 5- Remove the TX and RX cables from the Arduino board.

Step 6- Now we can start writing the code! Let’s begin with including some libraries, initializations and variables.

DHT dht(DHTPIN, DHTTYPE);       // Initialize the DHT sensor

/*----------------------------------------------------------*/

/*-----------------ESP8266 Serial Wi-Fi Module---------------*/

#define SSID "name"                          // "SSID-WiFi-name"

#define PASS "password"                              // "password"

//Libraries

#include <stdlib.h>

#include <DHT.h>

/*------------------------DHT 22 SENSOR------------------------*/

#define DHTPIN 2                 // DHT data pin connected to Arduino pin 2

#define DHTTYPE DHT22   // We are type 22 for DHT

#define IP "184.106.153.149"// thingspeak.com ip

String msg = "GET /update?key=YOUR_WRITE_KEY"; //Your API Key

 /*-----------------------------------------------------------*/

//Variables

float temp;

int hum;

String tempC;

int error;

void setup()

{

  Serial.begin(9600); //or use default 115200.

  Serial.println("AT"); //change ESP firmware from LUA to the AT firmware 

  delay(5000);

  if(Serial.find("OK")){

    connectWiFi();

  }

}

Step 7- The main area of the code.

void loop(){

  //Read temperature and humidity values from DHT sensor:

  start: //label

  error=0;

  temp = dht.readTemperature();

  hum = dht.readHumidity();

  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(900000); //Update every 15 minutes

}

void updateTemp(){

  String cmd = "AT+CIPSTART=\"TCP\",\"";

  cmd += IP;

  cmd += "\",80";

  Serial.println(cmd);

  delay(2000);

  if(Serial.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=");

  Serial.println(cmd.length());

  if(Serial.find(">")){

    Serial.print(cmd);

  }

  else{

    Serial.println("AT+CIPCLOSE");

    //Resend...

    error=1;

  }

}

boolean connectWiFi(){

  Serial.println("AT+CWMODE=1");

  delay(2000);

  String cmd="AT+CWJAP=\"";

  cmd+=SSID;

  cmd+="\",\"";

  cmd+=PASS;

  cmd+="\"";

  Serial.println(cmd);

  delay(5000);

  if(Serial.find("OK")){

    return true;

  }else{

    return false;

  }

}

Step 8- Reconnect the TX and RX cable before uploading the code on the board.

Step 9- Check your ThingSpeak channel every 15 minutes to check how your system is working. Below is an example of how your Temperature and Humidity readings should look like, and that’s it we are done.

temperature-and-humidity-graphical-readings

Signing Off Note: You can further improve the design of the project by enclosing your circuit in a closed box which preferably should be a square or rectangle in shape with the DHT-22 sensor just outside it so it is better exposed to the external environment and there are more accurate readings for the temperature and humidity. Placing it on wall would be a good option and make sure to check your channel after 15 minutes to see if it is updating. The circuit can be further improved by adding a real clock circuit to it which would control the time of data sending to the UNO with certain accuracy.

Since I have nothing to add anymore, I’ll say my goodbyes and I hope this tutorial was useful for you and you enjoyed it!

 

 

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

We are also an authorized Arduino distributor for the UAE, Oman, Qatar, Saudi, Kuwait and Bahrain. We can provide you with any product of your choice. Please, contact us for inquires.

- See more at: https://www.zeroohm.com/blog/introduction-to-iot-using-arduino/#sthash.2GtXZqyo.dpuf

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

We are also an authorized Arduino distributor for the UAE, Oman, Qatar, Saudi, Kuwait and Bahrain. We can provide you with any product of your choice. Please, contact us for inquires.

- See more at: https://www.zeroohm.com/blog/introduction-to-iot-using-arduino/#sthash.2GtXZqyo.dpuf

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

We are also an authorized Arduino distributor for the UAE, Oman, Qatar, Saudi, Kuwait and Bahrain. We can provide you with any product of your choice. Please, contact us for inquires.

- See more at: https://www.zeroohm.com/blog/introduction-to-iot-using-arduino/#sthash.2GtXZqyo.dpuf

Leave a Comment