BRIAN MINOR in MATLAB Answers
Letzte Aktivitätam 7 Sep. 2021

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++; } }

Info zu ThingSpeak

The community for students, researchers, and engineers looking to use MATLAB, Simulink, and ThingSpeak for Internet of Things applications. You can find the latest ThingSpeak news, tutorials to jump-start your next IoT project, and a forum to engage in a discussion on your latest cloud-based project. You can see answers to problems other users have solved and share how you solved a problem.