Created
February 17, 2024 14:03
-
-
Save TalalMash/5fdd61867e7fa05fc61c36efac7cfe0a to your computer and use it in GitHub Desktop.
ESP8266 Power cycle relay toggle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <FS.h> | |
const char *filename = "/relay_status.txt"; | |
byte relON[] = {0xA0, 0x01, 0x01, 0xA2}; | |
byte relOFF[] = {0xA0, 0x01, 0x00, 0xA1}; | |
void setup() { | |
Serial.begin(9600); | |
Serial.write(0); | |
if (!SPIFFS.begin()) { | |
return; | |
} | |
int previousStatus = readRelayStatus(); | |
int newStatus = 1 - previousStatus; | |
saveRelayStatus(newStatus); | |
if (newStatus == 1) { | |
Serial.write(relON, sizeof(relON)); | |
} else { | |
Serial.write(relOFF, sizeof(relOFF)); | |
} | |
Serial.flush(); | |
} | |
void loop() { | |
} | |
void saveRelayStatus(int status) { | |
File file = SPIFFS.open(filename, "w"); | |
if (!file) { | |
return; | |
} | |
file.print(status); | |
file.close(); | |
} | |
int readRelayStatus() { | |
int status = 0; | |
File file = SPIFFS.open(filename, "r"); | |
if (file) { | |
status = file.parseInt(); | |
file.close(); | |
} | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment