IOT

How to use GSM & Arduino Communication with Firebase or Thingspeak

In this blog, we will learn how to use the GSM Module with Arduino to communicate with Google Firebase or Thingspeak Server. We’ll utilize an Arduino IoT Box with a SIM800C and Atmega328 Microcontroller for this. Graylogix Arduino IoT box is a battery-powered IoT application that combines GSM and Arduino.

To interact with Firebase or Thingspeak, we use a combination of GSM and Arduino. The DHT11 sensor, which monitors temperature and humidity, is simple to connect to Arduino. The DHT11 Sensor Data will be sent to Thingspeak Server first. The data will then be received or monitored remotely using Google Firebase.

Hardware Required:

  • Arduino IoT Box-SIM800C+Arduino Combined
  • DHT11 Sensor-Humidity & Temperature Sensor
  • Male-to-Female Jumpers

About Arduino IoT Box

Arduino IoT Box

IOT Box is an Arduino + GSM Open Source Hardware with built-in battery and I/O Ports that is best suited for industrial IoT projects. Simply dump the code and you’re ready to go with white labelling.

Arduino IoT Box GSM

A PCB board with a SIM800C GSM Module and an Atmega328 Microcontroller with Arduino Bootloader is included in the box. A 3.7V, 1000mAh Lithium-Ion Battery powers the box. The TP4056 Battery Management System IC in the package is used to charge the battery. By inserting the micro-USB Cable into the Micro-USB port, the battery can be charged. The code is also dumped using the same USB port.

The box has an external GSM antenna that must be connected before the gadget may be powered. The nano-SIM card can be inserted into the specialized SIM slot. Remember that the SIM800 is a 2G module that only accepts 2G SIM cards.

By attaching berg strips on the board, dedicated I/O pins on the board can be utilized to easily interface external sensors and other I/O devices. External interfacing can also be accomplished by connecting AD0 to AD3 (pulled high internally) to the Arduino nano and connecting one of the white RMZ pins to the ground.

LED Indications

There are 5 different LEDs on the box to indicate the power, Network, Battery Charging, Battery Full, etc.

The orange LED indicates that the battery is charging via USB cable, while the green LED indicates that the charge is complete and the battery is full. When dumping code or transmitting data between the device and the computer, the COM port LED illuminates.

Blue LED Indicates the status of the GSM network,

  • 1-sec Blinking = in N/W Search Mode.
  • 3 Sec Blinking = Network Acquired Successfully.
  • ½ Sec Blinking = connected to GPRS.

Default Connection

  • TX of GSM SIM800C is connected to D8 of Arduino Nano
  • RX of GSM SIM800C is connected to D7 of Arduino Nano
  • Red color TEST LED is connected to D9 of Arduino Nano.

Applications

  • Arduino Projects
  • IoT Projects
  • Realtime Projects
  • GSM based server communication
  • Data logger
  • Temperature Monitoring System
  • Sensor Data Acquisition
  • SMS Triggering etc.

DHT11 Humidity and Temperature Sensor 

The DHT 11 is a low-powered electronic device that lets us get the value of the temperature and the moisture and it uses 1 wire protocol. The sensor consists of a capacitive humidity sensor and a Thermistor connected to a high-performance 8-bit microcontroller. It measures the surrounding air and outputs a digital signal on the data pin (no analog input pins are needed).DHT11 is strictly factory calibrated that is extremely accurate on humidity calibration, hence easy to interface with other microcontrollers. Using dedicated digital module acquisition technology and temperature, and humidity sensors ensure the products with high reliability and excellent long-term stability.

The sensor had a temperature range of 0°C to 50°C with a 1°C precision. It is often used to monitor temperature and perform remedial steps in controlled conditions such as heat ventilation systems, temperature chambers, and so on. Humidity is measured in percentages ranging from 20% to 90%, with a precision of 1%.

Features:

  • Operating Voltage: 3.5V to 5.5V
  • Operating current: 0.3mA (measuring) 60uA (standby)
  • Output: Serial data
  • Temperature Range: 0°C to 50°C
  • Humidity Range: 20% to 90%
  • Resolution: Temperature and Humidity both are 16-bit
  • Accuracy: ±1°C and ±1%

Pinout for DHT11 Humidity and Temperature Sensor 

  • VCC: This pin is used to supply power to the sensor. Although supply voltage ranges from 3.3V to 5.5V, a 5V supply is recommended. With a 5V power supply, the sensor can be kept for up to 20 meters. With a 3.3V supply voltage, however, the cable length must not exceed 1 meter. Otherwise, the line voltage drop will cause measurement mistakes.
  • Data: Used to communicate between the sensor and the microcontroller.
  • NC: Not connected
  • GND: Should be connected to the ground of Arduino.

