| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- #include <SPI.h>
- #include <WiFiManager.h>
- #include <ArduinoJson.h>
- #include <stdlib.h>
- #include <FS.h>
- #include <IntParsing.h>
- #include <Size.h>
- #include <LinkedList.h>
- #include <GroupStateStore.h>
- #include <MiLightRadioConfig.h>
- #include <MiLightRemoteConfig.h>
- #include <MiLightHttpServer.h>
- #include <Settings.h>
- #include <MiLightUdpServer.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266SSDP.h>
- #include <MqttClient.h>
- #include <RGBConverter.h>
- #include <MiLightDiscoveryServer.h>
- #include <MiLightClient.h>
- #include <BulbStateUpdater.h>
- #include <LEDStatus.h>
- WiFiManager wifiManager;
- static LEDStatus *ledStatus;
- Settings settings;
- MiLightClient* milightClient = NULL;
- MiLightRadioFactory* radioFactory = NULL;
- MiLightHttpServer *httpServer = NULL;
- MqttClient* mqttClient = NULL;
- MiLightDiscoveryServer* discoveryServer = NULL;
- uint8_t currentRadioType = 0;
- // For tracking and managing group state
- GroupStateStore* stateStore = NULL;
- BulbStateUpdater* bulbStateUpdater = NULL;
- int numUdpServers = 0;
- MiLightUdpServer** udpServers = NULL;
- WiFiUDP udpSeder;
- /**
- * Set up UDP servers (both v5 and v6). Clean up old ones if necessary.
- */
- void initMilightUdpServers() {
- if (udpServers) {
- for (int i = 0; i < numUdpServers; i++) {
- if (udpServers[i]) {
- delete udpServers[i];
- }
- }
- delete udpServers;
- }
- udpServers = new MiLightUdpServer*[settings.numGatewayConfigs];
- numUdpServers = settings.numGatewayConfigs;
- for (size_t i = 0; i < settings.numGatewayConfigs; i++) {
- GatewayConfig* config = settings.gatewayConfigs[i];
- MiLightUdpServer* server = MiLightUdpServer::fromVersion(
- config->protocolVersion,
- milightClient,
- config->port,
- config->deviceId
- );
- if (server == NULL) {
- Serial.print(F("Error creating UDP server with protocol version: "));
- Serial.println(config->protocolVersion);
- } else {
- udpServers[i] = server;
- udpServers[i]->begin();
- }
- }
- }
- /**
- * Milight RF packet handler.
- *
- * Called both when a packet is sent locally, and when an intercepted packet
- * is read.
- */
- void onPacketSentHandler(uint8_t* packet, const MiLightRemoteConfig& config) {
- StaticJsonBuffer<200> buffer;
- JsonObject& result = buffer.createObject();
- BulbId bulbId = config.packetFormatter->parsePacket(packet, result);
- // set LED mode for a packet movement
- ledStatus->oneshot(settings.ledModePacket, settings.ledModePacketCount);
- if (&bulbId == &DEFAULT_BULB_ID) {
- Serial.println(F("Skipping packet handler because packet was not decoded"));
- return;
- }
- const MiLightRemoteConfig& remoteConfig =
- *MiLightRemoteConfig::fromType(bulbId.deviceType);
- // update state to reflect changes from this packet
- GroupState& groupState = stateStore->get(bulbId);
- groupState.patch(result);
- stateStore->set(bulbId, groupState);
- if (mqttClient) {
- // Sends the state delta derived from the raw packet
- char output[200];
- result.printTo(output);
- mqttClient->sendUpdate(remoteConfig, bulbId.deviceId, bulbId.groupId, output);
- // Sends the entire state
- bulbStateUpdater->enqueueUpdate(bulbId, groupState);
- }
- httpServer->handlePacketSent(packet, remoteConfig);
- }
- /**
- * Listen for packets on one radio config. Cycles through all configs as its
- * called.
- */
- void handleListen() {
- if (! settings.listenRepeats) {
- return;
- }
- MiLightRadio* radio = milightClient->switchRadio(currentRadioType++ % milightClient->getNumRadios());
- for (size_t i = 0; i < settings.listenRepeats; i++) {
- if (milightClient->available()) {
- uint8_t readPacket[MILIGHT_MAX_PACKET_LENGTH];
- size_t packetLen = milightClient->read(readPacket);
- const MiLightRemoteConfig* remoteConfig = MiLightRemoteConfig::fromReceivedPacket(
- radio->config(),
- readPacket,
- packetLen
- );
- if (remoteConfig == NULL) {
- // This can happen under normal circumstances, so not an error condition
- #ifdef DEBUG_PRINTF
- Serial.println(F("WARNING: Couldn't find remote for received packet"));
- #endif
- return;
- }
- // update state to reflect this packet
- onPacketSentHandler(readPacket, *remoteConfig);
- }
- }
- }
- /**
- * Called when MqttClient#update is first being processed. Stop sending updates
- * and aggregate state changes until the update is finished.
- */
- void onUpdateBegin() {
- if (bulbStateUpdater) {
- bulbStateUpdater->disable();
- }
- }
- /**
- * Called when MqttClient#update is finished processing. Re-enable state
- * updates, which will flush accumulated state changes.
- */
- void onUpdateEnd() {
- if (bulbStateUpdater) {
- bulbStateUpdater->enable();
- }
- }
- /**
- * Apply what's in the Settings object.
- */
- void applySettings() {
- if (milightClient) {
- delete milightClient;
- }
- if (radioFactory) {
- delete radioFactory;
- }
- if (mqttClient) {
- delete mqttClient;
- delete bulbStateUpdater;
- mqttClient = NULL;
- bulbStateUpdater = NULL;
- }
- if (stateStore) {
- delete stateStore;
- }
- radioFactory = MiLightRadioFactory::fromSettings(settings);
- if (radioFactory == NULL) {
- Serial.println(F("ERROR: unable to construct radio factory"));
- }
- stateStore = new GroupStateStore(MILIGHT_MAX_STATE_ITEMS, settings.stateFlushInterval);
- milightClient = new MiLightClient(
- radioFactory,
- stateStore,
- &settings
- );
- milightClient->begin();
- milightClient->onPacketSent(onPacketSentHandler);
- milightClient->onUpdateBegin(onUpdateBegin);
- milightClient->onUpdateEnd(onUpdateEnd);
- milightClient->setResendCount(settings.packetRepeats);
- if (settings.mqttServer().length() > 0) {
- mqttClient = new MqttClient(settings, milightClient);
- mqttClient->begin();
- bulbStateUpdater = new BulbStateUpdater(settings, *mqttClient, *stateStore);
- }
- initMilightUdpServers();
- if (discoveryServer) {
- delete discoveryServer;
- discoveryServer = NULL;
- }
- if (settings.discoveryPort != 0) {
- discoveryServer = new MiLightDiscoveryServer(settings);
- discoveryServer->begin();
- }
- // update LED pin and operating mode
- if (ledStatus) {
- ledStatus->changePin(settings.ledPin);
- ledStatus->continuous(settings.ledModeOperating);
- }
- }
- /**
- *
- */
- bool shouldRestart() {
- if (! settings.isAutoRestartEnabled()) {
- return false;
- }
- return settings.getAutoRestartPeriod()*60*1000 < millis();
- }
- // give a bit of time to update the status LED
- void handleLED() {
- ledStatus->handle();
- }
- void setup() {
- Serial.begin(9600);
- String ssid = "ESP" + String(ESP.getChipId());
-
- // load up our persistent settings from the file system
- SPIFFS.begin();
- Settings::load(settings);
- applySettings();
- // set up the LED status for wifi configuration
- ledStatus = new LEDStatus(settings.ledPin);
- ledStatus->continuous(settings.ledModeWifiConfig);
- // start up the wifi manager
- if (! MDNS.begin("milight-hub")) {
- 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")) {
- // set LED mode for successful operation
- ledStatus->continuous(settings.ledModeOperating);
- Serial.println(F("Wifi connected succesfully\n"));
- // if the config portal was started, make sure to turn off the config AP
- WiFi.mode(WIFI_STA);
- } else {
- // set LED mode for Wifi failed
- ledStatus->continuous(settings.ledModeWifiFailed);
- Serial.println(F("Wifi failed. Restarting in 10 seconds.\n"));
- delay(10000);
- ESP.restart();
- }
- MDNS.addService("http", "tcp", 80);
- SSDP.setSchemaURL("description.xml");
- SSDP.setHTTPPort(80);
- SSDP.setName("ESP8266 MiLight Gateway");
- SSDP.setSerialNumber(ESP.getChipId());
- SSDP.setURL("/");
- SSDP.setDeviceType("upnp:rootdevice");
- SSDP.begin();
- httpServer = new MiLightHttpServer(settings, milightClient, stateStore);
- httpServer->onSettingsSaved(applySettings);
- httpServer->on("/description.xml", HTTP_GET, []() { SSDP.schema(httpServer->client()); });
- httpServer->begin();
- Serial.printf_P(PSTR("Setup complete (version %s)\n"), QUOTE(MILIGHT_HUB_VERSION));
- }
- void loop() {
- httpServer->handleClient();
- if (mqttClient) {
- mqttClient->handleClient();
- bulbStateUpdater->loop();
- }
- if (udpServers) {
- for (size_t i = 0; i < settings.numGatewayConfigs; i++) {
- udpServers[i]->handleClient();
- }
- }
- if (discoveryServer) {
- discoveryServer->handleClient();
- }
- handleListen();
- stateStore->limitedFlush();
- // update LED with status
- ledStatus->handle();
- if (shouldRestart()) {
- Serial.println(F("Auto-restart triggered. Restarting..."));
- ESP.restart();
- }
- }
|