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,
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);
}

View File

@ -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", "<form action=\"/save\" method=\"get\">"
"<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><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\">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\">DNS2 :</label><br/><input type=\"text\" name=\"dns2\" value=\"" + String(dns2) + "\" /> </div>"
"</fieldset>"
@ -88,13 +92,20 @@ void WebHandleGpio() {
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() {
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<uint32_t>(ip), static_cast<uint32_t>(gw),
static_cast<uint32_t>(mask), static_cast<uint32_t>(dns), static_cast<uint32_t>(dns2)) < 0) {
WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
return;
}

View File

@ -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) {