问题
I'm trying to control 8 channel relay with esp8266. and i've been following this tutorial on youtube https://www.youtube.com/watch?v=dLmz0xdDOLk I did exactly what the guy said. I uploaded my code to Arduino and I get a "parseObject() failed" error on the serial monitor while the code trying to parse JSON i think(not sure). I don't really understand coding much so I wonder if you guys help me out with my project.(the only thing I couldn't do was to change my web host PHP version to 5.2, I use 5.3.29- obsolete)
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t D9 = 3;
static const uint8_t D10 = 1;
int delays[] = {150,300,300,100,300,550,575,};
int Tone1[] = {660,660,660,510,660,770,380,};
int Tone2[] = {100,100,100,100,100,100,100,};
const char* ssid = "ASUS 2.4GHZ";
const char* password = "mywifipassword";
const char* host = "falconiot.atwebpages.com";
String url;
int i =0;
int count = 0;
int panjur1_status = 1;
int panjur2_status = 1;
String url2 = "/api/relays/update.php?id=8&status=on";
void setup() {
for(int a = 0; i < 7; i++){
tone(D0, Tone1[i], Tone2[i]);
delay(delays[i]);}
Serial.begin(115200);
delay(100);
pinMode(D0, INPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
digitalWrite(D5, LOW);
digitalWrite(D6, LOW);
digitalWrite(D7, LOW);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Netmask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
}
void loop() {
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
if (count == 0){
url = "/api/relays/read_all.php?id=1";
count = count + 1;
Serial.println("Here1");
}
else if (count == 1){
url = "/api/relays/read_all.php?id=2";
count = count + 1;
Serial.println("Here2");
}
else if (count == 2){
url = "/api/relays/read_all.php?id=3";
count = count + 1;
Serial.println("Here3");
}
else if (count == 3){
url = "/api/relays/read_all.php?id=4";
count = count + 1;
Serial.println("Here4");
}
else if (count == 4){
url = "/api/relays/read_all.php?id=5";
count = count + 1;
Serial.println("Here5");
}
else if (count == 5){
url = "/api/relays/read_all.php?id=6";
count = count + 1;
Serial.println("Here6");
}
else if (count == 6){
url = "/api/relays/read_all.php?id=7";
count = count + 1;
Serial.println("Here7");
}
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(500);
String section="header";
while(client.available()){
String line = client.readStringUntil('\r');
//Serial.print(line);
// we’ll parse the HTML body here
if (section=="header") { // headers..
if (line=="\n") { // skips the empty space at the beginning
section="json";
}
}
else if (section=="json") { // print the good stuff
section="ignore";
String result = line.substring(1);
// Parse JSON
int size = result.length() + 1;
char json[size];
result.toCharArray(json, size);
StaticJsonBuffer<400> jsonBuffer;
JsonObject& json_parsed = jsonBuffer.parseObject(json);
if (!json_parsed.success())
{
Serial.println("parseObject() failed");
return;
}
String relay = json_parsed["relay"][0]["status"];
if(count == 1){
if(relay == "on" && panjur1_status == 1){
digitalWrite(D1, LOW);
delay(30000);
digitalWrite(D1, HIGH);
panjur1_status = 0;
Serial.println("D1 is On..!");
}
else if(relay == "off" && panjur1_status == 0){
digitalWrite(D2, LOW);
delay(30000);
digitalWrite(D2, HIGH);
panjur1_status=1;
Serial.println("D2 is Off..!");
}
}
else if(count == 2){
if(relay == "on" && panjur2_status == 1){
digitalWrite(D3, LOW);
delay(30000);
digitalWrite(D3, HIGH);
panjur2_status = 0;
Serial.println("D3 is On..!");
}
if(relay == "on" && panjur2_status == 1){
digitalWrite(D4, LOW);
delay(30000);
digitalWrite(D4, HIGH);
panjur2_status = 1;
Serial.println("D4 is Off..!");
}
}
else if(count == 3){
if(relay == "on"){
digitalWrite(D5, LOW);
Serial.println("D5 is On..!");
}
else if(relay == "off"){
digitalWrite(D5, HIGH);
Serial.println("D5 is Off..!");
}
}
else if(count == 4){
if(relay == "on"){
digitalWrite(D6, LOW);
Serial.println("D6 is On..!");
}
else if(relay == "off"){
digitalWrite(D6, HIGH);
Serial.println("D6 is Off..!");
}
}
else if(count == 5){
if(relay == "on"){
digitalWrite(D7, LOW);
Serial.println("D7 is On..!");
}
else if(relay == "off"){
digitalWrite(D7, HIGH);
Serial.println("D7 is Off..!");
}
}
else if(count == 6){
if(relay == "on"){
digitalWrite(D8, 1);
Serial.println("D8 is On..!");
}
else if(relay == "off"){
digitalWrite(D8, 0);
Serial.println("D8 is Off..!");
}
}
else if(count == 7){
if(relay == "on"){
if(D0==HIGH){
digitalWrite(D8,HIGH);
delay(50);
digitalWrite(D8,LOW);
delay(50);
client.print(String("GET ") + url2 + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(500);
}
Serial.println("PIR Working!");
}
else if(relay == "off"){
digitalWrite(D8, 0);
Serial.println("PIR Is Not Wokring!");
}
count = 0;
}
if (count == 8)
count = 0;
}
}
Serial.println();
Serial.println("closing connection");
delay(5000);
}
来源:https://stackoverflow.com/questions/61526869/arduino-iot-project-parseobject-failed