Good day! I am working on my project on a function that the GSM shield and Arduino UNO will send me the sensor data when I call it and hang it up right away. I just sent a "STATUS" message and it will reply the sensor data to me.
#include <SoftwareSerial.h>
#include "DHT.h"
#define DHTPIN 7 //DHT OneWire pin and its type
#define DHTTYPE DHT22
char Received_SMS;
short STATUS_OK = -1;
String Data_SMS;
SoftwareSerial mySerial(2, 3); // RX,TX for Arduino and for the module it's TXD RXD, they should be inverted
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
mySerial.begin(9600); //Begin all the communications needed Arduino with PC serial and Arduino with all devices (SIM800L+DHT+MLX)
Serial.begin(9600);
dht.begin();
Serial.println("Starting ...");
delay(3000);
ReceiveMode();
}
void loop() {
String RSMS;
while (mySerial.available() > 0) { //When SIM800L sends something to the Arduino... problably the SMS received... if something else it's not a problem
Received_SMS = mySerial.read(); //"char Received_SMS" is now containing the full SMS received
Serial.print(Received_SMS); //Show it on the serial monitor (optional)
RSMS.concat(Received_SMS); //concatenate "char received_SMS" to RSMS which is "empty"
STATUS_OK = RSMS.indexOf("STATUS"); //And this is why we changed from char to String, it's to be able to use this function "indexOf"
//"indexOf function looks for the substring "x" within the String (here RSMS) and gives us its index or position
//For example if found at the beginning it will give "0" after 1 character it will be "1"
//If it's not found it will give "-1", so the variables are integers
}
if (STATUS_OK != -1) { //If "DHT" word is found within the SMS, it means that DHT_OK have other value than -1 so we can proceed
Serial.println("found DHT22"); //Shows on the serial monitor "found DHT" (optional)
float humidity = dht.readHumidity(); //Read temperature and humidity
float temperature = dht.readTemperature();
Serial.print("DHT22 Temperature = "); //Show it on the serial monitor also optional
Serial.print(temperature);
Serial.print("*C DHT22 Humidity = ");
Serial.print(humidity);
Serial.println(" %");
Data_SMS = "Smart Chicken Coop\nTemperature: " + String(temperature, 1) + "*C \nHumidity: " + String(humidity, 1) + " %";
Send_Data(); //This function set the sending SMS mode, prepare the phone number to which we gonna send, and send "Data_SMS" String
ReceiveMode();
STATUS_OK = -1; //If the DHT is found the variable should be reset to -1 otherwise it will be kept as !=-1 and will send SMS over and over
//And at this point I'm too lazy to reupload the code without it and test...
}
}
void Serialcom() //This is used with ReceiveMode function, it's okay to use for tests with Serial monitor
{
delay(500);
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while (mySerial.available())
{
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
}
}
void ReceiveMode() //Set the SIM800L Receive mode
{
mySerial.println("AT"); //If everything is Okay it will show "OK" on the serial monitor
Serialcom();
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
Serialcom();
mySerial.println("AT+CNMI=2,2,0,0,0"); //Configure the SIM800L on how to manage the Received SMS... Check the SIM800L AT commands manual
Serialcom();
}
void Send_Data()
{
Serial.println("Sending Data..."); //Displays on the serial monitor...Optional
mySerial.print("AT+CMGF=1\r"); //Set the module to SMS mode
delay(100);
mySerial.print("AT+CMGS=\"+639974012874\"\r"); //Your phone number don't forget to include your country code example +212xxxxxxxxx"
delay(500);
mySerial.print(Data_SMS); //This string is sent as SMS
delay(500);
mySerial.print((char)26);//Required to tell the module that it can send the SMS
delay(500);
mySerial.println();
Serial.println("Data Sent.");
delay(500);
}
Is this functionality is possible or nah?
Thanks guys!