Problem with GET requests to ThingSpeak

Hi everyone. I'm trying to make GET requests, ideally 1 per second, to the ThingSpeak server to update my ThingSpeak "channel". I'm using ESP32 and the SIM7600 4G module, I also would like to be able to make these GET calls even when the module is not connected to the PC.

This is the code I've been using:


#include <stdio.h>
#include <string.h>
 
//Change the API key to yours
String Apikey = "XXXXXXXXXXXXX";
 
#define DEBUG true
  
void setup()
{
    Serial.begin(115200);
    //while (!Serial)
    {
      ; // wait for Arduino serial Monitor port to connect
    }
    Serial1.begin(115200);
  
    //Serial1.begin(UART_BAUD, SERIAL_8N1, MODEM_RXD, MODEM_TXD);
  
    delay(5000);
 
    
    /*ModuleState = moduleStateCheck();
    if (ModuleState == false) //if it's off, turn on it.
    {
        digitalWrite(PWR_KEY, LOW);
        delay(3000);
        digitalWrite(PWR_KEY, HIGH);
        delay(10000);
        Serial.println("Now turnning the SIM7600 on.");
    }*/
    
 
    sendData("AT+CCID", 3000, DEBUG);
    sendData("AT+CREG?", 3000, DEBUG);
    sendData("AT+CGATT=1", 1000, DEBUG);
    sendData("AT+CGACT=1,1", 1000, DEBUG);
    sendData("AT+CGDCONT=1,\"IP\",\"apn\"", 1000, DEBUG);
 
    //sendData("AT+CIPSTART=\"TCP\",\"www.mirocast.com\",80", 2000, DEBUG);
    Serial.println("4G HTTP Test Begin!");
 
    delay(1000);
}
 
void loop()
{
   
   float a = 8;
   float b = 7;
   float c = 6;
   float d = 5;

    //-----------HTTP---------------------
    String http_str = "AT+HTTPPARA=\"URL\",\"https://api.thingspeak.com/update?api_key=" + Apikey + "&field1=" + (String)a + "&field2=" + (String)b + "&field3=" + (String)c + "&field4=" + (String)d + "\"\r\n";
    Serial.println(http_str);
 
    sendData("AT+HTTPINIT\r\n", 2000, DEBUG);
    sendData(http_str, 2000, DEBUG);
    sendData("AT+HTTPACTION=0\r\n", 3000, DEBUG);
    sendData("AT+HTTPTERM\r\n", 3000, DEBUG);
 
    delay(5000);   
}
 
bool moduleStateCheck()
{
    int i = 0;
    bool moduleState = false;
    for (i = 0; i < 5; i++)
    {
        String msg = String("");
        msg = sendData("AT", 1000, DEBUG);
        if (msg.indexOf("OK") >= 0)
        {
            Serial.println("SIM7600 Module had turned on.");
            moduleState = true;
            return moduleState;
        }
        delay(1000);
    }
    return moduleState;
}
 
String sendData(String command, const int timeout, boolean debug)
{
  String response = "";
  Serial1.println(command);
  
  long int time = millis();
  while ( (time + timeout) > millis())
  {
    while (Serial1.available())
    {
      char c = Serial1.read();
      response += c;
    }
  }
  if (debug)
  {
    Serial.print(response);
  }
  return response;
}

However when I run it I get these messages, as the whole is still initializing:

AT+HTTPINIT

ERROR
AT+HTTPPARA="URL","https://api.thingspeak.com/update?api_key=750EQRL0OSQGXXVC&field1=1&field2=2&field3=3&field4=4"

OK
AT+HTTPACTION=1

OK

+HTTPACTION: 1,406,0
AT+HTTPINIT

ERROR
AT+HTTPPARA="URL","https://api.thingspeak.com/update?api_key=750EQRL0OSQGXXVC&field1=1&field2=2&field3=3&field4=4"

OK
AT+HTTPACTION=1


and then, when the sketch is uploaded, I get tons of � and no GET requests. Any ideas?

Edoardo

That seems quite fast to me. Have you checked what Thingspeak's policy is on frequency of requests?

What do you mean by "the whole is still initializing"?

Are you telling me the output you shared above is from before you uploaded the sketch? What sketch did you previously upload? The output would be from that. But why are you even checking serial monitor before you have uploaded your sketch?

Those strange symbols are usually because of a mismatching baud rate, either between the PC and the ESP or between the ESP and the 4G module. Or maybe poor quality wiring.

Not today's problem, but this code will eventually cause problems, when it has been running for 49 days:

while ( (time + timeout) > millis())

Change it like this:

while (millis() - time < timeout)

and you will not encounter the "millis rollover" problem.