Browse Source

REST entry point for relay management, supporting GET and PUT

fastled
Xose Pérez 8 years ago
parent
commit
9f88c59262
6 changed files with 84 additions and 35 deletions
  1. +1
    -1
      code/src/button.ino
  2. +1
    -1
      code/src/emon.ino
  3. +4
    -4
      code/src/mqtt.ino
  4. +16
    -12
      code/src/relay.ino
  5. +2
    -2
      code/src/rf.ino
  6. +60
    -15
      code/src/web.ino

+ 1
- 1
code/src/button.ino View File

@ -21,7 +21,7 @@ void buttonSetup() {
void buttonLoop() { void buttonLoop() {
if (button1.loop()) { 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_DOUBLE_CLICK) createAP();
if (button1.getEvent() == EVENT_LONG_CLICK) ESP.reset(); if (button1.getEvent() == EVENT_LONG_CLICK) ESP.reset();
} }


+ 1
- 1
code/src/emon.ino View File

@ -71,7 +71,7 @@ void powerMonitorLoop() {
if (millis() > next_measurement) { if (millis() > next_measurement) {
// Safety check: do not read current if relay is OFF // Safety check: do not read current if relay is OFF
if (!digitalRead(RELAY_PIN)) {
if (!relayStatus(0)) {
current = 0; current = 0;
} else { } else {
current = emon.getCurrent(EMON_SAMPLES); current = emon.getCurrent(EMON_SAMPLES);


+ 4
- 4
code/src/mqtt.ino View File

@ -59,7 +59,7 @@ void _mqttOnConnect(bool sessionPresent) {
mqttSend((char *) MQTT_FSVERSION_TOPIC, buffer); mqttSend((char *) MQTT_FSVERSION_TOPIC, buffer);
// Publish current relay status // 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 // Subscribe to topic
DEBUG_MSG("[MQTT] Subscribing to %s\n", (char *) mqttTopic.c_str()); 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 // Action to perform
if ((char)payload[0] == '0') { if ((char)payload[0] == '0') {
isCallbackMessage = true; isCallbackMessage = true;
switchRelayOff();
relayStatus(0, false);
} }
if ((char)payload[0] == '1') { if ((char)payload[0] == '1') {
isCallbackMessage = true; isCallbackMessage = true;
switchRelayOn();
relayStatus(0, true);
} }
if ((char)payload[0] == '2') { if ((char)payload[0] == '2') {
toggleRelay();
relayToggle(0);
} }
isCallbackMessage = false; isCallbackMessage = false;


+ 16
- 12
code/src/relay.ino View File

@ -13,7 +13,7 @@ Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
// RELAY // RELAY
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void switchRelayOn() {
void _relayOn(unsigned char id) {
if (!digitalRead(RELAY_PIN)) { if (!digitalRead(RELAY_PIN)) {
DEBUG_MSG("[RELAY] ON\n"); DEBUG_MSG("[RELAY] ON\n");
@ -24,10 +24,10 @@ void switchRelayOn() {
} }
webSocketSend((char *) "{\"relayStatus\": true}"); webSocketSend((char *) "{\"relayStatus\": true}");
} }
void switchRelayOff() {
void _relayOff(unsigned char id) {
if (digitalRead(RELAY_PIN)) { if (digitalRead(RELAY_PIN)) {
DEBUG_MSG("[RELAY] OFF\n"); 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() { void relaySetup() {
pinMode(RELAY_PIN, OUTPUT); pinMode(RELAY_PIN, OUTPUT);
EEPROM.begin(4096); EEPROM.begin(4096);
byte relayMode = getSetting("relayMode", String(RELAY_MODE)).toInt(); 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);
} }

+ 2
- 2
code/src/rf.ino View File

@ -23,8 +23,8 @@ void rfLoop() {
return; return;
if (rfCode == 0) return; if (rfCode == 0) return;
DEBUG_MSG("[RF] Received code: %lu\n", rfCode); 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; rfCode = 0;
} }


+ 60
- 15
code/src/web.ino View File

@ -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("reset")) ESP.reset();
if (action.equals("reconnect")) wifiDisconnect(); 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["mqttUser"] = getSetting("mqttUser");
root["mqttPassword"] = getSetting("mqttPassword"); root["mqttPassword"] = getSetting("mqttPassword");
root["mqttTopic"] = getSetting("mqttTopic", MQTT_TOPIC); root["mqttTopic"] = getSetting("mqttTopic", MQTT_TOPIC);
root["relayStatus"] = digitalRead(RELAY_PIN) == HIGH;
root["relayStatus"] = relayStatus(0);
root["relayMode"] = getSetting("relayMode", String(RELAY_MODE)); root["relayMode"] = getSetting("relayMode", String(RELAY_MODE));
#if ENABLE_DHT #if ENABLE_DHT
@ -241,8 +241,14 @@ void webSocketEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsE
// WEBSERVER // 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); String password = getSetting("adminPass", ADMIN_PASS);
char httpPassword[password.length() + 1]; char httpPassword[password.length() + 1];
password.toCharArray(httpPassword, password.length() + 1); password.toCharArray(httpPassword, password.length() + 1);
@ -252,18 +258,55 @@ void onHome(AsyncWebServerRequest *request) {
request->send(SPIFFS, "/index.html"); 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"); 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"); 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() { void webSetup() {
// Setup websocket plugin // Setup websocket plugin
@ -271,12 +314,14 @@ void webSetup() {
server.addHandler(&ws); server.addHandler(&ws);
// Serve home (password protected) // 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) // 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 // Serve static files
server.serveStatic("/", SPIFFS, "/"); server.serveStatic("/", SPIFFS, "/");


Loading…
Cancel
Save