Save Static ip config as uint32_t

This commit is contained in:
Mathieu Maret 2016-04-07 02:02:51 +02:00
parent 96149ab5da
commit e82378e224
3 changed files with 69 additions and 39 deletions

View File

@ -8,8 +8,8 @@
int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host, int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host,
String mqttServer, String mqttUser, String mqttPasswd, String mqttServer, String mqttUser, String mqttPasswd,
int mqttPort, int ip_config, String ip, String gw, int mqttPort, int ip_config, uint32_t ip, uint32_t gw,
String mask, String dns, String dns2) { uint32_t mask, uint32_t dns, uint32_t dns2) {
String eeprom; String eeprom;
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" 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, void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host,
char **mqttServer, char **mqttUser, char **mqttPasswd, char **mqttServer, char **mqttUser, char **mqttPasswd,
int &mqttPort, int &ip_config, char **ip, char **gw, int &mqttPort, int &ip_config, uint32_t &ip, uint32_t &gw,
char **mask, char **dns, char**dns2) { uint32_t &mask, uint32_t &dns, uint32_t &dns2) {
int i = 2; int i = 2;
@ -85,9 +85,15 @@ void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **ho
mqttPort = atoi(tmpString); mqttPort = atoi(tmpString);
readConfElement(&tmpString, i); readConfElement(&tmpString, i);
ip_config = atoi(tmpString); ip_config = atoi(tmpString);
readConfElement(ip, i); readConfElement(&tmpString, i);
readConfElement(gw, i); ip = atoi(tmpString);
readConfElement(mask, i); readConfElement(&tmpString, i);
readConfElement(dns, i); gw = atoi(tmpString);
readConfElement(dns2, i); readConfElement(&tmpString, i);
mask = atoi(tmpString);
readConfElement(&tmpString, i);
dns = atoi(tmpString);
readConfElement(&tmpString, i);
dns2 = atoi(tmpString);
} }

View File

@ -33,6 +33,10 @@ void WebHandleRoot() {
); );
} }
void WebSendError(String error) {
server.send(500, "text/plain", error);
}
void WebHandleSetup() { void WebHandleSetup() {
uint8_t mode; uint8_t mode;
char *confSsid = ""; char *confSsid = "";
@ -43,13 +47,13 @@ void WebHandleSetup() {
char *mqttPasswd = ""; char *mqttPasswd = "";
int mqttPort = 1883; int mqttPort = 1883;
int ip_config = 0; int ip_config = 0;
char *ip = "192.168.0.123"; uint32_t ip;
char *gw = "192.168.0.1"; uint32_t gw;
char *mask = "255.255.255.0"; uint32_t mask;
char *dns = "8.8.8.8"; uint32_t dns;
char *dns2 = ""; 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", "<form action=\"/save\" method=\"get\">" server.send(200, "text/html", "<form action=\"/save\" method=\"get\">"
"<fieldset>" "<fieldset>"
@ -63,7 +67,7 @@ void WebHandleSetup() {
"<div><input type=\"radio\" name=\"ip_config\" value=\"0\" checked>DHCP <input type=\"radio\" name=\"ip_config\" value=\"1\">Static</div>" "<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=\"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=\"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\">Netmask :</label><br/><input type=\"text\" name=\"mask\" value=\"" + String(mask) + "\" /> </div>"
"<div><label for=\"mask\">DNS :</label><br/><input type=\"text\" name=\"dns\" value=\"" + String(dns) + "\" /> </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>" "<div><label for=\"mask\">DNS2 :</label><br/><input type=\"text\" name=\"dns2\" value=\"" + String(dns2) + "\" /> </div>"
"</fieldset>" "</fieldset>"
@ -88,13 +92,20 @@ void WebHandleGpio() {
server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>"); server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
} }
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() { void WebHandleSave() {
String password; IPAddress ip;
String ssid; IPAddress gw;
String hostName; IPAddress mask;
String mqttServer; IPAddress dns;
String mqttUser; IPAddress dns2;
String mqttPasswd;
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host") if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host")
|| !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd")
@ -105,12 +116,21 @@ void WebHandleSave() {
return; 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"), if (EepromSaveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"),
server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"), server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"),
server.arg("mqttPasswd"), server.arg("mqttPort").toInt(), server.arg("mqttPasswd"), server.arg("mqttPort").toInt(),
server.arg("ip_config").toInt(), server.arg("ip"), server.arg("gw"), server.arg("ip_config").toInt(), static_cast<uint32_t>(ip), static_cast<uint32_t>(gw),
server.arg("mask"), server.arg("dns"), server.arg("dns2")) < 0) { static_cast<uint32_t>(mask), static_cast<uint32_t>(dns), static_cast<uint32_t>(dns2)) < 0) {
server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
return; return;
} }

View File

@ -75,13 +75,13 @@ void WebSetupServer(int bootmode);
/* EEPROM decl */ /* EEPROM decl */
int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host, int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host,
String mqttServer, String mqttUser, String mqttPasswd, String mqttServer, String mqttUser, String mqttPasswd,
int mqttPort, int ip_config, String ip, String gw, int mqttPort, int ip_config, uint32_t ip, uint32_t gw,
String mask, String dns, String dns2); uint32_t mask, uint32_t dns, uint32_t dns2);
int EepromSaveBootMode(uint8_t bootMode); int EepromSaveBootMode(uint8_t bootMode);
void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host, void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host,
char **mqttServer, char **mqttUser, char **mqttPasswd, char **mqttServer, char **mqttUser, char **mqttPasswd,
int &mqttPort, int &ip_config, char **ip, char **gw, int &mqttPort, int &ip_config, uint32_t &ip, uint32_t &gw,
char **mask, char **dns, char**dns2); uint32_t &mask, uint32_t &dns, uint32_t &dns2);
/* MQTT decl */ /* MQTT decl */
int MqttConnect(); int MqttConnect();
@ -92,16 +92,20 @@ void MqttCheckSubscription();
void MqttChangeGpioValue(int gpio, int value); void MqttChangeGpioValue(int gpio, int value);
bool MqttIsConfigured(); 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; IPAddress myIP;
if (bootmode == BOOTMODE_SETUP || forceSetup) { 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 */ /* You can set a password to the AP here */
WiFi.softAP(ssid); WiFi.softAP(ssid);
myIP = WiFi.softAPIP(); myIP = WiFi.softAPIP();
} else { } else {
SKETCH_DEBUG_PRINTLN("Connecting to Wifi..."); 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); WiFi.begin(confSsid, confPassword);
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
@ -186,11 +190,11 @@ void setup() {
char *mqttPasswd = ""; char *mqttPasswd = "";
int mqttPort; int mqttPort;
int ip_mode; int ip_mode;
char *ip = ""; uint32_t ip;
char *gw = ""; uint32_t gw;
char *mask = ""; uint32_t mask;
char *dns = ""; uint32_t dns;
char *dns2 = ""; uint32_t dns2;
delay(1000); delay(1000);
SKETCH_DEBUG_INIT(115200); SKETCH_DEBUG_INIT(115200);
@ -205,7 +209,7 @@ void setup() {
#endif #endif
EEPROM.begin(CONFIG_EEPROM_SIZE); 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; hostName = confHost;
if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) { if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) {
@ -227,7 +231,7 @@ void setup() {
SKETCH_DEBUG_PRINT(txStatus ? "No" : "Yes"); SKETCH_DEBUG_PRINT(txStatus ? "No" : "Yes");
SKETCH_DEBUG_PRINTLN(); 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); MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort, confHost);
if (mode == BOOTMODE_OTA) { if (mode == BOOTMODE_OTA) {