summaryrefslogtreecommitdiff
path: root/platformio/temphum/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'platformio/temphum/src/main.cpp')
-rw-r--r--platformio/temphum/src/main.cpp70
1 files changed, 51 insertions, 19 deletions
diff --git a/platformio/temphum/src/main.cpp b/platformio/temphum/src/main.cpp
index 1fa7e66..83f35b4 100644
--- a/platformio/temphum/src/main.cpp
+++ b/platformio/temphum/src/main.cpp
@@ -4,42 +4,48 @@
#include <Ticker.h>
#include <Wire.h>
+#include <homekit/config.h>
+#include <homekit/logging.h>
+#ifndef CONFIG_TARGET_ESP01
+#include <homekit/http_server.h>
+#endif
+#include <homekit/wifi.h>
+
#include "mqtt.h"
-#include "config.h"
-#include "logging.h"
-#include "http_server.h"
-#include "led.h"
-#include "config.def.h"
-#include "wifi.h"
+#include "leds.h"
#include "temphum.h"
-#include "stopwatch.h"
using namespace homekit;
+#ifndef CONFIG_TARGET_ESP01
enum class WorkingMode {
RECOVERY, // AP mode, http server with configuration
NORMAL, // MQTT client
};
static enum WorkingMode working_mode = WorkingMode::NORMAL;
+static const uint16_t recovery_boot_detection_ms = 2000;
+static const uint8_t recovery_boot_delay_ms = 100;
+#endif
+
enum class WiFiConnectionState {
WAITING = 0,
JUST_CONNECTED = 1,
CONNECTED = 2
};
-static const uint16_t recovery_boot_detection_ms = 2000;
-static const uint8_t recovery_boot_delay_ms = 100;
-
static volatile enum WiFiConnectionState wifi_state = WiFiConnectionState::WAITING;
static void* service = nullptr;
static WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler;
static Ticker wifiTimer;
+
#if MQTT_BLINK
static StopWatch blinkStopWatch;
#endif
+#ifndef CONFIG_TARGET_ESP01
static DNSServer* dnsServer = nullptr;
+#endif
static void onWifiConnected(const WiFiEventStationModeGotIP& event);
static void onWifiDisconnected(const WiFiEventStationModeDisconnected& event);
@@ -60,8 +66,9 @@ static void wifiConnect() {
PRINT("connecting to wifi..");
}
+#ifndef CONFIG_TARGET_ESP01
static void wifiHotspot() {
- esp_led.on();
+ mcu_led->on();
auto scanResults = wifi::scan();
@@ -76,57 +83,72 @@ static void wifiHotspot() {
}
static void waitForRecoveryPress() {
- pinMode(FLASH_BUTTON_PIN, INPUT_PULLUP);
+ pinMode(CONFIG_FLASH_GPIO, INPUT_PULLUP);
for (uint16_t i = 0; i < recovery_boot_detection_ms; i += recovery_boot_delay_ms) {
delay(recovery_boot_delay_ms);
- if (digitalRead(FLASH_BUTTON_PIN) == LOW) {
+ if (digitalRead(CONFIG_FLASH_GPIO) == LOW) {
working_mode = WorkingMode::RECOVERY;
break;
}
}
}
+#endif
void setup() {
WiFi.disconnect();
+#ifndef CONFIG_TARGET_ESP01
waitForRecoveryPress();
-
- temphum::setup();
+#endif
#ifdef DEBUG
Serial.begin(115200);
#endif
+ temphum::setup();
+
auto cfg = config::read();
if (config::isDirty(cfg)) {
PRINTLN("config is dirty, erasing...");
config::erase(cfg);
- board_led.blink(10, 50);
+#ifdef CONFIG_TARGET_NODEMCU
+ board_led->blink(10, 50);
+#else
+ mcu_led->blink(10, 50);
+#endif
}
+#ifndef CONFIG_TARGET_ESP01
switch (working_mode) {
case WorkingMode::RECOVERY:
wifiHotspot();
break;
case WorkingMode::NORMAL:
+#endif
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnected);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnected);
wifiConnect();
+#ifndef CONFIG_TARGET_ESP01
break;
}
+#endif
}
void loop() {
+#ifndef CONFIG_TARGET_ESP01
if (working_mode == WorkingMode::NORMAL) {
+#endif
if (wifi_state == WiFiConnectionState::WAITING) {
PRINT(".");
- esp_led.blink(2, 50);
+ mcu_led->blink(2, 50);
delay(1000);
return;
}
if (wifi_state == WiFiConnectionState::JUST_CONNECTED) {
- board_led.blink(3, 300);
+#ifdef CONFIG_TARGET_NODEMCU
+ board_led->blink(3, 300);
+#endif
wifi_state = WiFiConnectionState::CONNECTED;
if (service == nullptr)
@@ -144,21 +166,30 @@ void loop() {
if (mqtt->ota.readyToRestart) {
mqtt->disconnect();
+
} else if (mqtt->diagnosticsStopWatch.elapsed(10000)) {
mqtt->sendDiagnostics();
auto data = temphum::read();
+ PRINT("temp:");
+ PRINT(data.temp);
+ PRINT(", rh: ");
+ PRINTLN(data.rh);
+
mqtt->sendTempHumData(data.temp, data.rh);
}
#if MQTT_BLINK
// periodically blink board led
if (blinkStopWatch.elapsed(5000)) {
- board_led.blink(1, 10);
+#ifdef CONFIG_TARGET_NODEMCU
+ board_led->blink(1, 10);
+#endif
blinkStopWatch.save();
}
#endif
}
+#ifndef CONFIG_TARGET_ESP01
} else {
if (dnsServer != nullptr)
dnsServer->processNextRequest();
@@ -167,6 +198,7 @@ void loop() {
if (httpServer != nullptr)
httpServer->loop();
}
+#endif
}
static void onWifiConnected(const WiFiEventStationModeGotIP& event) {