Add proper json api

This commit is contained in:
sqozz 2018-02-20 19:28:57 +01:00
parent 0cc2aaf4fe
commit e65242500f

View file

@ -1,40 +1,57 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <RCSwitch.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>
#include "secrets.h" #include "secrets.h"
ESP8266WebServer server(80);
bool still_pressed = false;
bool relay_active = false;
void setup() { void setup() {
WiFi.mode(WIFI_STA);
pinMode(12, OUTPUT); // relay pinMode(12, OUTPUT); // relay
pinMode(13, OUTPUT); // button pinMode(13, OUTPUT); // button
pinMode(0, INPUT); // led pinMode(0, INPUT); // led
Serial.begin(9600);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA); // setup serial and network
Serial.begin(115200);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
Serial.println("Serial interface initialized");
Serial.print("Connecting to wifi ");
Serial.print(ssid);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
digitalWrite(13, !digitalRead(13)); digitalWrite(13, !digitalRead(13));
delay(500); delay(500);
Serial.print("."); Serial.print(".");
} }
// serial output of connection details
Serial.println(""); Serial.println("");
Serial.println("WiFi connected"); Serial.print("Connected to ");
Serial.println("IP address: "); Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
// create HTTP server
server.on("/", handleMainpage);
server.on("/relay", handleRelay);
server.begin();
Serial.println("HTTP server started");
} }
bool still_pressed = false;
void loop() { void loop() {
if(!digitalRead(0)) { if(!digitalRead(0)) {
uint8_t relay_status = digitalRead(12); setRelay(!digitalRead(12));
Serial.print("Old status: "); delay(500); // give the user time to release the button
Serial.println(relay_status);
digitalWrite(13, !relay_status); // set LED
digitalWrite(12, !relay_status); // set relay
delay(200);
if(!digitalRead(0)) { if(!digitalRead(0)) {
still_pressed = true; still_pressed = true;
} else { } else {
@ -46,10 +63,76 @@ void loop() {
software_reset(); software_reset();
} }
delay(10); server.handleClient();
} }
void software_reset() { void software_reset() {
wdt_enable(WDTO_15MS); wdt_enable(WDTO_15MS);
while(1) {}; while(1) {};
} }
void handleMainpage() {
server.send(200, "text/html", "<html><a href=\"https://git.geekify.de/sqozz/switcheroo\">https://git.geekify.de/sqozz/switcheroo</a></html>");
}
void handleRelay() {
String data = server.arg("plain");
String result;
Serial.println("relay endpoint requested");
if (data != "") {
Serial.println("change request");
StaticJsonBuffer<200> newBuffer;
JsonObject& json = newBuffer.parseObject(data);
if (json["active"] == "true") {
Serial.println("on");
result = setRelay(true);
} else if (json["active"] == "false") {
Serial.println("off");
result = setRelay(false);
}
} else {
Serial.println("status requested");
result = generateStatusJson();
}
server.send(200, "text/json", result);
Serial.println("");
Serial.println("");
}
String setRelay(bool req_status) {
Serial.print("Old status: ");
Serial.println(relay_active);
Serial.print("New status: ");
Serial.println(req_status);
digitalWrite(12, req_status); // set relay
bool old_status = relay_active;
relay_active = digitalRead(12);
digitalWrite(13, !relay_active); // set LED to the physical relay status
Serial.println("");
Serial.println("");
return generateChangeJson(old_status, req_status, relay_active);
}
String generateChangeJson(bool old_status, bool req_status, bool new_status) {
String status = "";
bool success = (req_status == new_status);
if (success) {
status = ", \"status\" : { \"old\" : " + String(old_status) + ", \"new\" : " + String(new_status) + ", \"req\": " + String(req_status) + " }";
}
String retJson = "{ \"success\" : " + String(success) + status + " }";
return retJson;
}
String generateStatusJson() {
String status = "{ \"status\" : " + String(relay_active) + " }";
return status;
}