From e82378e2246c88597686fe58b471f833106d99c0 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Thu, 7 Apr 2016 02:02:51 +0200 Subject: [PATCH] Save Static ip config as uint32_t --- WifiControlSensor/EEPROM.ino | 24 +++++++----- WifiControlSensor/WebServer.ino | 52 +++++++++++++++++-------- WifiControlSensor/WifiControlSensor.ino | 32 ++++++++------- 3 files changed, 69 insertions(+), 39 deletions(-) diff --git a/WifiControlSensor/EEPROM.ino b/WifiControlSensor/EEPROM.ino index 66c5325..5908fc1 100644 --- a/WifiControlSensor/EEPROM.ino +++ b/WifiControlSensor/EEPROM.ino @@ -8,8 +8,8 @@ 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 mqttPort, int ip_config, uint32_t ip, uint32_t gw, + uint32_t mask, uint32_t dns, uint32_t dns2) { String eeprom; eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" @@ -57,8 +57,8 @@ 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, int &ip_config, char **ip, char **gw, - char **mask, char **dns, char**dns2) { + int &mqttPort, int &ip_config, uint32_t &ip, uint32_t &gw, + uint32_t &mask, uint32_t &dns, uint32_t &dns2) { int i = 2; @@ -85,9 +85,15 @@ void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **ho 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); + readConfElement(&tmpString, i); + ip = atoi(tmpString); + readConfElement(&tmpString, i); + gw = atoi(tmpString); + readConfElement(&tmpString, i); + mask = atoi(tmpString); + readConfElement(&tmpString, i); + dns = atoi(tmpString); + readConfElement(&tmpString, i); + dns2 = atoi(tmpString); + } diff --git a/WifiControlSensor/WebServer.ino b/WifiControlSensor/WebServer.ino index 1b05261..9db8dfe 100644 --- a/WifiControlSensor/WebServer.ino +++ b/WifiControlSensor/WebServer.ino @@ -33,6 +33,10 @@ void WebHandleRoot() { ); } +void WebSendError(String error) { + server.send(500, "text/plain", error); +} + void WebHandleSetup() { uint8_t mode; char *confSsid = ""; @@ -43,13 +47,13 @@ void WebHandleSetup() { 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 = ""; + uint32_t ip; + uint32_t gw; + uint32_t mask; + uint32_t dns; + uint32_t dns2; - EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort, ip_config, &ip, &gw, &mask, &dns, &dns2); + EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort, ip_config, ip, gw, mask, dns, dns2); server.send(200, "text/html", "
" "
" @@ -63,7 +67,7 @@ void WebHandleSetup() { "
DHCP Static
" "

" "

" - "

" + "

" "

" "

" "
" @@ -88,13 +92,20 @@ void WebHandleGpio() { server.send(200, "text/html", "

GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "

"); } +boolean WebSetIp(IPAddress &addr, char *id, String error) { + if (server.arg(id) != "" && !addr.fromString(server.arg(id).c_str())) { + WebSendError(error); + return false; + } + return true; +} + void WebHandleSave() { - String password; - String ssid; - String hostName; - String mqttServer; - String mqttUser; - String mqttPasswd; + IPAddress ip; + IPAddress gw; + IPAddress mask; + IPAddress dns; + IPAddress dns2; if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host") || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") @@ -105,12 +116,21 @@ void WebHandleSave() { return; } + + //Check Ip configuration + if (server.arg("ip_config").toInt() == 1) { + if (!WebSetIp(ip, "ip", "Incorrect IP") || !WebSetIp(gw, "gw", "Incorrect Gateway") || !WebSetIp(mask, "mask", "Incorrect NetMask") || + !WebSetIp(dns, "dns", "Incorrect DNS") || !WebSetIp(dns2, "dns2", "Incorrect DNS2")) { + 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(), - 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"); + server.arg("ip_config").toInt(), static_cast(ip), static_cast(gw), + static_cast(mask), static_cast(dns), static_cast(dns2)) < 0) { + WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); return; } diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index 7ae8272..51abd09 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -75,13 +75,13 @@ 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 ip_config, String ip, String gw, - String mask, String dns, String dns2); + int mqttPort, int ip_config, uint32_t ip, uint32_t gw, + uint32_t mask, uint32_t dns, uint32_t 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, int &ip_config, char **ip, char **gw, - char **mask, char **dns, char**dns2); + int &mqttPort, int &ip_config, uint32_t &ip, uint32_t &gw, + uint32_t &mask, uint32_t &dns, uint32_t &dns2); /* MQTT decl */ int MqttConnect(); @@ -92,16 +92,20 @@ void MqttCheckSubscription(); void MqttChangeGpioValue(int gpio, int value); bool MqttIsConfigured(); -void WifiSetup(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) { +void WifiSetup(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost, int ip_config, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t dns, uint32_t dns2) { IPAddress myIP; if (bootmode == BOOTMODE_SETUP || forceSetup) { - SKETCH_DEBUG_PRINTLN("Configuring access point..."); + SKETCH_DEBUG_PRINT("Configuring access point..."); + SKETCH_DEBUG_PRINTLN(ssid); /* You can set a password to the AP here */ WiFi.softAP(ssid); myIP = WiFi.softAPIP(); } else { SKETCH_DEBUG_PRINTLN("Connecting to Wifi..."); - + if(ip_config == 1){ + SKETCH_DEBUG_PRINTLN("Configure using statis ip"); + WiFi.config(IPAddress(ip), IPAddress(gw), IPAddress(mask), IPAddress(dns), IPAddress(dns2)); + } WiFi.begin(confSsid, confPassword); while (WiFi.status() != WL_CONNECTED) { delay(500); @@ -186,11 +190,11 @@ void setup() { char *mqttPasswd = ""; int mqttPort; int ip_mode; - char *ip = ""; - char *gw = ""; - char *mask = ""; - char *dns = ""; - char *dns2 = ""; + uint32_t ip; + uint32_t gw; + uint32_t mask; + uint32_t dns; + uint32_t dns2; delay(1000); SKETCH_DEBUG_INIT(115200); @@ -205,7 +209,7 @@ void setup() { #endif EEPROM.begin(CONFIG_EEPROM_SIZE); - EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort, ip_mode, &ip, &gw, &mask, &dns, &dns2); + 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) { @@ -227,7 +231,7 @@ void setup() { SKETCH_DEBUG_PRINT(txStatus ? "No" : "Yes"); SKETCH_DEBUG_PRINTLN(); - WifiSetup(mode, txStatus == 0, confSsid, confPassword, confHost); + WifiSetup(mode, txStatus == 0, confSsid, confPassword, confHost, ip_mode, ip, gw, mask, dns, dns2); MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort, confHost); if (mode == BOOTMODE_OTA) {