diff --git a/code/html/custom.css b/code/html/custom.css index a83594ce..07f2c8e2 100644 --- a/code/html/custom.css +++ b/code/html/custom.css @@ -14,20 +14,29 @@ .page { margin-top: 40px; } -.center { - text-align: center; -} .pure-button { color: white; - padding: 8px 16px; + padding: 8px 12px; border-radius: 4px; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2); } -.button-update { - width: 100px; +.main-buttons { margin: 50px auto; + text-align: center; +} +.main-buttons button { + width: 100px; + margin: 5px auto; +} +.button-update { background: #1f8dd6; } +.button-reset { + background: rgb(202, 60, 60); +} +.button-reconnect { + background: rgb(202, 60, 60); +} .pure-g { margin-bottom: 20px; } diff --git a/code/html/custom.js b/code/html/custom.js index 9e69cb8a..bdd44729 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -15,6 +15,28 @@ function doUpdate() { }); } +function doReset() { + var response = window.confirm("Are you sure you want to reset the device?"); + if (response == false) return; + var self = $(this); + self.addClass("loading"); + $.ajax({ + 'method': 'GET', + 'url': '/reset' + }); +} + +function doReconnect() { + var response = window.confirm("Are you sure you want to disconnect from the current WIFI network?"); + if (response == false) return; + var self = $(this); + self.addClass("loading"); + $.ajax({ + 'method': 'GET', + 'url': '/reconnect' + }); +} + function showPanel() { $(".panel").hide(); $("#" + $(this).attr("data")).show(); @@ -113,6 +135,8 @@ function init() { $("#menuLink").on('click', toggleMenu); $(".button-update").on('click', doUpdate); + $(".button-reset").on('click', doReset); + $(".button-reconnect").on('click', doReconnect); $(".pure-menu-link").on('click', showPanel); var host = window.location.hostname; diff --git a/code/html/index.html b/code/html/index.html index 8892db9a..023021ad 100644 --- a/code/html/index.html +++ b/code/html/index.html @@ -36,6 +36,10 @@ STATUS +
  • + GENERAL +
  • +
  • WIFI
  • @@ -50,8 +54,10 @@ -
    +
    + +
    @@ -77,13 +83,18 @@
    - - + +
    - - + + +
    + +
    + +
    @@ -129,6 +140,37 @@
    +
    + +
    +

    GENERAL

    +

    +
    + +
    + +
    + +
    + +
    This name will identify this device in your network (http://<hostname>.local). For this setting to take effect you should restart the wifi interface clicking the "Reconnect" button.
    + +
    + +
    + +
    Here you can define what will be the status of the relay after a reboot.
    + +
    + +
    +
    +
    +
    diff --git a/code/src/defaults.h b/code/src/defaults.h index a0dff926..c98ad4e2 100644 --- a/code/src/defaults.h +++ b/code/src/defaults.h @@ -71,6 +71,9 @@ #define NOFUSS_SERVER "http://192.168.1.100" #define NOFUSS_INTERVAL 3600000 +// 0 means OFF, 1 ON and 2 whatever was before +#define RELAY_MODE 1 + // ----------------------------------------------------------------------------- // MQTT // ----------------------------------------------------------------------------- diff --git a/code/src/main.ino b/code/src/main.ino index fd30e3c5..682e79ff 100644 --- a/code/src/main.ino +++ b/code/src/main.ino @@ -131,8 +131,10 @@ void setup() { welcome(); settingsSetup(); - setSetting("hostname", String() + getIdentifier()); - saveSettings(); + if (getSetting("hostname").length() == 0) { + setSetting("hostname", String() + getIdentifier()); + saveSettings(); + } relaySetup(); wifiSetup(); diff --git a/code/src/mqtt.ino b/code/src/mqtt.ino index ff3a5bda..90f46c94 100644 --- a/code/src/mqtt.ino +++ b/code/src/mqtt.ino @@ -44,11 +44,21 @@ void mqttSend(char * topic, char * message) { void mqttCallback(char* topic, byte* payload, unsigned int length) { - char buffer[length+1]; - memcpy(buffer, payload, length); - buffer[length] = 0; - - DEBUG_MSG("[MQTT] Received %s %s\n", topic, buffer); + static bool isFirstMessage = true; + + #ifdef DEBUG_PORT + char buffer[length+1]; + memcpy(buffer, payload, length); + buffer[length] = 0; + DEBUG_MSG("[MQTT] Received %s %s\n", topic, buffer); + #endif + + // If relayMode is not SAME avoid responding to a retained message + if (isFirstMessage) { + isFirstMessage = false; + byte relayMode = getSetting("relayMode", String(RELAY_MODE)).toInt(); + if (relayMode != 2) return; + } // Action to perform if ((char)payload[0] == '0') { diff --git a/code/src/relay.ino b/code/src/relay.ino index 662f558f..f857f8c5 100644 --- a/code/src/relay.ino +++ b/code/src/relay.ino @@ -16,33 +16,29 @@ Copyright (C) 2016 by Xose PĂ©rez void switchRelayOn() { if (!digitalRead(RELAY_PIN)) { - DEBUG_MSG("[RELAY] ON\n"); digitalWrite(RELAY_PIN, HIGH); EEPROM.write(0, 1); EEPROM.commit(); - mqttSend((char *) MQTT_STATUS_TOPIC, (char *) "1"); - webSocketSend((char *) "{\"relayStatus\": true}"); - } + webSocketSend((char *) "{\"relayStatus\": true}"); + } void switchRelayOff() { if (digitalRead(RELAY_PIN)) { - DEBUG_MSG("[RELAY] OFF\n"); digitalWrite(RELAY_PIN, LOW); EEPROM.write(0, 0); EEPROM.commit(); - mqttSend((char *) MQTT_STATUS_TOPIC, (char *) "0"); - webSocketSend((char *) "{\"relayStatus\": false}"); - } + webSocketSend((char *) "{\"relayStatus\": false}"); + } void toggleRelay() { @@ -56,5 +52,8 @@ void toggleRelay() { void relaySetup() { pinMode(RELAY_PIN, OUTPUT); EEPROM.begin(4096); - EEPROM.read(0) == 1 ? switchRelayOn() : switchRelayOff(); + 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(); } diff --git a/code/src/webserver.ino b/code/src/webserver.ino index 4133617a..5bd97f5b 100644 --- a/code/src/webserver.ino +++ b/code/src/webserver.ino @@ -34,6 +34,16 @@ String getContentType(String filename) { return "text/plain"; } +void handleReconnect() { + DEBUG_MSG("[WEBSERVER] Request: /reconnect\n"); + wifiDisconnect(); +} + +void handleReset() { + DEBUG_MSG("[WEBSERVER] Request: /reset\n"); + ESP.reset(); +} + void handleRelayOn() { DEBUG_MSG("[WEBSERVER] Request: /relay/on\n"); switchRelayOn(); @@ -112,7 +122,6 @@ void handleSave() { // Reconfigure networks wifiConfigure(); - wifiDisconnect(); // Check if we should reconigure MQTT connection if (dirtyMQTT) { @@ -126,10 +135,10 @@ void webServerSetup() { //SPIFFS.begin(); // Relay control + server.on("/reconnect", HTTP_GET, handleReconnect); + server.on("/reset", HTTP_GET, handleReset); server.on("/relay/on", HTTP_GET, handleRelayOn); server.on("/relay/off", HTTP_GET, handleRelayOff); - - // Configuration page server.on("/save", HTTP_POST, handleSave); // Anything else diff --git a/code/src/websockets.ino b/code/src/websockets.ino index 2c88e832..b1b3cca8 100644 --- a/code/src/websockets.ino +++ b/code/src/websockets.ino @@ -29,14 +29,19 @@ bool webSocketSend(uint8_t num, char * payload) { void webSocketStart(uint8_t num) { - char buffer[64]; - sprintf(buffer, "%s %s", APP_NAME, APP_VERSION); + char app[64]; + sprintf(app, "%s %s", APP_NAME, APP_VERSION); + + char chipid[6]; + sprintf(chipid, "%06X", ESP.getChipId()); DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); - root["app"] = buffer; + root["app"] = app; root["manufacturer"] = String(MANUFACTURER); + root["chipid"] = chipid; + root["mac"] = WiFi.macAddress(); root["device"] = String(DEVICE); root["hostname"] = getSetting("hostname", HOSTNAME); root["network"] = getNetwork(); @@ -48,6 +53,7 @@ void webSocketStart(uint8_t num) { root["mqttPassword"] = getSetting("mqttPassword"); root["mqttTopic"] = getSetting("mqttTopic", MQTT_TOPIC); root["relayStatus"] = digitalRead(RELAY_PIN) == HIGH; + root["relayMode"] = getSetting("relayMode", String(RELAY_MODE)); #if ENABLE_RF root["rfChannel"] = getSetting("rfChannel", String(RF_CHANNEL)); diff --git a/code/src/wifi.ino b/code/src/wifi.ino index 2f6cd316..56861fb6 100644 --- a/code/src/wifi.ino +++ b/code/src/wifi.ino @@ -44,19 +44,18 @@ bool createAP() { } void wifiConfigure() { + jw.scanNetworks(true); + jw.setHostname((char *) getSetting("hostname", HOSTNAME).c_str()); + jw.setSoftAP((char *) getSetting("hostname", HOSTNAME).c_str(), (char *) AP_PASS); + jw.setAPMode(AP_MODE_ALONE); jw.cleanNetworks(); if (getSetting("ssid0").length() > 0) jw.addNetwork((char *) getSetting("ssid0").c_str(), (char *) getSetting("pass0").c_str()); if (getSetting("ssid1").length() > 0) jw.addNetwork((char *) getSetting("ssid1").c_str(), (char *) getSetting("pass1").c_str()); if (getSetting("ssid2").length() > 0) jw.addNetwork((char *) getSetting("ssid2").c_str(), (char *) getSetting("pass2").c_str()); - jw.disconnect(); } void wifiSetup() { - jw.setHostname((char *) getSetting("hostname", HOSTNAME).c_str()); - jw.scanNetworks(true); - jw.setAPMode(AP_MODE_ALONE); - jw.setSoftAP((char *) getIdentifier().c_str(), (char *) AP_PASS); wifiConfigure(); // Message callbacks