diff espNode/desk/src/wifi.cpp @ 781:6c42c1f64f00

new driver for esp32 and R503 fingerprint sensor
author drewp@bigasterisk.com
date Mon, 24 Aug 2020 01:27:33 -0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/espNode/desk/src/wifi.cpp	Mon Aug 24 01:27:33 2020 -0700
@@ -0,0 +1,45 @@
+#include "wifi.h"
+
+#include "config.h"
+#include "mqtt.h"
+#include "fingerprint.h"
+
+namespace wifi {
+
+TimerHandle_t wifiReconnectTimer;
+namespace {
+void connectToWifi() {
+  Serial.println("Connecting to Wi-Fi...");
+  fingerprint::BlinkNotConnected();
+  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
+}
+void WiFiEvent(WiFiEvent_t event) {
+  Serial.printf("[WiFi-event] event: %d\n", event);
+  switch (event) {
+    case SYSTEM_EVENT_STA_GOT_IP:
+      Serial.println("WiFi connected");
+      Serial.println("IP address: ");
+      Serial.println(WiFi.localIP());
+      mqtt::ConnectToMqtt();
+      break;
+    case SYSTEM_EVENT_STA_DISCONNECTED:
+      Serial.println("WiFi lost connection");
+      mqtt::StopTimer();
+      xTimerStart(wifiReconnectTimer, 0);
+      break;
+    default:
+      // ??
+      break;
+  }
+}
+}  // namespace
+void Setup() {
+  wifiReconnectTimer =
+      xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0,
+                   reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
+
+  WiFi.onEvent(WiFiEvent);
+  connectToWifi();
+}
+bool IsConnected() { return WiFi.isConnected(); }
+}  // namespace wifi
\ No newline at end of file