Measuring Temperature and Humidity using DHT22 and Sending data via SMS using SIM800C module

Overview

This project measures the temperature and humidity of the surroundings using the DHT22 sensor. Also, the gathered data is then sent to the user’s mobile phone using the SIM800C GSM module.

SIM800C is a type of module that uses GSM to communicate. GSM, or Global System for Mobile Communications, is the most popular cell phone standard in the world. Data transmission uses GSM to send and receive data across the globe through mobile networks.

On the other hand, a DHT22 sensor measures temperature and humidity of the area that surrounds it. When compared to the DHT11 sensor, the DHT22 sensor is more accurate and more efficient because of its capability to measure more extensive temperature ranges with minimal error percentage.

Hardware Used

  • RobotDyn Uno R3

  • SIM800C GSM Module
  • 
    
  • SIM (Subscriber Identity Module) Card
  • DHT22 Temperature and Humidity Sensor
  • 
    
  • Breadboard
  • 
    
  • Jumper Wires
  • 
    
    

    You can buy all this Hardware at Createlabz.

    Software Used

    Libraries Used

    • SoftwareSerial.h
    • DHT.h

    Application Description

    This project utilizes the capabilities of the SIM900C GSM module to transmit and receive data. Also, this project uses the ability of the DHT22 module to measure temperature and humidity. This project gathers the data from the DHT22 module and sends it to the user using the GSM module. But, the user can only access the data by texting the Arduino Uno. Once the text is received by the Arduino Uno, it transmits the gathered data to the user through a text.

    Set-up the Hardware

    Figure 1 – Schematic Diagram

    Figure 2 – SIM800C GSM Module Front View

    Figure 3 – SIM800C GSM Module Back View

    Figure 1 shows the connections between the different equipment used in this project. The RX and TX pins of the SIM900C module are connected to pins 10 and 9 of the Arduino Uno, respectively. The RX pin of the GSM module should be connected to the TX pin of the Arduino and the TX pin of the module to the Arduino’s RX pin. This allows the data to be transmitted and received in both sources.

    Set-up the Software

     

    #include<SoftwareSerial.h>
    #include "DHT.h"
    
    
    #define DHTPIN 2
    #define DHTTYPE DHT22
    DHT dht(DHTPIN, DHTTYPE);
    
    SoftwareSerial mySerial(9, 10);
    
    int index = 0;
    float h;
    float temp;
    
    
    char incomingByte;
    String incomingData;
    bool atCommand = true;
    String message = "";
    String number = "";
    
    void setup()
    {
      Serial.begin(9600);
      mySerial.begin(9600);
      dht.begin();
    
      while (!mySerial.available()) {
        mySerial.println("AT");
        delay(1000);
        Serial.println("connecting....");
      }
    
      mySerial.println("AT+CMGF=1");  //Set SMS Text Mode
      delay(1000);
      mySerial.println("AT+CNMI=1,2,0,0,0");  //procedure, how to receive messages from the network
      delay(1000);
      //mySerial.println("AT+CMGL=\"REC UNREAD\""); // Read unread messages
      mySerial.println("AT+CBAND=DCS_MODE"); 
      Serial.println("Ready to receive Commands..");
    }
    
    void loop()
    {
      
      
      if (mySerial.available()) {
        delay(100);
        // Serial buffer
        while (mySerial.available()) {
          incomingByte = mySerial.read();
          incomingData += incomingByte;
        }
        delay(10);
        if (atCommand == false) {
          receivedMessage(incomingData);
    
        }
        else {
          atCommand = false;
        }
        //delete messages to save memory
        if (incomingData.indexOf("OK") == -1) {
          mySerial.println("AT+CMGDA=\"DEL ALL\"");
          delay(1000);
          atCommand = true;
        }
        incomingData = "";
      }
      if (message.indexOf("SEND") > -1) {
        SendTextMessage();
        Serial.println("send");
      }
    }
    
    
    void SendTextMessage()
    {
      
    
      // 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 temp = dht.readTemperature();
      // Read temperature as Fahrenheit (isFahrenheit = true)
      float f = dht.readTemperature(true);
    
      mySerial.println("AT+CMGF=1");    //To send SMS in Text Mode
      delay(1000);
      mySerial.println("AT+CMGS=\"+63xxxxxxxxxx\"\r"); // change to the phone number you using
      delay(1000);
      mySerial.print("Humidity: ");
      mySerial.print(h);
      mySerial.print(" % ");
      mySerial.print("Temperature: ");
      mySerial.print(temp);
      mySerial.println(" *C ");
      delay(200);
      mySerial.println((char)26);//the stopping character
      delay(1000);
    
    }
    void receivedMessage(String inputString) {
    
      //Get The number of the sender
      index = inputString.indexOf('"') + 1;
      inputString = inputString.substring(index);
      index = inputString.indexOf('"');
      number = inputString.substring(0, index);
      Serial.println("Number: " + number);
    
      //Get The Message of the sender
      index = inputString.indexOf("\n") + 1;
      message = inputString.substring(index);
      message.trim();
      Serial.println("Message: " + message);
      message.toUpperCase(); // uppercase the message received
      Serial.println("receive");
    
      delay(50);
    }

    The code shown above is the program needed for this project. It is divided into five major parts. These are the initialization, setup, loop, SendTextMessage(); function, and the receivedMessage(); function. These five parts will be thoroughly explained below.

    #include<SoftwareSerial.h>
    #include "DHT.h"
    
    
    #define DHTPIN 2
    #define DHTTYPE DHT22
    DHT dht(DHTPIN, DHTTYPE);
    
    SoftwareSerial mySerial(9, 10);
    
    int index = 0;
    float h;
    float temp;
    
    
    char incomingByte;
    String incomingData;
    bool atCommand = true;
    String message = "";
    String number = "";

    The first significant part of the code is the initialization of variables. This is where the variables are given an initial value. These values may change as the code progresses.

    Also, this is where the libraries are declared and defined. The SoftwareSerial.h and DHT.h are the libraries used in this project. The SoftwareSerial library is used to incorporate serial communication onto the different pins of the Arduino Uno rather than using pins 0 and 1 for serial communication. Serial communication is a type of communication wherein data is transmitted and received using a communication channel one bit at a time. In the code shown above, the pins used for serial communication are pins 9 and 10. The pin 9 is the RX pin, and pin 10 is the TX pin.

    On the other hand, the DHT library is defined to allow the DHT22 sensor to measure the temperature and humidity of its surroundings. This part also establishes the pin used for the DHT22 sensor, specifically pin 2. The DHT22 library can be downloaded here.

    void setup()
    {
      Serial.begin(9600);
      mySerial.begin(9600);
      dht.begin();
    
      while (!mySerial.available()) {
        mySerial.println("AT");
        delay(1000);
        Serial.println("connecting....");
      }
    
      mySerial.println("AT+CMGF=1");  //Set SMS Text Mode
      delay(1000);
      mySerial.println("AT+CNMI=1,2,0,0,0");  //procedure, how to receive messages from the network
      delay(1000);
      //mySerial.println("AT+CMGL=\"REC UNREAD\""); // Read unread messages
      mySerial.println("AT+CBAND=DCS_MODE"); 
      Serial.println("Ready to receive Commands..");
    }

    The second significant part of the code is the setup function. From the name itself, this is where the set-up is created. This is where the baud rate is initialized. Baud rate is defined as the rate of transfer of information in a communication channel. In this project, a baud rate of 9600 is used. This means that the serial port is capable of sending and receiving a maximum of 9600 bits per second.

    Moreover, the setup function contains the AT commands. AT commands are instructions needed to control the modem to perform a specific operation. In this project, the AT commands instructs the SIM800C module to set itself to SMS Text Mode, allows itself to receive messages and read the unread ones. Furthermore, the AT commands also tells the module to set the mobile operating band to DCS. This allows the module to connect to a network faster since it is instructed to only connect to GSM frequencies inside the DCS frequency band.

    void loop()
    {
      if (mySerial.available()) {
        delay(100);
        // Serial buffer
        while (mySerial.available()) {
          incomingByte = mySerial.read();
          incomingData += incomingByte;
        }
        delay(10);
        if (atCommand == false) {
          receivedMessage(incomingData);
    
        }
        else {
          atCommand = false;
        }
        //delete messages to save memory
        if (incomingData.indexOf("OK") == -1) {
          mySerial.println("AT+CMGDA=\"DEL ALL\"");
          delay(1000);
          atCommand = true;
        }
        incomingData = "";
      }
      if (message.indexOf("SEND") > -1) {
        SendTextMessage();
        Serial.println("send");
      }
    }

    The third part of the code is the loop function. This is where the Arduino does its processes and repeats it continuously. In this project, the loop function contains the process when the Arduino senses the availability of the software serial communication.

    Furthermore, the loop function contains the if-statement which allows the Arduino Uno to send the data gathered in the DHT22 module to the user via a text. Once the GSM module receives a text, the received message is compared to the word “SEND”. If the received message is the word “SEND”, the Arduino sends the data obtained from the DHT22 sensor to the user.

    void SendTextMessage()
    {
      // 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 temp = dht.readTemperature();
      // Read temperature as Fahrenheit (isFahrenheit = true)
      float f = dht.readTemperature(true);
    
      mySerial.println("AT+CMGF=1");    //To send SMS in Text Mode
      delay(1000);
      mySerial.println("AT+CMGS=\"+xxxxxxxxx\"\r"); // change to the phone number you using
      delay(1000);
      mySerial.print("Humidity: ");
      mySerial.print(h);
      mySerial.print(" % ");
      mySerial.print("Temperature: ");
      mySerial.print(temp);
      mySerial.println(" *C ");
      delay(200);
      mySerial.println((char)26);//the stopping character
      delay(1000);
    }

    The fourth part of the code is the SendTextMessage() function. This function allows the Arduino Uno to send the data to the user. The first portion of this function is the acquisition of data from the DHT22 sensor. Also, this is where the Arduino prints the message to the GSM module before transmission.

    When receiving the message, it is vital to change the phone number to ensure proper transmission and reception of data. You may change the phone number in the given code.

    void receivedMessage(String inputString) {
    
      //Get The number of the sender
      index = inputString.indexOf('"') + 1;
      inputString = inputString.substring(index);
      index = inputString.indexOf('"');
      number = inputString.substring(0, index);
      Serial.println("Number: " + number);
    
      //Get The Message of the sender
      index = inputString.indexOf("\n") + 1;
      message = inputString.substring(index);
      message.trim();
      Serial.println("Message: " + message);
      message.toUpperCase(); // uppercase the message received
      Serial.println("receive");
    
      delay(50);
    }

    The fifth and final part of the code is the receivedMessage() function. This function gets the message and number of the sender. The importance of the message to the overall project was explained in the earlier portions of this tutorial, wherein the Arduino send the data if and only if the message is the word “SEND”.

    Conclusion

    The SIM800C GSM module can be interfaced with different sensors as a way to gather and send real-time data. The DHT22 sensor is just one of them. It is always up to you to think of different methods to fully harness the capabilities of the GSM module.

    References

     

    The post Measuring Temperature and Humidity using DHT22 and Sending data via SMS using SIM800C module appeared first on CreateLabz.

    ArduinoDhtDht22GprsGsmHumidityKnowledgebaseSim800cSmsTemperature

    Leave a comment

    All comments are moderated before being published