Problem updating channel. HTTP error code -301
9 次查看(过去 30 天)
显示 更早的评论
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
char ssid[] = "F"; // your network SSID (name)
char pass[] = "qwerty"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = 919998; //SECRET_CH_ID;
const char * myWriteAPIKey = "1D95P000GF21W88Z"; // SECRET_WRITE_APIKEY;
int value1 = 0, value2 = 0, value3 = 0, value4 = 0;
void setup()
{
Serial.begin(9600);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
while (!Serial) {
;
}
}
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);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
while (!Serial.available());
String incommingStr = Serial.readStringUntil('\n');
Serial.println(incommingStr);
// splitting incomingStr
int data1 = incommingStr.indexOf(','); //finds location of first ,
int value1 = incommingStr.substring(0, data1).toInt(); //captures first data String
int data2 = incommingStr.indexOf(',', data1 + 1 ); //finds location of second ,
int value2 = incommingStr.substring(data1 + 1, data2 + 1).toInt(); //captures second data String
int data3 = incommingStr.indexOf(',', data2 + 1 );
int value3 = incommingStr.substring(data2 + 1, data3 + 1).toInt();
int data4 = incommingStr.indexOf(',', data3 + 1 );
int value4 = incommingStr.substring(data3 + 1).toInt(); //captures remain part of data after last ,
// set the fields with the values
ThingSpeak.setField(1, value1);
ThingSpeak.setField(2, value2);
ThingSpeak.setField(3, value3);
ThingSpeak.setField(4, value4);
Serial.println(String(value1) + " " + String(value2) + " " + String(value3) + " " + String(value4));
// set the status
ThingSpeak.setStatus("4 fields added.");
// 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);
}
0 个评论
回答(0 个)
社区
更多回答在 ThingSpeak Community
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Act on Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!