Skip to content

Commit

Permalink
add wifi timeout and fix empty ssid check
Browse files Browse the repository at this point in the history
  • Loading branch information
tcsullivan committed Mar 26, 2024
1 parent 9bbb992 commit f85c131
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 28 deletions.
80 changes: 54 additions & 26 deletions noisemeter-device/noisemeter-device.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static Storage Creds;
// Uncomment these to disable WiFi and/or data upload
//#define UPLOAD_DISABLED

constexpr auto WIFI_CONNECT_TIMEOUT_MS = 20 * 1000;

const unsigned long UPLOAD_INTERVAL_SEC = 60 * 5; // Upload every 5 mins
// const unsigned long UPLOAD_INTERVAL_SEC = 30; // Upload every 30 secs
const unsigned long OTA_INTERVAL_SEC = 60 * 60 * 24; // Check for updates daily
Expand Down Expand Up @@ -108,6 +110,9 @@ static Timestamp lastUpload = Timestamp::invalidTimestamp();
static Timestamp lastOTACheck = Timestamp::invalidTimestamp();

static UUID buildDeviceId();
[[noreturn]]
static void enterAccessPointMode();
static int tryWifiConnection();

/**
* Initialization routine.
Expand Down Expand Up @@ -145,44 +150,32 @@ void setup() {
packets.emplace_front();

#ifndef UPLOAD_DISABLED
// Run the access point if it is requested or if there are no valid credentials.
bool resetPressed = !digitalRead(PIN_BUTTON);
if (resetPressed || !Creds.valid() || Creds.get(Storage::Entry::SSID).isEmpty()) {
AccessPoint ap (saveNetworkCreds);
bool isAPNeeded = false;

if (!digitalRead(PIN_BUTTON) || !Creds.valid()) {
SERIAL.print("Erasing stored credentials...");
Creds.clear();
SERIAL.println(" done.");

ap.run(); // does not return
isAPNeeded = true;
} else if (tryWifiConnection() < 0 || Timestamp::synchronize() < 0) {
isAPNeeded = true;
}

// Valid credentials: Next step would be to connect to the network.
const auto ssid = Creds.get(Storage::Entry::SSID);

SERIAL.print("Ready to connect to ");
SERIAL.println(ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), Creds.get(Storage::Entry::Passkey).c_str());

// wait for WiFi connection
SERIAL.print("Waiting for WiFi to connect...");
while (WiFi.status() != WL_CONNECTED) {
SERIAL.print(".");
delay(500);
// Run the access point if it is requested or if there are no valid credentials.
if (isAPNeeded) {
enterAccessPointMode(); // Does not return
}
SERIAL.println("\nConnected to the WiFi network");
SERIAL.print("Local ESP32 IP: ");
SERIAL.println(WiFi.localIP());

SERIAL.println("Waiting for NTP time sync...");
Timestamp::synchronize();
Timestamp now;
lastUpload = now;
lastOTACheck = now;

SERIAL.println("Connected to the WiFi network.");
SERIAL.print("Local ESP32 IP: ");
SERIAL.println(WiFi.localIP());
SERIAL.print("Current time: ");
SERIAL.println(lastUpload);
SERIAL.println(now);
#endif // !UPLOAD_DISABLED

digitalWrite(PIN_LED1, HIGH);
Expand Down Expand Up @@ -289,7 +282,7 @@ void saveNetworkCreds(WebServer& httpServer) {
const auto psk = httpServer.arg("psk");

// Confirm that the given credentials will fit in the allocated EEPROM space.
if (Creds.canStore(ssid) && Creds.canStore(psk)) {
if (!ssid.isEmpty() && Creds.canStore(ssid) && Creds.canStore(psk)) {
Creds.set(Storage::Entry::SSID, ssid);
Creds.set(Storage::Entry::Passkey, psk);
Creds.commit();
Expand All @@ -314,6 +307,41 @@ UUID buildDeviceId()
return UUID(mac[0] | (mac[1] << 8) | (mac[2] << 16), mac[3] | (mac[4] << 8) | (mac[5] << 16));
}

void enterAccessPointMode()
{
AccessPoint ap (saveNetworkCreds);

SERIAL.print("Erasing stored credentials...");
Creds.clear();
SERIAL.println(" done.");

ap.run(); // does not return
}

int tryWifiConnection()
{
const auto ssid = Creds.get(Storage::Entry::SSID);

SERIAL.print("Ready to connect to ");
SERIAL.println(ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), Creds.get(Storage::Entry::Passkey).c_str());

// wait for WiFi connection
SERIAL.print("Waiting for WiFi to connect...");
const auto start = millis();
bool connected;

do {
connected = WiFi.status() != WL_CONNECTED;
SERIAL.print(".");
delay(500);
} while (!connected && millis() - start < WIFI_CONNECT_TIMEOUT_MS);

return connected ? 0 : -1;
}

String createJSONPayload(const DataPacket& dp)
{
#ifdef BUILD_PLATFORMIO
Expand Down
12 changes: 10 additions & 2 deletions noisemeter-device/timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <Arduino.h>
#include <ctime>

constexpr auto NTP_CONNECT_TIMEOUT_MS = 20 * 1000;

class Timestamp
{
public:
Expand All @@ -26,12 +28,18 @@ class Timestamp
return std::difftime(ts.tm, tm);
}

static void synchronize() {
static int synchronize() {
configTime(0, 0, "pool.ntp.org");

const auto start = millis();
bool connected;

do {
delay(1000);
} while (!Timestamp().valid());
connected = Timestamp().valid();
} while (!connected && millis() - start < NTP_CONNECT_TIMEOUT_MS);

return connected ? 0 : -1;
}

static Timestamp invalidTimestamp() {
Expand Down

0 comments on commit f85c131

Please sign in to comment.