0% found this document useful (0 votes)
9 views7 pages

Sim800l Multi Sensor - Ino

The document is a code for an Arduino project that utilizes DHT11 and gas sensors (MQ2 and MQ7) to read environmental data. It connects to a GSM module (SIM800) to send this data to ThingSpeak via HTTP requests. The code includes functions for initializing the GSM module, connecting to GPRS, and handling AT commands for communication with the GSM module.

Uploaded by

ndeh cabrel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Sim800l Multi Sensor - Ino

The document is a code for an Arduino project that utilizes DHT11 and gas sensors (MQ2 and MQ7) to read environmental data. It connects to a GSM module (SIM800) to send this data to ThingSpeak via HTTP requests. The code includes functions for initializing the GSM module, connecting to GPRS, and handling AT commands for communication with the GSM module.

Uploaded by

ndeh cabrel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

/*

* ******************************************************************
* Created By: Tauseef Ahmad
* Created On: September 20, 2021
* Tutorial Link: https://youtu.be/uLJqNfX81C4
* My Channel: https://www.youtube.com/channel/UCOXYfOHgu-C-UfGyDcu5sYw/
*
* ******************************************************************
* Download DHT11 Library:
* https://github.com/adafruit/DHT-sensor-library
* ******************************************************************
*/

#include <DHT.h>
#define DHT11_PIN 4
#define DHTTYPE DHT11
DHT dht(DHT11_PIN, DHTTYPE);

#define MQ2_PIN A0
#define MQ7_PIN A1

//
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
void init_gsm();
void gprs_connect();
boolean gprs_disconnect();
boolean is_gprs_connected();
boolean waitResponse(String expected_answer="OK", unsigned int timeout=2000);

//
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
#include <SoftwareSerial.h>
//GSM Module RX pin to Arduino 3
//GSM Module TX pin to Arduino 2
#define rxPin 2
#define txPin 3
SoftwareSerial SIM800(rxPin,txPin);
//
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

const String APN = "ENTER_APN";


const String USER = "ENTER_USER_NAME";
const String PASS = "ENTER_PASSWORD";

const String THING_SPEAK_API_URL = "ENTER_THING_SPEAK_API_URL";


const String THING_SPEAK_API_KEY = "ENTER_THING_SPEAK_API_KEY";
String request_url = "";

#define USE_SSL true


