Send/Receive SMS using SIM800L with Arduino + DHT11/MLX90614 [DigitSpace.com]
Hello everyone, and welcome to another tutorial Sponsored by: DigitSpace
Product used: SIM800L Module Replace SIM900A GSM GPRS 5V Serial with Antenna
This tutorial is a follow up of two previous ones, make sure to check them, to be able to use the SIM800L module easily:
- Send SMS/Text from Arduino using SIM800L GSM/GPRS Module [DigitSpace.com]
- Arduino SIM800L – Dial phone numbers & LASER Tripwire GSM Alarm project [DigitSpace.com]
Summary:
So on this one we gonna first check how to receive SMS/Text and displays it on the Arduino IDE Serial monitor, and as a project we gonna request data to be sent to us all via SMS: I’ll add two sensors (as example) DHT11 and MLX90614 Contactless thermal sensor, and I’ll send a SMS from my phone to the Arduino containing “DHT” or “MLX” and Arduino will have to look for one of those keywords within the received SMS, and then send the measured values, depending on which sensor the user choosed, by SMS to the programmed phone number.
You can use your own modules, chose how many of them, send multiple SMS to multiple phones…. it all depends on you, give it a try.
Halt !!
Yes I use this in pretty much all tutorials related to SIM800L, because it’s easy to forget about, the SIM card you are using shouldn’t be protected with a “pin code”, if it’s the case you can either put it in your phone for example and deactivate the “sim lock” from the phone… Or you can use the suitable “AT command” to enter the pin code from Arduino directly which is “AT+CPIN”, I didn’t use this last method but here you can find all “AT commands” to use the module:
SIM800L Full manual of AT commands
Another thing that some may forget about is that your SIM card should have enough credit to be able to send a SMS, it depends on your network provider.
Enough talking… Let’s check our stuffs
So this is the module I used it’s the EVB version, there are several versions and in the codes things can change so be careful.
Module pinout, the wires used are 5V/Gnd for power, and then TXD/RXD/GND to communicate with Arduino.
And also as I did before, I’m powering the module using a 7.5V Lipo battery, with a Buck converter to be able to power the module with 5V, so before I powered the module directly from Arduino but it was sketchy… so I switched to external power source.
Test 1
In the test#1 we gonna use a direct wiring with Arduino, set the module on the receiving SMS mode, set the way to handle the SMS received and then show it on the Arduino IDE Serial monitor.
Wiring
Libraries
You won’t need any external libraries for this, only the default SoftwareSerial.
Code
So the code basically just sets the module on the receiving SMS mode, sets the way to handle the SMS in the “Setup()”, but on the loop it uses a function that permits the user to check the feedback from the SIM800L and also send Commands from the serial monitor, and as we told the module to transfer the received SMS via Serial to Arduino, we gonna print the SMS received on the Serial monitor.
Download the code here or check below. The code contains a lot of comments to help you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/* This code works with SIM800L Evb version, it sets the module on receiving SMS mode * And when the user sends SMS it will be shown on the serial monitor * The receiving mode is only set at the setup, you can later set the mode you want and check the AT functions you want * It's a basic code to test the SIM800L * Refer to http://www.SurtrTech.com for more detaims */ #include <SoftwareSerial.h> //Library required for serial communication SoftwareSerial sim800l(2, 3); // RX,TX for Arduino and for the module it's TXD RXD, they should be inverted void setup() { sim800l.begin(9600); //Module baude rate, this is on max, it depends on the version Serial.begin(9600); //Begin the Serial communication with PC Serial.println("Starting ..."); delay(5000); //Delay to let the module to connect to network, if turned on before you can remove this sim800l.println("AT"); //Send AT command it will says "OK" if everything is fine Serialcom(); //description below sim800l.println("AT+CMGF=1"); // Configuring TEXT mode Serialcom(); sim800l.println("AT+CNMI=2,2,0,0,0"); //The way to handle the SMS received, check the module manual, to sum up: store the text or transfert it to serial or other number.... Serialcom(); } void loop() { Serialcom(); //The only function to run in loop, this function just check if the user has sent something using the Serial monitor and transfers it to the module //and if the module has something to say it will be displayed on the serial monitor, here used to show the feedback of the AT commands sent in the setup //and shows the received SMS } void Serialcom() { delay(500); while(Serial.available()) // IDE serial l serial sim800L { sim800l.write(Serial.read());//Forward what Serial received to Software Serial Port } while(sim800l.available()) //serialsim800L l serial dial IDE { Serial.write(sim800l.read());//Forward what Software Serial received to Serial Port } } |
Result
After wiring the module and plugging the Arduino, the uploaded code will automatically set the mode for the SIM800l to receive texts/SMS, then we send a SMS from the phone.
The SMS received is now displayed on the Serial monitor alongside the phone number.
Project: Requesting specific sensor data
So here’s the project, now we can receive SMSs and transfer them to the Arduino board, but we need to detect certain keywords within the SMS, to be able to chose which sensor data is requested, do the measures and send the Data via SMS.
So here as mentioned above, I used DHT11 and MLX90614 Contactless thermal sensor as example, you can use your own sensors* and use how many you can* (* give it a try).
Wiring
The Arduino is powered via USB, but you can power it using the PS too just add the 5V wire.
Libraries
Two libraries should be add (Because of the modules), modules you use will define which libraries to use:
- DHT 11 library: Download library here
- MLX90614 (GY906) Adafruit library : Download here from Github or Download here
Pay attention to the libraries used, as from time to time they get updated, if you download them from Github make sure to test them first and adapt them to your project.
Code
So the code is actually where the tricky part is, the SMS received should be stored in a “char” type variable but then we have to change it to “String” to be able to search the keywords within the SMS.
Here’s a look from the code as you can see a lot of comments, and those are pretty much the tricks I did to able to achieve the objective, if you have better ones please share it with us.
Download the code here or check below. The code contains a lot of comments to help you understand.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
/* This code works with SIM800L Evb version, DHT11 and MLX90614 * It sets the module on receiving SMS mode, wait if the user has sent a SMS containing keywords "DHT" or "MLX" * Check which one is received then proceed to get data from the chosen sensor and send it via SMS to the programmed phone number * And come back to receiving mode. * Refer to http://www.SurtrTech.com for more detaims */ #include <SoftwareSerial.h> //Libraries required for Serial communication, i²c communication, DHT11 and MLX90614 #include <Wire.h> #include <Adafruit_MLX90614.h> #include "DHT.h" #define DHTPIN 7 //DHT OneWire pin and its type #define DHTTYPE DHT11 char Received_SMS; //Here we store the full received SMS (with phone sending number and date/time) as char short DHT_OK=-1,MLX_OK=-1; //Used later it shows if there's the word "DHT"/"MLX" within the received SMS "-1" means they are not found String Data_SMS; //Here's the SMS that we gonna send to the phone number, it may contain DHT data or MLX data SoftwareSerial sim800l(2, 3); // RX,TX for Arduino and for the module it's TXD RXD, they should be inverted Adafruit_MLX90614 mlx = Adafruit_MLX90614(); //Declaring MLX instance and DHT DHT dht(DHTPIN, DHTTYPE); void setup() { sim800l.begin(9600); //Begin all the communications needed Arduino with PC serial and Arduino with all devices (SIM800L+DHT+MLX) Serial.begin(9600); mlx.begin(); dht.begin(); Serial.println("Starting ..."); delay(3000); //Delay to let the module connect to network, can be removed ReceiveMode(); //Calling the function that puts the SIM800L moduleon receiving SMS mode } void loop() { String RSMS; //We add this new variable String type, and we put it in loop so everytime gets initialized //This is where we put the Received SMS, yes above there's Recevied_SMS variable, we use a trick below //To concatenate the "char Recevied_SMS" to "String RSMS" which makes the "RSMS" contains the SMS received but as a String //The recevied SMS cannot be stored directly as String while(sim800l.available()>0){ //When SIM800L sends something to the Arduino... problably the SMS received... if something else it's not a problem Received_SMS=sim800l.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" DHT_OK=RSMS.indexOf("DHT"); //And this is why we changed from char to String, it's to be able to use this function "indexOf" MLX_OK=RSMS.indexOf("MLX"); //"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(DHT_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 DHT"); //Shows on the serial monitor "found DHT" (optional) float h = dht.readHumidity(); //Read temperature and humidity float t = dht.readTemperature(); Serial.print("DHT11 Temperature = "); Serial.print(t); Serial.print("*C DHT11 Humidity = "); Serial.print(h); Serial.println(" %"); //Show it on the serial monitor also optional Data_SMS = "DHT11\nTemp = "+String(t,1)+" C"+" \nHumidity ="+String(h,1)+" %"; //Prepare the SMS to send, it contains some strings like "DHT" "Temperature"... //And then the values read Send_Data(); //This function set the sending SMS mode, prepare the phone number to which we gonna send, and send "Data_SMS" String ReceiveMode(); //Come back to Receving SMS mode and wait for other SMS DHT_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 MLX_OK=-1; //Maybe not required... I did a lot of tests and maybe at the beginning the RSMS string kept concating and MLX word was kept there //And at this point I'm too lazy to reupload the code without it and test... } if(MLX_OK!=-1){ //Same thing if the "MLX" word is found, Serial.print things are optionnal delay(1000); Serial.println("found MLX"); Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempC()); Serial.print("*C\tObject = "); Serial.print(mlx.readObjectTempC()); Serial.println("*C"); Data_SMS = "MLX\nAmbient = "+String(mlx.readAmbientTempC(),1)+" C"+" \nObject ="+String(mlx.readObjectTempC(),1)+" C"; Send_Data(); ReceiveMode(); MLX_OK=-1; DHT_OK=-1; } } void Serialcom() //This is used with ReceiveMode function, it's okay to use for tests with Serial monitor { delay(500); while(Serial.available()) { sim800l.write(Serial.read());//Forward what Serial received to Software Serial Port } while(sim800l.available()) { Serial.write(sim800l.read());//Forward what Software Serial received to Serial Port } } void ReceiveMode(){ //Set the SIM800L Receive mode sim800l.println("AT"); //If everything is Okay it will show "OK" on the serial monitor Serialcom(); sim800l.println("AT+CMGF=1"); // Configuring TEXT mode Serialcom(); sim800l.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 sim800l.print("AT+CMGF=1\r"); //Set the module to SMS mode delay(100); sim800l.print("AT+CMGS=\"+***********\"\r"); //Your phone number don't forget to include your country code example +212xxxxxxxxx" delay(500); sim800l.print(Data_SMS); //This string is sent as SMS delay(500); sim800l.print((char)26);//Required to tell the module that it can send the SMS delay(500); sim800l.println(); Serial.println("Data Sent."); delay(500); } |
Result
After plugging the Arduino open the serial monitor, you’ll see that the module’s mode has been set, then send a SMS containing the keyword you already set in the code. Here I sent just simple “DHT” and you can see that the Arduino has “found DHT” retrieved the data and displayed it on the serial monitor (before “Sending Data”)
Then the data has been sent and you’ll see a feedback on the monitor showing the message partially.
And last, on the phone you can see the data received.
Now we repeat the test by sending the other keyword we already set.
Here I send a nonsense but it contains the word “DHT”.
Improvements
- You can improve the code, because that’s the trick that worked for me, maybe you have a better one, I’m sure there’s a better way.
- You can do multiple readings + averaging before sending the SMS.
- Improve the keyword detection so things like the last SMS won’t be counted
- Add some commands to clear the SIM800L memory
- Send “Error SMS” if a sensor is not working properly
Well that’s all folks a big thanks to DigitSpace for their sponsorship.
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
Sir .how to use ultrasonic sensor with gsm sim 800l with .
My motive is to detect the ultrasonic sensor input .
And send and receive sms. To check the input of sensor
.need an code for that .
Sir .how to use ultrasonic sensor with gsm sim 800l with .
My motive is to detect the ultrasonic sensor input .
And send and receive sms. To check the input of sensor
now how to code Arduino uno
Hi, excuse me. I have a error in my IDE, this is the error “compilation terminated.exit status 1
Error compilando para la tarjeta Arduino Uno.” Some of my colleagues told me that it could be a parenthesis ( ) or { } but I checked and they are all correct. I thank you. Please Help ME 🙁 I was using the second code with DHT11.
Hello, what’s the full error message it will tell you if it’s a library problem or a syntax…
I don’t think I forgot any “;” or bracket, those code I tested directly, may be you have a library problem.
Hello there!
Hey Yassine,
when compiling i get this messge:
In file included from C:\Users\ELIAS\Desktop\SIM800L_RequestData\SIM800L_RequestData.ino:10:0:
C:\Users\ELIAS\Documents\Arduino\libraries\Adafruit-MLX90614-Library-master\libraries\Adafruit_MLX90614_Library/Adafruit_MLX90614.h:18:10: fatal error: Adafruit_I2CDevice.h: No such file or directory
#include
^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
Erreur de compilation pour la carte Arduino Pro or Pro Mini
NB: i notice that the library is installed and the arduino card is up to date.
This is the error log when i compile:
SIM800L_RequestData:34:7: error: expected constructor, destructor, or type conversion before ‘(‘ token
DHT_OK(5, String);
^
C:\Users\ELIAS\Desktop\SIM800L_RequestData\SIM800L_RequestData.ino: In function ‘void setup()’:
SIM800L_RequestData:42:6: error: expected unqualified-id before ‘.’ token
dht.begin();
^
SIM800L_RequestData:45:3: error: ‘ReceiveMode’ was not declared in this scope
ReceiveMode(); //Calling the function that puts the SIM800L moduleon receiving SMS mode
^~~~~~~~~~~
C:\Users\ELIAS\Desktop\SIM800L_RequestData\SIM800L_RequestData.ino:45:3: note: suggested alternative: ‘Received_SMS’
ReceiveMode(); //Calling the function that puts the SIM800L moduleon receiving SMS mode
^~~~~~~~~~~
Received_SMS
C:\Users\ELIAS\Desktop\SIM800L_RequestData\SIM800L_RequestData.ino: In function ‘void loop()’:
SIM800L_RequestData:72:18: error: expected primary-expression before ‘.’ token
float h = dht.readHumidity(); //Read temperature and humidity
^
SIM800L_RequestData:73:18: error: expected primary-expression before ‘.’ token
float t = dht.readTemperature();
^
SIM800L_RequestData:80:5: error: ‘Send_Data’ was not declared in this scope
Send_Data(); //This function set the sending SMS mode, prepare the phone number to which we gonna send, and send “Data_SMS” String
^~~~~~~~~
SIM800L_RequestData:81:5: error: ‘ReceiveMode’ was not declared in this scope
ReceiveMode(); //Come back to Receving SMS mode and wait for other SMS
^~~~~~~~~~~
C:\Users\ELIAS\Desktop\SIM800L_RequestData\SIM800L_RequestData.ino:81:5: note: suggested alternative: ‘Received_SMS’
ReceiveMode(); //Come back to Receving SMS mode and wait for other SMS
^~~~~~~~~~~
Received_SMS
SIM800L_RequestData:91:3: error: ‘Send_Data’ was not declared in this scope
Send_Data();
^~~~~~~~~
SIM800L_RequestData:92:3: error: ‘ReceiveMode’ was not declared in this scope
ReceiveMode();
^~~~~~~~~~~
C:\Users\ELIAS\Desktop\SIM800L_RequestData\SIM800L_RequestData.ino:92:3: note: suggested alternative: ‘Received_SMS’
ReceiveMode();
^~~~~~~~~~~
Received_SMS
C:\Users\ELIAS\Desktop\SIM800L_RequestData\SIM800L_RequestData.ino: At global scope:
SIM800L_RequestData:99:1: error: expected declaration before ‘}’ token
}
^
exit status 1
expected constructor, destructor, or type conversion before ‘(‘ token
NB: i copied and pasted, so nothing forgotten i hope.
Hello, i got the others working but the code for SIM800L_RequestData show the error “exit status 1
Error compiling for board Arduino Uno” during compiling. Can you please help me…
Hello sir,
First of all, thank you for this tutorial.
I have tested all the tutorials on internet I think, but I can’t read the Serial message of the SIM800L.
“Starting…’ is written, but it stopped here… Do you know why ?
Thanks for your time and your help
Nicolas