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

28 views (last 30 days)
reshma kumara
reshma kumara on 12 Apr 2022
Commented: reshma kumara on 13 Apr 2022
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();
}
}

Accepted Answer

Vinod
Vinod on 12 Apr 2022
Have you tried a different network than your phone's wifi hotspot? Do you still get 301 status code on that?

More Answers (0)

Communities

More Answers in the  ThingSpeak Community

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by