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 <AccelStepper.h>
|
||||
|
||||
#include<EEPROM.h>
|
||||
|
||||
WiFiClient net;
|
||||
MQTTClient client;
|
||||
AccelStepper stepper(AccelStepper::HALF4WIRE, D0, D2, D1, D3);
|
||||
@@ -14,11 +16,22 @@ const char mqtt_pw[] = "";
|
||||
const char mqtt_addr[] = "";
|
||||
const char client_name[] = "";
|
||||
|
||||
const int INIT_POS = 511;
|
||||
const bool CLEAR_EEPROM = false;
|
||||
|
||||
|
||||
// End Config
|
||||
|
||||
unsigned long lastMillis = 0;
|
||||
|
||||
struct Config {
|
||||
long openPosition;
|
||||
long closedPosition;
|
||||
long lastPosition;
|
||||
bool blindsOpen;
|
||||
};
|
||||
|
||||
|
||||
// States
|
||||
const int SETUP = 0;
|
||||
const int SLACK = 1;
|
||||
@@ -29,10 +42,8 @@ const int ADJUSTING = 5;
|
||||
|
||||
int state = SETUP;
|
||||
int previousState = SETUP;
|
||||
bool blindsOpen = false;
|
||||
|
||||
long fullyOpenPosition = -14000;
|
||||
long fullyClosedPosition = 0;
|
||||
Config config;
|
||||
|
||||
void connect() {
|
||||
Serial.println("checking wifi...");
|
||||
@@ -68,7 +79,7 @@ void messageReceived(String &topic, String &payload) {
|
||||
} else if (payload.indexOf("reset") != -1) {
|
||||
Serial.println("Resetting current position and setting blinds state to closed");
|
||||
stepper.setCurrentPosition(0);
|
||||
blindsOpen = false;
|
||||
config.blindsOpen = false;
|
||||
} else {
|
||||
Serial.println(payload);
|
||||
}
|
||||
@@ -80,9 +91,13 @@ void updateState(int newState) {
|
||||
state = newState;
|
||||
|
||||
if(newState == IDLE) {
|
||||
config.lastPosition = stepper.currentPosition();
|
||||
|
||||
Serial.print("Transitioning to idle (");
|
||||
Serial.print(stepper.currentPosition());
|
||||
Serial.print(config.lastPosition);
|
||||
Serial.println(")");
|
||||
EEPROM.put(0, config);
|
||||
EEPROM.commit();
|
||||
} else {
|
||||
Serial.print("Moving ");
|
||||
Serial.print(previousState);
|
||||
@@ -92,20 +107,20 @@ void updateState(int newState) {
|
||||
}
|
||||
|
||||
void openBlinds() {
|
||||
if (!blindsOpen && state == IDLE) {
|
||||
if (!config.blindsOpen && state == IDLE) {
|
||||
Serial.println("Opening Blinds");
|
||||
updateState(OPENING_BLINDS);
|
||||
stepper.enableOutputs();
|
||||
stepper.moveTo(fullyOpenPosition);
|
||||
stepper.moveTo(config.openPosition);
|
||||
}
|
||||
}
|
||||
|
||||
void closeBlinds() {
|
||||
if (blindsOpen && state == IDLE) {
|
||||
if (config.blindsOpen && state == IDLE) {
|
||||
Serial.println("Closing Blinds");
|
||||
updateState(CLOSING_BLINDS);
|
||||
stepper.enableOutputs();
|
||||
stepper.moveTo(fullyClosedPosition);
|
||||
stepper.moveTo(config.closedPosition);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,13 +130,27 @@ void adjustClosedPosition(int offset) {
|
||||
Serial.print("Adjust by: ");
|
||||
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);
|
||||
stepper.enableOutputs();
|
||||
stepper.move(offset);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while(!Serial);
|
||||
|
||||
WiFi.begin(ssid, wifi_pw);
|
||||
|
||||
@@ -130,11 +159,45 @@ void setup() {
|
||||
|
||||
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.setSpeed(100);
|
||||
stepper.setAcceleration(250);
|
||||
}
|
||||
|
||||
void clearEeprom() {
|
||||
if(CLEAR_EEPROM) {
|
||||
for(int i = 0; i < 512; i++) {
|
||||
EEPROM.put(i, 0);
|
||||
}
|
||||
EEPROM.commit();
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
client.loop();
|
||||
|
||||
@@ -179,7 +242,7 @@ void loop() {
|
||||
} else {
|
||||
stepper.disableOutputs();
|
||||
bool open = state == OPENING_BLINDS;
|
||||
blindsOpen = open;
|
||||
config.blindsOpen = open;
|
||||
sendBlindsUpdate(open);
|
||||
updateState(SLACK);
|
||||
}
|
||||
@@ -198,18 +261,6 @@ void loop() {
|
||||
|
||||
updateState(IDLE);
|
||||
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;
|
||||
@@ -217,8 +268,8 @@ void loop() {
|
||||
}
|
||||
|
||||
void setup_stepper() {
|
||||
stepper.setCurrentPosition(config.lastPosition);
|
||||
updateState(IDLE);
|
||||
stepper.setCurrentPosition(-3000);
|
||||
}
|
||||
|
||||
void sendBlindsUpdate(bool isOpen) {
|
||||
|
||||
Reference in New Issue
Block a user