Generate ip.gcode after wifi connected

This commit is contained in:
George Fu
2020-09-08 15:08:44 +08:00
parent 1ac7da5260
commit e042b8f6c3
3 changed files with 51 additions and 1 deletions

View File

@@ -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;

View File

@@ -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;
};

View File

@@ -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;