#define DELAY_MS 500

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
//Function: setup() start
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
void setup() {
//Begin serial communication with Serial Monitor
Serial.begin(115200);

dht.begin();

//Begin serial communication with SIM800


SIM800.begin(9600);

Serial.println("Initializing SIM800...");
//init_gsm();
SIM800.println("AT");
waitResponse();
delay(DELAY_MS);

SIM800.println("AT+CPIN?");
delay(DELAY_MS);
}

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
//Function: loop() start
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
void loop() {

//------------------------------------------------------------
float mq2_val = analogRead(MQ2_PIN);
if (isnan(mq2_val)){
Serial.println("Failed to read from MQ-2 sensor!");
return;
}
mq2_val = mq2_val/1023*100;
Serial.println("MQ-2 Data: " + String(mq2_val));
delay(2000);
//------------------------------------------------------------
float mq7_val = analogRead(MQ2_PIN);
if (isnan(mq7_val)){
Serial.println("Failed to read from MQ-2 sensor!");
return;
}
mq7_val = mq7_val/1023*100;
Serial.println("MQ-7 Data: " + String(mq7_val));
delay(2000);
//------------------------------------------------------------
float huumidity = dht.readHumidity();
float temprature = dht.readTemperature();
if (isnan(huumidity) or isnan(temprature)){
Serial.println("Failed to read from DHT11 sensor!");
return;
}
Serial.println("Humidity Data: " + String(huumidity));
Serial.println("Temperature Data: " + String(temprature));
//------------------------------------------------------------

//if(!is_gprs_connected()){
//gprs_connect();
//}
request_url = THING_SPEAK_API_URL;
request_url += "?key=" + THING_SPEAK_API_KEY;
request_url += "&field1=";
request_url += temprature;
request_url += "&field2=";
request_url += huumidity;
request_url += "&field3=";
request_url += mq7_val;
request_url += "&field4=";
request_url += mq2_val;

if(!is_gprs_connected()){
gprs_connect();
}

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Start HTTP connection
SIM800.println("AT+HTTPINIT");
waitResponse();
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Enabling SSL 1.0
if(USE_SSL == true){
SIM800.println("AT+HTTPSSL=1");
waitResponse();
delay(DELAY_MS);
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Set the HTTP URL - Firebase URL and FIREBASE SECRET
SIM800.println("AT+HTTPPARA=\"URL\","+request_url);
waitResponse();
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//set http action type 0 = GET, 1 = POST, 2 = HEAD
//+HTTPACTION: 1,603,0 (POST to Firebase failed)
//+HTTPACTION: 0,200,0 (POST to Firebase successfull)
SIM800.println("AT+HTTPACTION=0");
for (uint32_t start = millis(); millis() - start < 20000;){
while(SIM800.available() > 0){
String response = SIM800.readString();
Serial.println(response);
if(response.indexOf("+HTTPACTION:") > 0){
goto OutFor;
}
}
}
OutFor:
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Read the response
SIM800.println("AT+HTTPREAD");
waitResponse("OK");
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Stop HTTP connection
SIM800.println("AT+HTTPTERM");
waitResponse("OK",1000);
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

delay(5000);
}

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
// Initialize GSM Module
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
void init_gsm()
{
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Testing AT Command
SIM800.println("AT");
waitResponse();
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Checks if the SIM is ready
SIM800.println("AT+CPIN?");
waitResponse("+CPIN: READY");
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Turning ON full functionality
SIM800.println("AT+CFUN=1");
waitResponse();
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Register Network (+CREG: 0,1 or +CREG: 0,5 for valid network)
//+CREG: 0,1 or +CREG: 0,5 for valid network connection
SIM800.println("AT+CREG?");
waitResponse("+CREG: 0,");
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
}

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
//Connect to the internet
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
void gprs_connect()
{
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// attach or detach from GPRS service
SIM800.println("AT+CGATT?");
waitResponse("OK",2000);
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Connecting to GPRS: GPRS - bearer profile 1
SIM800.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
waitResponse();
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//sets the APN settings for your sim card network provider.
SIM800.println("AT+SAPBR=3,1,\"APN\","+APN);
waitResponse();
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//sets the user name settings for your sim card network provider.
if(USER != ""){
SIM800.println("AT+SAPBR=3,1,\"USER\","+USER);
waitResponse();
delay(DELAY_MS);
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//sets the password settings for your sim card network provider.
if(PASS != ""){
SIM800.println("AT+SAPBR=3,1,\"PASS\","+PASS);
waitResponse();
delay(DELAY_MS);
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//after executing the following command. the LED light of
//sim800l blinks very fast (twice a second)
//enable the GPRS: enable bearer 1
SIM800.println("AT+SAPBR=1,1");
waitResponse("OK", 30000);
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Get IP Address - Query the GPRS bearer context status
SIM800.println("AT+SAPBR=2,1");
waitResponse("OK");
delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
}

/
*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!
* Function: is_gprs_connected()
* checks if the gprs connected.
* if IP address equals "0.0.0.0" its mean not connect to internet
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!*/
boolean is_gprs_connected()
{
SIM800.println("AT+SAPBR=2,1");
if(waitResponse("0.0.0.0") == 1) { return false; }

return true;
}

/
*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!
* Function: gprs_disconnect()
* AT+CGATT = 1 modem is attached to GPRS to a network.
* AT+CGATT = 0 modem is not attached to GPRS to a network
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!*/
boolean gprs_disconnect()
{
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//Disconnect GPRS
SIM800.println("AT+CGATT=0");
waitResponse("OK",60000);
//delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//DISABLE GPRS
//SIM800.println("AT+SAPBR=0,1");
//waitResponse("OK",60000);
//delay(DELAY_MS);
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

return true;
}

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
//Handling AT COMMANDS
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
boolean waitResponse(String expected_answer, unsigned int timeout)
{
uint8_t x=0, answer=0;
String response;
unsigned long previous;

//Clean the input buffer


while( SIM800.available() > 0) SIM800.read();

//NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
previous = millis();
do{
//if data in UART INPUT BUFFER, reads it
if(SIM800.available() != 0){
char c = SIM800.read();
response.concat(c);
x++;
//checks if the (response == expected_answer)
if(response.indexOf(expected_answer) > 0){
answer = 1;
}
}
}while((answer == 0) && ((millis() - previous) < timeout));
//NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN

Serial.println(response);
return answer;
}

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!
//Function: waitResponse() End
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!

You might also like