ESP8266 ESP-01 intermittently but regularly returning -301 error

23 views (last 30 days)
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 Comment
Christopher Stapels
Christopher Stapels on 17 Nov 2020
If you are going to keep this code, please consider a small delay in between attempts to write.

Sign in to comment.

Answers (3)

Vinod
Vinod on 12 Nov 2020
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 Comment
BRIAN MINOR
BRIAN MINOR on 12 Nov 2020
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.

Sign in to comment.


John Rice
John Rice on 16 Feb 2021
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 Comment
Christopher Stapels
Christopher Stapels on 7 Sep 2021
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.

Sign in to comment.


tebraxin tebraxin
tebraxin tebraxin on 7 Sep 2021
Edited: tebraxin tebraxin on 7 Sep 2021
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 Comments
tebraxin tebraxin
tebraxin tebraxin on 7 Sep 2021
thanks, but i'm not using wifi, i'm using arduino uno with ethernet shield, with cable
thanks

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Write Data to Channel in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!