Save state to EEPROM
Set defaults in the Config on first launch, then update it as it transitions to IDLE (only if its changed). This should make the module more resistant to power loss and resets so it doesn't lose position.
This commit is contained in:
99
blinds.ino
99
blinds.ino
@@ -2,6 +2,8 @@
|
|||||||
#include <MQTT.h>
|
#include <MQTT.h>
|
||||||
#include <AccelStepper.h>
|
#include <AccelStepper.h>
|
||||||
|
|
||||||
|
#include<EEPROM.h>
|
||||||
|
|
||||||
WiFiClient net;
|
WiFiClient net;
|
||||||
MQTTClient client;
|
MQTTClient client;
|
||||||
AccelStepper stepper(AccelStepper::HALF4WIRE, D0, D2, D1, D3);
|
AccelStepper stepper(AccelStepper::HALF4WIRE, D0, D2, D1, D3);
|
||||||
@@ -14,11 +16,22 @@ const char mqtt_pw[] = "";
|
|||||||
const char mqtt_addr[] = "";
|
const char mqtt_addr[] = "";
|
||||||
const char client_name[] = "";
|
const char client_name[] = "";
|
||||||
|
|
||||||
|
const int INIT_POS = 511;
|
||||||
|
const bool CLEAR_EEPROM = false;
|
||||||
|
|
||||||
|
|
||||||
// End Config
|
// End Config
|
||||||
|
|
||||||
unsigned long lastMillis = 0;
|
unsigned long lastMillis = 0;
|
||||||
|
|
||||||
|
struct Config {
|
||||||
|
long openPosition;
|
||||||
|
long closedPosition;
|
||||||
|
long lastPosition;
|
||||||
|
bool blindsOpen;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// States
|
// States
|
||||||
const int SETUP = 0;
|
const int SETUP = 0;
|
||||||
const int SLACK = 1;
|
const int SLACK = 1;
|
||||||
@@ -29,10 +42,8 @@ const int ADJUSTING = 5;
|
|||||||
|
|
||||||
int state = SETUP;
|
int state = SETUP;
|
||||||
int previousState = SETUP;
|
int previousState = SETUP;
|
||||||
bool blindsOpen = false;
|
|
||||||
|
|
||||||
long fullyOpenPosition = -14000;
|
Config config;
|
||||||
long fullyClosedPosition = 0;
|
|
||||||
|
|
||||||
void connect() {
|
void connect() {
|
||||||
Serial.println("checking wifi...");
|
Serial.println("checking wifi...");
|
||||||
@@ -68,7 +79,7 @@ void messageReceived(String &topic, String &payload) {
|
|||||||
} else if (payload.indexOf("reset") != -1) {
|
} else if (payload.indexOf("reset") != -1) {
|
||||||
Serial.println("Resetting current position and setting blinds state to closed");
|
Serial.println("Resetting current position and setting blinds state to closed");
|
||||||
stepper.setCurrentPosition(0);
|
stepper.setCurrentPosition(0);
|
||||||
blindsOpen = false;
|
config.blindsOpen = false;
|
||||||
} else {
|
} else {
|
||||||
Serial.println(payload);
|
Serial.println(payload);
|
||||||
}
|
}
|
||||||
@@ -80,9 +91,13 @@ void updateState(int newState) {
|
|||||||
state = newState;
|
state = newState;
|
||||||
|
|
||||||
if(newState == IDLE) {
|
if(newState == IDLE) {
|
||||||
|
config.lastPosition = stepper.currentPosition();
|
||||||
|
|
||||||
Serial.print("Transitioning to idle (");
|
Serial.print("Transitioning to idle (");
|
||||||
Serial.print(stepper.currentPosition());
|
Serial.print(config.lastPosition);
|
||||||
Serial.println(")");
|
Serial.println(")");
|
||||||
|
EEPROM.put(0, config);
|
||||||
|
EEPROM.commit();
|
||||||
} else {
|
} else {
|
||||||
Serial.print("Moving ");
|
Serial.print("Moving ");
|
||||||
Serial.print(previousState);
|
Serial.print(previousState);
|
||||||
@@ -92,20 +107,20 @@ void updateState(int newState) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void openBlinds() {
|
void openBlinds() {
|
||||||
if (!blindsOpen && state == IDLE) {
|
if (!config.blindsOpen && state == IDLE) {
|
||||||
Serial.println("Opening Blinds");
|
Serial.println("Opening Blinds");
|
||||||
updateState(OPENING_BLINDS);
|
updateState(OPENING_BLINDS);
|
||||||
stepper.enableOutputs();
|
stepper.enableOutputs();
|
||||||
stepper.moveTo(fullyOpenPosition);
|
stepper.moveTo(config.openPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void closeBlinds() {
|
void closeBlinds() {
|
||||||
if (blindsOpen && state == IDLE) {
|
if (config.blindsOpen && state == IDLE) {
|
||||||
Serial.println("Closing Blinds");
|
Serial.println("Closing Blinds");
|
||||||
updateState(CLOSING_BLINDS);
|
updateState(CLOSING_BLINDS);
|
||||||
stepper.enableOutputs();
|
stepper.enableOutputs();
|
||||||
stepper.moveTo(fullyClosedPosition);
|
stepper.moveTo(config.closedPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,13 +130,27 @@ void adjustClosedPosition(int offset) {
|
|||||||
Serial.print("Adjust by: ");
|
Serial.print("Adjust by: ");
|
||||||
Serial.println(offset);
|
Serial.println(offset);
|
||||||
|
|
||||||
|
if(config.blindsOpen) {
|
||||||
|
config.openPosition += offset;
|
||||||
|
stepper.moveTo(config.openPosition);
|
||||||
|
|
||||||
|
Serial.print("New open position: ");
|
||||||
|
Serial.println(config.openPosition);
|
||||||
|
} else {
|
||||||
|
config.closedPosition += offset;
|
||||||
|
stepper.moveTo(config.closedPosition);
|
||||||
|
|
||||||
|
Serial.print("New closed position: ");
|
||||||
|
Serial.println(config.closedPosition);
|
||||||
|
}
|
||||||
|
|
||||||
updateState(ADJUSTING);
|
updateState(ADJUSTING);
|
||||||
stepper.enableOutputs();
|
stepper.enableOutputs();
|
||||||
stepper.move(offset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
while(!Serial);
|
||||||
|
|
||||||
WiFi.begin(ssid, wifi_pw);
|
WiFi.begin(ssid, wifi_pw);
|
||||||
|
|
||||||
@@ -130,11 +159,45 @@ void setup() {
|
|||||||
|
|
||||||
connect();
|
connect();
|
||||||
|
|
||||||
|
EEPROM.begin(512);
|
||||||
|
|
||||||
|
// Only clears if flag is set
|
||||||
|
clearEeprom();
|
||||||
|
|
||||||
|
if(EEPROM.read(INIT_POS) != 'T') {
|
||||||
|
config.closedPosition = 0;
|
||||||
|
config.openPosition = -12000;
|
||||||
|
config.lastPosition = -3000;
|
||||||
|
|
||||||
|
Serial.println("Save default config state");
|
||||||
|
Serial.print("Open pos: ");
|
||||||
|
Serial.println(config.openPosition);
|
||||||
|
EEPROM.put(0, config);
|
||||||
|
|
||||||
|
EEPROM.write(INIT_POS, 'T');
|
||||||
|
if(!EEPROM.commit()) {
|
||||||
|
Serial.println("ERROR WRITING CONFIG");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("Config already saved");
|
||||||
|
EEPROM.get(0, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
stepper.setMaxSpeed(600);
|
stepper.setMaxSpeed(600);
|
||||||
stepper.setSpeed(100);
|
stepper.setSpeed(100);
|
||||||
stepper.setAcceleration(250);
|
stepper.setAcceleration(250);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void clearEeprom() {
|
||||||
|
if(CLEAR_EEPROM) {
|
||||||
|
for(int i = 0; i < 512; i++) {
|
||||||
|
EEPROM.put(i, 0);
|
||||||
|
}
|
||||||
|
EEPROM.commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
client.loop();
|
client.loop();
|
||||||
|
|
||||||
@@ -179,7 +242,7 @@ void loop() {
|
|||||||
} else {
|
} else {
|
||||||
stepper.disableOutputs();
|
stepper.disableOutputs();
|
||||||
bool open = state == OPENING_BLINDS;
|
bool open = state == OPENING_BLINDS;
|
||||||
blindsOpen = open;
|
config.blindsOpen = open;
|
||||||
sendBlindsUpdate(open);
|
sendBlindsUpdate(open);
|
||||||
updateState(SLACK);
|
updateState(SLACK);
|
||||||
}
|
}
|
||||||
@@ -198,18 +261,6 @@ void loop() {
|
|||||||
|
|
||||||
updateState(IDLE);
|
updateState(IDLE);
|
||||||
stepper.disableOutputs();
|
stepper.disableOutputs();
|
||||||
|
|
||||||
if(blindsOpen) {
|
|
||||||
fullyOpenPosition = stepper.currentPosition();
|
|
||||||
|
|
||||||
Serial.print("New open position: ");
|
|
||||||
Serial.println(fullyOpenPosition);
|
|
||||||
} else {
|
|
||||||
fullyClosedPosition = stepper.currentPosition();
|
|
||||||
|
|
||||||
Serial.print("New closed position: ");
|
|
||||||
Serial.println(fullyClosedPosition);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -217,8 +268,8 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setup_stepper() {
|
void setup_stepper() {
|
||||||
|
stepper.setCurrentPosition(config.lastPosition);
|
||||||
updateState(IDLE);
|
updateState(IDLE);
|
||||||
stepper.setCurrentPosition(-3000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendBlindsUpdate(bool isOpen) {
|
void sendBlindsUpdate(bool isOpen) {
|
||||||
|
|||||||
Reference in New Issue
Block a user