KenB001
Published © LGPL

SMS Temperature Reply

Send an SMS to the Arduino requesting the ambient temperature and get a reply.

BeginnerFull instructions provided2 hours19,017
SMS Temperature Reply

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
SIM800L
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Schematics

DHT22 SIM

Code

DHT_SIMPosting.ino

Arduino
Send SMS "InTemp" requests the temperature and the Arduino reply's in Degrees C.
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

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

#include <gprs.h>
#include <softwareserial.h>
#define TIMEOUT    5000
GPRS gprs;
bool ITemp = false;
float temp;
String MyString;
#define LED_PIN    13
bool ledStatus;
//Variable to hold last line of serial output from SIM800
char currentLine[500] = "";
int currentLineIndex = 0;
//Boolean to be set to true if message notificaion was found and next
//line of serial output is the actual SMS message content
bool nextLineIsMessage = false;

void Reply ()// Function starts here
{
 Serial.println("GPRS - Send SMS Test ...");
  gprs.preInit();
  delay(1000);
  while(0 != gprs.init()) {
      delay(1000);
      Serial.print("init error\r\n");
  }  
  Serial.println("Init success, start to send SMS message...");
  if (ITemp == true){
  float t = dht.readTemperature();
  temp=t;
  ITemp = false;
  
  
 }

    MyString = String(temp,0); //Convert float to String
     MyString = (MyString + " Degrees C");
     
       // convert string to char starts here
          // Length (with one extra character for the null terminator)
          int str_len = MyString.length() + 1; 
          // Prepare the character array (the buffer) 
          char char_array[str_len];
          // Copy it over 
          MyString.toCharArray(char_array, str_len);
       // convert string to char ends here

  
  gprs.sendSMS("04++++++++",char_array); //define phone number and text
  // Function ends here
}
void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht.begin();
    gprs.preInit();
  delay(1000);
 
  while(0 != gprs.init()) {
      delay(1000);
      Serial.print("init error\r\n");
      //later display SIM ERROR on Nextion HMI Home Page
  } 
 
  //Set SMS mode to ASCII
  if(0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI");
    return;
  }
   
  //Start listening to New SMS Message Indications
  if(0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {
    ERROR("ERROR:CNMI");
    return;
  }
 
  Serial.println("Init success");

}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // 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 t = dht.readTemperature();
 

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.println(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
  
 SIM();
}
void SIM()
{
   //Write current status to LED pin
  digitalWrite(LED_PIN, ledStatus);
   Serial.println("Looking for SMS");
  //If there is serial output from SIM800
  if(gprs.serialSIM800.available()){
    char lastCharRead = gprs.serialSIM800.read();
    //Read each character from serial output until \r or \n is reached (which denotes end of line)
    if(lastCharRead == '\r' || lastCharRead == '\n'){
        String lastLine = String(currentLine);
         
        //If last line read +CMT, New SMS Message Indications was received.
        //Hence, next line is the message content.
        if(lastLine.startsWith("+CMT:")){
           
          Serial.println(lastLine);
          nextLineIsMessage = true;
           
        } else if (lastLine.length() > 0) {
           
          if(nextLineIsMessage) {
            Serial.println(lastLine);
             
            //Read message content and set status according to SMS content
            if(lastLine.indexOf("InTemp") >= 0){
               Serial.println("InTemp");
               ITemp = true;
              Reply ();
             // ledStatus = 1;
                 // Serial.println("Turn ON the Light");
           // } else if(lastLine.indexOf("LED OFF") >= 0) {
             // ledStatus = 0;
             // Serial.println("Turn OFF the Light");
            }
          
             
            nextLineIsMessage = false;
          }
           
        }
         
        //Clear char array for next line of read
        for( int i = 0; i < sizeof(currentLine);  ++i ) {
         currentLine[i] = (char)0;
        }
        currentLineIndex = 0;
    } else {
      currentLine[currentLineIndex++] = lastCharRead;
    }
  }
}

Credits

KenB001

KenB001

1 project • 1 follower

Comments