Components and supplies
Breadboard (generic)
Resistor 10k ohm
Arduino Mega 2560
Jumper wires (generic)
SIM800L
DHT22 Temperature Sensor
Project description
Code
DHT_SIMPosting.ino
arduino
Send SMS "InTemp" requests the temperature and the Arduino reply's in Degrees C.
1// Example testing sketch for various DHT humidity/temperature sensors 2// Written by ladyada, public domain 3 4#include "DHT.h" 5 6#define DHTPIN 2 // what digital pin we're connected to 7#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 8//#define DHTTYPE DHT21 // DHT 21 (AM2301) 9 10// Connect pin 1 (on the left) of the sensor to +5V 11// Connect pin 2 of the sensor to whatever your DHTPIN is 12// Connect pin 4 (on the right) of the sensor to GROUND 13// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor 14 15// Initialize DHT sensor. 16DHT dht(DHTPIN, DHTTYPE); 17 18#include <gprs.h> 19#include <softwareserial.h> 20#define TIMEOUT 5000 21GPRS gprs; 22bool ITemp = false; 23float temp; 24String MyString; 25#define LED_PIN 13 26bool ledStatus; 27//Variable to hold last line of serial output from SIM800 28char currentLine[500] = ""; 29int currentLineIndex = 0; 30//Boolean to be set to true if message notificaion was found and next 31//line of serial output is the actual SMS message content 32bool nextLineIsMessage = false; 33 34void Reply ()// Function starts here 35{ 36 Serial.println("GPRS - Send SMS Test ..."); 37 gprs.preInit(); 38 delay(1000); 39 while(0 != gprs.init()) { 40 delay(1000); 41 Serial.print("init error\ \ 42"); 43 } 44 Serial.println("Init success, start to send SMS message..."); 45 if (ITemp == true){ 46 float t = dht.readTemperature(); 47 temp=t; 48 ITemp = false; 49 50 51 } 52 53 MyString = String(temp,0); //Convert float to String 54 MyString = (MyString + " Degrees C"); 55 56 // convert string to char starts here 57 // Length (with one extra character for the null terminator) 58 int str_len = MyString.length() + 1; 59 // Prepare the character array (the buffer) 60 char char_array[str_len]; 61 // Copy it over 62 MyString.toCharArray(char_array, str_len); 63 // convert string to char ends here 64 65 66 gprs.sendSMS("04++++++++",char_array); //define phone number and text 67 // Function ends here 68} 69void setup() { 70 Serial.begin(9600); 71 Serial.println("DHTxx test!"); 72 73 dht.begin(); 74 gprs.preInit(); 75 delay(1000); 76 77 while(0 != gprs.init()) { 78 delay(1000); 79 Serial.print("init error\ \ 80"); 81 //later display SIM ERROR on Nextion HMI Home Page 82 } 83 84 //Set SMS mode to ASCII 85 if(0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\ \ 86", "OK", TIMEOUT)) { 87 ERROR("ERROR:CNMI"); 88 return; 89 } 90 91 //Start listening to New SMS Message Indications 92 if(0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\ \ 93", "OK", TIMEOUT)) { 94 ERROR("ERROR:CNMI"); 95 return; 96 } 97 98 Serial.println("Init success"); 99 100} 101 102void loop() { 103 // Wait a few seconds between measurements. 104 delay(2000); 105 106 // Reading temperature or humidity takes about 250 milliseconds! 107 // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) 108 float h = dht.readHumidity(); 109 // Read temperature as Celsius (the default) 110 float t = dht.readTemperature(); 111 112 113 Serial.print("Humidity: "); 114 Serial.print(h); 115 Serial.println(" %\ "); 116 Serial.print("Temperature: "); 117 Serial.print(t); 118 Serial.println(" *C "); 119 120 SIM(); 121} 122void SIM() 123{ 124 //Write current status to LED pin 125 digitalWrite(LED_PIN, ledStatus); 126 Serial.println("Looking for SMS"); 127 //If there is serial output from SIM800 128 if(gprs.serialSIM800.available()){ 129 char lastCharRead = gprs.serialSIM800.read(); 130 //Read each character from serial output until \ or \ 131 is reached (which denotes end of line) 132 if(lastCharRead == '\ ' || lastCharRead == '\ 133'){ 134 String lastLine = String(currentLine); 135 136 //If last line read +CMT, New SMS Message Indications was received. 137 //Hence, next line is the message content. 138 if(lastLine.startsWith("+CMT:")){ 139 140 Serial.println(lastLine); 141 nextLineIsMessage = true; 142 143 } else if (lastLine.length() > 0) { 144 145 if(nextLineIsMessage) { 146 Serial.println(lastLine); 147 148 //Read message content and set status according to SMS content 149 if(lastLine.indexOf("InTemp") >= 0){ 150 Serial.println("InTemp"); 151 ITemp = true; 152 Reply (); 153 // ledStatus = 1; 154 // Serial.println("Turn ON the Light"); 155 // } else if(lastLine.indexOf("LED OFF") >= 0) { 156 // ledStatus = 0; 157 // Serial.println("Turn OFF the Light"); 158 } 159 160 161 nextLineIsMessage = false; 162 } 163 164 } 165 166 //Clear char array for next line of read 167 for( int i = 0; i < sizeof(currentLine); ++i ) { 168 currentLine[i] = (char)0; 169 } 170 currentLineIndex = 0; 171 } else { 172 currentLine[currentLineIndex++] = lastCharRead; 173 } 174 } 175} 176
DHT_SIMPosting.ino
arduino
Send SMS "InTemp" requests the temperature and the Arduino reply's in Degrees C.
1// Example testing sketch for various DHT humidity/temperature sensors 2// Written by ladyada, public domain 3 4#include "DHT.h" 5 6#define DHTPIN 2 // what digital pin we're connected to 7#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 8//#define DHTTYPE DHT21 // DHT 21 (AM2301) 9 10// Connect pin 1 (on the left) of the sensor to +5V 11// Connect pin 2 of the sensor to whatever your DHTPIN is 12// Connect pin 4 (on the right) of the sensor to GROUND 13// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor 14 15// Initialize DHT sensor. 16DHT dht(DHTPIN, DHTTYPE); 17 18#include <gprs.h> 19#include <softwareserial.h> 20#define TIMEOUT 5000 21GPRS gprs; 22bool ITemp = false; 23float temp; 24String MyString; 25#define LED_PIN 13 26bool ledStatus; 27//Variable to hold last line of serial output from SIM800 28char currentLine[500] = ""; 29int currentLineIndex = 0; 30//Boolean to be set to true if message notificaion was found and next 31//line of serial output is the actual SMS message content 32bool nextLineIsMessage = false; 33 34void Reply ()// Function starts here 35{ 36 Serial.println("GPRS - Send SMS Test ..."); 37 gprs.preInit(); 38 delay(1000); 39 while(0 != gprs.init()) { 40 delay(1000); 41 Serial.print("init error\ \ 42"); 43 } 44 Serial.println("Init success, start to send SMS message..."); 45 if (ITemp == true){ 46 float t = dht.readTemperature(); 47 temp=t; 48 ITemp = false; 49 50 51 } 52 53 MyString = String(temp,0); //Convert float to String 54 MyString = (MyString + " Degrees C"); 55 56 // convert string to char starts here 57 // Length (with one extra character for the null terminator) 58 int str_len = MyString.length() + 1; 59 // Prepare the character array (the buffer) 60 char char_array[str_len]; 61 // Copy it over 62 MyString.toCharArray(char_array, str_len); 63 // convert string to char ends here 64 65 66 gprs.sendSMS("04++++++++",char_array); //define phone number and text 67 // Function ends here 68} 69void setup() { 70 Serial.begin(9600); 71 Serial.println("DHTxx test!"); 72 73 dht.begin(); 74 gprs.preInit(); 75 delay(1000); 76 77 while(0 != gprs.init()) { 78 delay(1000); 79 Serial.print("init error\ \ 80"); 81 //later display SIM ERROR on Nextion HMI Home Page 82 } 83 84 //Set SMS mode to ASCII 85 if(0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\ \ 86", "OK", TIMEOUT)) { 87 ERROR("ERROR:CNMI"); 88 return; 89 } 90 91 //Start listening to New SMS Message Indications 92 if(0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\ \ 93", "OK", TIMEOUT)) { 94 ERROR("ERROR:CNMI"); 95 return; 96 } 97 98 Serial.println("Init success"); 99 100} 101 102void loop() { 103 // Wait a few seconds between measurements. 104 delay(2000); 105 106 // Reading temperature or humidity takes about 250 milliseconds! 107 // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) 108 float h = dht.readHumidity(); 109 // Read temperature as Celsius (the default) 110 float t = dht.readTemperature(); 111 112 113 Serial.print("Humidity: "); 114 Serial.print(h); 115 Serial.println(" %\ "); 116 Serial.print("Temperature: "); 117 Serial.print(t); 118 Serial.println(" *C "); 119 120 SIM(); 121} 122void SIM() 123{ 124 //Write current status to LED pin 125 digitalWrite(LED_PIN, ledStatus); 126 Serial.println("Looking for SMS"); 127 //If there is serial output from SIM800 128 if(gprs.serialSIM800.available()){ 129 char lastCharRead = gprs.serialSIM800.read(); 130 //Read each character from serial output until \ or \ 131 is reached (which denotes end of line) 132 if(lastCharRead == '\r' || lastCharRead == '\n'){ 133 String lastLine = String(currentLine); 134 135 //If last line read +CMT, New SMS Message Indications was received. 136 //Hence, next line is the message content. 137 if(lastLine.startsWith("+CMT:")){ 138 139 Serial.println(lastLine); 140 nextLineIsMessage = true; 141 142 } else if (lastLine.length() > 0) { 143 144 if(nextLineIsMessage) { 145 Serial.println(lastLine); 146 147 //Read message content and set status according to SMS content 148 if(lastLine.indexOf("InTemp") >= 0){ 149 Serial.println("InTemp"); 150 ITemp = true; 151 Reply (); 152 // ledStatus = 1; 153 // Serial.println("Turn ON the Light"); 154 // } else if(lastLine.indexOf("LED OFF") >= 0) { 155 // ledStatus = 0; 156 // Serial.println("Turn OFF the Light"); 157 } 158 159 160 nextLineIsMessage = false; 161 } 162 163 } 164 165 //Clear char array for next line of read 166 for( int i = 0; i < sizeof(currentLine); ++i ) { 167 currentLine[i] = (char)0; 168 } 169 currentLineIndex = 0; 170 } else { 171 currentLine[currentLineIndex++] = lastCharRead; 172 } 173 } 174} 175
Downloadable files
DHT22 SIM
DHT22 SIM
Comments
Only logged in users can leave comments
KenB001
0 Followers
•0 Projects
Table of contents
Intro
8
0