IOT

STM32 & SIM900/800 HTTP Post Request in JSON Format

In addition to WiFi and Ethernet Modules, the SIM800/900 GSM GPRS Module is another option for IoT Projects. The primary benefit of cellular IoT over wifi IoT is the ubiquitous availability of networks and signal. 

But today, utilizing the STM32F103C8T6 as the interface between the SIM800/900 GSM GPRS Module and STM32, we’ll write code to send data to any web address or web server using API. Since transmitting a string of data directly seems challenging, we must send the data in JSON format. This post explains the entire procedure and the finished code.

Components Required

  • STM32F103C8T6 Bluepill Development Board
  • SIM800/SIM900 GSM GPRS Module
  • DHT11 Humidity Temperature Sensor
  • DS3231 Real Time Clock Module
  • 12/9V Power Supply
  • Connecting Wires
  • Breadboard

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 compact format for transferring and storing data. When information is transferred from a server to a web page, it is frequently used.

An example of a simple User object serialized to XML is the following:

<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:

{
“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

Now let’s set up some hardware. I’m using two modules, and they both send data in JSON format via an API to the server. I want to send humidity and temperature data, thus I’m using a DHT11 humidity and temperature sensor. In a similar manner, I’m also checking the time and sending it to the server using the DS3231 Real Time Clock (RTC) Module.

Here is a connection diagram that you may use to put the circuit together as indicated in the illustration.

STM32 GSM JSON API

So I made the same connection here as shown below.  Therefore, I used the computer USB port to provide the GSM Module with 12V and the STM32 Board with 5V.

STM32F103C8T6 GSM JSON

Arduino JSON Library

Using the ArduinoJson library, which was created to be the most user-friendly JSON library with the smallest footprint and most effective memory management for Arduino, is the simplest way to decode and encode JSON strings with the Arduino IDE. An IoT and C++ JSON library for Arduino is called ArduinoJson (Internet Of Things).

Features

  • JSON decoding (comments are supported)
  • JSON encoding (with optional indentation)
  • Elegant API, very easy to use
  • Fixed memory allocation (zero mallocs)
  • No data duplication (zero-copy)
  • Portable (written in C++98)
  • Self-contained (no external dependency)
  • Small footprint
  • Header-only library

Source Code: STM32 & SIM900/800 HTTP Post Request in JSON Format

The source code for the STM32 SIM900/800 GSM Module’s HTTP POST in JSON Format using the API is provided below.

You need some libraries first. Add the libraries to the Arduino IDE by downloading them from the provided URL.

1. RTC Lib for DS3231: Download

2. DHT Library for DHT11 Sensor: Download

3. Arduino JSON Library: Download

Changes to the APN should be made in the following code. Airtel’s APN was mine. Make modifications after checking your Operator APN. Additionally, modify the web server or add the API webserver URL where you want the data to be sent.

#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 <ArduinoJson.h>
StaticJsonBuffer<200> jsonBuffer;

#define BOARD_USART3_TX_PIN PB10 // Connect to TX3/RX3
#define BOARD_USART3_RX_PIN PB11 // Connect to TX3/RX3

#define DHTPIN PA0
#define DHTTYPE DHT11

RTC_DS3231 rtc;
DHT dht(DHTPIN, DHTTYPE);

char t[32];
char deviceID[12] = “MYTEST56”;


void setup()
{
Serial3.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 (Serial3.available())
Serial.write(Serial3.read());

Serial3.println(“AT”);
delay(3000);

Serial3.println(“AT+SAPBR=3,1,\”Contype\”,\”GPRS\””);
delay(6000);
ShowSerialData();

Serial3.println(“AT+SAPBR=3,1,\”APN\”,\”airtelgprs.com\””);//APN
delay(6000);
ShowSerialData();

Serial3.println(“AT+SAPBR=1,1”);
delay(6000);
ShowSerialData();

Serial3.println(“AT+SAPBR=2,1”);
delay(6000);
ShowSerialData();


Serial3.println(“AT+HTTPINIT”);
delay(6000);
ShowSerialData();

Serial3.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);

Serial3.println(“AT+HTTPPARA=\”URL\”,\”http://192.138.xxxxxxxxxxxxxxxx.php\””); //Server address
delay(4000);
ShowSerialData();

Serial3.println(“AT+HTTPPARA=\”CONTENT\”,\”application/json\””);
delay(4000);
ShowSerialData();


Serial3.println(“AT+HTTPDATA=” + String(sendtoserver.length()) + “,100000”);
Serial.println(sendtoserver);
delay(6000);
ShowSerialData();

Serial3.println(sendtoserver);
delay(6000);
ShowSerialData;

Serial3.println(“AT+HTTPACTION=1”);
delay(6000);
ShowSerialData();

Serial3.println(“AT+HTTPREAD”);
delay(6000);
ShowSerialData();

Serial3.println(“AT+HTTPTERM”);
delay(10000);
ShowSerialData;

/********************GSM Communication Stops********************/

}


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

}

Conclusion

  • The module will attempt to connect to the internet and deliver data in JSON format as soon as you upload the code to the STM32 and turn it on.
  • You might occasionally miss data and experience certain mistakes. By lengthening the delays after each step in the code above, this can be minimised. Or you can typically access the info provided the network connectivity and signal strength are good.
  • You should see the following results when you open your serial monitor.

Using STM32 & SIM900/800 for HTTP Post Request for API in JSON Format was the sole focus of this article.

Leave a Reply

Your email address will not be published.