Sending Sensor Data to Thingspeak Server

To begin, we must first connect a DHT11 Sensor to the Arduino IoT Box. You can either solder some male header pins on the PCB or connect the DHT11 sensor directly.

Connect the DHT11 Sensor’s GND and VCC pins to the Arduino’s GND and 4V pins. Connect the output pin to the Arduino’s D3 as well. 

Setting up Thingspeak

ThingSpeak is an excellent tool for IoT-related applications. Using the Channels and web pages supplied by ThingSpeak, we can monitor and operate our system over the Internet. To begin, you must first sign up for ThingSpeak. So visit https://thingspeak.com and create an account.

Thingspeak Setup
  • Then set up a new channel and configure it as needed.
  • Create the API keys after that. This key is necessary for programming changes and data settings.
  • Now you may observe the online data streaming by clicking on channels.

Source Code/Program

The Arduino source code for sending GSM SIM800/900 GPRS data to Thingspeak is provided below. Simply copy and paste the code onto the Arduino Board.

Change the APN in the code. For example, Airtel’s APN is airtelgprs.com. You may look up your SIM APN on Google or through your cellphone operator.

#include <SoftwareSerial.h>
#include <DHT.h>

#define DHTPIN 3

DHT dht(DHTPIN, DHT11);

SoftwareSerial gprsSerial(8, 7);

#include <String.h>


void setup()
{
gprsSerial.begin(9600); // the GPRS baud rate
Serial.begin(9600);
dht.begin();
delay(1000);
}

void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
delay(100);

Serial.print(“Temperature = “);
Serial.print(t);
Serial.println(” °C”);
Serial.print(“Humidity = “);
Serial.print(h);
Serial.println(” %”);


if (gprsSerial.available())
Serial.write(gprsSerial.read());

gprsSerial.println(“AT”);
delay(1000);

gprsSerial.println(“AT+CPIN?”);
delay(1000);

gprsSerial.println(“AT+CREG?”);
delay(1000);

gprsSerial.println(“AT+CGATT?”);
delay(1000);

gprsSerial.println(“AT+CIPSHUT”);
delay(1000);

gprsSerial.println(“AT+CIPSTATUS”);
delay(2000);

gprsSerial.println(“AT+CIPMUX=0”);
delay(2000);

ShowSerialData();

gprsSerial.println(“AT+CSTT=\”airtelgprs.com\””);//start task and setting the APN,
delay(1000);

ShowSerialData();

gprsSerial.println(“AT+CIICR”);//bring up wireless connection
delay(3000);

ShowSerialData();

gprsSerial.println(“AT+CIFSR”);//get local IP adress
delay(2000);

ShowSerialData();

gprsSerial.println(“AT+CIPSPRT=0”);
delay(3000);

ShowSerialData();

gprsSerial.println(“AT+CIPSTART=\”TCP\”,\”api.thingspeak.com\”,\”80\””);//start up the connection
delay(6000);

ShowSerialData();

gprsSerial.println(“AT+CIPSEND”);//begin send data to remote server
delay(4000);
ShowSerialData();

String str = “GET https://api.thingspeak.com/update?api_key=19FK4V08TGZKFI5K&field1=” + String(t) +”&field2=”+String(h);
Serial.println(str);
gprsSerial.println(str);//begin send data to remote server

delay(4000);
ShowSerialData();

gprsSerial.println((char)26);//sending
delay(5000);//waitting for reply, important! the time is base on the condition of internet
gprsSerial.println();

ShowSerialData();

gprsSerial.println(“AT+CIPSHUT”);//close the connection
delay(100);
ShowSerialData();
}

void ShowSerialData()
{
while (gprsSerial.available() != 0)
Serial.write(gprsSerial.read());
delay(5000);

}

Test/Results

The GSM Module will attempt to connect to the Cellular Network after uploading the code. The connection to Thingspeak Server is then established. To observe all of the stages, open the Serial Monitor.

  • Similarly, go to Thingspeak Server’s Private view. After 30 seconds, the temperature and humidity data will be uploaded.

Sending Sensor Data to Firebase using GSM/Arduino

Let’s look at how we can use the SIM800C GSM Module and Arduino to communicate DHT11 sensor data to Google Firebase. Initially, set up the Google Firebase.

To connect the GSM to Firebase for communication, we’ll need the Firebase Host and Authentication. The Firebase Host can be found in the Realtime Database.

