add EEPROM stuff, save last relay state in EEPROM

This commit is contained in:
hg
2015-05-31 14:57:32 +02:00
parent bf179114cc
commit 0fc11af5e0
9 changed files with 236 additions and 20 deletions

View File

@ -7,6 +7,7 @@
#include <Streaming.h>
#include <PubSubClient.h>
#include <PString.h>
#include "config.h"
@ -64,18 +65,41 @@ void callback(char* topic, byte* payload, unsigned int length) {
(indexStr != 0) && (*indexStr != 0) &&
(stateStr != 0) && (*stateStr != 0)) {
String command = String(commandStr);
int index = atoi(indexStr);
int index = 99;
if (! strcmp(indexStr, "all")) {
index = -1;
} else {
index = atoi(indexStr);
}
String state = String(stateStr);
bool validCommand = command.equals("switch");
bool validIndex = ! ((index < 0) || (index >= NUM_OF_LINES));
bool validIndex = (! ((index < 0) || (index >= NUM_OF_LINES))) || (index == -1);
if (validCommand && validIndex) {
if (state.equalsIgnoreCase("on")) {
switches[index].on();
if (index == -1) {
for (uint8_t i = 0; i < NUM_OF_LINES; i++) {
switches[i].on();
}
} else {
switches[index].on();
}
} else if (state.equalsIgnoreCase("off")) {
switches[index].off();
if (index == -1) {
for (uint8_t i = 0; i < NUM_OF_LINES; i++) {
switches[i].off();
}
} else {
switches[index].off();
}
} else if (state.equalsIgnoreCase("toggle")) {
switches[index].toggle();
if (index == -1) {
for (uint8_t i = 0; i < NUM_OF_LINES; i++) {
switches[i].toggle();
}
} else {
switches[index].toggle();
}
} else {
Serial << "Invalid state " << state << endl;
}
@ -96,9 +120,11 @@ void callback(char* topic, byte* payload, unsigned int length) {
void setup() {
Serial.begin(9600);
// delay(5000);
Serial << "Starting ... " << endl;
configInit();
Ethernet.begin(mac);
Serial << "Got IP address: " << Ethernet.localIP() << endl;
@ -109,7 +135,7 @@ void setup() {
for (uint8_t i = 0; i < NUM_OF_LINES; i++) {
switches[i].begin(FEEDBACK_PIN[i], BUTTON_PIN[i], RELAY_PIN[i], LED_PIN[i]);
switches[i].begin(FEEDBACK_PIN[i], BUTTON_PIN[i], RELAY_PIN[i], LED_PIN[i], i);
}
wdt_enable(WDTO_8S);
@ -171,7 +197,7 @@ void loop() {
if (second.check() == 1) {
uptime++;
Serial.println("tick");
// Serial.println("tick");
if (disconnectState == 0) {
String msg = String("{ \"metadata\": { \"device\": \"RelayBox\" }, \"data\": { \"uptime\": ") + uptime + String("}}");
@ -191,6 +217,10 @@ void loop() {
"\"state\": " << switches[i].getState() << ", " <<
"\"feedbackState\": " << switches[i].getFeedback() << ", " <<
"\"stateConflict\": " << switches[i].getStateConflict() << " }";
if (switches[i].getStateConflict()) {
Serial << "State conflict on channel " << i << ", should be " << switches[i].getState() <<
", is " << switches[i].getFeedback() << endl;
}
}
buf << "], " <<
"\"uptime\": " << uptime <<