Skip to content

Commit

Permalink
Improv (#24)
Browse files Browse the repository at this point in the history
Added a param `IMPROV_ENABLED` that can be used to specify the numbers of milliseconds where Improv WiFi is active, after that period web server is started.
  • Loading branch information
sblantipodi authored Apr 3, 2022
1 parent b25b7d7 commit f5e24e5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/sblantipodi/arduino_bootstrapper.git"
},
"version": "1.12.9",
"version": "1.12.10",
"examples": "examples/*.cpp",
"exclude": "tests",
"frameworks": "arduino",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Bootstrapper
version=1.12.9
version=1.12.10
author=Davide Perini <[email protected]>
maintainer=Davide Perini <[email protected]>
sentence=A client library for MQTT messaging.
Expand Down
8 changes: 4 additions & 4 deletions src/BootstrapManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void BootstrapManager::bootstrapSetup(void (*manageDisconnections)(), void (*man
bool rcpResponseSent = false;
void BootstrapManager::bootstrapLoop(void (*manageDisconnections)(), void (*manageQueueSubscription)(), void (*manageHardwareButton)()) {

#if (IMPROV_ENABLED)
#if (IMPROV_ENABLED > 0)
if (!rcpResponseSent && wifiManager.isConnected()) {
rcpResponseSent = true;
wifiManager.sendImprovRPCResponse(0x01, true);
Expand Down Expand Up @@ -569,13 +569,13 @@ bool BootstrapManager::isWifiConfigured() {
// if no ssid available, launch web server to get config params via browser
void BootstrapManager::launchWebServerForOTAConfig() {

#if (IMPROV_ENABLED)
#if (IMPROV_ENABLED > 0)
unsigned long timeNowStatus = 0;
bool switchToWebServer = false;
// If WiFi is not configured, handle improv packet for 15 seconds, then switch to settinigs managed by web server
WiFi.disconnect();
while ((WiFi.status() != WL_CONNECTED && !switchToWebServer) || improvePacketReceived) {
if(millis() > timeNowStatus + 15000) {
while (((WiFi.localIP()[0] == 0 && WiFi.status() != WL_CONNECTED) && !switchToWebServer) || improvePacketReceived) {
if(millis() > timeNowStatus + IMPROV_ENABLED) {
timeNowStatus = millis();
switchToWebServer = true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@
extern Adafruit_SSD1306 display;
#endif

// Values greater then 0 enables Improv for that milliseconds period
#ifndef IMPROV_ENABLED
#define IMPROV_ENABLED false
#define IMPROV_ENABLED 0
#endif

// SENSORNAME will be used as device network name
Expand Down
23 changes: 11 additions & 12 deletions src/WifiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,11 @@ void WifiManager::sendImprovInfoResponse() {
//out[8] = 2; //Length (set below)
out[9] = ImprovRPCType::Request_Info;
//out[10] = 0; //Data len (set below)
out[11] = 4; //Firmware len ("GLOW")
out[12] = 'G';
out[13] = 'W';
out[14] = ' ';
out[15] = ' ';
out[11] = 4; //Firmware len ("FIRM")
out[12] = 'F';
out[13] = 'I';
out[14] = 'R';
out[15] = 'M';
uint8_t lengthSum = 17;
uint8_t vlen = sprintf_P(out + lengthSum, firmwareVersion.c_str());
out[16] = vlen;
Expand All @@ -653,8 +653,8 @@ void WifiManager::sendImprovInfoResponse() {
#endif
out[lengthSum] = hlen;
lengthSum += hlen + 1;
//Use serverDescription if it has been changed from the default "GLOW", else mDNS name
bool useMdnsName = (strcmp(serverDescription, "GLOW") == 0 && strlen(cmDNS) > 0);
//Use serverDescription if it has been changed from the default "FIRM", else mDNS name
bool useMdnsName = (strcmp(serverDescription, "FIRM") == 0 && strlen(cmDNS) > 0);
strcpy(out + lengthSum + 1, useMdnsName ? cmDNS : serverDescription);
uint8_t nlen = strlen(useMdnsName ? cmDNS : serverDescription);
out[lengthSum] = nlen;
Expand Down Expand Up @@ -689,8 +689,8 @@ void WifiManager::parseWiFiCommand(char *rpcData) {
sendImprovStateResponse(0x03, false); //provisioning
improvActive = 2;
DynamicJsonDocument doc(1024);
String devName = String(random(0, 100000));
doc["deviceName"] = "GLOW_WORM_" + devName;
String devName = String(random(0, 90000));
doc["deviceName"] = String(DEVICE_NAME) + "_" + devName;
doc["microcontrollerIP"] = "DHCP";
doc["qsid"] = clientSSID;
doc["qpass"] = clientPass;
Expand Down Expand Up @@ -726,9 +726,8 @@ void WifiManager::parseWiFiCommand(char *rpcData) {
}
delay(DELAY_200);
#endif
delay(DELAY_2000);
sendImprovRPCResponse(ImprovRPCType::Request_State);
delay(DELAY_2000);
delay(DELAY_200);
sendImprovStateResponse(0x04, false);
delay(DELAY_200);
ESP.restart();
Expand All @@ -740,10 +739,10 @@ void WifiManager::handleImprovPacket() {

uint8_t header[6] = {'I', 'M', 'P', 'R', 'O', 'V'};
bool timeout = false;
uint8_t waitTime = 25;
uint16_t packetByte = 0;
uint8_t packetLen = 9;
uint8_t checksum = 0;
uint8_t waitTime = 25;
uint8_t rpcCommandType = 0;
char rpcData[128];
rpcData[0] = 0;
Expand Down

0 comments on commit f5e24e5

Please sign in to comment.