Firebase GMS Arduino
  • You can copy the Authentication Token from Database Secrets.

Source Code for Arduino GSM Firebase

The code below can be used to connect the GSM and Arduino to Google Firebase. But the code requires TinyGsmClient Library as well as ArduinoHttpClient Library. To begin, download and place these libraries in the Arduino Library Folder.

const char FIREBASE_HOST[] = “********************************************”;
const String FIREBASE_AUTH = “********************************************”;
  • From the above lines, change the Firebase Host and Authentication Token.
char apn[]  = “www”;//airtel ->”airtelgprs.com”
  • Change the above line to SIM or Service Provider APN.
  • You can now copy and paste the code below into the Arduino board.


#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_RX_BUFFER 256

#include <TinyGsmClient.h> //https://github.com/vshymanskyy/TinyGSM
#include <ArduinoHttpClient.h> //https://github.com/arduino-libraries/ArduinoHttpClient
#include <DHT.h>
#include <SoftwareSerial.h>

#define rxPin 7
#define txPin 8

SoftwareSerial sim800(txPin, rxPin);

#define DHTPIN 3

DHT dht(DHTPIN, DHT11);

const char FIREBASE_HOST[] = “***************************************”;
const String FIREBASE_AUTH = “***************************************”;
const String FIREBASE_PATH = “/”;
const int SSL_PORT = 443;

char apn[] = “www”;//airtel ->”airtelgprs.com”
char user[] = “”;
char pass[] = “”;


TinyGsm modem(sim800);

TinyGsmClientSecure gsm_client_secure_modem(modem, 0);
HttpClient http_client = HttpClient(gsm_client_secure_modem, FIREBASE_HOST, SSL_PORT);

unsigned long previousMillis = 0;


void setup()
{
Serial.begin(115200);
dht.begin();
Serial.println(“device serial initialize”);

sim800.begin(9600);
Serial.println(“SIM800L serial initialize”);

Serial.println(“Initializing modem…”);
modem.restart();
String modemInfo = modem.getModemInfo();
Serial.print(“Modem: “);
Serial.println(modemInfo);

http_client.setHttpResponseTimeout(10 * 1000); //^0 secs timeout
}

void loop()
{

Serial.print(F(“Connecting to “));
Serial.print(apn);
if (!modem.gprsConnect(apn, user, pass))
{
Serial.println(” fail”);
delay(1000);
return;
}
Serial.println(” OK”);

http_client.connect(FIREBASE_HOST, SSL_PORT);

while (true) {
if (!http_client.connected())
{
Serial.println();
http_client.stop();// Shutdown
Serial.println(“HTTP not connect”);
break;
}
else
{
dht_loop();
}

}

}



void PostToFirebase(const char* method, const String & path , const String & data, HttpClient* http)
{
String response;
int statusCode = 0;
http->connectionKeepAlive(); // Currently, this is needed for HTTPS

String url;
if (path[0] != ‘/’)
{
url = “/”;
}
url += path + “.json”;
url += “?auth=” + FIREBASE_AUTH;
Serial.print(“POST:”);
Serial.println(url);
Serial.print(“Data:”);
Serial.println(data);

String contentType = “application/json”;
http->put(url, contentType, data);

statusCode = http->responseStatusCode();
Serial.print(“Status code: “);
Serial.println(statusCode);
response = http->responseBody();
Serial.print(“Response: “);
Serial.println(response);

if (!http->connected())
{
Serial.println();
http->stop();// Shutdown
Serial.println(“HTTP POST disconnected”);
}

}

void dht_loop()
{
String h = String(dht.readHumidity(), 2);
String t = String(dht.readTemperature(), 2);
delay(100);

Serial.print(“Temperature = “);
Serial.print(t);
Serial.println(” °C”);
Serial.print(“Humidity = “);
Serial.print(h);
Serial.println(” %”);

String Data = “{“;
Data += “\”temp\”:” + t + “,”;

Data += “\”humid\”:” + h + “”;
Data += “}”;

PostToFirebase(“PATCH”, FIREBASE_PATH, Data, &http_client);


}

Test/Results

The Nodemcu will connect to the WiFi network after uploading the code. The serial monitor will now display the temperature and humidity readings.

Google Firebase GSM Arduino

The data is transferred to Google Firebase Database when the temperature and humidity data are presented on Serial Monitor at the same time. Simply open the Google Firebase console window and see the data’s real-time input.

Conclusion

Hope this blog helps you to understand how to use GSM & Arduino Communication with Firebase or Thingspeak. We, MATHA ELECTRONICS  will come back with more informative blogs.

Leave a Reply

Your email address will not be published.