Losing connection with Thingspeak after exactly 1 hour

Adrian on 17 Oct 2023
Latest activity Edit by Adrian on 26 Oct 2023

Hi, I'm losing comms with thingspeak after exactly one hour. I'm on the free plan while I evaluate TS.
I am sending data once a minute. It works fine for one hour, and I get 200-OK responses. After exactly 1 hour, the response from TS is a 404-not found. I do a new DNS lookup, and get a new IP address for TS. Still no response. I do more DNS lookups, and get another different IP address. It cycles through 3 different IP addresses, but noe of them work one hour after bootup on my device.
I'm running an Arduino Nano with an Ethercard and the JNH library. I can still ping the arduino, serve webpages and ping a monitored device, and get NTP time, all in a 1 minute loop, so I doubt the TCP/IP stack has crashed.
after I restart the device, it works again for exactly one hour, and the I lose comms with Thingspeak again.
Any help would be appreciated!
Thanks,
Adrian
Christopher Stapels
Christopher Stapels on 17 Oct 2023
Why are you doing a manual DNS lookup?
What is your hardware?
Adrian
Adrian on 18 Oct 2023
Hi, thank you for responding.
I am running an Arduino Nano 5V with the Ethercard having an ENC28J60 ethernet chip.
The library for the ENC28J60 appears to require a resolved ip address. I will attempt it without the DNS lookup, and just using the url 'api.thingspeak.com'
Update: I have tried it without the DNS lookup. It does not work, and is sending the api request to the last device communicated with (in this case, my laptop).
Here is my code: (I have commented out the DNS lookup section of the code. This is inside the Arduino main loop)
#ifdef ENABLE_THINGSPEAK_UPDATE
// ----------------- update Thingspeak
if (!b_waiting_for_NTP_response && (millis() > TStimer) && (millis() - TStimer) >= TS_UPDATE_PERIOD_MILLIS) {
Thingspeak_timeout_count += 1;
system_temperature = read_temperature ();
cpu_voltage = read_5V_system_voltage ();
battery_voltage = read_12V_system_voltage ();
system_current = read_system_current ();
float system_watts = battery_voltage * system_current;
// ether.copyIp(ether.hisip, thingspeak_ip);
// ether.printIp(F("Stored Thingspeak ip: "), ether.hisip);
// if (thingspeak_ip[0] > 0 && Thingspeak_timeout_count < TS_TIMEOUT_LIMIT) { // keep using stored TS ip for at least 5 timeouts
// ether.copyIp(ether.hisip, thingspeak_ip);
// ether.printIp(F("Stored Thingspeak ip: "), ether.hisip);
// } else {
// Serial.println(F("ThingSpeak DNS lookup in process"));
// if (!ether.dnsLookup(TSwebsite)) {
// Serial.println(F("ThingSpeak DNS failed"));
// } else {
// ether.copyIp(thingspeak_ip, ether.hisip);
// Thingspeak_timeout_count = 0; // reset timeout count
// }
// }
ether.printIp(F("Thingspeak: "), ether.hisip);
byte sd = stash.create();
stash.print(F("field1="));
stash.print(battery_voltage);
stash.print(F("&field2="));
stash.print(cpu_voltage);
stash.print(F("&field3="));
stash.print(system_current);
stash.print(F("&field4="));
stash.print(system_watts);
stash.print(F("&field5="));
stash.print(system_temperature);
stash.save();
// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
Stash::prepare(PSTR("POST /update HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"Connection: close" "\r\n"
"X-THINGSPEAKAPIKEY: $F" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
TSwebsite, // "api.thingspeak.com"
PSTR(TS_APIKEY),
stash.size(),
sd);
// send the packet - this also releases all stash buffers once done
session = ether.tcpSend();
// added from: http://jeelabs.net/boards/7/topics/2241
int freeCount = stash.freeCount();
if (freeCount <= 3) {
Stash::initMap(56);
}
TStimer = millis();
}
const char* reply = ether.tcpReply(session);
if (reply != 0) {
Serial.println(F(" >>> TSREPLY:"));
Serial.println(reply);
ether.printIp(F("TS IP: "), ether.hisip);
if (strncmp(reply, "HTTP/1.1 404 Not Found", 22) == 0) { // PSTR isn't working here
Thingspeak_timeout_count = TS_TIMEOUT_LIMIT;
// thingspeak_ip[0] = 0; // Invalidate the TS ip address, force a new DNS lookup
} else {
Thingspeak_timeout_count = 0;
}
Adrian
Adrian on 18 Oct 2023
Christopher Stapels
Christopher Stapels on 18 Oct 2023
That seems quite involved. Im sorry I dont think I can help you much more on your specific issue except to say that the DNS address does seem to be what is changing and Most hardware I am used to uses the api.thingspeak.com address. You could consider replacing the Ether card with a low cost ESP32 board, or replacing the nano with a Nano IoT board. Can you share why you decided to use the Ethercard? Does it have some advantage for your system?
Adrian
Adrian on 18 Oct 2023
Hi Christopher,
I think you are right, I will probably need to replace the Ethercard with a better one with a better library. This was the only one I found that could piggy back the Arduino Nano easily. I will install a separate module with an ethernet card. I am not using Wifi, I need to monitor devices that are all on LAN cable connections.
Thanks for the help!
Christopher Stapels
Christopher Stapels on 18 Oct 2023
OOPS, I totally missed the ether vs wifi deal. It was all over the place. Thanks for clarification. My plugged in (ethernet) devices for IoT are usually RPi's.
Adrian
Adrian on 21 Oct 2023
Hi Christopher,
No worries. I have some further feedback.
I have removed everything else from my Arduino loop, and am still finding that comms with Thingspeak fail after an hour. So perhaps the ethercard had not crashed, but I could not be sure.
I now do one initial DNS request, and get an ip. This ip works for that hour, and then some time after that hour I start getting "404- Not Found" tcp replies.
So I re-enabled the ping porton of my loop, where i ping a local device to determine whether it has crashed. The ping monitor continues to function, but comms to TS stop after that hour. So I don't think the Ethercard has crashed.
Once my IoT solution stops communicating, I do a ping from my laptop to api.thingspeak.com and notice that it resolves to a different ip address, which I can expect, since I expect that you are using a cloud solution with many servers. How then do I resolve the problem?
Previously, after 5 failed uploads (with or without a 404- Not found response), I would do a new DNS query. I would receive a different IP which did not work, and I would not receive a '404 - Not found'. 5 missed attempts later I would get a new ip, and so it goes.
I'm at a loss to understand the problem. I don't mind a few missing packets while the code switches to a new server, but once it fails it generaly doesn't come right again. There seem to be about 4 server ip addresses involved. When I start the device, it works first time. Once it stops successfully uploading, a new server ip obtained by a DNS query does not fix it. This is most likey a problem on my end, but I'm not seeing how.
Any advice you can give would be greatly appreciated!!!
Adrian
Adrian on 21 Oct 2023
Wait, it has just come back up again after 1.5 hours (on the same ip, no new DNS lookup). I will continue to monitor. This could be an international ISP comms problem.
Christopher Stapels
Christopher Stapels on 21 Oct 2023
Appreciate the updates, let us know if you get it. I suspect you will need to continuously querry for the DNS. Im not a wiFi expert, but Im suspect its a known thing. Looks like this is a good chance for me to learn more wifi!
Adrian
Adrian on 22 Oct 2023 (Edited on 22 Oct 2023)
Thanks Cristopher,
To be clear, I'm not using WiFi, this is strictly a cabled ethernet solution.
I'm still getting exactly one hour of successful comms, and then nothing. :(
After every 5 timeouts, I try to get a new ip via DNS. The new ip will sometimes respond immediately with a HTTP 404 - Not found, but only once. After that... nothing. No response.
The ip's I've found so far are:
18.206.67.175 <- works for one hour
54.88.148.61 <- nothing
52.23.32.122 <- nothing
From this morning: Started at 08:40:33
Last successful comm at 09:40:59
Adrian
Adrian on 25 Oct 2023 (Edited on 26 Oct 2023)
Hi Christopher, no worries, we're all learning, all the time. And networking is complicated!
I have some more feedback. I was previously sending one packet of data every minute, and the server stopped accepting new data after 60 minutes.
So I increased the interval to two minutes, and now the server stops accepting after 120 minutes.
It's starting to look like there is some sort of throttling on the server side after receiving 60 packets? Is that a possibility?
Christopher Stapels
Christopher Stapels on 23 Oct 2023
I meant to learn more about network connections. Equating wifi and networking is too easy in my brain, but certainly false. Thanks again, Im presently brushing up on my DNS knowledge!