Arm Mbed OS support forum

Problem connecting to Thingspeak with 3G module

Hello everyone, I am trying to make a connection to the thingspeak server, but I don’t know why it could not get the data to the server correctly.
I use two functions, one to configure the modem and another to send, basically when I call the send function, I give it a temperature value in float format, the problem I think is in this part:

    sprintf (http_cmd, "GET https://api.thingspeak.com/update?api_key=%s&field1=%f",Update_Key,value1);
       modem.printf (http_cmd);
       modem.putc (26); // special "CTRL-Z" character

The code that manipulates the module is:

  void ModemInit()
    {
      wait(4);
      uart_debug.printf("Configuring MODEM Telit UL865-NAD");
      modem.printf("AT\r\n");   
      wait(2);
      modem.printf("AT+CGDCONT=1,\"IP\",\"web.tmovil.cl\"\r\n"); 
      wait(3);
      modem.printf("AT#SGACT=1,1\r\n");   
      wait(3);
      modem.printf("AT&K0\r\n");   
      wait(3);
      //modem.printf("AT#SD=1,0,80,\"api.thingspeak.com\",0,0,1\r\n"); 
      //wait(3);

    }

    void server_send(float value1)
    {
      wait(3);
      char* Update_Key = "HOA9X7ZGZ0HLJG9N";
      char http_cmd[300];
      uart_debug.printf("Sending Data to Server ....\r\n");
      uart_debug.printf("AT#SD=1,0,80,\"api.thingspeak.com\",0,0,1\r\n");
      modem.printf("AT#SD=1,0,80,\"api.thingspeak.com\",0,0,1\r\n"); //Open remote connection trough socket.
      wait(3);
      uart_debug.printf("AT#SSEND=1\r\n");
      modem.printf("AT#SSEND=1\r\n"); //SEnd Data
      wait(0.7);
      sprintf(http_cmd,"GET https://api.thingspeak.com/update?api_key=%s&field1=%f",Update_Key,value1);
      modem.printf(http_cmd);
      modem.putc(26); // special "CTRL-Z" character
      wait(0.5);
      uart_debug.printf("AT#SH=1\r\n");
      modem.printf("AT#SH=1\r\n"); //End Connection
      wait(0.5);
    }

Board : STM32F103 aka Bluepill
Module : Telit UL865-NAD

Regards
Francisco

Hello,

did you check the response of your AT commands? I think that can help.

BR, Jan

Dear JohnnyK, the response to the command is OK, the truth is I don’t know what it could be, I don’t know if I’m creating the string wrong with sprintf, maybe that’s the problem.

Regards
Francisco

Hello,

so that OK response tell you the data was send. Now you need read response from the server that will tell you what is wrong, I think.

I found this (section Examples> Write Data with GET, you can verified it from browser) and this maybe it will be useful.

BR, Jan

I made this Arduino Code and works perfect :

#include <SoftwareSerial.h>
SoftwareSerial GPRSBee(3,2); // RX, TX

float temp; 
int salida=0;
char dato;


#define SENSOR_TEMP1 "Tem"

void setup() {
  
  Serial.begin(9600); 
  GPRSBee.begin(9600);
  delay(1000);
  
    while(salida==0)
    {
    configuracionGPRSBee();  
    delay(1000);
    }
  serial();
}

void loop() {

  temp= 24.5;
  String Tem = String(temp);
  SendtempGPRSBee(SENSOR_TEMP1, Tem); 
  delay(20000);
   
  }

int serial() 
{
  while(Serial.available() != 0)
  {
    dato = Serial.read();
    GPRSBee.print(dato);
  }
  while(GPRSBee.available() != 0)
  {
    dato = GPRSBee.read();
    Serial.print(dato);
  }
 }

int configuracionGPRSBee()
{

  serial();
  GPRSBee.println("AT");   
  serial();
  delay(3000);
  GPRSBee.println("AT+CGDCONT=1,\"IP\",\"web.tmovil.cl\""); 
  serial();
  delay(3000);
  GPRSBee.println("AT#SGACT=1,1");   
  serial();
  delay(3000);
  GPRSBee.println("AT&K0");   
  serial();
  delay(3000);
  GPRSBee.println("AT#SD=1,0,80,\"api.thingspeak.com\",0,0,1"); 
   serial();
  delay(3000);
  salida=1;
}

int SendtempGPRSBee(String sensor_name, String valor)
{
  GPRSBee.println("AT#SD=1,0,80,\"api.thingspeak.com\",0,0,1"); 
  delay(3000);
  serial();
  delay(1000);
  GPRSBee.println("AT#SSEND=1"); 
  serial();
  delay(700);
  GPRSBee.println("GET https://api.thingspeak.com/update?api_key=HOA9X7ZGZ0HLJG9N&field1="+valor); //pagina web a la que enviamos los datos WEXO4RBDQ7AOHBW2 api KEY
  serial();
  delay(2000);
  GPRSBee.println((char)26); //Caracter para finalizar el mensaje
  delay(500);
  serial();
  GPRSBee.println("AT#SH=1"); 
  delay(500);
  serial();
}

So, my problem is in the part of MBED where I send the data, in arduino it is done as follows:
GPRSBee.println (“GET https://api.thingspeak.com/update?api_key=HOA9X7ZGZ0HLJG9N&field1=”+value);
In the case of arduino, the float value is converted to string.
In the case of mbed, it is done like this:

modem.printf (“AT # SSEND = 1 \ r \ n”); // It works well
wait (0.7);
sprintf (http_cmd, “GET https://api.thingspeak.com/update?api_key=%s&field1=%f”,Update_Key,value1); // Here may be the problem.
modem.printf (http_cmd); // Here may be the problem.
modem.putc (26); // It works fine apparently since the send command is terminated
wait (0.5);
uart_debug.printf (“AT # SH = 1 \ r \ n”);
modem.printf (“AT # SH = 1 \ r \ n”); //It works well
wait (0.5);

Regards
Francisco

Ok
I am not familiar with arduino but println method probably include also \r\n at the end of line automatically.
Yes, it is it - Serial.println() - Arduino Reference

You can try to change this

      sprintf(http_cmd,"GET https://api.thingspeak.com/update?api_key=%s&field1=%f",Update_Key,value1);
      modem.printf(http_cmd);

to this

 modem.printf("GET https://api.thingspeak.com/update?api_key=%s&field1=%f\r\n",Update_Key,value1);

And if you want to convert it to string before you can try function to_string (float val); of STD.

 modem.printf("GET https://api.thingspeak.com/update?api_key=%s&field1=%s\r\n",Update_Key, to_string(value1).c_str());

BR, Jan

Succes!

modem.printf("GET https://api.thingspeak.com/update?api_key=HOA9X7ZGZ0HLJG9N&field1=%f\r\n",value1);

Thanks so much!!!

Regards
Francisco

1 Like

That is great :slight_smile:
Good luck with your project. :+1:

BR, Jan

1 Like