In this post we will learn how to use SIM800 or SIM900 with Arduino & make HTTP Post Request in JSON format to any API Server.
Overview
Apart from Wifi Module or Ethernet Module, the IoT Projects can also be done through SIM800/900 GSM GPRS Module. The main advantage of Cellular IoT compared to Wifi IoT is the availability of network and signal everywhere. In one of my earlier posts, I explained how you can send the sensor data wirelessly using GSM Module to Thingspeak Server. You can check the post here: Send SIM800/900 GPRS Data to Thingspeak with Arduino
But today we will interface the SIM800/900 GSM GPRS Module with Arduino and develop a code to send the data to any web address or web server using API. We need to send the data in JSON format as direct sending of a string data looks difficult. The whole process and the final code is explained in this post.
Bill of Materials
Following are the components required to learn and get practical experience of this tutorial. All the components can be easily purchased from Amazon. The components purchase link is given as well.
| S.N. | Components | Quantity | Purchase Links |
|---|---|---|---|
| 1 | Arduino UNO Board | 1 | Amazon | AliExpress |
| 2 | SIM800/900 GSM Module | 1 | Amazon | AliExpress |
| 3 | DHT11 Sensor | 1 | Amazon | AliExpress |
| 4 | RTC Module DS3231 | 1 | Amazon | AliExpress |
| 5 | 12/9V Power Supply | 1 | Amazon | AliExpress |
| 6 | Connecting Wires | 10 | Amazon | AliExpress |
| 7 | Breadboard | 1 | Amazon | AliExpress |
What is API?
API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message or check the weather on your phone, you’re using an API.
Example of an API: When you use an application on your mobile phone, the application connects to the Internet and sends data to a server. The server then retrieves that data, interprets it, performs the necessary actions and sends it back to your phone. The application then interprets that data and presents you with the information you wanted in a readable way. This is what an API is – all of this happens via API.
What is JSON Format?
JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. It is often used when data is sent from a server to a web page.
For example, the following is an example of a simple User object serialized to XML:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<xml> <user> <firstName>Jason</firstName> <middleName>Alexander</middleName> <lastName>Smith</lastName> <address> <street1>1234 Someplace Avenue</street1> <street2>Apt. 302</street2> <city>Anytown</city> <state>NY</state> <postalCode>12345</postalCode> <country>US</country> </address> </user> </xml> |
As you can see, the same data represented in JSON is far more efficient, while retaining all of its human-readability:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "firstName" : "Jason", "middleName" : "Alexander", "lastName" : "Smith", "address" : { "street1" : "1234 Someplace Avenue", "street2" : "Apt. 302", "city" : "Anytown", "state" : "NY", "postalCode" : "12345", "country" : "US" } } |
JSON is typically used together with IoT protocols that do not provide native support for data structure serialization such as HTTP/Rest, WebSockets, MQTT, and SMQ.
In JSON, data is structured in a specific way. JSON uses symbols like { } , : ” ” [ ] and it has the following syntax:
Data is represented in key/value pairs
1. The colon (:) assigns a value to key
2. key/value pairs are separated with commas (,)
3. Curly brackets hold objects ({ })
4. Square brackets hold arrays ([ ])
Hardware Setup
Let us do some hardware setup now. I am using 2 modules whose data is to be sent to the server via API in JSON format. I am using DHT11 Humidity & Temperature Sensor as I want to send Humidity & Temperature Data. Similarly, I am also using the DS3231 Real Time Clock (RTC) Module to check the time and send it to Server.
Following is the connection diagram below assemble the circuit as shown in the figure.
So I made the same connection here as shown below. So I have powered GSM Module with 12V and Arduino with 5V from computer USB port.
Arduino JSON Library
The easiest way to decode and encode JSON strings with the Arduino IDE is using the ArduinoJson library which was designed to be the most intuitive JSON library, with the smallest footprint and most efficient memory management for Arduino. ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things).
Features
1. JSON decoding (comments are supported)
2. JSON encoding (with optional indentation)
3. Elegant API, very easy to use
4. Fixed memory allocation (zero mallocs)
5. No data duplication (zero-copy)
6. Portable (written in C++98)
7. Self-contained (no external dependency)
8. Small footprint
9.Header-only library
Source Code: SIM900/800 HTTP post request in JSON Format with Arduino
The source code for making HTTP POST in JSON Format with API & SIM900/800 GSM Module is given below.
Before that you need some libraries. Download the libraries from the following link and add to the Arduino IDE.
1. RTC Lib for DS3231: Download
2. DHT Library for DHT11 Sensor: Download
3. Arduino JSON Library: Download
In the following code, make changes to the APN. I used Airtel APN. Check your Operator APN and make changes. Also make changes to the webserver or add your api webserver address where you want to send the data.
|
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
#include <RTClib.h> // Download the library from https://github.com/adafruit/RTClib #include <Wire.h> // Library for I2C Communication with DS3231 Module #include <DHT.h> #include <SoftwareSerial.h> SoftwareSerial myserial(10, 11); // RX: 10, TX:11 #include <ArduinoJson.h> StaticJsonBuffer<200> jsonBuffer; #define DHTPIN A1 #define DHTTYPE DHT11 RTC_DS3231 rtc; DHT dht(DHTPIN, DHTTYPE); char t[32]; char deviceID[12] = "MYTEST56"; void setup() { myserial.begin(9600); // the GPRS baud rate Serial.begin(9600); Serial.println("Initializing.........."); dht.begin(); Wire.begin(); DynamicJsonBuffer jsonBuffer; if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //rtc.adjust(DateTime(2020, 02, 29, 17, 50, 40)); delay(5000); } void loop() { Serial.println(""); Serial.println("************************************************************"); float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); DateTime now = rtc.now(); sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year()); Serial.print("Device ID: "); Serial.println(deviceID); Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" °C"); Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %"); Serial.print(F("Time/Date: ")); Serial.println(t); delay(1000); /********************GSM Communication Starts********************/ if (myserial.available()) Serial.write(myserial.read()); myserial.println("AT"); delay(3000); myserial.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\""); delay(6000); ShowSerialData(); myserial.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"");//APN delay(6000); ShowSerialData(); myserial.println("AT+SAPBR=1,1"); delay(6000); ShowSerialData(); myserial.println("AT+SAPBR=2,1"); delay(6000); ShowSerialData(); myserial.println("AT+HTTPINIT"); delay(6000); ShowSerialData(); myserial.println("AT+HTTPPARA=\"CID\",1"); delay(6000); ShowSerialData(); StaticJsonBuffer<200> jsonBuffer; JsonObject& object = jsonBuffer.createObject(); object.set("deviceID",deviceID); object.set("humidity",humidity); object.set("temperature",temperature); object.set("timedate",t); object.printTo(Serial); Serial.println(" "); String sendtoserver; object.prettyPrintTo(sendtoserver); delay(4000); myserial.println("AT+HTTPPARA=\"URL\",\"http://192.xxxxxxxxxxxxxxxxxxxxxxxx.php\""); //Server address delay(4000); ShowSerialData(); myserial.println("AT+HTTPPARA=\"CONTENT\",\"application/json\""); delay(4000); ShowSerialData(); myserial.println("AT+HTTPDATA=" + String(sendtoserver.length()) + ",100000"); Serial.println(sendtoserver); delay(6000); ShowSerialData(); myserial.println(sendtoserver); delay(6000); ShowSerialData; myserial.println("AT+HTTPACTION=1"); delay(6000); ShowSerialData(); myserial.println("AT+HTTPREAD"); delay(6000); ShowSerialData(); myserial.println("AT+HTTPTERM"); delay(10000); ShowSerialData; /********************GSM Communication Stops********************/ } void ShowSerialData() { while (myserial.available() != 0) Serial.write(myserial.read()); delay(1000); } |
Result & Responses
Once you upload the code to the Arduino and Power on the module, the Module will try connecting to the internet and will send data in JSON format.
Sometimes you might miss data and get some errors. This can be reduced by increasing delays after step by step in the above code. Or if the network connectivity and signal strength is good you can get the data most of the times.
Open your serial monitor and you should get the following results.
So this was all about how to make to use SIM900/800 Module & make HTTP post request in JSON Format with Arduino.










4 Comments
hello
How can I have php code?
Hello, I would like to ask, why did you define three jsonbuffers
Thanks, this is amazing, Relay you have done the extensive efforts for creating step by step connection and data sending commands. so many times serial monitor displays OK. I could make the HTTP POST request to my own website api using this program.
A small request, there is compilation error for Json Object. It is necessary to upgrade for ver. 6. For my work insted of creating JSON data string using JSON object, I have assigned it directly. However If possible, please try to upgrade the code.