diff --git a/config.cpp b/config.cpp index cc2df90..19512b3 100644 --- a/config.cpp +++ b/config.cpp @@ -99,6 +99,10 @@ unsigned char Config::load() { } EEPROM.commit(); + if(data.flag) { + SERIAL_ECHOLN("Going to use the old config to connect the network"); + } + SERIAL_ECHOLN("We didn't connect the network before"); return data.flag; } @@ -150,4 +154,39 @@ void Config::save() { EEPROM.commit(); } +// Save to ip address to sdcard +int Config::save_ip(const char *ip) { + SdFat sdfat; + + SERIAL_ECHOLN("Going to save config to ip.gcode file"); + + if(!sdcontrol.canWeTakeBus()) { + SERIAL_ECHOLN("Marlin is controling the bus"); + return -1; + } + sdcontrol.takeBusControl(); + + if(!sdfat.begin(SD_CS, SPI_FULL_SPEED)) { + SERIAL_ECHOLN("Initial SD failed"); + sdcontrol.relinquishBusControl(); + return -2; + } + + // Remove the old file + sdfat.remove("ip.gcode"); + + File file = sdfat.open("ip.gcode", FILE_WRITE); + if (!file) { + SERIAL_ECHOLN("Open ip file failed"); + sdcontrol.relinquishBusControl(); + return -3; + } + + // Get SSID and PASSWORD from file + char buf[21] = "M117 "; + strncat(buf,ip,15); + file.write(buf, 21); + file.close(); +} + Config config; diff --git a/config.h b/config.h index ab6f5a8..0101ad7 100644 --- a/config.h +++ b/config.h @@ -26,7 +26,8 @@ public: void password(char* password); void save(const char*ssid,const char*password); void save(); - + int save_ip(const char *ip); + protected: CONFIG_TYPE data; }; diff --git a/network.cpp b/network.cpp index 616b6a3..f531884 100644 --- a/network.cpp +++ b/network.cpp @@ -6,6 +6,14 @@ #include "ESPWebDAV.h" #include "sdControl.h" +String IpAddress2String(const IPAddress& ipAddress) +{ + return String(ipAddress[0]) + String(".") +\ + String(ipAddress[1]) + String(".") +\ + String(ipAddress[2]) + String(".") +\ + String(ipAddress[3]) ; +} + bool Network::start() { wifiConnected = false; @@ -41,6 +49,8 @@ bool Network::start() { wifiConnected = true; config.save(); + String sIp = IpAddress2String(WiFi.localIP()); + config.save_ip(sIp.c_str()); SERIAL_ECHOLN("Going to start DAV server"); if(startDAVServer() < 0) return false;