Hi,
I am making a project that uses json to send data from Serial to webpage but it doesent work.
code:
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <Arduino.h>
const char* ssid = "Test net";
const char* password = "Abc112233Abc";
//OTA_setup
const int OTA_port = 8266;
const char* OTA_password = "admin";
//OTA_setup
//OTA
bool OTA_update = false;
uint16_t OTA_time = 0;
//OTA
//JSON_board
const int BOARD_number = 1;
const String BOARD_hardware = "Station";
const String BOARD_name = BOARD_hardware + String(BOARD_number,DEC);
const float JSON_software_version = 0.5;
//JSON_board
//COMANDS
String COMANDS_serial;
int COMANDS_devices_int = 0;
String COMAND_device = "";
bool COMANDS_devices_bool = false;
int COMANDS_number = 0;
String COMAND_no_device;
//COMANDS
//OTHER
ESP8266WebServer server;
String Gun1 = "Gun1";
const int DEVICES_int = 1;
String DEVICES[DEVICES_int] = {Gun1};
//OTHER
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WIFI_check();
OTA_setup();
COMANDS();
SERVER_web();
}
void loop() {
WIFI_check();
OTA();
server.handleClient();
COMANDS();
}
String JSON_board() {
DynamicJsonBuffer jsonBuffer;
JsonObject& root_board = jsonBuffer.createObject();
root_board["Software Version"] = JSON_software_version;
root_board["Name"] = BOARD_name;
root_board["Hardware"] = BOARD_hardware;
root_board["Number"] = BOARD_number;
String JSON_board_json;
root_board.prettyPrintTo(JSON_board_json);
return JSON_board_json;
}
void COMANDS(){
COMANDS_serial = Serial.readString();
int COMAND_contains = COMANDS_serial.indexOf(DEVICES[COMANDS_devices_int]);
if (COMAND_contains >= 0){
for (COMANDS_devices_int = 0; COMANDS_devices_bool == true; COMANDS_devices_int++){
if (COMAND_contains >= -1){
COMAND_device = DEVICES[COMANDS_devices_int];
COMAND_no_device.replace(COMAND_device, "");
COMAND_no_device.replace(" ", "");
COMANDS_devices_bool = false;
}
}
COMANDS_number++;
}
}
String JSON_comands(){
DynamicJsonBuffer jsonBuffer;
JsonObject& root_comands = jsonBuffer.createObject();
root_comands["Gun1"]["comand_number"] = "null";
root_comands["Gun1"]["comand"] = "null";
root_comands[COMAND_device]["comand_number"] = COMANDS_number;
root_comands[COMAND_device]["comand"] = COMAND_no_device;
String JSON_comands_json;
root_comands.prettyPrintTo(JSON_comands_json);
return JSON_comands_json;
}
void SERVER_web() {
server.on("/restart", []() {
server.send(200, "text/plain", "Restarting ESP");
ESP.restart();
});
server.on("/update", []() {
server.send(200, "text/plain", "Update Called \n5 seconds left");
OTA_update = true;
OTA_time = 0;
});
server.on("/board", []() {
server.send(200, "text/json", JSON_board());
});
server.on("/comands", []() {
server.send(200, "text/json", JSON_comands());
});
server.begin();
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void OTA_setup() {
ArduinoOTA.setPort(OTA_port);
ArduinoOTA.setHostname(BOARD_name.c_str());
ArduinoOTA.setPassword(OTA_password);
ArduinoOTA.onStart([]() {
String OTA_type;
if (ArduinoOTA.getCommand() == U_FLASH) {
OTA_type = "sketch";
} else { // U_SPIFFS
OTA_type = "filesystem";
}
Serial.println("OTA Start updating " + OTA_type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nOTA End");
});
ArduinoOTA.onProgress([](unsigned int OTA_progress, unsigned int OTA_total) {
Serial.printf("OTA Update Progress: %u%%\r", (OTA_progress / (OTA_total / 100)));
});
ArduinoOTA.onError([](ota_error_t OTA_error) {
Serial.printf("OTA Error[%u]: ", OTA_error);
if (OTA_error == OTA_AUTH_ERROR) {
Serial.println("OTA Auth Failed");
} else if (OTA_error == OTA_BEGIN_ERROR) {
Serial.println("OTA Begin Failed");
} else if (OTA_error == OTA_CONNECT_ERROR) {
Serial.println("OTA Connect Failed");
} else if (OTA_error == OTA_RECEIVE_ERROR) {
Serial.println("OTA Receive Failed");
} else if (OTA_error == OTA_END_ERROR) {
Serial.println("OTA End Failed");
}
});
uint16_t OTA_time_start = millis();
while (OTA_time < 1000)
{
ArduinoOTA.handle();
OTA_time = millis() - OTA_time_start;
delay(10);
}
}
void WIFI_check() {
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Connection Failed! Connecting...");
WiFi.begin(ssid, password);
delay(100);
}
}
void OTA() {
if (OTA_update == true) {
uint16_t OTA_time_start = millis();
while (OTA_time < 5000)
{
ArduinoOTA.handle();
OTA_time = millis() - OTA_time_start;
delay(10);
}
delay(500);
ESP.restart();
}
}
the json on the /comands should be:
{
"Gun1": {
"comand_number": 1,
"comand": "" },
"Gun2": {
"comand_number": 1,
"comand": "" }
}