Save ip config into eeprom
This commit is contained in:
parent
fe3dba6cad
commit
132ba26720
@ -1,14 +1,24 @@
|
||||
/* EEPROM LAYOUT
|
||||
"BOOTMODE;SSID;PASSWORD;HOSTNAME;MQTT_SERVER;MQTT_USERNAME;MQTT_PASSWD;MQTT_PORT;"
|
||||
"BOOTMODE;SSID;PASSWORD;HOSTNAME;MQTT_SERVER;MQTT_USERNAME;MQTT_PASSWD;MQTT_PORT;IP_CONFIG;IP;GATEWAY;NETMASK;DNS1;DNS2;"
|
||||
BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA
|
||||
IP_CONFIG could be 0 for DHCP, 1 for static
|
||||
Setup mode is trigger by setting GPIO3 to ground or at first boot
|
||||
*/
|
||||
|
||||
|
||||
int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttPasswd, int mqttPort) {
|
||||
int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host,
|
||||
String mqttServer, String mqttUser, String mqttPasswd,
|
||||
int mqttPort, int ip_config, String ip, String gw,
|
||||
String mask, String dns, String dns2) {
|
||||
String eeprom;
|
||||
|
||||
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";" + mqttServer + ";" + mqttUser + ";" + mqttPasswd + ";" + String(mqttPort) + ";";
|
||||
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";"
|
||||
+ host + ";" + mqttServer + ";"
|
||||
+ mqttUser + ";" + mqttPasswd + ";"
|
||||
+ String(mqttPort) + ";"
|
||||
+ String(ip_config) + ";"
|
||||
+ ip + ";" + gw + ";" + mask + ";"
|
||||
+ dns + ";" + dns2 + ";";
|
||||
|
||||
if (eeprom.length() > CONFIG_EEPROM_SIZE )
|
||||
return -EMSGSIZE;
|
||||
@ -45,12 +55,15 @@ void readConfElement(char** element, int &i) {
|
||||
|
||||
}
|
||||
|
||||
void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd, int &mqttPort) {
|
||||
void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host,
|
||||
char **mqttServer, char **mqttUser, char **mqttPasswd,
|
||||
int &mqttPort, int &ip_config, char **ip, char **gw,
|
||||
char **mask, char **dns, char**dns2) {
|
||||
|
||||
int i = 2;
|
||||
|
||||
uint8_t boot = EEPROM.read(0);
|
||||
char *mqttPortString = "";
|
||||
char *tmpString = "";
|
||||
|
||||
if (boot == '1') {
|
||||
bootMode = BOOTMODE_NORMAL;
|
||||
@ -68,6 +81,13 @@ void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **ho
|
||||
readConfElement(mqttServer, i);
|
||||
readConfElement(mqttUser, i);
|
||||
readConfElement(mqttPasswd, i);
|
||||
readConfElement(&mqttPortString, i);
|
||||
mqttPort = atoi(mqttPortString);
|
||||
readConfElement(&tmpString, i);
|
||||
mqttPort = atoi(tmpString);
|
||||
readConfElement(&tmpString, i);
|
||||
ip_config = atoi(tmpString);
|
||||
readConfElement(ip, i);
|
||||
readConfElement(gw, i);
|
||||
readConfElement(mask, i);
|
||||
readConfElement(dns, i);
|
||||
readConfElement(dns2, i);
|
||||
}
|
||||
|
@ -42,8 +42,14 @@ void WebHandleSetup() {
|
||||
char *mqttUser = "";
|
||||
char *mqttPasswd = "";
|
||||
int mqttPort = 1883;
|
||||
int ip_config = 0;
|
||||
char *ip = "192.168.0.123";
|
||||
char *gw = "192.168.0.1";
|
||||
char *mask = "255.255.255.0";
|
||||
char *dns = "8.8.8.8";
|
||||
char *dns2 = "";
|
||||
|
||||
EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort);
|
||||
EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort, ip_config, &ip, &gw, &mask, &dns, &dns2);
|
||||
|
||||
server.send(200, "text/html", "<form action=\"/save\" method=\"get\">"
|
||||
"<fieldset>"
|
||||
@ -53,6 +59,15 @@ void WebHandleSetup() {
|
||||
"<div><label for=\"host\">Hostname :</label><br/><input type=\"text\" name=\"host\" value=\"" + String(confHost) + "\" /> </div>"
|
||||
"</fieldset>"
|
||||
"<fieldset>"
|
||||
"<legend>IP Configuration</legend>"
|
||||
"<div><input type=\"radio\" name=\"ip_config\" value=\"0\" checked>DHCP <input type=\"radio\" name=\"ip_config\" value=\"1\">Static</div>"
|
||||
"<div><label for=\"ip\">Ip :</label><br/><input type=\"text\" name=\"ip\" value=\"" + String(ip) + "\" /> </div>"
|
||||
"<div><label for=\"gw\">Gateway :</label><br/><input type=\"text\" name=\"gw\" value=\"" + String(gw) + "\" /> </div>"
|
||||
"<div><label for=\"mask\">Netmask :</label><br/><input type=\"text\" name=\"mask\" value=\"" + String(mask) + "0\" /> </div>"
|
||||
"<div><label for=\"mask\">DNS :</label><br/><input type=\"text\" name=\"dns\" value=\"" + String(dns) + "\" /> </div>"
|
||||
"<div><label for=\"mask\">DNS2 :</label><br/><input type=\"text\" name=\"dns2\" value=\"" + String(dns2) + "\" /> </div>"
|
||||
"</fieldset>"
|
||||
"<fieldset>"
|
||||
"<legend>MQTT:</legend>"
|
||||
"<div><label for=\"mqttServer\">Server :</label><br/><input type=\"text\" name=\"mqttServer\" value=\"" + String(mqttServer) + "\" /> </div>"
|
||||
"<div><label for=\"mqttUser\">Username :</label><br/><input type=\"text\" name=\"mqttUser\" value=\"" + String(mqttUser) + "\" /> </div>"
|
||||
@ -81,12 +96,20 @@ void WebHandleSave() {
|
||||
String mqttUser;
|
||||
String mqttPasswd;
|
||||
|
||||
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host") || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") || !server.hasArg("mqttPort") ) {
|
||||
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host")
|
||||
|| !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd")
|
||||
|| !server.hasArg("mqttPort") || !server.hasArg("ip_config") || !server.hasArg("ip")
|
||||
|| !server.hasArg("gw") || !server.hasArg("mask") || !server.hasArg("dns")
|
||||
|| !server.hasArg("dns2")) {
|
||||
server.send(500, "text/plain", "Bad arguments\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (EepromSaveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"), server.arg("mqttPasswd"), server.arg("mqttPort").toInt()) < 0) {
|
||||
if (EepromSaveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"),
|
||||
server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"),
|
||||
server.arg("mqttPasswd"), server.arg("mqttPort").toInt(),
|
||||
server.arg("ip_config").toInt(), server.arg("ip"), server.arg("gw"),
|
||||
server.arg("mask"), server.arg("dns"), server.arg("dns2")) < 0) {
|
||||
server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
|
||||
return;
|
||||
}
|
||||
|
@ -73,9 +73,15 @@ void WebHandleNotFound();
|
||||
void WebSetupServer(int bootmode);
|
||||
|
||||
/* EEPROM decl */
|
||||
int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttpasswd, int mqttPort);
|
||||
int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host,
|
||||
String mqttServer, String mqttUser, String mqttPasswd,
|
||||
int mqttPort, int ip_config, String ip, String gw,
|
||||
String mask, String dns, String dns2);
|
||||
int EepromSaveBootMode(uint8_t bootMode);
|
||||
void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd, int &mqttPort);
|
||||
void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host,
|
||||
char **mqttServer, char **mqttUser, char **mqttPasswd,
|
||||
int &mqttPort, int &ip_config, char **ip, char **gw,
|
||||
char **mask, char **dns, char**dns2);
|
||||
|
||||
/* MQTT decl */
|
||||
int MqttConnect();
|
||||
@ -95,6 +101,7 @@ void WifiSetup(int bootmode, int forceSetup, char *confSsid, char *confPassword,
|
||||
myIP = WiFi.softAPIP();
|
||||
} else {
|
||||
SKETCH_DEBUG_PRINTLN("Connecting to Wifi...");
|
||||
|
||||
WiFi.begin(confSsid, confPassword);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
@ -171,13 +178,19 @@ void OTASetup() {
|
||||
void setup() {
|
||||
pinMode(3, OUTPUT);
|
||||
|
||||
char *confSsid;
|
||||
char *confPassword;
|
||||
char *confHost;
|
||||
char *mqttServer;
|
||||
char *mqttUser;
|
||||
char *mqttPasswd;
|
||||
char *confSsid = "";
|
||||
char *confPassword = "";
|
||||
char *confHost = "";
|
||||
char *mqttServer = "";
|
||||
char *mqttUser = "";
|
||||
char *mqttPasswd = "";
|
||||
int mqttPort;
|
||||
int ip_mode;
|
||||
char *ip = "";
|
||||
char *gw = "";
|
||||
char *mask = "";
|
||||
char *dns = "";
|
||||
char *dns2 = "";
|
||||
|
||||
delay(1000);
|
||||
SKETCH_DEBUG_INIT(115200);
|
||||
@ -192,7 +205,7 @@ void setup() {
|
||||
#endif
|
||||
|
||||
EEPROM.begin(CONFIG_EEPROM_SIZE);
|
||||
EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort);
|
||||
EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort, ip_mode, &ip, &gw, &mask, &dns, &dns2);
|
||||
|
||||
hostName = confHost;
|
||||
if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) {
|
||||
|
Loading…
Reference in New Issue
Block a user