add huzzah with support for ota and wifi

This commit is contained in:
Nicu Hodos 2024-04-26 13:25:10 +02:00
parent 5b30e0317d
commit 100a89a92f
6 changed files with 154 additions and 0 deletions

1
gateway/.gitignore vendored
View File

@ -3,3 +3,4 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
include/credentials.h

View File

@ -0,0 +1,4 @@
struct WifiCredentials {
const char* ssid;
const char* password;
} credentials[] = {"foo", "bar"};

32
gateway/include/ota.h Normal file
View File

@ -0,0 +1,32 @@
#include <ArduinoOTA.h>
namespace Ota {
void loop();
Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true);
void setup() {
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
}
void loop() {
ArduinoOTA.handle();
}
}

74
gateway/include/wifi.h Normal file
View File

@ -0,0 +1,74 @@
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h>
#include "credentials.h"
namespace Wifi {
WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler;
void reconnect();
Task tWifiReconnect(1 * TASK_MINUTE, TASK_FOREVER, reconnect, &ts);
String currentSSID;
String currentPsk;
void setup() {
stationConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& e) {
Serial.println("Reconnected to network.");
tWifiConnected.restart();
tWifiReconnect.cancel();
});
stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
Serial.println("Disconnected from network.");
tWifiReconnect.restartDelayed();
});
ESP8266WiFiMulti wifiMulti;
for (uint32_t i = 0; i < sizeof(credentials) / sizeof(WifiCredentials); i++) {
wifiMulti.addAP(credentials[i].ssid, credentials[i].password);
}
Serial.println("Connecting to WiFi netowrk.");
while (wifiMulti.run() != WL_CONNECTED) {
delay(500);
}
WiFi.setHostname("esp-clock");
currentSSID = WiFi.SSID();
currentPsk = WiFi.psk();
}
void reconnect() {
if (WiFi.status() != WL_CONNECTED) {
WiFi.forceSleepWake();
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
Serial.println("Reconnecting to WiFi netowrk...");
}
}
void disconnect() {
Serial.println("Disconnecting WiFi");
WiFi.disconnect();
WiFi.forceSleepBegin();
}
void printStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
}

View File

@ -11,6 +11,25 @@
[platformio]
default_envs = pro-mini
[env:huzzah]
platform = espressif8266
board = huzzah
framework = arduino
lib_extra_dirs =
../libraries
lib_deps =
sui77/rc-switch@^2.6.4
bblanchon/ArduinoJson@6.21.5
adafruit/Adafruit Unified Sensor@^1.1.4
arkhipenko/TaskScheduler@^3.7.0
https://git.hodos.ro/arduino/lib_serial-reader.git@^1.0.0
build_flags = -D DHT_SENSOR=0 -D DEBUG_RAW=0
upload_port = /dev/ttyUSB0
check_tool = cppcheck
check_flags = --enable=all
check_skip_packages = yes
check_severity = medium, high
[env:pro-mini]
platform = atmelavr
board = pro16MHzatmega328

View File

@ -10,6 +10,15 @@
#define SEND_PIN 11
#define RECEIVE_PIN 2
#if defined(ESP8266)
void onWifiConnected();
#include <TaskScheduler.h>
Scheduler ts;
Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts);
#include "wifi.h"
#include "ota.h"
#endif
RCSwitch mySwitch;
SerialReader<200> serialReader;
@ -24,6 +33,10 @@ void setup() {
mySwitch.setRepeatTransmit(10);
Dht::setup();
#if defined(ESP8266)
Wifi::setup();
Ota::setup();
#endif
Serial.begin(9600);
@ -113,4 +126,15 @@ void loop() {
readCommand();
readRcSwitch();
Dht::read();
#if defined(ESP8266)
ts.execute();
#endif
}
#if defined(ESP8266)
void onWifiConnected() {
Serial.println("Wifi connected event");
Wifi::printStatus();
Ota::tLoop.enable();
}
#endif