ESP8266 ESP-01 intermittently but regularly returning -301 error

조회 수: 14 (최근 30일)
BRIAN MINOR
BRIAN MINOR 2020년 11월 12일
댓글: tebraxin tebraxin 2021년 9월 7일
I'm using the ThingSpeak library that I had downloaded from github on 10/27/20. I'm sending data to ThingSpeak just over every 15 seconds. The data will send without issue a couple of times and will return a value of 200, and then the next time will return a value of -301. I've incorporated a while loop to immediately send the data again when encountering this, and it always goes through successfully the second time. The only issue is that when I'm getting the -301 error it's pausing the program for about 10 seconds. When researching this issue, I was finding examples where the user was unable to send data at all, I wasn't really seeing examples of intermittent issues like mine. Any thoughts on what the issue could be?
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
//------- WI-FI details ----------//
char ssid[] = "xxxxxxxxx"; //SSID here
char pass[] = "xxxxxxxxx"; // Passowrd here
//-----------------------------//
//----------- Channel details ----------------//
unsigned long Channel_ID = xxxxxxxxx; // Your Channel ID
const char * myWriteAPIKey = "xxxxxxxxxx"; //Your write API key
//-------------------------------------------//
#define SENSOR 2
const int Field_Number_1 = 1;
int sensorVal = 0;
unsigned long timeNow = 0;
unsigned long timerStart = 0;
unsigned long timerStart2 = 0;
unsigned long instance = 0;
int writeSuccess = 0;
WiFiClient client;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
pinMode(SENSOR, INPUT);
internet();
}
void loop() {
// put your main code here, to run repeatedly:
timeNow = millis();
internet();
if (timeNow - timerStart > 2000) {}
sensorVal = digitalRead(SENSOR);
Serial.println(sensorVal);
timerStart = millis();
}
upload();
}
void internet() {
if (WiFi.status() != WL_CONNECTED) {
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
delay(5000);
}
}
}
void upload() {
if (timeNow - timerStart2 > 15100) {
ThingSpeak.setField(1,(String)sensorVal);
ThingSpeak.setField(2,(String)instance);
writeSuccess = ThingSpeak.writeFields(Channel_ID, myWriteAPIKey);
Serial.print("Write success: ");
Serial.println(writeSuccess);
while (writeSuccess == -301) {
writeSuccess = ThingSpeak.writeFields(Channel_ID, myWriteAPIKey);
Serial.print("Write success: ");
Serial.println(writeSuccess);
}
timerStart2 = millis();
instance++;
}
}
  댓글 수: 1
Christopher Stapels
Christopher Stapels 2020년 11월 17일
If you are going to keep this code, please consider a small delay in between attempts to write.

댓글을 달려면 로그인하십시오.

답변 (3개)

Vinod
Vinod 2020년 11월 12일
I suspect that your router or internet connectivity gateway is caching DNS entries. This results in the request going to an IP address that is no longer the correct one for the ThingSpeak API servers, resulting in a HTTP 301 status code from the library. One way around this is to disable DNS caching. Note that this has performance implications and I would recommend thinking through the repercussions of disabling DNS caching.
Are you using a cell phone network, or, a WiFi/wired ethernet to connect your embedded device to the network? Are you using any sort of VPN or Tor service between your device or router and the public internet? These may have a bearing on the DNS cache.
  댓글 수: 1
BRIAN MINOR
BRIAN MINOR 2020년 11월 12일
Thanks! I'll research those repurcussions. I'm using a wi-fi router with no VPN or other service. I also have not set up my device with a static IP address or anything like that.

댓글을 달려면 로그인하십시오.


John Rice
John Rice 2021년 2월 16일
I had exactly this situation: -301 followed by successful post.
I pinned down the reason to a time-out setting in the code below.
/**
* Resolve the given hostname to an IP address.
* @param aHostname Name to be resolved
* @param aResult IPAddress structure to store the returned IP address
* @return 1 if aIPAddrString was successfully converted to an IP address,
* else error code
*/
int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
{
Serial.println("Started DNS stuff"); //added by me
ip_addr_t addr;
aResult = static_cast<uint32_t>(0);
waitStatusBits(WIFI_DNS_IDLE_BIT, 10000); //increased by me from 5000
clearStatusBits(WIFI_DNS_IDLE_BIT);
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
Serial.print("Err = "); Serial.println(err); //added by me
if(err == ERR_OK && addr.u_addr.ip4.addr) {
aResult = addr.u_addr.ip4.addr;
} else if(err == ERR_INPROGRESS) {
waitStatusBits(WIFI_DNS_DONE_BIT, 10000); //increased by me from 4000
clearStatusBits(WIFI_DNS_DONE_BIT);
}
setStatusBits(WIFI_DNS_IDLE_BIT);
if((uint32_t)aResult == 0){
Serial.print("DNS Failed for "); Serial.println(aHostname); //added by me
log_e("DNS Failed for %s", aHostname);
}
return (uint32_t)aResult != 0;
}
I am using ESP32 in an Arduino environment, but it might be interesting to see if the tweak to the timeouts is effective in your situation as well.
The code is part of WiFiGeneric.cpp. There are probably several such-named files on your system, so you need to find out which one is relevant. I set compile to verbose in Arduino settings to discover the path to the relevant file.
  댓글 수: 1
Christopher Stapels
Christopher Stapels 2021년 9월 7일
Thanks for the work to hunt down the setting. Ill see if there is something we can change in the ThingSpeak library to preempt this issue.

댓글을 달려면 로그인하십시오.


tebraxin tebraxin
tebraxin tebraxin 2021년 9월 7일
편집: tebraxin tebraxin 2021년 9월 7일
hello to everybody
i'm also using Arduino Uno with ethernet shield, and I still have error 301 with the example code (write multiple field)
I already read a lot of forum, questions etc.. all around internet but I still have the problem.
My network work cooretly with all my divecies,
I tried to change internal IP address, DNS IP address, cables, example codes form library Tingspeak... without solving the problem
thanks
  댓글 수: 2
Martin Rice
Martin Rice 2021년 9월 7일
I think the currently distributed WiFi library has corrected the time-outs. It might be worth un-installing Arduino and then re-installing, in order to get an up-to-date WiFi library. I don't think the ThingSpeak library is at fault.
tebraxin tebraxin
tebraxin tebraxin 2021년 9월 7일
thanks, but i'm not using wifi, i'm using arduino uno with ethernet shield, with cable
thanks

댓글을 달려면 로그인하십시오.

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Write Data to Channel에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by