Need faster Thingspeak connection timeout in ESP32 code

Pete Shepherd on 16 Aug 2022 (Edited on 16 Aug 2022)
Latest activity Reply by Christopher Stapels on 18 Aug 2022

I have an ESP32 that has 5 values (solarW, batteryV, pumpW, pumpRPM and pumpLPM) that I'm writing to 5 fields on a Thingspeak channel.
if ((millis() - fifteenTstart) > 15000){
Serial.println("Updating Thingspeak....");
ThingSpeak.setField(1, solarW);
ThingSpeak.setField(2, batteryV);
ThingSpeak.setField(3, pumpW);
ThingSpeak.setField(4, pumpRPM);
ThingSpeak.setField(5, pumpLPM);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}
It works fine when there's a good connection to the internet and Thingspeak, but there's a 10 second or so delay when Thingspeak is unavailable (WiFi, internet or website non-connectivity).
I would like the code to recognise that there's no ThingSpeak connection musch faster (1 second or less woud be good, but I'll take two!).
Or, if it's possible to just send the data out without the code waiting for a confirmation back (I only need the data to go OUT from the ESP32 to Thingspeak, nothing needs to come back).
The reason for doing this is there's other code on the ESP32 that I need to get back to, having a 10 second delay in the middle of the code ain't good for the pump safety reaction code.
Christopher Stapels
Christopher Stapels on 16 Aug 2022 (Edited on 18 Aug 2022)
MQTT is more of a fire and forget routine that you can use with ThingSpeak, though you may still have some connection delay. Our REST interface requires your client to wait for the response from the server. If you disconnect before the response with the REST interface, your data wont be posted.
I've also been able to get paralell processes running on an ESP32, here is one tutorial I've used.
Pete Shepherd
Pete Shepherd on 17 Aug 2022
Thanks Christopher. Looks like the parallel prcoessing route will be a good solution.
I didn't know the ESP32 had dual core processing, definitely worth learning about!
Christopher Stapels
Christopher Stapels on 18 Aug 2022
Multi threading is fun, but it does add some serious complexity to debugging. I would still recomend MQTT for this purpose and do the multi threads for learning sake. Good luck either way.