Skip to content

Commit

Permalink
Blinker class for WiFi indications
Browse files Browse the repository at this point in the history
  • Loading branch information
tcsullivan committed Apr 2, 2024
1 parent 72afe04 commit f29ae85
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
32 changes: 32 additions & 0 deletions noisemeter-device/blinker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef BLINKER_H
#define BLINKER_H

#include "board.h"

#include <Ticker.h>

class Blinker : public Ticker
{
public:
Blinker(unsigned ms):
Ticker()
{
attach_ms(ms, callback, &state);
}

~Blinker() {
// turn off the LED
digitalWrite(PIN_LED1, HIGH);
}

private:
int state = HIGH;

static void callback(int *st) {
digitalWrite(PIN_LED1, *st);
*st ^= HIGH;
}
};

#endif // BLINKER_H

38 changes: 20 additions & 18 deletions noisemeter-device/noisemeter-device.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <ArduinoJson.h> // https://arduinojson.org/
#include <ArduinoJson.hpp>
#include <HTTPClient.h>
#include <Ticker.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <dummy.h> // ESP32 core
Expand All @@ -20,6 +19,7 @@
#include <esp_efuse_table.h>

#include "access-point.h"
#include "blinker.h"
#include "board.h"
#include "data-packet.h"
#include "sos-iir-filter.h"
Expand All @@ -33,6 +33,7 @@
#include <cstdint>
#include <list>
#include <iterator>
#include <optional>

#if defined(BUILD_PLATFORMIO) && defined(BOARD_ESP32_PCB)
HWCDC USBSerial;
Expand Down Expand Up @@ -159,12 +160,8 @@ void setup() {
// Run the access point if it is requested or if there are no valid credentials.
if (isAPNeeded) {
AccessPoint ap (saveNetworkCreds);
Ticker blink;
Blinker bl (500);

blink.attach_ms(500, [] {
static bool state = HIGH;
digitalWrite(PIN_LED1, state ^= HIGH);
});
ap.run(); // does not return
}

Expand Down Expand Up @@ -198,15 +195,23 @@ void loop() {
}

if (WiFi.status() == WL_CONNECTED) {
packets.remove_if([](const auto& pkt) {
if (pkt.count > 0) {
const auto payload = createJSONPayload(pkt);
WiFiClientSecure client;
return uploadData(&client, payload) == 0;
} else {
return true; // Discard empty packets
}
});
{
std::optional<Blinker> bl;

// Only blink if there's multiple packets to send
if (++packets.cbegin() != packets.cend())
bl.emplace(300);

packets.remove_if([](const auto& pkt) {
if (pkt.count > 0) {
const auto payload = createJSONPayload(pkt);
WiFiClientSecure client;
return uploadData(&client, payload) == 0;
} else {
return true; // Discard empty packets
}
});
}

#if defined(BOARD_ESP32_PCB)
// We have WiFi: also check for software updates
Expand All @@ -218,16 +223,13 @@ void loop() {
if (ota.available()) {
SERIAL.print(ota.version);
SERIAL.println(" available!");
digitalWrite(PIN_LED1, LOW);

if (ota.download()) {
SERIAL.println("Download success! Restarting...");
digitalWrite(PIN_LED1, HIGH);
delay(1000);
ESP.restart();
} else {
SERIAL.println("Update download failed.");
digitalWrite(PIN_LED1, HIGH);
}
} else {
SERIAL.println("No update available.");
Expand Down

0 comments on commit f29ae85

Please sign in to comment.