Я работаю над iot-проектом. Я создал веб-сайт для управления моей базой данных и PHP кодами, чтобы получать значения базы данных и генерировать текст JSON. Эта сторона работает отлично, однако, когда я пытаюсь разобрать JSON и установить значения в переменные, я ничего не получаю, поэтому мне нужна ваша помощь для завершения sh моего проекта. (обратите внимание, что у меня плохой английский sh и плохое знание кодирования)
МОЙ КОД ARDUINO:
#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 = "A45CB547CBDB6A32";
const char* host = "falconiot.atwebpages.com"; //replace it with your webhost url
String url;
int i =0;
String relay ;
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);
// Parse JSON
StaticJsonBuffer<400> jsonBuffer;
char json[] = "{\"success\":1,\"relays\":[{\"id\":\"2\",\"status\":\"on\"}]}";
JsonObject& json_parsed = jsonBuffer.parseObject(json);
if (!json_parsed.success())
{
Serial.println("parseObject() failed");
return;
}
char relays_0_status = json_parsed["relays"][0]["status"];
Serial.println("DATA");
Serial.println(relays_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!");
}
}
if (count == 7){
count = 0;
}
Serial.println();
Serial.println("closing connection");
delay(5000);
}
СЕРИЙНЫЙ МОНИТОР:
WiFi connected
IP address:
192.168.0.15
Netmask: 255.255.255.0
Gateway: 192.168.0.1
connecting to falconiot.atwebpages.com
Here1
Requesting URL: /api/relays/read_all.php?id=1
DATA
JSON КОД С МОЕГО САЙТА:
{"success":1,"relays":[{"id":"2","status":"on"}]}
PHP ДЛЯ СОЗДАНИЯ JSON ФОРМАТИРОВАННЫЙ ТЕКСТ:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//Creating Array for JSON response
$response = array();
// Include data base connect class
$filepath = realpath (dirname(__FILE__));
require_once($filepath."/db_connect.php");
// Connecting to database
$db = new DB_CONNECT();
// Check if we got the field from the user
if (isset($_GET["id"])) {
$id = $_GET['id'];
// Fire SQL query to get relays data by id
$result = mysql_query("SELECT *FROM relays WHERE id = '$id'");
//If returned result is not empty
if (!empty($result)) {
// Check for succesfull execution of query and no results found
if (mysql_num_rows($result) > 0) {
// Storing the returned array in response
$result = mysql_fetch_array($result);
// temperoary user array
$led = array();
$led["id"] = $result["id"];
$led["status"] = $result["status"];
$response["success"] = 1;
$response["relays"] = array();
// Push all the items
array_push($response["relays"], $led);
// Show JSON response
echo json_encode($response);
} else {
// If no data is found
$response["success"] = 0;
$response["message"] = "No data on relays found";
// Show JSON response
echo json_encode($response);
}
} else {
// If no data is found
$response["success"] = 0;
$response["message"] = "No data on relays found";
// Show JSON response
echo json_encode($response);
}
} else {
// If required parameter is missing
$response["success"] = 0;
$response["message"] = "Parameter(s) are missing. Please check the request";
// echoing JSON response
echo json_encode($response);
}
?>