From 9f88c5926224500cfe3db4b7e23711a651433990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Thu, 17 Nov 2016 09:30:47 +0100 Subject: [PATCH] REST entry point for relay management, supporting GET and PUT --- code/src/button.ino | 2 +- code/src/emon.ino | 2 +- code/src/mqtt.ino | 8 ++--- code/src/relay.ino | 28 +++++++++-------- code/src/rf.ino | 4 +-- code/src/web.ino | 75 ++++++++++++++++++++++++++++++++++++--------- 6 files changed, 84 insertions(+), 35 deletions(-) diff --git a/code/src/button.ino b/code/src/button.ino index 6fc2aa3e..bc79e8a0 100644 --- a/code/src/button.ino +++ b/code/src/button.ino @@ -21,7 +21,7 @@ void buttonSetup() { void buttonLoop() { if (button1.loop()) { - if (button1.getEvent() == EVENT_SINGLE_CLICK) toggleRelay(); + if (button1.getEvent() == EVENT_SINGLE_CLICK) relayToggle(0); if (button1.getEvent() == EVENT_DOUBLE_CLICK) createAP(); if (button1.getEvent() == EVENT_LONG_CLICK) ESP.reset(); } diff --git a/code/src/emon.ino b/code/src/emon.ino index e278b082..31871a94 100644 --- a/code/src/emon.ino +++ b/code/src/emon.ino @@ -71,7 +71,7 @@ void powerMonitorLoop() { if (millis() > next_measurement) { // Safety check: do not read current if relay is OFF - if (!digitalRead(RELAY_PIN)) { + if (!relayStatus(0)) { current = 0; } else { current = emon.getCurrent(EMON_SAMPLES); diff --git a/code/src/mqtt.ino b/code/src/mqtt.ino index aceb501b..f595c744 100644 --- a/code/src/mqtt.ino +++ b/code/src/mqtt.ino @@ -59,7 +59,7 @@ void _mqttOnConnect(bool sessionPresent) { mqttSend((char *) MQTT_FSVERSION_TOPIC, buffer); // Publish current relay status - mqttSend((char *) MQTT_STATUS_TOPIC, (char *) (digitalRead(RELAY_PIN) ? "1" : "0")); + mqttSend((char *) MQTT_STATUS_TOPIC, (char *) (relayStatus(0) ? "1" : "0")); // Subscribe to topic DEBUG_MSG("[MQTT] Subscribing to %s\n", (char *) mqttTopic.c_str()); @@ -91,14 +91,14 @@ void _mqttOnMessage(char* topic, char* payload, AsyncMqttClientMessageProperties // Action to perform if ((char)payload[0] == '0') { isCallbackMessage = true; - switchRelayOff(); + relayStatus(0, false); } if ((char)payload[0] == '1') { isCallbackMessage = true; - switchRelayOn(); + relayStatus(0, true); } if ((char)payload[0] == '2') { - toggleRelay(); + relayToggle(0); } isCallbackMessage = false; diff --git a/code/src/relay.ino b/code/src/relay.ino index f857f8c5..6868adaa 100644 --- a/code/src/relay.ino +++ b/code/src/relay.ino @@ -13,7 +13,7 @@ Copyright (C) 2016 by Xose PĂ©rez // RELAY // ----------------------------------------------------------------------------- -void switchRelayOn() { +void _relayOn(unsigned char id) { if (!digitalRead(RELAY_PIN)) { DEBUG_MSG("[RELAY] ON\n"); @@ -24,10 +24,10 @@ void switchRelayOn() { } webSocketSend((char *) "{\"relayStatus\": true}"); - + } -void switchRelayOff() { +void _relayOff(unsigned char id) { if (digitalRead(RELAY_PIN)) { DEBUG_MSG("[RELAY] OFF\n"); @@ -41,19 +41,23 @@ void switchRelayOff() { } -void toggleRelay() { - if (digitalRead(RELAY_PIN)) { - switchRelayOff(); - } else { - switchRelayOn(); - } +void relayStatus(unsigned char id, bool status) { + status ? _relayOn(id) : _relayOff(id); +} + +bool relayStatus(unsigned char id) { + return (digitalRead(RELAY_PIN) == HIGH); +} + +void relayToggle(unsigned char id) { + relayStatus(id, !relayStatus(id)); } void relaySetup() { pinMode(RELAY_PIN, OUTPUT); EEPROM.begin(4096); byte relayMode = getSetting("relayMode", String(RELAY_MODE)).toInt(); - if (relayMode == 0) switchRelayOff(); - if (relayMode == 1) switchRelayOn(); - if (relayMode == 2) EEPROM.read(0) == 1 ? switchRelayOn() : switchRelayOff(); + if (relayMode == 0) relayStatus(0, false); + if (relayMode == 1) relayStatus(0, true); + if (relayMode == 2) relayStatus(0, EEPROM.read(0) == 1); } diff --git a/code/src/rf.ino b/code/src/rf.ino index b5711e8d..8a07f9d7 100644 --- a/code/src/rf.ino +++ b/code/src/rf.ino @@ -23,8 +23,8 @@ void rfLoop() { return; if (rfCode == 0) return; DEBUG_MSG("[RF] Received code: %lu\n", rfCode); - if (rfCode == rfCodeON) switchRelayOn(); - if (rfCode == rfCodeOFF) switchRelayOff(); + if (rfCode == rfCodeON) relayStatus(0, true); + if (rfCode == rfCodeOFF) relayStatus(0, false); rfCode = 0; } diff --git a/code/src/web.ino b/code/src/web.ino index 31bff6a2..eb5bea78 100644 --- a/code/src/web.ino +++ b/code/src/web.ino @@ -63,8 +63,8 @@ void webSocketParse(uint32_t client_id, uint8_t * payload, size_t length) { if (action.equals("reset")) ESP.reset(); if (action.equals("reconnect")) wifiDisconnect(); - if (action.equals("on")) switchRelayOn(); - if (action.equals("off")) switchRelayOff(); + if (action.equals("on")) relayStatus(0, true); + if (action.equals("off")) relayStatus(0, false); }; @@ -177,7 +177,7 @@ void webSocketStart(uint32_t client_id) { root["mqttUser"] = getSetting("mqttUser"); root["mqttPassword"] = getSetting("mqttPassword"); root["mqttTopic"] = getSetting("mqttTopic", MQTT_TOPIC); - root["relayStatus"] = digitalRead(RELAY_PIN) == HIGH; + root["relayStatus"] = relayStatus(0); root["relayMode"] = getSetting("relayMode", String(RELAY_MODE)); #if ENABLE_DHT @@ -241,8 +241,14 @@ void webSocketEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsE // WEBSERVER // ----------------------------------------------------------------------------- -void onHome(AsyncWebServerRequest *request) { - DEBUG_MSG("[WEBSERVER] Request: %s\n", request->url().c_str()); +void _logRequest(AsyncWebServerRequest *request) { + DEBUG_MSG("[WEBSERVER] Request: %s %s\n", request->methodToString(), request->url().c_str()); +} + +void _onHome(AsyncWebServerRequest *request) { + + _logRequest(request); + String password = getSetting("adminPass", ADMIN_PASS); char httpPassword[password.length() + 1]; password.toCharArray(httpPassword, password.length() + 1); @@ -252,18 +258,55 @@ void onHome(AsyncWebServerRequest *request) { request->send(SPIFFS, "/index.html"); } -void onRelayOn(AsyncWebServerRequest *request) { - DEBUG_MSG("[WEBSERVER] Request: %s\n", request->url().c_str()); - switchRelayOn(); +void _onRelayOn(AsyncWebServerRequest *request) { + + _logRequest(request); + + relayStatus(0, true); request->send(200, "text/plain", "ON"); + }; -void onRelayOff(AsyncWebServerRequest *request) { - DEBUG_MSG("[WEBSERVER] Request: %s\n", request->url().c_str()); - switchRelayOff(); +void _onRelayOff(AsyncWebServerRequest *request) { + + _logRequest(request); + + relayStatus(0, false); request->send(200, "text/plain", "OFF"); + }; +ArRequestHandlerFunction _onRelayStatusWrapper(bool relayID) { + + return [&](AsyncWebServerRequest *request) { + + _logRequest(request); + + if (request->method() == HTTP_PUT) { + if (request->hasParam("status", true)) { + AsyncWebParameter* p = request->getParam("status", true); + relayStatus(relayID, p->value().toInt() == 1); + } + } + + bool asJson = false; + if (request->hasHeader("Accept")) { + AsyncWebHeader* h = request->getHeader("Accept"); + asJson = h->value().equals("application/json"); + } + + if (asJson) { + char buffer[40]; + sprintf(buffer, "{\"status\": %d}", relayStatus(relayID) ? 1 : 0); + request->send(200, "application/json", buffer); + } else { + request->send(200, "text/plain", relayStatus(relayID) ? "1" : "0"); + } + + }; + +} + void webSetup() { // Setup websocket plugin @@ -271,12 +314,14 @@ void webSetup() { server.addHandler(&ws); // Serve home (password protected) - server.on("/", HTTP_GET, onHome); - server.on("/index.html", HTTP_GET, onHome); + server.on("/", HTTP_GET, _onHome); + server.on("/index.html", HTTP_GET, _onHome); // API entry points (non protected) - server.on("/relay/on", HTTP_GET, onRelayOn); - server.on("/relay/off", HTTP_GET, onRelayOff); + server.on("/relay/on", HTTP_GET, _onRelayOn); + server.on("/relay/off", HTTP_GET, _onRelayOff); + server.on("/relay/0/status", HTTP_GET + HTTP_PUT, _onRelayStatusWrapper(0)); + //server.on("/relay/1/status", HTTP_GET + HTTP_PUT, _onRelayStatusWrapper(1)); // Serve static files server.serveStatic("/", SPIFFS, "/");