Created
February 23, 2020 06:57
-
-
Save rosmianto/be5b16ea3ffc7efed247995c72eac7a5 to your computer and use it in GitHub Desktop.
Implementation of Resettable EEPROM config using external button Arduino Platform
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
/* | |
IoT.ino -- Example code for demonstration only. | |
Written by Rosmianto A. Saputro. Aug 25, 2017. | |
To do list: | |
* Implement OTA | |
* Implement Server/client code. | |
* Implement EEPROM update code. | |
*/ | |
#include <EEPROM.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WebServer.h> | |
#define ROLE_FLAG_ADDRESS 0 | |
#define CLEAR D1 | |
byte role_value; | |
// Things that need to be stored in EEPROM | |
String SSID = ""; | |
String password = ""; | |
String host = ""; | |
String port = ""; | |
ESP8266WebServer server(80); | |
void setup() { | |
pinMode(CLEAR, INPUT_PULLUP); | |
EEPROM.begin(512); // Initialize 512 bytes of EEPROM block. | |
Serial.begin(115200); | |
Serial.println("\n\nDevice is starting up."); | |
if(digitalRead(CLEAR) == LOW){ // Clear prev. configuration | |
Serial.print("Clearing configuration... "); | |
EEPROM.write(ROLE_FLAG_ADDRESS, 0); | |
EEPROM.commit(); | |
Serial.println("Done!"); | |
while(digitalRead(CLEAR) != HIGH); // Wait until user release the RESET button. | |
Serial.println("Restarting..."); | |
ESP.restart(); | |
} | |
role_value = EEPROM.read(ROLE_FLAG_ADDRESS); | |
if(role_value == 0){ | |
// Enter server mode | |
Serial.println("Server mode."); | |
// Insert webpage server here... | |
// ... | |
// ... | |
WiFi.mode(WIFI_AP); | |
WiFi.softAP("Xirka NodeMCU", "abcdefghijkl"); | |
server.on("/submit", submitHandler); | |
server.begin(); | |
// Update role flag in eeprom | |
EEPROM.write(ROLE_FLAG_ADDRESS, 1); | |
EEPROM.commit(); | |
} | |
else{ | |
// Enter client mode | |
Serial.println("Client mode."); | |
// Insert connecting to SSID here... | |
// ... | |
// ... | |
} | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
if(role_value == 0){ | |
server.handleClient(); | |
} | |
} | |
void submitHandler(){ | |
SSID = server.arg("ssid"); | |
password = server.arg("pass"); | |
Serial.println(SSID); | |
Serial.println(password); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment