Getting -401 error code when using WritingMulitpleFields

23 views (last 30 days)
I modified the WriteMultipleFields example to include a MAX31855 which measures the board temp and a K-type thermocouple. I seem to be getting the "Problem updating channel. HTTP error code -401" in my seriel monitor however it seems to updating my channel properly. I copied and pasted my API so I dont think there is a typo, especially since it updates the channel. Any ideas on how to fix this? or can it be left alone since it still works?
Also, can you change the timezone in the visualization from GMT to a different zone? I updated my profile but that didn't do it
I am using an Arduino Nano 33 IoT

Accepted Answer

Christopher Stapels
Christopher Stapels on 17 May 2021
There are two calls to writefields in your code, I don't see a delay between them. I see a delay at the end of 20 seconds. I would expect you to get a 429 error, but can you try removing one of the calls to writefields, and see if the error stops? The channel would update correctly as you said, and then also return the error on the second call.
When you chnge your prodile time zone your charts should update to the profile timezone unless they are custom visualizations witha set timezone. Can you sign out and in and then let us know if you still see this problem?
  4 Comments
Adam
Adam on 18 May 2021
Thank you I somehow missed that. No more error however as mentioned, the timezone doesn't seem to update.
Christopher Stapels
Christopher Stapels on 18 May 2021
Can you share a screen shot? When I hover on my plots, it says GMT -4 and shows the time in my local time zone.

Sign in to comment.

More Answers (1)

Vinod
Vinod on 15 May 2021
What is most likely happening is you are making requests to the server far exceeding your allowed data rate. One out of every 15 or more requests is being accepted and updates your channel. The other requests get a 401 and are rejected. These numbers assume you have a free license.
You must immediately reduce your rate of updating ThingSpeak to less than one update every 15s. Otherwise, it will be treated as a deliberate abuse of service and appropriate actions taken.
  2 Comments
Adam
Adam on 15 May 2021
Thanks for the reply. I have a delay of 20 seconds which is included in the example code, so I don't think that would be causing it.
Adam
Adam on 15 May 2021
#include "secrets.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
#include "Adafruit_MAX31855.h"
#define MAXDO 3
#define MAXCS 4
#define MAXCLK 5
#include <SPI.h>
#include <WiFiNINA.h>
// initialize the Thermocouple
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
// Initialize our values
float number1 = 0;
float number2 = 0;
float number3 = 0;
float t2=0;
double Tem;
//int number3 = random(0,100);
//int number4 = random(0,100);
String myStatus = "";
unsigned long StartTime = millis();
unsigned long Previous;
void setup() {
Serial.begin(115200); // Initialize serial
pinMode(LED_BUILTIN, OUTPUT); //Initializes LED for diangostics
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != "1.0.0") {
Serial.println("Please upgrade the firmware");
}
ThingSpeak.begin(client); //Initialize ThingSpeak
}
void loop() {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
Serial.print("Signal: ");
int number4=WiFi.RSSI();
Serial.print(number4);
Serial.println(" dBm");
Serial.println("");
ThingSpeak.setField(4,number4);
//// MAX31855 STUFF/////////////////////////////////////////////////////////////////
double c = thermocouple.readCelsius();
if (isnan(c)) {
Serial.println("Something wrong with thermocouple!");
} else {
//Serial.print("C = ");
//Serial.println(c);
}
Serial.print("F = ");
Serial.println(thermocouple.readFahrenheit());
Tem = thermocouple.readInternal();
Serial.print("Internal Temp = ");
number2=(Tem*1.8)+32;
Serial.println(number2);
number1 = thermocouple.readFahrenheit();
ThingSpeak.setField(1,number1);
ThingSpeak.setField(2,number2);
//Calculating Temp rate per hour
unsigned long CurrentTime = millis();
float t1=number1;
unsigned long ElapsedTime = (CurrentTime- StartTime)/1000;
Serial.print("Previous= ");
Serial.println(Previous);
Serial.print("Current= ");
Serial.println(CurrentTime);
Serial.print("Start= ");
Serial.println(StartTime);
Serial.print("Elapsed= ");
Serial.println(ElapsedTime);
double delta=(CurrentTime-Previous);
delta=delta/1000;
Serial.print("delta= ");
Serial.println(delta);
number3=(t1-t2)*(3600/delta);
Previous=CurrentTime;
Serial.print("Previous= ");
Serial.println(Previous);
t2=t1;
if (number3 > 1000 || number3 <= 0) number3=0;
ThingSpeak.setField(3,number3); //Sends number3 (temp rate)
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.print("Temp Rate=");
Serial.print(number3);
Serial.println (" F/hour");
digitalWrite(LED_BUILTIN, LOW);
//MAX31855 Stuff ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// set the fields with the values
// ThingSpeak.setField(1, number1);
//ThingSpeak.setField(2, number2);
// ThingSpeak.setField(3, number3);
// ThingSpeak.setField(4, number4);
// figure out the status message
if(number1 > number2){
myStatus = String("field1 is greater than field2");
}
else if(number1 < number2){
myStatus = String("field1 is less than field2");
}
else{
myStatus = String("field1 equals field2");
}
// set the status
ThingSpeak.setStatus(myStatus);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(20000); // Wait 20 seconds to update the channel again delay(15000);
digitalWrite(LED_BUILTIN, HIGH);
unsigned long StartTime = millis();
Serial.println(" ");
Serial.println("----------------------------------------");
Serial.println(" ");
}

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Act on Data 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!