Added SoftAP mode by default

This commit is contained in:
Charles 2015-10-07 06:00:57 +02:00
parent 5ecfd2a3b4
commit 4420ee7670
3 changed files with 117 additions and 56 deletions

View File

@ -89,7 +89,7 @@ typedef struct
{
String sys_uptime;
String sys_free_ram;
String sys_flash_size;
String sys_flash_real_size;
String sys_flash_speed;
String sys_firmware_size;
String sys_firmware_free;

View File

@ -89,7 +89,7 @@ void UpdateSysinfo(boolean first_call, boolean show_debug)
// Values not subject to change during running sketch
if (first_call) {
sprintf( buff, "%d KB", ESP.getFlashChipRealSize()/1024 );
sysinfo.sys_flash_size = buff;
sysinfo.sys_flash_real_size = buff;
sprintf( buff, "%d KB", ESP.getSketchSize()/1024 );
sysinfo.sys_firmware_size = buff;
sprintf( buff, "%d KB", ESP.getFreeSketchSpace()/1024 );
@ -101,7 +101,7 @@ void UpdateSysinfo(boolean first_call, boolean show_debug)
if (show_debug) {
Debug(F("Firmware : ")); Debugln(__DATE__ " " __TIME__);
Debug(F("Flash real id: ")); Serial1.printf("0x%08X\r\n", ESP.getFlashChipId());
Debug(F("Flash Size : ")); Debugln(sysinfo.sys_flash_size);
Debug(F("Flash RSize : ")); Debugln(sysinfo.sys_flash_real_size);
Debug(F("CPU Speed : ")); Debugln(sysinfo.sys_flash_speed);
Debug(F("Sketch size : ")); Debugln(sysinfo.sys_firmware_size);
Debug(F("Free size : ")); Debugln(sysinfo.sys_firmware_free);
@ -260,6 +260,8 @@ Comments: If upgraded, no return, perform update and reboot ESP
====================================================================== */
void CheckOTAUpdate(void)
{
bool spiffs = false;
//OTA detection
if (OTA.parsePacket()) {
IPAddress remote = OTA.remoteIP();
@ -269,6 +271,14 @@ void CheckOTAUpdate(void)
LedRGBON(COLOR_MAGENTA);
DebugF("OTA received command ");
Debugln(cmd);
if (cmd == U_SPIFFS) {
spiffs = true;
DebugF("Get SPIFFS image");
}
DebugF("Update Start: ip:");
Debug(remote);
Debugf(", port:%d, size:%dKB\n", port, size/1024);
@ -276,7 +286,7 @@ void CheckOTAUpdate(void)
WiFiUDP::stopAll();
if(!Update.begin(size)) {
if(!Update.begin(size, cmd)) {
DebugF("Update Begin Error");
return;
}
@ -316,17 +326,40 @@ void CheckOTAUpdate(void)
}
/* ======================================================================
Function: WifiHandleConn
Purpose : Handle Wifi connection / reconnection and OTA updates
Function: WifiSoftAP
Purpose : Change Wifi mode to Soft AP
Input : -
Output : state of the wifi status
Comments: -
====================================================================== */
int WifiHandleConn()
int WifiSoftAP()
{
int ret = WiFi.status();
}
// Wait for connection if disconnected
/* ======================================================================
Function: WifiHandleConn
Purpose : Handle Wifi connection / reconnection and OTA updates
Input : setup true if we're called 1st Time from setup
Output : state of the wifi status
Comments: -
====================================================================== */
int WifiHandleConn(boolean setup = false)
{
int ret ;
if (setup)
{
// Check WiFi connection mode, at startup
// try to connect to AP
if (WiFi.getMode()!=WIFI_STA) {
WiFi.mode(WIFI_STA);
delay(10);
}
// Get Wifi Status
ret = WiFi.status();
// Try to get 1st connexion
if ( ret != WL_CONNECTED ) {
// Orange we're not connected anymore
@ -348,10 +381,6 @@ int WifiHandleConn()
DebugF("IP address : "); Debugln(WiFi.localIP());
DebugF("MAC address : "); Debugln(WiFi.macAddress());
MDNS.begin(DEFAULT_HOSTNAME);
MDNS.addService("arduino", "tcp", DEFAULT_OTA_PORT);
OTA.begin(DEFAULT_OTA_PORT);
// just in case your sketch sucks, keep update OTA Available
// Trust me, when coding and testing it happens, this could save
// the need to connect FTDI to reflash
@ -367,8 +396,38 @@ int WifiHandleConn()
}
}
// Handle OTA if we're connected
if ( ret == WL_CONNECTED )
// We did not succeded to connect ?
if ( ret != WL_CONNECTED ) {
uint8_t mac[WL_MAC_ADDR_LENGTH];
char ssid[32];
// start Soft AP
DebuglnF("Starting Soft AP mode");
WiFi.mode(WIFI_AP);
// Add the last two bytes of the MAC address to AP name
WiFi.softAPmacAddress(mac);
sprintf_P(ssid, PSTR("%s_%02X%02X"), DEFAULT_WIFI_AP_SSID, mac[4], mac[5] );
DebugF("SSID : "); Debugln(ssid);
//WiFi.softAP(ssid, DEFAULT_WIFI_AP_PSK);
//DebuglnF("PSK : " DEFAULT_WIFI_AP_PSK);
// No password
WiFi.softAP(ssid);
DebugF("IP : ");
Debugln(WiFi.softAPIP());
}
// Advertise US for Arduino IDE
// not very usefull on Windows (IDE does not always sees us)
MDNS.begin(DEFAULT_HOSTNAME);
MDNS.addService("arduino", "tcp", DEFAULT_OTA_PORT);
// Setup OTA feature
OTA.begin(DEFAULT_OTA_PORT);
}
// Handle OTA
CheckOTAUpdate();
return ret;
@ -407,8 +466,8 @@ void setup()
// enough for debugging purpose
Serial1.begin(115200);
Debugln(F("============"));
Debugln(F("Wifinfo V1.0"));
Debugln(F("=============="));
Debugln(F("Wifinfo V1.0.1"));
Debugln();
Debugflush();
@ -463,8 +522,8 @@ void setup()
LedRedOFF();
LedBluOFF();
// connect
WifiHandleConn();
// start Wifi connect or soft AP
WifiHandleConn(true);
// Update sysinfor variable and print them
UpdateSysinfo(true, true);
@ -485,9 +544,9 @@ void setup()
LedRGBOFF();
// control watchdog
ESP.wdtEnable(WDTO_4S);
//ESP.wdtEnable();
//ESP.wdtDisable()
ESP.wdtFeed();
//ESP.wdtFeed();
// Update sysinfo every second
Every_1_Sec.attach(1, Task_1_Sec);
@ -502,16 +561,12 @@ Comments: -
====================================================================== */
void loop()
{
static char c;
// Handle connection/disconnection/OTA update
if ( WifiHandleConn() == WL_CONNECTED ) {
char c;
// Do all related network stuff
server.handleClient();
//webSocket.loop();
}
// 1 second task job ?
if (task_1_sec) {

View File

@ -34,6 +34,12 @@
#define DEFAULT_WIFI_SSID "************"
#define DEFAULT_WIFI_PASS "************"
#define DEFAULT_HOSTNAME "WifInfo-esp01"
// En mode acces point
#define DEFAULT_WIFI_AP_SSID "WifInfo"
#define DEFAULT_WIFI_AP_PSK "WifInfoPSK"
// Port pour l'OTA
#define DEFAULT_OTA_PORT 8266
// Bit definition for different configuration modes