MATLAB Answers

Getting -401 error code when using WritingMulitpleFields

4 views (last 30 days)
Adam on 14 May 2021
Edited: Vinod on 19 May 2021
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?
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 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.
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: ");
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("Signal: ");
int number4=WiFi.RSSI();
Serial.println(" dBm");
//// MAX31855 STUFF/////////////////////////////////////////////////////////////////
double c = thermocouple.readCelsius();
if (isnan(c)) {
Serial.println("Something wrong with thermocouple!");
} else {
//Serial.print("C = ");
Serial.print("F = ");
Tem = thermocouple.readInternal();
Serial.print("Internal Temp = ");
number1 = thermocouple.readFahrenheit();
//Calculating Temp rate per hour
unsigned long CurrentTime = millis();
float t1=number1;
unsigned long ElapsedTime = (CurrentTime- StartTime)/1000;
Serial.print("Previous= ");
Serial.print("Current= ");
Serial.print("Start= ");
Serial.print("Elapsed= ");
double delta=(CurrentTime-Previous);
Serial.print("delta= ");
Serial.print("Previous= ");
if (number3 > 1000 || number3 <= 0) number3=0;
ThingSpeak.setField(3,number3); //Sends number3 (temp rate)
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.print("Temp Rate=");
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");
myStatus = String("field1 equals field2");
// set the status
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
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(" ");

Sign in to comment.


More Answers in the  ThingSpeak Community

Community Treasure Hunt

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

Start Hunting!