[RESOLVED] How to send data from my Arduino to Thingspeak using a GSM SIM800c ?

34 views (last 30 days)
Cécilia Facca
Cécilia Facca on 11 Jan 2022 at 13:46
Edited: Cécilia Facca on 11 Jan 2022 at 14:52
Hello :)
This is my first big project using Arduino components, so I don't really understand everything, that's why I encounter so many difficulties.
My goal is to send data from my ultrasonic sensor (HC-SR04) to Thingspeak using an Arduino UNO and a GSM SIM800c.
Material :
  • Arduino UNO ;
  • GSM SIM 800c shield (https://www.eagle-robotics.com/accueil/257-shield-sim800c-arduino.html) ;
  • Ultrasonic sensor (HC-SR04) ;
  • Cables for my ultrasonic sensor ;
  • Cable to connect my Arduino to my computer.
I want to send data to Thinspeak from everywhere, so I can't use Wifi. I found a solution on google : https://github.com/707amitkumar/Send-Data-on-cloud-using-gsm-800c/blob/main/gsmangthingspeak.txt
In order to send data to my channel on thingspeak, I did use the code mentionned on the link and I just changed the part concerning the data to send (because I want to send data from my ultrasonic sensor) :
#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(7,8);
#include <String.h>
#include <HCSR04.h>
const int trigPin = 2 ;
const int echoPin = 3 ;
UltraSonicDistanceSensor distanceSensor(trigPin, echoPin) ;
void setup()
{
gprsSerial.begin(9600); // the GPRS baud rate
Serial.begin(9600); // the GPRS baud rate
delay(1000);
}
void loop()
{
float distance = distanceSensor.measureDistanceCm();
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
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=\"tango.lu\"");//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=F2WFEAUL58M062LM&field1=0" + String(distance) ;
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);
}
When I run this code to my Arduino, I notice 2 problems :
  1. The piece of information is "sent" to my Thingspeak Channel but it doesn't match with the data sent from my sensors (It always show "0" on Thingspeak while the sensor sends a positive value :
2. I can notice that my code doesn't run as well as expected because the Serial Monitor dislay this :
I really don't kow what to do... And I thank you in advance for any help you may give me.
I hope my english is not so bad and you cand understand me :)
I wish you a good day,
Cécilia.

Accepted Answer

Christopher Stapels
Christopher Stapels on 11 Jan 2022 at 14:22
Seems like you have made a nice start on your first project! You seem pretty close to the goal.
Since you shared the API key, I was able to look at your private channel and see that you have entries corresponding to the data that I think you are interested in
{"created_at": "2022-01-11T13:55:33Z","entry_id": 10,"field1": "5.90"},
Is that the data you were looking for?
I would recommend using the API keys tab in your channel view to change your api key, then you will have to reprogram the device with the new API key. Then replace your API key with something else when you post. (i.e. "xxxxxxxxxxxxx") Once you change the API key, the key above will no longer work.
Its possible you might need additional delay in your command before disconnecting. If your device disconnects before ThingSpeak responds, ThingSpeak may not keep your data.
  1 Comment
Cécilia Facca
Cécilia Facca on 11 Jan 2022 at 14:51
Hello :)
Thanks for your answer !!
Actually, I just found what was the issue : I had the wrong APN on my AT command !!
I change it to : gprsSerial.println("AT+CSTT=\"internet\"") and everything works perfectly !
Tanks for your answer and your help :)
I wish you a good day.

Sign in to comment.

More Answers (0)

Communities

More Answers in the  ThingSpeak Community

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!