Error 401 while writing feild to channel from ESP32 WROOM 32

7 ビュー (過去 30 日間)
Shreeya
Shreeya 2023 年 12 月 6 日
移動済み: Christopher Stapels 2024 年 1 月 5 日
```/*
WriteSingleField
Description: Writes a value to a channel on ThingSpeak every 20 seconds.
Hardware: ESP32 based boards
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
Note:
- Requires installation of EPS32 core. See https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/boards_manager.md for details.
- Select the target hardware from the Tools->Board menu
- This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly.
ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and
analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.
Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.
For licensing information, see the accompanying license file.
Copyright 2020, The MathWorks, Inc.
*/
#include <WiFi.h>
#include "secrets.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
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;
// int number = 0;
#define sensorPin 34
void setup() {
Serial.begin(115200); //Initialize serial
ThingSpeak.begin(client); // Initialize ThingSpeak
pinMode(sensorPin, INPUT);
delay(20000);
}
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.");
}
int smokeValue = analogRead(sensorPin);
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
Serial.println("Smoke value : ");
Serial.println(smokeValue);
if(smokeValue > 100) {
int x = ThingSpeak.writeField(myChannelNumber, 1, smokeValue, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful, Code : " + String(x));
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(20000); // 20 sec
}
// change the value
// number++;
// if(number > 99){
// number = 0;
// }
delay(2000);// Wait 2 seconds to update the channel again
}```
I am getting error of 401 and all my data is correct.
// Below is secret.h file
// Use this file to store all of the private credentials
// and connection details
#define SECRET_SSID "Shreyas" // replace MySSID with your WiFi network name
#define SECRET_PASS "sarkar2003" // replace MyPassword with your WiFi password
#define SECRET_CH_ID 2369873 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "QO2EB******TSA" // replace XYZ with your channel write API Key

回答 (1 件)

Christopher Stapels
Christopher Stapels 2023 年 12 月 7 日
移動済み: Christopher Stapels 2024 年 1 月 5 日
401 is unauthorized, it means you have the wrong api and channel id combo. Use your browser window to update your channel to make sure you have the format correct. If you go to the channel view and click the api keys tab, you will see the format you can just paste into your address bar on the browser to test channel update. That will make sure you have the correct keys and channel id's
Can you please use the code button to format the code above? Also maybe take out that big comment block at the beggining? That would make the post much easier to read for a future visitor.
Please let us know if you get it working. Also, be careful with the 2 second update - you will need a paid license to update that fast. I didnt look to close at your code, there may be longer time between posts but I just wanted to make sure you know not to post too fast.

コミュニティ

その他の回答  ThingSpeak コミュニティ

カテゴリ

Help Center および File ExchangeRead Data from Channel についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by