main.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #ifndef UNIT_TEST
  2. #include <SPI.h>
  3. #include <WiFiManager.h>
  4. #include <ArduinoJson.h>
  5. #include <stdlib.h>
  6. #include <FS.h>
  7. #include <IntParsing.h>
  8. #include <Size.h>
  9. #include <LinkedList.h>
  10. #include <GroupStateStore.h>
  11. #include <MiLightRadioConfig.h>
  12. #include <MiLightRemoteConfig.h>
  13. #include <MiLightHttpServer.h>
  14. #include <Settings.h>
  15. #include <MiLightUdpServer.h>
  16. #include <ESP8266mDNS.h>
  17. #include <ESP8266SSDP.h>
  18. #include <MqttClient.h>
  19. #include <RGBConverter.h>
  20. #include <MiLightDiscoveryServer.h>
  21. #include <MiLightClient.h>
  22. #include <BulbStateUpdater.h>
  23. #include <LEDStatus.h>
  24. WiFiManager wifiManager;
  25. static LEDStatus *ledStatus;
  26. Settings settings;
  27. MiLightClient* milightClient = NULL;
  28. MiLightRadioFactory* radioFactory = NULL;
  29. MiLightHttpServer *httpServer = NULL;
  30. MqttClient* mqttClient = NULL;
  31. MiLightDiscoveryServer* discoveryServer = NULL;
  32. uint8_t currentRadioType = 0;
  33. // For tracking and managing group state
  34. GroupStateStore* stateStore = NULL;
  35. BulbStateUpdater* bulbStateUpdater = NULL;
  36. int numUdpServers = 0;
  37. MiLightUdpServer** udpServers = NULL;
  38. WiFiUDP udpSeder;
  39. /**
  40. * Set up UDP servers (both v5 and v6). Clean up old ones if necessary.
  41. */
  42. void initMilightUdpServers() {
  43. if (udpServers) {
  44. for (int i = 0; i < numUdpServers; i++) {
  45. if (udpServers[i]) {
  46. delete udpServers[i];
  47. }
  48. }
  49. delete udpServers;
  50. }
  51. udpServers = new MiLightUdpServer*[settings.numGatewayConfigs];
  52. numUdpServers = settings.numGatewayConfigs;
  53. for (size_t i = 0; i < settings.numGatewayConfigs; i++) {
  54. GatewayConfig* config = settings.gatewayConfigs[i];
  55. MiLightUdpServer* server = MiLightUdpServer::fromVersion(
  56. config->protocolVersion,
  57. milightClient,
  58. config->port,
  59. config->deviceId
  60. );
  61. if (server == NULL) {
  62. Serial.print(F("Error creating UDP server with protocol version: "));
  63. Serial.println(config->protocolVersion);
  64. } else {
  65. udpServers[i] = server;
  66. udpServers[i]->begin();
  67. }
  68. }
  69. }
  70. /**
  71. * Milight RF packet handler.
  72. *
  73. * Called both when a packet is sent locally, and when an intercepted packet
  74. * is read.
  75. */
  76. void onPacketSentHandler(uint8_t* packet, const MiLightRemoteConfig& config) {
  77. StaticJsonBuffer<200> buffer;
  78. JsonObject& result = buffer.createObject();
  79. BulbId bulbId = config.packetFormatter->parsePacket(packet, result);
  80. // set LED mode for a packet movement
  81. ledStatus->oneshot(settings.ledModePacket, settings.ledModePacketCount);
  82. if (&bulbId == &DEFAULT_BULB_ID) {
  83. Serial.println(F("Skipping packet handler because packet was not decoded"));
  84. return;
  85. }
  86. const MiLightRemoteConfig& remoteConfig =
  87. *MiLightRemoteConfig::fromType(bulbId.deviceType);
  88. // update state to reflect changes from this packet
  89. GroupState* groupState = stateStore->get(bulbId);
  90. if (groupState != NULL) {
  91. groupState->patch(result);
  92. // Copy state before setting it to avoid group 0 re-initialization clobbering it
  93. stateStore->set(bulbId, GroupState(*groupState));
  94. }
  95. if (mqttClient) {
  96. // Sends the state delta derived from the raw packet
  97. char output[200];
  98. result.printTo(output);
  99. mqttClient->sendUpdate(remoteConfig, bulbId.deviceId, bulbId.groupId, output);
  100. // Sends the entire state
  101. if (groupState != NULL) {
  102. bulbStateUpdater->enqueueUpdate(bulbId, *groupState);
  103. }
  104. }
  105. httpServer->handlePacketSent(packet, remoteConfig);
  106. }
  107. /**
  108. * Listen for packets on one radio config. Cycles through all configs as its
  109. * called.
  110. */
  111. void handleListen() {
  112. if (! settings.listenRepeats) {
  113. return;
  114. }
  115. MiLightRadio* radio = milightClient->switchRadio(currentRadioType++ % milightClient->getNumRadios());
  116. for (size_t i = 0; i < settings.listenRepeats; i++) {
  117. if (milightClient->available()) {
  118. uint8_t readPacket[MILIGHT_MAX_PACKET_LENGTH];
  119. size_t packetLen = milightClient->read(readPacket);
  120. const MiLightRemoteConfig* remoteConfig = MiLightRemoteConfig::fromReceivedPacket(
  121. radio->config(),
  122. readPacket,
  123. packetLen
  124. );
  125. if (remoteConfig == NULL) {
  126. // This can happen under normal circumstances, so not an error condition
  127. #ifdef DEBUG_PRINTF
  128. Serial.println(F("WARNING: Couldn't find remote for received packet"));
  129. #endif
  130. return;
  131. }
  132. // update state to reflect this packet
  133. onPacketSentHandler(readPacket, *remoteConfig);
  134. }
  135. }
  136. }
  137. /**
  138. * Called when MqttClient#update is first being processed. Stop sending updates
  139. * and aggregate state changes until the update is finished.
  140. */
  141. void onUpdateBegin() {
  142. if (bulbStateUpdater) {
  143. bulbStateUpdater->disable();
  144. }
  145. }
  146. /**
  147. * Called when MqttClient#update is finished processing. Re-enable state
  148. * updates, which will flush accumulated state changes.
  149. */
  150. void onUpdateEnd() {
  151. if (bulbStateUpdater) {
  152. bulbStateUpdater->enable();
  153. }
  154. }
  155. /**
  156. * Apply what's in the Settings object.
  157. */
  158. void applySettings() {
  159. if (milightClient) {
  160. delete milightClient;
  161. }
  162. if (radioFactory) {
  163. delete radioFactory;
  164. }
  165. if (mqttClient) {
  166. delete mqttClient;
  167. delete bulbStateUpdater;
  168. mqttClient = NULL;
  169. bulbStateUpdater = NULL;
  170. }
  171. if (stateStore) {
  172. delete stateStore;
  173. }
  174. radioFactory = MiLightRadioFactory::fromSettings(settings);
  175. if (radioFactory == NULL) {
  176. Serial.println(F("ERROR: unable to construct radio factory"));
  177. }
  178. stateStore = new GroupStateStore(MILIGHT_MAX_STATE_ITEMS, settings.stateFlushInterval);
  179. milightClient = new MiLightClient(
  180. radioFactory,
  181. stateStore,
  182. &settings
  183. );
  184. milightClient->begin();
  185. milightClient->onPacketSent(onPacketSentHandler);
  186. milightClient->onUpdateBegin(onUpdateBegin);
  187. milightClient->onUpdateEnd(onUpdateEnd);
  188. milightClient->setResendCount(settings.packetRepeats);
  189. if (settings.mqttServer().length() > 0) {
  190. mqttClient = new MqttClient(settings, milightClient);
  191. mqttClient->begin();
  192. bulbStateUpdater = new BulbStateUpdater(settings, *mqttClient, *stateStore);
  193. }
  194. initMilightUdpServers();
  195. if (discoveryServer) {
  196. delete discoveryServer;
  197. discoveryServer = NULL;
  198. }
  199. if (settings.discoveryPort != 0) {
  200. discoveryServer = new MiLightDiscoveryServer(settings);
  201. discoveryServer->begin();
  202. }
  203. // update LED pin and operating mode
  204. if (ledStatus) {
  205. ledStatus->changePin(settings.ledPin);
  206. ledStatus->continuous(settings.ledModeOperating);
  207. }
  208. WiFi.hostname(settings.hostname);
  209. }
  210. /**
  211. *
  212. */
  213. bool shouldRestart() {
  214. if (! settings.isAutoRestartEnabled()) {
  215. return false;
  216. }
  217. return settings.getAutoRestartPeriod()*60*1000 < millis();
  218. }
  219. // give a bit of time to update the status LED
  220. void handleLED() {
  221. ledStatus->handle();
  222. }
  223. void setup() {
  224. Serial.begin(9600);
  225. String ssid = "ESP" + String(ESP.getChipId());
  226. // load up our persistent settings from the file system
  227. SPIFFS.begin();
  228. Settings::load(settings);
  229. applySettings();
  230. // set up the LED status for wifi configuration
  231. ledStatus = new LEDStatus(settings.ledPin);
  232. ledStatus->continuous(settings.ledModeWifiConfig);
  233. // start up the wifi manager
  234. if (! MDNS.begin("milight-hub")) {
  235. Serial.println(F("Error setting up MDNS responder"));
  236. }
  237. // tell Wifi manager to call us during the setup. Note that this "setSetupLoopCallback" is an addition
  238. // made to Wifi manager in a private fork. As of this writing, WifiManager has a new feature coming that
  239. // allows the "autoConnect" method to be non-blocking which can implement this same functionality. However,
  240. // that change is only on the development branch so we are going to continue to use this fork until
  241. // that is merged and ready.
  242. wifiManager.setSetupLoopCallback(handleLED);
  243. wifiManager.setConfigPortalTimeout(180);
  244. if (wifiManager.autoConnect(ssid.c_str(), "milightHub")) {
  245. // set LED mode for successful operation
  246. ledStatus->continuous(settings.ledModeOperating);
  247. Serial.println(F("Wifi connected succesfully\n"));
  248. // if the config portal was started, make sure to turn off the config AP
  249. WiFi.mode(WIFI_STA);
  250. } else {
  251. // set LED mode for Wifi failed
  252. ledStatus->continuous(settings.ledModeWifiFailed);
  253. Serial.println(F("Wifi failed. Restarting in 10 seconds.\n"));
  254. delay(10000);
  255. ESP.restart();
  256. }
  257. MDNS.addService("http", "tcp", 80);
  258. SSDP.setSchemaURL("description.xml");
  259. SSDP.setHTTPPort(80);
  260. SSDP.setName("ESP8266 MiLight Gateway");
  261. SSDP.setSerialNumber(ESP.getChipId());
  262. SSDP.setURL("/");
  263. SSDP.setDeviceType("upnp:rootdevice");
  264. SSDP.begin();
  265. httpServer = new MiLightHttpServer(settings, milightClient, stateStore);
  266. httpServer->onSettingsSaved(applySettings);
  267. httpServer->on("/description.xml", HTTP_GET, []() { SSDP.schema(httpServer->client()); });
  268. httpServer->begin();
  269. Serial.printf_P(PSTR("Setup complete (version %s)\n"), QUOTE(MILIGHT_HUB_VERSION));
  270. }
  271. void loop() {
  272. httpServer->handleClient();
  273. if (mqttClient) {
  274. mqttClient->handleClient();
  275. bulbStateUpdater->loop();
  276. }
  277. if (udpServers) {
  278. for (size_t i = 0; i < settings.numGatewayConfigs; i++) {
  279. udpServers[i]->handleClient();
  280. }
  281. }
  282. if (discoveryServer) {
  283. discoveryServer->handleClient();
  284. }
  285. handleListen();
  286. stateStore->limitedFlush();
  287. // update LED with status
  288. ledStatus->handle();
  289. if (shouldRestart()) {
  290. Serial.println(F("Auto-restart triggered. Restarting..."));
  291. ESP.restart();
  292. }
  293. }
  294. #endif