In this project we will learn about IoT using GSM Module. We will use SIM800/900 GSM Module with STM32 Microcontroller, i.e STM32F103C8T6 and send the DHT11 Sensor data to Thingspeak Server.
Overview
Nowadays we use ESP8266 or ESP32 or any other wifi Modules to send any sensor data to the Internet wirelessly. Hence Wifi comes into action and thus we need Wifi Connection for wireless communication with any server. But the disadvantage of using Wifi is, it is not available everywhere. The wifi signal is limited to certain locations and to a certain range up to a few meters. For example, in order to use IoT Connectivity and to get data from the farmer’s fields, we can’t rely on Wifi due to unavailability. Similarly forest, river zone, mountains are the areas where wifi connection is not available.
So, GSM GPRS is the only alternative left as per the present scenario and current technology. GSM GPRS Module allows you to add location-tracking, voice, text, SMS, and data to your application. The big advantage of GSM/GPRS Connectivity is, it covers a wide area and signal/connectivity is available almost everywhere.
So in this project, we will learn about the Internet of Things using GSM GPRS Module, i.e Cellular IoT. We will take SIM900A as a reference GSM GPRS Module and Interface it with STM32 Microcontroller, i.e STM32F103C8T6. We will sense the surrounding humidity and temperature using DHT11 Humidity/Temperature Sensor. The humidity & temperature data will be sent to Thingspeak server using AT Commands for GSM Module. ThingSpeak server is an open data platform and API for the Internet of Things that enables you to collect, store, analyze, visualize, and act on data from sensors. So let us learn all about GSM Module with Thingspeak & STM32.
You can check one of our post related to similar system where the data is sent in JSON Format to any server: STM32 & SIM900/800 HTTP Post Request in JSON Format
Bill of Materials
Following are the components required for learning the Cellular IoT project. All the components can be easily purchased from Amazon. The component purchase link is given below.
| S.N. | Components | Quantity | Purchase Links |
|---|---|---|---|
| 1 | STM32F103C Microcontroller | 1 | Amazon | AliExpress |
| 2 | SIM800/900 GSM Module | 1 | Amazon | AliExpress |
| 3 | DHT11 Sensor | 1 | Amazon | AliExpress |
| 4 | 12/9V Power Supply | 1 | Amazon | AliExpress |
| 5 | Connecting Wires | 10 | Amazon | AliExpress |
| 6 | Breadboard | 1 | Amazon | AliExpress |
SIM900A GSM/GPRS Module
The SIM900A is a readily available GSM/GPRS module, used in many mobile phones and PDA. The module can also be used for developing IoT (Internet of Things) and Embedded Applications. It works on frequencies 900/ 1800 MHz. The Modem comes with an RS232 interface, which allows you to connect PC as well as a microcontroller with RS232 Chip(MAX232). The baud rate is configurable from 9600-115200 through AT command. The GSM/GPRS Modem is having internal TCP/IP stack to enable you to connect with the internet via GPRS. It is suitable for SMS, Voice as well as DATA transfer applications in the M2M interface.
The onboard Regulated Power supply allows you to connect a wide range of unregulated power supply. Using this modem, you can make & receive audio calls, Send & Read SMS, GPRS Internet, etc through simple AT commands.
SIM900A GSM/GPRS Modem Fetures
1. Input Voltage: 12V DC
2. Supports MIC, Audio Input & Speakers
3. Dual-Band GSM/GPRS 900/ 1800 MHz
4. RS232 interface for direct communication with computer or MCU kit
5. Configurable baud rate
6. Wire Antenna ( SMA connector with GSM Antenna Optional )
7. SIM Cardholder
8. Built-in Network Status LED
9. Inbuilt Powerful TCP/IP protocol stack for internet data transfer over GPRS
01. DATA GPRS: download transfer max is 85.6KBps, Upload transfer max 42.8KBps
Circuit/Connection between GSM Module STM32 & DHT11
Here is a circuit diagram for interfacing SIM800/900 GSM Module with STM32 (STM32F103C8T6) & DHT11 for Sending data to Thingspeak Server. The connection is fairly simple as shown below.
SIM800/900 is a UART Module. We need to connect SIM900A with STM32 UART Pin for Serial Communication. STM32F103C8T6 has 4 UART Pins. So I will use UART 3 for interfacing SIM900A. So, connect the SIM900 TX & RX Pins to STM32F103C8T6 pin PB10 & PB11 respectively. Supply the GSM Module with 9V/12V Power supply. Similarly, connect the VCC & GND Pin of DHT11 Sensor to STM32F103C8T6 5V & GND. Connect its output pin to PA0 of STM32F103C8T6.
Setting up Thingspeak
ThingSpeak provides very good tool for IoT based projects. By using ThingSpeak site, we can monitor our data and control our system over the Internet, using the Channels and web pages provided by ThingSpeak. So first you need to sign up for ThingSpeak. So visit https://thingspeak.com and create an account.
Then create a new channel and set up what you want. The tutorial in the video below. Follow the video for more clarifications.
Then create the API keys. This key is required for programming modifications and setting your data.
Now click on channels so that you can see the online data streaming
Source Code/Program
The source code/program to Send GSM SIM800/900 GPRS Data to Thingspeak with STM32 (STM32F103C8T6) is given below. No libraries are required. You can simply copy the code and upload to the STM32 Board.
But before that, you have to make two changes in the code. Change your APN Services first. In my case, I am using Airtel SIM and the default APN for Airtel is airtelgprs.com. You can check your SIM APN from your google or cellular provider.
You also need to make changes to the API key from thingspeak.
|
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 |
#define BOARD_USART3_TX_PIN PB10 #define BOARD_USART3_RX_PIN PB11 #include <String.h> #include <DHT.h> #define DHTPIN PA0 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial3.begin(9600); // the GPRS baud rate Serial.begin(9600); dht.begin(); delay(1000); } void loop() { float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); delay(100); Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" °C"); Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %"); if (Serial3.available()) Serial.write(Serial3.read()); Serial3.println("AT"); delay(1000); Serial3.println("AT+CPIN?"); delay(1000); Serial3.println("AT+CREG?"); delay(1000); Serial3.println("AT+CGATT?"); delay(1000); Serial3.println("AT+CIPSHUT"); delay(1000); Serial3.println("AT+CIPSTATUS"); delay(2000); Serial3.println("AT+CIPMUX=0"); delay(2000); ShowSerialData(); Serial3.println("AT+CSTT=\"airtelgprs.com\"");//start task and setting the APN, delay(1000); ShowSerialData(); Serial3.println("AT+CIICR");//bring up wireless connection delay(3000); ShowSerialData(); Serial3.println("AT+CIFSR");//get local IP adress delay(2000); ShowSerialData(); Serial3.println("AT+CIPSPRT=0"); delay(3000); ShowSerialData(); Serial3.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection delay(6000); ShowSerialData(); Serial3.println("AT+CIPSEND");//begin send data to remote server delay(4000); ShowSerialData(); String str="GET https://api.thingspeak.com/update?api_key=O13AOCHYYNU2LQ19&field1=" + String(temperature) +"&field2="+String(humidity); Serial.println(str); Serial3.println(str);//begin send data to remote server delay(4000); ShowSerialData(); Serial3.println((char)26);//sending delay(5000);//waitting for reply, important! the time is base on the condition of internet Serial3.println(); ShowSerialData(); Serial3.println("AT+CIPSHUT");//close the connection delay(100); ShowSerialData(); } void ShowSerialData() { while(Serial3.available()!=0) Serial.write(Serial3.read()); delay(5000); } |
Results & Observations
Once the code is uploaded to STM32F103C8T6 Board, the module will start responding. You can open the Serial Monitor and see the following responses there.
Similarly, you can open the Thingspeak private view and see the temperature and humidity logged data there.
So this is how you can Send GSM SIM800/900 GPRS Data to Thingspeak with STM32.















1 Comment
i tried running the code, but it says serial3 is undefined, how can i fix this?