separate pro-mini and huzzah logic into dedicated files
This commit is contained in:
parent
100a89a92f
commit
61a7ca0bde
29
gateway/include/huzzah.h
Normal file
29
gateway/include/huzzah.h
Normal file
@ -0,0 +1,29 @@
|
||||
#include <TaskScheduler.h>
|
||||
|
||||
#define SEND_PIN PIN_SPI_MOSI
|
||||
#define RECEIVE_PIN PIN_SPI_MISO
|
||||
#define RED_LED LED_BUILTIN
|
||||
// #define BLUE_LED 2
|
||||
|
||||
Scheduler ts;
|
||||
|
||||
#include "wifi.h"
|
||||
|
||||
namespace Board {
|
||||
void turnOffLed(uint8_t led) {
|
||||
digitalWrite(led, HIGH);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(RED_LED, OUTPUT);
|
||||
turnOffLed(RED_LED);
|
||||
// pinMode(BLUE_LED, OUTPUT);
|
||||
// turnOffLed(BLUE_LED);
|
||||
Wifi::setup();
|
||||
Ota::setup();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
ts.execute();
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,10 @@
|
||||
|
||||
namespace Ota {
|
||||
|
||||
void loop();
|
||||
void loop() {
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
|
||||
Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true);
|
||||
|
||||
void setup() {
|
||||
@ -25,8 +28,4 @@ namespace Ota {
|
||||
});
|
||||
ArduinoOTA.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
}
|
||||
|
||||
37
gateway/include/pro-mini.h
Normal file
37
gateway/include/pro-mini.h
Normal file
@ -0,0 +1,37 @@
|
||||
#include <SerialReader.h>
|
||||
|
||||
#define RESET_PIN 10
|
||||
#define SEND_PIN 11
|
||||
#define RECEIVE_PIN 2
|
||||
|
||||
namespace Board {
|
||||
SerialReader<200> serialReader;
|
||||
|
||||
void setup() {
|
||||
digitalWrite(RESET_PIN, HIGH);
|
||||
pinMode(RESET_PIN, OUTPUT);
|
||||
}
|
||||
|
||||
void blink() {
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
delay(200);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
}
|
||||
|
||||
void readCommand() {
|
||||
if (serialReader.readLine(Serial) > 0) {
|
||||
char* cmd = serialReader.getBuffer();
|
||||
if (strcmp("reset", cmd) == 0) {
|
||||
Serial.println("resetting...");
|
||||
delay(1200);
|
||||
digitalWrite(RESET_PIN, LOW);
|
||||
Serial.println("resetting failed");
|
||||
}
|
||||
runJsonCommand(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
readCommand();
|
||||
}
|
||||
}
|
||||
@ -1,33 +1,46 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
#include "ota.h"
|
||||
#include "credentials.h"
|
||||
|
||||
namespace Wifi {
|
||||
|
||||
WiFiEventHandler stationConnectedHandler;
|
||||
WiFiEventHandler stationDisconnectedHandler;
|
||||
void reconnect();
|
||||
|
||||
Task tWifiReconnect(1 * TASK_MINUTE, TASK_FOREVER, reconnect, &ts);
|
||||
ESP8266WiFiMulti wifiMulti;
|
||||
|
||||
String currentSSID;
|
||||
String currentPsk;
|
||||
|
||||
void printStatus();
|
||||
|
||||
Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER, [](){
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
WiFi.forceSleepWake();
|
||||
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
|
||||
Serial.println("Reconnecting to WiFi netowrk...");
|
||||
}
|
||||
}, &ts);
|
||||
Task tConnected(TASK_IMMEDIATE, TASK_ONCE, [](){
|
||||
Serial.println("Wifi connected event");
|
||||
printStatus();
|
||||
Ota::tLoop.enable();
|
||||
}, &ts);
|
||||
|
||||
void setup() {
|
||||
stationConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& e) {
|
||||
Serial.println("Reconnected to network.");
|
||||
tWifiConnected.restart();
|
||||
tWifiReconnect.cancel();
|
||||
});
|
||||
tConnected.restart();
|
||||
tReconnect.cancel();
|
||||
});
|
||||
|
||||
stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
|
||||
Serial.println("Disconnected from network.");
|
||||
tWifiReconnect.restartDelayed();
|
||||
});
|
||||
tReconnect.restartDelayed();
|
||||
});
|
||||
|
||||
|
||||
ESP8266WiFiMulti wifiMulti;
|
||||
for (uint32_t i = 0; i < sizeof(credentials) / sizeof(WifiCredentials); i++) {
|
||||
wifiMulti.addAP(credentials[i].ssid, credentials[i].password);
|
||||
}
|
||||
@ -36,19 +49,11 @@ namespace Wifi {
|
||||
while (wifiMulti.run() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
}
|
||||
WiFi.setHostname("esp-clock");
|
||||
WiFi.setHostname("rc-gateway");
|
||||
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();
|
||||
|
||||
@ -22,13 +22,17 @@ lib_deps =
|
||||
bblanchon/ArduinoJson@6.21.5
|
||||
adafruit/Adafruit Unified Sensor@^1.1.4
|
||||
arkhipenko/TaskScheduler@^3.7.0
|
||||
adafruit/DHT sensor library@1.3.2
|
||||
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
|
||||
upload_port = 192.168.6.161
|
||||
upload_protocol = espota
|
||||
upload_flags =
|
||||
--host_port=10000
|
||||
|
||||
[env:pro-mini]
|
||||
platform = atmelavr
|
||||
@ -40,7 +44,7 @@ lib_deps =
|
||||
sui77/rc-switch@^2.6.3
|
||||
bblanchon/ArduinoJson@6.21.5
|
||||
adafruit/Adafruit Unified Sensor@^1.1.4
|
||||
adafruit/DHT sensor library@1.3.10
|
||||
adafruit/DHT sensor library@1.3.2
|
||||
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
|
||||
|
||||
@ -4,41 +4,27 @@
|
||||
#include "Protocol_1.h"
|
||||
#include "Protocol_2.h"
|
||||
#include "output.h"
|
||||
#include <SerialReader.h>
|
||||
|
||||
#define RESET_PIN 10
|
||||
#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;
|
||||
void runJsonCommand(char* cmd);
|
||||
|
||||
#if defined(ESP8266)
|
||||
#include "huzzah.h"
|
||||
#else
|
||||
#include "pro-mini.h"
|
||||
#endif
|
||||
|
||||
|
||||
void setup() {
|
||||
digitalWrite(RESET_PIN, HIGH);
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
pinMode(RESET_PIN, OUTPUT);
|
||||
Serial.begin(9600);
|
||||
|
||||
mySwitch.enableReceive(digitalPinToInterrupt(RECEIVE_PIN));
|
||||
mySwitch.enableTransmit(SEND_PIN);
|
||||
mySwitch.setRepeatTransmit(10);
|
||||
|
||||
Dht::setup();
|
||||
#if defined(ESP8266)
|
||||
Wifi::setup();
|
||||
Ota::setup();
|
||||
#endif
|
||||
|
||||
Serial.begin(9600);
|
||||
Board::setup();
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
@ -109,32 +95,8 @@ void runJsonCommand(char* cmd) {
|
||||
}
|
||||
}
|
||||
|
||||
void readCommand() {
|
||||
if (serialReader.readLine(Serial) > 0) {
|
||||
char* cmd = serialReader.getBuffer();
|
||||
if (strcmp("reset", cmd) == 0) {
|
||||
Serial.println("resetting...");
|
||||
delay(1200);
|
||||
digitalWrite(RESET_PIN, LOW);
|
||||
Serial.println("resetting failed");
|
||||
}
|
||||
runJsonCommand(cmd);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
readCommand();
|
||||
Board::loop();
|
||||
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
|
||||
Loading…
x
Reference in New Issue
Block a user