|
@@ -19,12 +19,9 @@
|
|
|
#include <MiLightDiscoveryServer.h>
|
|
#include <MiLightDiscoveryServer.h>
|
|
|
#include <MiLightClient.h>
|
|
#include <MiLightClient.h>
|
|
|
#include <BulbStateUpdater.h>
|
|
#include <BulbStateUpdater.h>
|
|
|
-#include <LEDStatus.h>
|
|
|
|
|
|
|
|
|
|
WiFiManager wifiManager;
|
|
WiFiManager wifiManager;
|
|
|
|
|
|
|
|
-static LEDStatus *ledStatus;
|
|
|
|
|
-
|
|
|
|
|
Settings settings;
|
|
Settings settings;
|
|
|
|
|
|
|
|
MiLightClient* milightClient = NULL;
|
|
MiLightClient* milightClient = NULL;
|
|
@@ -89,10 +86,6 @@ void onPacketSentHandler(uint8_t* packet, const MiLightRemoteConfig& config) {
|
|
|
JsonObject& result = buffer.createObject();
|
|
JsonObject& result = buffer.createObject();
|
|
|
BulbId bulbId = config.packetFormatter->parsePacket(packet, result, stateStore);
|
|
BulbId bulbId = config.packetFormatter->parsePacket(packet, result, stateStore);
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- // blip LED to indicate we saw a packet (send or receive)
|
|
|
|
|
- ledStatus->oneshot(LEDStatus::LEDMode::Flicker, 3);
|
|
|
|
|
-
|
|
|
|
|
if (&bulbId == &DEFAULT_BULB_ID) {
|
|
if (&bulbId == &DEFAULT_BULB_ID) {
|
|
|
Serial.println(F("Skipping packet handler because packet was not decoded"));
|
|
Serial.println(F("Skipping packet handler because packet was not decoded"));
|
|
|
return;
|
|
return;
|
|
@@ -101,7 +94,6 @@ void onPacketSentHandler(uint8_t* packet, const MiLightRemoteConfig& config) {
|
|
|
const MiLightRemoteConfig& remoteConfig =
|
|
const MiLightRemoteConfig& remoteConfig =
|
|
|
*MiLightRemoteConfig::fromType(bulbId.deviceType);
|
|
*MiLightRemoteConfig::fromType(bulbId.deviceType);
|
|
|
|
|
|
|
|
- // update state to reflect changes from this packet
|
|
|
|
|
GroupState& groupState = stateStore->get(bulbId);
|
|
GroupState& groupState = stateStore->get(bulbId);
|
|
|
groupState.patch(result);
|
|
groupState.patch(result);
|
|
|
stateStore->set(bulbId, groupState);
|
|
stateStore->set(bulbId, groupState);
|
|
@@ -149,7 +141,6 @@ void handleListen() {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // update state to reflect this packet
|
|
|
|
|
onPacketSentHandler(readPacket, *remoteConfig);
|
|
onPacketSentHandler(readPacket, *remoteConfig);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -233,10 +224,6 @@ void applySettings() {
|
|
|
discoveryServer = new MiLightDiscoveryServer(settings);
|
|
discoveryServer = new MiLightDiscoveryServer(settings);
|
|
|
discoveryServer->begin();
|
|
discoveryServer->begin();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // update LED pin
|
|
|
|
|
- if (ledStatus)
|
|
|
|
|
- ledStatus->changePin(settings.ledPin);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -250,43 +237,20 @@ bool shouldRestart() {
|
|
|
return settings.getAutoRestartPeriod()*60*1000 < millis();
|
|
return settings.getAutoRestartPeriod()*60*1000 < millis();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// give a bit of time to update the status LED
|
|
|
|
|
-void handleLED() {
|
|
|
|
|
- ledStatus->handle();
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
void setup() {
|
|
void setup() {
|
|
|
Serial.begin(9600);
|
|
Serial.begin(9600);
|
|
|
String ssid = "ESP" + String(ESP.getChipId());
|
|
String ssid = "ESP" + String(ESP.getChipId());
|
|
|
-
|
|
|
|
|
- // load up our persistent settings from the file system
|
|
|
|
|
|
|
+
|
|
|
|
|
+ wifiManager.setConfigPortalTimeout(180);
|
|
|
|
|
+ wifiManager.autoConnect(ssid.c_str(), "milightHub");
|
|
|
|
|
+
|
|
|
SPIFFS.begin();
|
|
SPIFFS.begin();
|
|
|
Settings::load(settings);
|
|
Settings::load(settings);
|
|
|
applySettings();
|
|
applySettings();
|
|
|
|
|
|
|
|
- // set up the LED status
|
|
|
|
|
- ledStatus = new LEDStatus(settings.ledPin);
|
|
|
|
|
- ledStatus->continuous(LEDStatus::LEDMode::FastToggle);
|
|
|
|
|
-
|
|
|
|
|
- // start up the wifi manager
|
|
|
|
|
if (! MDNS.begin("milight-hub")) {
|
|
if (! MDNS.begin("milight-hub")) {
|
|
|
Serial.println(F("Error setting up MDNS responder"));
|
|
Serial.println(F("Error setting up MDNS responder"));
|
|
|
}
|
|
}
|
|
|
- // tell Wifi manager to call us during the setup. Note that this "setSetupLoopCallback" is an addition
|
|
|
|
|
- // made to Wifi manager in a private fork. As of this writing, WifiManager has a new feature coming that
|
|
|
|
|
- // allows the "autoConnect" method to be non-blocking which can implement this same functionality. However,
|
|
|
|
|
- // that change is only on the development branch so we are going to continue to use this fork until
|
|
|
|
|
- // that is merged and ready.
|
|
|
|
|
- wifiManager.setSetupLoopCallback(handleLED);
|
|
|
|
|
- wifiManager.setConfigPortalTimeout(180);
|
|
|
|
|
- if (wifiManager.autoConnect(ssid.c_str(), "milightHub")) {
|
|
|
|
|
- ledStatus->continuous(LEDStatus::LEDMode::SlowBlip);
|
|
|
|
|
- Serial.println(F("Wifi connected succesfully\n"));
|
|
|
|
|
- } else {
|
|
|
|
|
- ledStatus->continuous(LEDStatus::LEDMode::On);
|
|
|
|
|
- Serial.println(F("Wifi failed. Oh well.\n"));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
MDNS.addService("http", "tcp", 80);
|
|
MDNS.addService("http", "tcp", 80);
|
|
|
|
|
|
|
@@ -328,9 +292,6 @@ void loop() {
|
|
|
|
|
|
|
|
stateStore->limitedFlush();
|
|
stateStore->limitedFlush();
|
|
|
|
|
|
|
|
- // update LED with status
|
|
|
|
|
- ledStatus->handle();
|
|
|
|
|
-
|
|
|
|
|
if (shouldRestart()) {
|
|
if (shouldRestart()) {
|
|
|
Serial.println(F("Auto-restart triggered. Restarting..."));
|
|
Serial.println(F("Auto-restart triggered. Restarting..."));
|
|
|
ESP.restart();
|
|
ESP.restart();
|