ThingSpeak Community

Evmorfia Bataka

Things Speak will sometimes not update the fields (Feather Fona)

Evmorfia Bataka 2022-12-1 (编辑时间: 2022-12-2)
最新活动Christopher Stapels 回复于 2022-12-20

Hello!
The fields sometimes will update most of the times but not always. The Serial monitor shows the following output when it does not update:
while when it update it shows the following:
There is no issue with the update rate since it is more than 15 seconds.
Iam using a feather fona and the chunk of the code Iam using to send the data is the following:
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=\"internet\"");//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(8000);
ShowSerialData();
delay(4000);
gprsSerial.println(str3);//begin send data to remote server
delay(8000);
ShowSerialData();
delay(6000);
gprsSerial.println((char)26);//sending
delay(6000);//waitting for reply, important! the time is base on the condition of internet
gprsSerial.println();
ShowSerialData();
gprsSerial.println("AT+CIPSHUT");//close the connection
delay(3000);
ShowSerialData();
delay(3000);
}
Thank you in advance!
Christopher Stapels
Christopher Stapels 2022-12-1
My guess is that your device is disconnecting before the server sends a response. Lookng at your code however, you have quite long delays built it. It may also be possible that your device is disconnecting while its waiting for you to do something. I would condsider using loops where you check the time (perhaps with millis()) and consider using yield instead of delay, or even reducing the delay slightly to give the device time to take action. (maybe 1 second instead of 6)
Also, this comment makes me wonder if your connection is poor:
delay(6000);//waitting for reply, important! the time is base on the condition of internet
Evmorfia Bataka
Evmorfia Bataka 2022-12-1 (编辑时间: 2022-12-1)
Thank you so much for your prompt reply!
I increased the delays because of this issue. Originally, i used the following code:
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=\"airtelgprs.com\"");//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=xxxxxxxxxxxxxxxx&field1=" + String(t) +"&field2="+String(h);
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);
}
which I found on this link:
Will use yield instead and see what happens.
Thanks again!
Christopher Stapels
Christopher Stapels 2022-12-1
Can you try moving the device to improve the connection? I was kind of spitballing with yield, I confess I havent had much luck that way. Let us know though please.
Evmorfia Bataka
Evmorfia Bataka 2022-12-2
I think it is not a connection issue since the reception in this location is proven to be optimal.
I used yield but when I searched about it I figured out it is applied mostly for esp8266. For a 32u4 feather I believe it will not work properly (but will try for sure).
What I did now is to decrease the 6000 to 4000 seconds of delay. I will do some trial an error on fixing those delays and will share the progress.
Once again, thank you so much!
Christopher Stapels
Christopher Stapels 2022-12-2 (编辑时间: 2022-12-2)
I havent seen any other examples for AT command set that use that much delay except when the connection is poor.
Another thing you can try for waiting is
startTime=millis(); //when the request starts
while (millis()-startTime)<waitTime { //or while there is no response from the device
delay(1);
//also can check the serial output for data here }
Evmorfia Bataka
Evmorfia Bataka 2022-12-20
Hello!
It has been 7 days since I used the new code and the performance is much better.
As I posted above I decreased the delay but also changed the delay in the function
ShowSerialData();
to millis() as you suggested.
void ShowSerialData()
{
while(gprsSerial.available()!=0)
Serial.write(gprsSerial.read());
int startTime=millis(); //when the request starts
while (millis()-startTime<4000){ //or while there is no response from the device
//delay(1);
}
}
It only fails to update 1 out of 24 updates per day (much better than before) or sometimes it doesnt fail at all per day. So I am convinced now that the key is to replace the delay for sure. Maybe some modifications in the other delays too will give the optimal solution.
Notice that I didnt use the delay(1) at all.
I realized that it only "freezes" the system and probably this is why it fails.
Thanks again for your valuable advices!
Cheers!
Christopher Stapels
Christopher Stapels 2022-12-20
In the showSerialData method, you could check the response and then disconnect and complete the transaction only if the response is correct.
Perhaps you could log the response to see what is happening when the post fails.
Another thing you could try is to repost if the proper response isnt provided. You can have a look at the code in the write data example in the thingspeak arduino library to see how to test the response.
I appreciate you updating the thread. Im glad to hear you made progress.