Browse Source

Hostname can now be changed. Added initial relay mode. Added reconnect and reset buttons to web interface.

fastled
Xose Pérez 8 years ago
parent
commit
75a2e923f0
10 changed files with 141 additions and 38 deletions
  1. +15
    -6
      code/html/custom.css
  2. +24
    -0
      code/html/custom.js
  3. +47
    -5
      code/html/index.html
  4. +3
    -0
      code/src/defaults.h
  5. +4
    -2
      code/src/main.ino
  6. +15
    -5
      code/src/mqtt.ino
  7. +8
    -9
      code/src/relay.ino
  8. +12
    -3
      code/src/webserver.ino
  9. +9
    -3
      code/src/websockets.ino
  10. +4
    -5
      code/src/wifi.ino

+ 15
- 6
code/html/custom.css View File

@ -14,20 +14,29 @@
.page { .page {
margin-top: 40px; margin-top: 40px;
} }
.center {
text-align: center;
}
.pure-button { .pure-button {
color: white; color: white;
padding: 8px 16px;
padding: 8px 12px;
border-radius: 4px; border-radius: 4px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2); text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
} }
.button-update {
width: 100px;
.main-buttons {
margin: 50px auto; margin: 50px auto;
text-align: center;
}
.main-buttons button {
width: 100px;
margin: 5px auto;
}
.button-update {
background: #1f8dd6; background: #1f8dd6;
} }
.button-reset {
background: rgb(202, 60, 60);
}
.button-reconnect {
background: rgb(202, 60, 60);
}
.pure-g { .pure-g {
margin-bottom: 20px; margin-bottom: 20px;
} }


+ 24
- 0
code/html/custom.js View File

