ESP8266 connection issues but

18 次查看(过去 30 天)
Perky Whale
Perky Whale 2023-11-25
评论: Perky Whale 2023-11-29
I have a few ESP8266 running various sensor tasks, and sending GET or POST requests to Thingspeak via api.thingspeak.com. They all connect to the household router.
Recently, I became aware of a problem with the devices occasionally timing out when trying to communicate with Thingspeak. For example, here's a bit of code from a basic water level sensor:
String url = "http://api.thingspeak.com/update?api_key=xxxxxxxxxxxxx&field1=";
url += getdistance();
url += "&field8=";
url += batt;
http.begin(client, url);
int httpcode = http.GET();
Serial.println(httpcode);
String payload = http.getString();
Serial.println(payload);
http.end();
Fairly often, the connection will fail with a -1 httpcode. Sometimes it will run for weeks with no issues and then suddenly stop connecting. If I type the URL in a browser, it will always work immediately. If I connect the ESP to a mobile hotspot, it will also connect immediately with no issues. I can't find any issues at all with the router, and I've fully reset it etc. I've also confirmed the wifi connection between ESP and router is not the problem.
Sometimes one ESP will be running fine, and another will be having connection issues. The next day, the first one will have issues and the other will be fine.
So I added a bit of code to see if I could time the connection:
server = "api.thingspeak.com"
timer= millis();
while(client.connect(server,80)){
delay(10)}
t=millis()-timer;
Serial.print(t);
The devices now connect every time. But the connection can take up to 180 seconds. Some ESP's updating to older channels are much more reliable and connect within a second or two; it seems as though the more recently created channels take longer to connect to.
I'm reaching out here because I can't really think of where to begin trying to fix this. Why does a browser succeed immediately updating a channel with the api.thingspeak url but the ESP takes up to 180 seconds? Why do I not have any problems connecting the ESP via a mobile hotspot? Why does the same device work for weeks then suddenly play up?
I think I've isolated the problem to either the router or the thingspeak api, and I can't find anything wrong with the router.
Might be worth noting that I have an ESP in a heat pump which updates every 15 seconds to thingspeak by MQTT and it hasn't skipped a beat in months so maybe I should upgrade them all to MQTT.
Is the api just that flakey? I've seen other posters here with the same issue but it never seems to get resolved.

回答(1 个)

Christopher Stapels
Christopher Stapels 2023-11-27
I've seen ESP8266 with long connection times of 10-15 s, but nothing quite like what you see. I know you have the MQTT devices as a comparison, but everything else seems to point to wifi strength. Have you moved the devices or tried using an external antenna?
I have some devices in my yard, about 200' from the house or more that connect to the home wifi sporadically due to the large distance. There is definitely a temperature and humidity effect.
The API is definitely not flakey though. I have tens of other devices that update over wifi regularly for years at a time with periods of seconds, hours, and days.. We also monitor the number of requests over time, and would most likety be able to observe if there was a sudden connection issue casued by ThingSpeak, since thousands of clients would not be able to connect simultaneously.
I appreciate the work you have dont to study the problem thus far. Please let us know if you find a solution.
  3 个评论
Christopher Stapels
Christopher Stapels 2023-11-29
Definitely appreciate you sharing what you learned. Its interesting, I wonder if there is something in the DNS lookup firmware for the ESP library that is causing the delay. Are you using Arduino IDE to program? Perhaps you could upgrade or downgrade the device libraries and see if that makes a difference.
Perky Whale
Perky Whale 2023-11-29
Let me add another bit of info which I think may be important. I've noticed that the API connection times will be variable (and long) if the ESP has recently been connected to a power supply, either directly to the PC or by charging the battery via TP4056 board. If I unplug and leave it alone just on battery power for 20 minutes, often the connection time will drop substantially and it will stabilise out. I wonder if there is a floating pin which needs to be pulled high or low that might be interfering with the connection somehow. Can you think of any pins on the 8266 that might be a culprit here?

请先登录,再进行评论。

社区

更多回答在  ThingSpeak Community

类别

Help CenterFile Exchange 中查找有关 Read Data from Channel 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by