why do I get the error "Problem updating channel. HTTP error code -301" when connecting the esp board and trying to transfer the data to thingspeak
51 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
include <WiFi.h>
#include "ThingSpeak.h"
#include "DHT.h"
#define DHTTYPE DHT11
#define DHTPIN 4
#define soilpin1 34
#define soilpin2 35
#define soilpin3 32
#define soilpin4 33
#define soilpin5 25
DHT dht(DHTPIN, DHT11);
const char* ssid = "Realme 3"; // your network SSID (name)
const char* password = "REDACTED"; // your network password
WiFiClient client;
unsigned long myChannelNumber = 1702182;
const char * myWriteAPIKey = "REDACTED";
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
void setup() {
Serial.begin(9600); //Initialize serial
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected.");
}
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
float sensorValue1 = analogRead(soilpin1);
float sensorValue2 = analogRead(soilpin2);
float sensorValue3 = analogRead(soilpin3);
float sensorValue4 = analogRead(soilpin4);
float sensorValue5 = analogRead(soilpin5);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
//float T = h*2;
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
Serial.println(soilpin1);
Serial.println(soilpin2);
Serial.println(soilpin3);
Serial.println(soilpin4);
Serial.println(soilpin5);
// set the fields with the values
ThingSpeak.setField(1, h);
ThingSpeak.setField(2, t);
ThingSpeak.setField(3, soilpin1);
ThingSpeak.setField(4, soilpin2);
ThingSpeak.setField(5, soilpin3);
//ThingSpeak.setField(6, soilpin4);
//ThingSpeak.setField(7, soilpin5);
// 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.
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));
}
lastTime = millis();
}
}
1 Commento
Sinjini
il 18 Ott 2022
i have same type of issue
[WiFiEsp] Connecting to api.thingspeak.com
[WiFiEsp] >>> TIMEOUT >>>
Problem updating channel. HTTP error code -301
my code is
/*
Write temparature and humidity to cloud
Write temparature and humidity to LCD
Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds.
Hardware: Arduino compatible hardware controlling ESP8266 through AT commands. THIS IS CODE FOR THE ARDUINO, NOT THE ESP8266!
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
Copyright 2021, Dr. S Karan,PhD, sankar.karan@gmail.com, The MathWorks, Inc.
*/
#include "ThingSpeak.h"
#include "WiFiEsp.h"
#include "secrets.h"
#include "DHT.h"
#include <LiquidCrystal.h> // Include LCD library code
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// oanic button
const int buttonPin = 8; // Panic button the pin that the pushbutton is attached to
const int ledPin = 9; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
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)
WiFiEspClient client;
#define DHTPIN A3 // DHT data pin is connected to Arduino pin A3
#define DHTTYPE DHT11 // DHT 11
char temperature[] = " 00.0";
char humidity[] = " 00.0 %";
int Temp, RH;
// Connect pin 1 (on the left) of the sensor to +3.3V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 3 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX pin 7, TX pin 6 of esp 8266
#define ESP_BAUDRATE 9600
#else
#define ESP_BAUDRATE 115200
#endif
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
// Initialize our values
//int number1 = 20;
//float number1 = 20.5;
//int number2 = random(0,100);
//int number3 = random(0,100);
//int number4 = random(0,100);
String myStatus = "";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600); // Initialize serial
//Serial.println(F("DHTxx test!"));
dht.begin(); // initiate dht
lcd.begin(20, 4); // set up the LCD's number of columns and rows:
//*******************
lcd.setCursor(0, 0); lcd.print("WOMAN SAFETY DEVICE");
lcd.setCursor(0, 1); lcd.print(" GDGPS DAKSHINESWAR ");
lcd.setCursor(0, 2); lcd.print("AmT");
lcd.setCursor(8, 2); lcd.write(223); // Print degree symbol ( °)
lcd.setCursor(9, 2); lcd.write("C");
lcd.setCursor(11, 2); lcd.write("RH");
//lcd.setCursor(19, 2); lcd.write("%");
lcd.setCursor(0, 3); lcd.print("SL Normal");
lcd.setCursor(10, 3); lcd.write("PS Low");
// initialize serial for ESP module
setEspBaudRate(ESP_BAUDRATE);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
Serial.print("Searching for ESP8266...");
// initialize ESP module
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
Serial.println("found it!");
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 1 == 10) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Compute humidity
RH = h * 10;
//Read temperature in degree Celsius
Temp = t * 10;
if(Temp < 0){
temperature[0] = '-'; // If temperature < 0 put minus sign
Temp = abs(Temp); // Absolute value of 'Temp'
}
else
temperature[0] = ' '; // otherwise (temperature > 0) put space
temperature[1] = (Temp / 100) % 10 + 48;
temperature[2] = (Temp / 10) % 10 + 48;
temperature[4] = Temp % 10 + 48;
if(RH >= 1000)
humidity[0] = '1'; // If humidity >= 100.0% put '1' of hundreds
else
humidity[0] = ' '; // otherwise (humidity < 100) put space
humidity[1] = (RH / 100) % 10 + 48;
humidity[2] = (RH / 10) % 10 + 48;
humidity[4] = RH % 10 + 48;
//****************** LCD Write ******************
lcd.setCursor(3, 2); lcd.print(temperature);
lcd.setCursor(13, 2); lcd.print(humidity);
//******************
//lcd.setCursor(2, 1);
//lcd.print(temperature);
//lcd.setCursor(11, 1);
//lcd.print(humidity);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
//?????// return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
// 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.");
}
// set the fields with the values
//ThingSpeak.setField(1, number1);
//ThingSpeak.setField(2, number2);
ThingSpeak.setField(1, h);
delay(15000);
// ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
ThingSpeak.setField(2, t);
delay(15000);
// ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
//ThingSpeak.setField(3, number3);
//ThingSpeak.setField(4, number4);
// 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));
}
// change the values
// number1++;
// if(number1 > 99){
// number1 = 0;
// }
// number2 = random(0,100);
// number3 = random(0,100);
// number4 = random(0,100);
delay(30000); // Wait 20 seconds to update the channel again
}
// This function attempts to set the ESP8266 baudrate. Boards with additional hardware serial ports
// can use 115200, otherwise software serial is limited to 19200.
void setEspBaudRate(unsigned long baudrate){
long rates[6] = {115200,74880,57600,38400,19200,9600};
Serial.print("Setting ESP8266 baudrate to ");
Serial.print(baudrate);
Serial.println("...");
for(int i = 0; i < 6; i++){
Serial1.begin(rates[i]);
delay(100);
Serial1.print("AT+UART_DEF=");
Serial1.print(baudrate);
Serial1.print(",8,1,0,0\r\n");
delay(100);
}
Serial1.begin(baudrate);
}
kindly help.
Risposta accettata
Vinod
il 12 Apr 2022
Have you tried a different network than your phone's wifi hotspot? Do you still get 301 status code on that?
3 Commenti
Austin Jacobs
il 27 Lug 2022
Hi. I had an ESP running the same sketch and the same WiFi ssid & password for about 9 months. After taking it offline for a month and reconnecting it find its giving a -301 http error. I switched on debug to find it connects to WiFi but it cannot read from Thingspeak server over HTTPS using a certificate. I tried a different WiFi network and have the same result. Any clues? My sketch is large so would need to edit it and narrow down the problem before posting code.
Tchubby Thésard
il 30 Lug 2022
I have also newly problem with https, http unsecure is working see https://fr.mathworks.com/matlabcentral/discussions/thingspeak/755715-thingspeak-communication-library-for-arduino-down-since-19-07-2022
and
for more.
Più risposte (1)
Tchubby Thésard
il 30 Lug 2022
Don't you need to initiate WiFiClient client before ThingSpeak.begin(client) in you Setup.
I am personaly doing both that in a fonction each time I am sending a value to ThingSpeak.
0 Commenti
Community
Più risposte nel ThingSpeak Community
Vedere anche
Categorie
Scopri di più su Read Data from Channel in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!