Send IoT Data to ThingSpeak using AT Commands (Arduino and SIM800L Tutorial)

Introduction

The SIM800L module is a GSM/GPRS module that can be used to connect to the internet via a cellular network. It has a built-in SIM card slot and can be used to send and receive data over a cellular network connection instead of WI-Fi. ThingSpeak is an IoT platform that allows you to send and receive data from devices over the internet.

In this tutorial, we will use the SIM800L module and ThingSpeak’s API to send data from an Arduino board to be stored in the cloud.

Hardware

To complete this tutorial, you will need the following hardware:

  • Arduino board
  • SIM800L module

Setting up the SIM800L module

First, let’s set up the SIM800L module. To use the SIM800L module with your Arduino board, you’ll need to connect it to the appropriate pins on the Arduino. The SIM800L module has a number of different pins, including power pins, serial communication pins, and control pins. You’ll need to consult the documentation for your particular module to determine which pins to use.

Once you have the SIM800L module connected to your Arduino board, you’ll need to use AT commands to communicate with it. AT commands are simple text-based commands that are used to control the module and send and receive data. Some common AT commands include AT+CPIN, which is used to enter the SIM card PIN number, and AT+CREG, which is used to check the registration status of the module.

To connect to the internet using the SIM800L module, you’ll need to attach it to the GPRS service and get an IP address. You can do this using the following AT commands:

sendATCommand("AT+CFUN=1", 2000, DEBUG); // Enable full functionality
sendATCommand("AT+CIPMUX=0", 1000, DEBUG); // Set single connection mode
sendATCommand("AT+CGATT=1", 1000, DEBUG); // Attach to GPRS service
sendATCommand("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", 1000, DEBUG); // Set connection type to GPRS
sendATCommand("AT+SAPBR=3,1,\"APN\",\"YOUR_APN\"", 1000, DEBUG); // Set APN
sendATCommand("AT+SAPBR=1,1", 1000, DEBUG); // Enable GPRS
sendATCommand("AT+SAPBR=2,1", 1000, DEBUG); // Get IP address

Setting up ThingSpeak

Now that we have the SIM800L module set up, let’s set up ThingSpeak. To use ThingSpeak’s API with your SIM800L module, you’ll need to create a new “channel” on the ThingSpeak website. A channel is a way to represent a device or a group of devices on the ThingSpeak platform. Once you’ve created a channel, you’ll be given an API key that you can use to access and update the channel from your Arduino sketch.

Sending data to ThingSpeak

To send data to ThingSpeak using the SIM800L module, you’ll need to use the AT+HTTPPARA command to set the necessary parameters for the HTTP request, and then use the AT+HTTPACTION command to send the request.

Receiving a response from ThingSpeak

After sending the request, you can use the AT+HTTPREAD command to read the response from the server. The response will be a string of text, which you can parse to check for any errors or confirm that the data was successfully sent.

Putting it all together

Here is an example of how you can use the SIM800L module and ThingSpeak’s API to send data from your Arduino board to the internet:

#include <SoftwareSerial.h>

SoftwareSerial sim800l(10, 11); // RX, TX

void setup() {
  sim800l.begin(9600);
  sendATCommand("AT+CFUN=1", 2000, DEBUG); // Enable full functionality
  sendATCommand("AT+CIPMUX=0", 1000, DEBUG); // Set single connection mode
  sendATCommand("AT+CGATT=1", 1000, DEBUG); // Attach to GPRS service
  sendATCommand("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", 1000, DEBUG); // Set connection type to GPRS
  sendATCommand("AT+SAPBR=3,1,\"APN\",\"YOUR_APN\"", 1000, DEBUG); // Set APN
  sendATCommand("AT+SAPBR=1,1", 1000, DEBUG); // Enable GPRS
  sendATCommand("AT+SAPBR=2,1", 1000, DEBUG); // Get IP address
}

void loop() {
  sendDataToThingSpeak();
  delay(20000); // delay 20 seconds
}

void sendDataToThingSpeak() {
  String cmd = "AT+HTTPPARA=\"URL\",\"http://api.thingspeak.com/update?api_key=YOUR_API_KEY&field1=42\"";
  sendATCommand(cmd, 1000, DEBUG); // Set URL
  cmd = "AT+HTTPACTION=0";
  sendATCommand(cmd, 3000, DEBUG); // Send request
  delay(1000);
  String response = sendATCommand("AT+HTTPREAD", 1000, DEBUG); // Read response
  Serial.println(response);
}

String sendATCommand(String cmd, const int timeout, boolean debug) {
  String response = "";
  sim800l.println(cmd);
  long int time = millis();
  while((time+timeout) > millis()) {
    while(sim800l.available()) {
      char c = sim800l.read();
      response += c;
    }
  }
  if (debug) {
    Serial.println(response);
  }
  return response;
}

In this example code, the sendATCommand function sends AT commands to the SIM800L module and reads the response. The sendDataToThingSpeak function is used to set the URL for the HTTP request using the AT+HTTPPARA command, send the request using the AT+HTTPACTION command, and read the response using the AT+HTTPREAD command. The loop function calls the sendDataToThingSpeak function every 20 seconds to send data (the number 42 in this case) to ThingSpeak. You can customize the code to send any data that you want to the fields of your ThingSpeak channel.

Conclusion

In this tutorial, we have learned how to use the SIM800L module and ThingSpeak’s API to send data from an Arduino board to the internet. We have set up the SIM800L module and used AT commands to connect to the internet and send HTTP requests. We have also set up a ThingSpeak channel and used the API to send data to the internet.

With these tools and techniques, you can build your own IoT projects and start collecting and analyzing data from your devices. You can use ThingSpeak to visualize the data and create charts and graphs, or you can use the data in your own applications or MATLAB analyses.

If you found this tutorial helpful and would like to stay updated with more IoT and Arduino tips and tricks, consider subscribing to my blog. I post about once per week.

4 comments

    1. ThingSpeak allows for up to eight fields.

      Just add another field for other sensor values that you want to measure: “&field2=100” for example.

      “AT+HTTPPARA=\”URL\”,\”http://api.thingspeak.com/update?api_key=YOUR_API_KEY&field1=42&field2=100\””;

    1. Yes, you can. My code, as it is written, does not account for sleeping and watchdog timers to make that happen.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.