Skip to content

Commit

Permalink
allow to restart in wifimanager mode from config page
Browse files Browse the repository at this point in the history
  • Loading branch information
technyon committed Apr 10, 2022
1 parent 142f0a0 commit 6d4850c
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 57 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ file(GLOB SRCFILES
PreferencesKeys.h
BleScanner.cpp
BleInterfaces.h
SpiffsCookie.cpp
Version.h
lib/ESP32_BLE_Arduino-1.0.1/src/*.cpp
lib/ESP32_BLE_Arduino-1.0.1/src/*.h
Expand Down Expand Up @@ -50,9 +51,9 @@ target_link_arduino_libraries(${PROJECT_NAME}
WebServer
DNSServer
Preferences
SPIFFS
# esp32
# Wire
# SPIFFS
# FS
)

Expand Down
28 changes: 24 additions & 4 deletions Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,24 @@ void Network::initialize()
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wm;

// reset settings - wipe stored credentials for testing
// these are stored by the esp library
//wm.resetSettings();
std::vector<const char *> wm_menu;
wm_menu.push_back("wifi");
wm_menu.push_back("exit");
wm.setShowInfoUpdate(false);
wm.setMenu(wm_menu);

bool res = wm.autoConnect(); // password protected ap
bool res = false;

if(_cookie.isSet())
{
Serial.println(F("Opening WiFi configuration portal."));
_cookie.clear();
res = wm.startConfigPortal();
}
else
{
res = wm.autoConnect(); // password protected ap
}

if(!res) {
Serial.println(F("Failed to connect"));
Expand Down Expand Up @@ -154,6 +167,13 @@ void Network::publishPresenceDetection(char *csv)
// Serial.println(_presenceCsv);
}

void Network::restartAndConfigureWifi()
{
_cookie.set();
delay(200);
ESP.restart();
}

void Network::publishFloat(const char* topic, const float value, const uint8_t precision)
{
char str[30];
Expand Down
10 changes: 4 additions & 6 deletions Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <PubSubClient.h>
#include <WiFiClient.h>
#include <Preferences.h>
#include "SpiffsCookie.h"

class Network
{
Expand All @@ -17,9 +18,9 @@ class Network

void publishPresenceDetection(char* csv);

private:
void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length);
void restartAndConfigureWifi();

private:
void publishFloat(const char* topic, const float value, const uint8_t precision = 2);
void publishInt(const char* topic, const int value);
void publishBool(const char* topic, const bool value);
Expand All @@ -32,6 +33,7 @@ class Network
PubSubClient _mqttClient;
WiFiClient _wifiClient;
Preferences* _preferences;
SpiffsCookie _cookie;

bool _mqttConnected = false;

Expand All @@ -42,8 +44,4 @@ class Network
char _mqttPass[31] = {0};

char* _presenceCsv = nullptr;

bool _firstTunerStatePublish = true;

void (*_lockActionReceivedCallback)(const char* value) = NULL;
};
43 changes: 43 additions & 0 deletions SpiffsCookie.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "SpiffsCookie.h"
#include "FS.h"
#include "SPIFFS.h"

SpiffsCookie::SpiffsCookie()
{
if(!SPIFFS.begin(true))
{
Serial.println(F("SPIFFS Mount Failed"));
}
}

void SpiffsCookie::set()
{
File file = SPIFFS.open("/cookie", FILE_WRITE);
if(!file)
{
Serial.println(F("- failed to open file for writing"));
return;
}

if(file.write('#'))
{
Serial.println(F("- file written"));
} else {
Serial.println(F("- write failed"));
}
file.close();
}

void SpiffsCookie::clear()
{
if(!SPIFFS.remove("/cookie"))
{
Serial.println(F("Failed to remove file"));
}

}

const bool SpiffsCookie::isSet()
{
return SPIFFS.exists("/cookie");
}
13 changes: 13 additions & 0 deletions SpiffsCookie.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

class SpiffsCookie
{
public:
SpiffsCookie();
virtual ~SpiffsCookie() = default;

void set();
void clear();
const bool isSet();

};
2 changes: 1 addition & 1 deletion Version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#define blescanner_hub_version "1.0"
#define blescanner_hub_version "1.1"
Loading

0 comments on commit 6d4850c

Please sign in to comment.