@ -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() { function showPanel() {
$(".panel").hide(); $(".panel").hide();
$("#" + $(this).attr("data")).show(); $("#" + $(this).attr("data")).show();
@ -113,6 +135,8 @@ function init() {
$("#menuLink").on('click', toggleMenu); $("#menuLink").on('click', toggleMenu);
$(".button-update").on('click', doUpdate); $(".button-update").on('click', doUpdate);
$(".button-reset").on('click', doReset);
$(".button-reconnect").on('click', doReconnect);
$(".pure-menu-link").on('click', showPanel); $(".pure-menu-link").on('click', showPanel);
var host = window.location.hostname; var host = window.location.hostname;


+ 47
- 5
code/html/index.html View File

@ -36,6 +36,10 @@
<a href="#" class="pure-menu-link" data="panel-status">STATUS</a> <a href="#" class="pure-menu-link" data="panel-status">STATUS</a>
</li> </li>
<li class="pure-menu-item">
<a href="#" class="pure-menu-link" data="panel-general">GENERAL</a>
</li>
<li class="pure-menu-item"> <li class="pure-menu-item">
<a href="#" class="pure-menu-link" data="panel-wifi">WIFI</a> <a href="#" class="pure-menu-link" data="panel-wifi">WIFI</a>
</li> </li>
@ -50,8 +54,10 @@
</ul> </ul>
<div class="center">
<div class="main-buttons">
<button class="pure-button button-update">Update</button> <button class="pure-button button-update">Update</button>
<button class="pure-button button-reconnect">Reconnect</button>
<button class="pure-button button-reset">Reset</button>
</div> </div>
</div> </div>
@ -77,13 +83,18 @@
</div> </div>
<div class="pure-g"> <div class="pure-g">
<label class="pure-u-1 pure-u-sm-1-4" for="device">Device</label>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="device" disabled>
<label class="pure-u-1 pure-u-sm-1-4" for="chipid">Chip ID</label>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="chipid" disabled>
</div> </div>
<div class="pure-g"> <div class="pure-g">
<label class="pure-u-1 pure-u-sm-1-4" for="hostname">Hostname</label>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="hostname" readonly />
<label class="pure-u-1 pure-u-sm-1-4" for="mac">MAC</label>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="mac" disabled>
</div>
<div class="pure-g">
<label class="pure-u-1 pure-u-sm-1-4" for="device">Device</label>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="device" disabled>
</div> </div>
<div class="pure-g"> <div class="pure-g">
@ -129,6 +140,37 @@
<form id="formSave" class="pure-form pure-form-stacked" action="/" method="post"> <form id="formSave" class="pure-form pure-form-stacked" action="/" method="post">
<div class="panel" id="panel-general">
<div class="header">
<h1>GENERAL</h1>
<h2></h2>
</div>
<div class="page">
<fieldset>
<div class="pure-u-1">
<label class="form-label" for="hostname">Hostname</label>
<div class="hint">This name will identify this device in your network (http://&lt;hostname&gt;.local). For this setting to take effect you should restart the wifi interface clicking the "Reconnect" button.</div>
<input name="hostname" type="text" class="pure-u-1-4" tabindex="1">
</div>
<div class="pure-u-1">
<label class="form-label" for="relayMode">Relay boot mode</label>
<div class="hint">Here you can define what will be the status of the relay after a reboot.</div>
<select name="relayMode" class="pure-u-1-4" tabindex="2">
<option value="0">Always OFF</a>
<option value="1">Always ON</a>
<option value="2">Same as before</a>
</select>
</div>
</fieldset>
</div>
</div>
<div class="panel" id="panel-wifi"> <div class="panel" id="panel-wifi">
<div class="header"> <div class="header">


+ 3
- 0
code/src/defaults.h View File

@ -71,6 +71,9 @@
#define NOFUSS_SERVER "http://192.168.1.100" #define NOFUSS_SERVER "http://192.168.1.100"
#define NOFUSS_INTERVAL 3600000 #define NOFUSS_INTERVAL 3600000
// 0 means OFF, 1 ON and 2 whatever was before
#define RELAY_MODE 1
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// MQTT // MQTT
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------


+ 4
- 2
code/src/main.ino View File

@ -131,8 +131,10 @@ void setup() {
welcome(); welcome();
settingsSetup(); settingsSetup();
setSetting("hostname", String() + getIdentifier());
saveSettings();
if (getSetting("hostname").length() == 0) {
setSetting("hostname", String() + getIdentifier());
saveSettings();
}
relaySetup(); relaySetup();
wifiSetup(); wifiSetup();


+ 15
- 5
code/src/mqtt.ino View File

@ -44,11 +44,21 @@ void mqttSend(char * topic, char * message) {
void mqttCallback(char* topic, byte* payload, unsigned int length) { 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 // Action to perform
if ((char)payload[0] == '0') { if ((char)payload[0] == '0') {


+ 8
- 9
code/src/relay.ino View File

@ -16,33 +16,29 @@ Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
void switchRelayOn() { void switchRelayOn() {
if (!digitalRead(RELAY_PIN)) { if (!digitalRead(RELAY_PIN)) {
DEBUG_MSG("[RELAY] ON\n"); DEBUG_MSG("[RELAY] ON\n");
digitalWrite(RELAY_PIN, HIGH); digitalWrite(RELAY_PIN, HIGH);
EEPROM.write(0, 1); EEPROM.write(0, 1);
EEPROM.commit(); EEPROM.commit();
mqttSend((char *) MQTT_STATUS_TOPIC, (char *) "1"); mqttSend((char *) MQTT_STATUS_TOPIC, (char *) "1");
webSocketSend((char *) "{\"relayStatus\": true}");
} }
webSocketSend((char *) "{\"relayStatus\": true}");
} }
void switchRelayOff() { void switchRelayOff() {
if (digitalRead(RELAY_PIN)) { if (digitalRead(RELAY_PIN)) {
DEBUG_MSG("[RELAY] OFF\n"); DEBUG_MSG("[RELAY] OFF\n");
digitalWrite(RELAY_PIN, LOW); digitalWrite(RELAY_PIN, LOW);
EEPROM.write(0, 0); EEPROM.write(0, 0);
EEPROM.commit(); EEPROM.commit();
mqttSend((char *) MQTT_STATUS_TOPIC, (char *) "0"); mqttSend((char *) MQTT_STATUS_TOPIC, (char *) "0");
webSocketSend((char *) "{\"relayStatus\": false}");
} }
webSocketSend((char *) "{\"relayStatus\": false}");
} }
void toggleRelay() { void toggleRelay() {
@ -56,5 +52,8 @@ void toggleRelay() {
void relaySetup() { void relaySetup() {
pinMode(RELAY_PIN, OUTPUT); pinMode(RELAY_PIN, OUTPUT);
EEPROM.begin(4096); 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();
} }

+ 12
- 3
code/src/webserver.ino View File

@ -34,6 +34,16 @@ String getContentType(String filename) {
return "text/plain"; return "text/plain";
} }
void handleReconnect() {
DEBUG_MSG("[WEBSERVER] Request: /reconnect\n");
wifiDisconnect();
}
void handleReset() {
DEBUG_MSG("[WEBSERVER] Request: /reset\n");
ESP.reset();
}
void handleRelayOn() { void handleRelayOn() {
DEBUG_MSG("[WEBSERVER] Request: /relay/on\n"); DEBUG_MSG("[WEBSERVER] Request: /relay/on\n");
switchRelayOn(); switchRelayOn();
@ -112,7 +122,6 @@ void handleSave() {
// Reconfigure networks // Reconfigure networks
wifiConfigure(); wifiConfigure();
wifiDisconnect();
// Check if we should reconigure MQTT connection // Check if we should reconigure MQTT connection
if (dirtyMQTT) { if (dirtyMQTT) {
@ -126,10 +135,10 @@ void webServerSetup() {
//SPIFFS.begin(); //SPIFFS.begin();
// Relay control // Relay control
server.on("/reconnect", HTTP_GET, handleReconnect);
server.on("/reset", HTTP_GET, handleReset);
server.on("/relay/on", HTTP_GET, handleRelayOn); server.on("/relay/on", HTTP_GET, handleRelayOn);
server.on("/relay/off", HTTP_GET, handleRelayOff); server.on("/relay/off", HTTP_GET, handleRelayOff);
// Configuration page
server.on("/save", HTTP_POST, handleSave); server.on("/save", HTTP_POST, handleSave);
// Anything else // Anything else


+ 9
- 3
code/src/websockets.ino View File

@ -29,14 +29,19 @@ bool webSocketSend(uint8_t num, char * payload) {
void webSocketStart(uint8_t num) { 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; DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject(); JsonObject& root = jsonBuffer.createObject();
root["app"] = buffer;
root["app"] = app;
root["manufacturer"] = String(MANUFACTURER); root["manufacturer"] = String(MANUFACTURER);
root["chipid"] = chipid;
root["mac"] = WiFi.macAddress();
root["device"] = String(DEVICE); root["device"] = String(DEVICE);
root["hostname"] = getSetting("hostname", HOSTNAME); root["hostname"] = getSetting("hostname", HOSTNAME);
root["network"] = getNetwork(); root["network"] = getNetwork();
@ -48,6 +53,7 @@ void webSocketStart(uint8_t num) {
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"] = digitalRead(RELAY_PIN) == HIGH;
root["relayMode"] = getSetting("relayMode", String(RELAY_MODE));
#if ENABLE_RF #if ENABLE_RF
root["rfChannel"] = getSetting("rfChannel", String(RF_CHANNEL)); root["rfChannel"] = getSetting("rfChannel", String(RF_CHANNEL));


+ 4
- 5
code/src/wifi.ino View File

@ -44,19 +44,18 @@ bool createAP() {
} }
void wifiConfigure() { 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(); jw.cleanNetworks();
if (getSetting("ssid0").length() > 0) jw.addNetwork((char *) getSetting("ssid0").c_str(), (char *) getSetting("pass0").c_str()); 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("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()); if (getSetting("ssid2").length() > 0) jw.addNetwork((char *) getSetting("ssid2").c_str(), (char *) getSetting("pass2").c_str());
jw.disconnect();
} }
void wifiSetup() { 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(); wifiConfigure();
// Message callbacks // Message callbacks


Loading…
Cancel
Save