main.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #include <SPI.h>
  2. #include <WiFiManager.h>
  3. #include <ArduinoJson.h>
  4. #include <stdlib.h>
  5. #include <FS.h>
  6. #include <IntParsing.h>
  7. #include <Size.h>
  8. #include <LinkedList.h>
  9. #include <GroupStateStore.h>
  10. #include <MiLightRadioConfig.h>
  11. #include <MiLightRemoteConfig.h>
  12. #include <MiLightHttpServer.h>
  13. #include <Settings.h>
  14. #include <MiLightUdpServer.h>
  15. #include <ESP8266mDNS.h>
  16. #include <ESP8266SSDP.h>
  17. #include <MqttClient.h>
  18. #include <RGBConverter.h>
  19. #include <MiLightDiscoveryServer.h>
  20. #include <MiLightClient.h>
  21. #include <BulbStateUpdater.h>
  22. WiFiManager wifiManager;
  23. Settings settings;
  24. MiLightClient* milightClient = NULL;
  25. MiLightRadioFactory* radioFactory = NULL;
  26. MiLightHttpServer *httpServer = NULL;
  27. MqttClient* mqttClient = NULL;
  28. MiLightDiscoveryServer* discoveryServer = NULL;
  29. uint8_t currentRadioType = 0;
  30. // For tracking and managing group state
  31. GroupStateStore* stateStore = NULL;
  32. BulbStateUpdater* bulbStateUpdater = NULL;
  33. int numUdpServers = 0;
  34. MiLightUdpServer** udpServers = NULL;
  35. WiFiUDP udpSeder;
  36. /**
  37. * Set up UDP servers (both v5 and v6). Clean up old ones if necessary.
  38. */
  39. void initMilightUdpServers() {
  40. if (udpServers) {
  41. for (int i = 0; i < numUdpServers; i++) {
  42. if (udpServers[i]) {
  43. delete udpServers[i];
  44. }
  45. }
  46. delete udpServers;
  47. }
  48. udpServers = new MiLightUdpServer*[settings.numGatewayConfigs];
  49. numUdpServers = settings.numGatewayConfigs;
  50. for (size_t i = 0; i < settings.numGatewayConfigs; i++) {
  51. GatewayConfig* config = settings.gatewayConfigs[i];
  52. MiLightUdpServer* server = MiLightUdpServer::fromVersion(
  53. config->protocolVersion,
  54. milightClient,
  55. config->port,
  56. config->deviceId
  57. );
  58. if (server == NULL) {
  59. Serial.print(F("Error creating UDP server with protocol version: "));
  60. Serial.println(config->protocolVersion);
  61. } else {
  62. udpServers[i] = server;
  63. udpServers[i]->begin();
  64. }
  65. }
  66. }
  67. /**
  68. * Milight RF packet handler.
  69. *
  70. * Called both when a packet is sent locally, and when an intercepted packet
  71. * is read.
  72. */
  73. void onPacketSentHandler(uint8_t* packet, const MiLightRemoteConfig& config) {
  74. StaticJsonBuffer<200> buffer;
  75. JsonObject& result = buffer.createObject();
  76. BulbId bulbId = config.packetFormatter->parsePacket(packet, result, stateStore);
  77. if (&bulbId == &DEFAULT_BULB_ID) {
  78. Serial.println(F("Skipping packet handler because packet was not decoded"));
  79. return;
  80. }
  81. const MiLightRemoteConfig& remoteConfig =
  82. *MiLightRemoteConfig::fromType(bulbId.deviceType);
  83. GroupState& groupState = stateStore->get(bulbId);
  84. groupState.patch(result);
  85. stateStore->set(bulbId, groupState);
  86. if (mqttClient) {
  87. // Sends the state delta derived from the raw packet
  88. char output[200];
  89. result.printTo(output);
  90. mqttClient->sendUpdate(remoteConfig, bulbId.deviceId, bulbId.groupId, output);
  91. // Sends the entire state
  92. bulbStateUpdater->enqueueUpdate(bulbId, groupState);
  93. }
  94. httpServer->handlePacketSent(packet, remoteConfig);
  95. }
  96. /**
  97. * Listen for packets on one radio config. Cycles through all configs as its
  98. * called.
  99. */
  100. void handleListen() {
  101. if (! settings.listenRepeats) {
  102. return;
  103. }
  104. MiLightRadio* radio = milightClient->switchRadio(currentRadioType++ % milightClient->getNumRadios());
  105. for (size_t i = 0; i < settings.listenRepeats; i++) {
  106. if (milightClient->available()) {
  107. uint8_t readPacket[MILIGHT_MAX_PACKET_LENGTH];
  108. size_t packetLen = milightClient->read(readPacket);
  109. const MiLightRemoteConfig* remoteConfig = MiLightRemoteConfig::fromReceivedPacket(
  110. radio->config(),
  111. readPacket,
  112. packetLen
  113. );
  114. if (remoteConfig == NULL) {
  115. // This can happen under normal circumstances, so not an error condition
  116. #ifdef DEBUG_PRINTF
  117. Serial.println(F("WARNING: Couldn't find remote for received packet"));
  118. #endif
  119. return;
  120. }
  121. onPacketSentHandler(readPacket, *remoteConfig);
  122. }
  123. }
  124. }
  125. /**
  126. * Called when MqttClient#update is first being processed. Stop sending updates
  127. * and aggregate state changes until the update is finished.
  128. */
  129. void onUpdateBegin() {
  130. if (bulbStateUpdater) {
  131. bulbStateUpdater->disable();
  132. }
  133. }
  134. /**
  135. * Called when MqttClient#update is finished processing. Re-enable state
  136. * updates, which will flush accumulated state changes.
  137. */
  138. void onUpdateEnd() {
  139. if (bulbStateUpdater) {
  140. bulbStateUpdater->enable();
  141. }
  142. }
  143. /**
  144. * Apply what's in the Settings object.
  145. */
  146. void applySettings() {
  147. if (milightClient) {
  148. delete milightClient;
  149. }
  150. if (radioFactory) {
  151. delete radioFactory;
  152. }
  153. if (mqttClient) {
  154. delete mqttClient;
  155. delete bulbStateUpdater;
  156. mqttClient = NULL;
  157. bulbStateUpdater = NULL;
  158. }
  159. if (stateStore) {
  160. delete stateStore;
  161. }
  162. radioFactory = MiLightRadioFactory::fromSettings(settings);
  163. if (radioFactory == NULL) {
  164. Serial.println(F("ERROR: unable to construct radio factory"));
  165. }
  166. stateStore = new GroupStateStore(MILIGHT_MAX_STATE_ITEMS, settings.stateFlushInterval);
  167. milightClient = new MiLightClient(
  168. radioFactory,
  169. *stateStore,
  170. settings.packetRepeatThrottleThreshold,
  171. settings.packetRepeatThrottleSensitivity,
  172. settings.packetRepeatMinimum
  173. );
  174. milightClient->begin();
  175. milightClient->onPacketSent(onPacketSentHandler);
  176. milightClient->onUpdateBegin(onUpdateBegin);
  177. milightClient->onUpdateEnd(onUpdateEnd);
  178. milightClient->setResendCount(settings.packetRepeats);
  179. if (settings.mqttServer().length() > 0) {
  180. mqttClient = new MqttClient(settings, milightClient);
  181. mqttClient->begin();
  182. bulbStateUpdater = new BulbStateUpdater(settings, *mqttClient, *stateStore);
  183. }
  184. initMilightUdpServers();
  185. if (discoveryServer) {
  186. delete discoveryServer;
  187. discoveryServer = NULL;
  188. }
  189. if (settings.discoveryPort != 0) {
  190. discoveryServer = new MiLightDiscoveryServer(settings);
  191. discoveryServer->begin();
  192. }
  193. }
  194. /**
  195. *
  196. */
  197. bool shouldRestart() {
  198. if (! settings.isAutoRestartEnabled()) {
  199. return false;
  200. }
  201. return settings.getAutoRestartPeriod()*60*1000 < millis();
  202. }
  203. void setup() {
  204. Serial.begin(9600);
  205. String ssid = "ESP" + String(ESP.getChipId());
  206. wifiManager.setConfigPortalTimeout(180);
  207. wifiManager.autoConnect(ssid.c_str(), "milightHub");
  208. SPIFFS.begin();
  209. Settings::load(settings);
  210. applySettings();
  211. if (! MDNS.begin("milight-hub")) {
  212. Serial.println(F("Error setting up MDNS responder"));
  213. }
  214. MDNS.addService("http", "tcp", 80);
  215. SSDP.setSchemaURL("description.xml");
  216. SSDP.setHTTPPort(80);
  217. SSDP.setName("ESP8266 MiLight Gateway");
  218. SSDP.setSerialNumber(ESP.getChipId());
  219. SSDP.setURL("/");
  220. SSDP.setDeviceType("upnp:rootdevice");
  221. SSDP.begin();
  222. httpServer = new MiLightHttpServer(settings, milightClient, stateStore);
  223. httpServer->onSettingsSaved(applySettings);
  224. httpServer->on("/description.xml", HTTP_GET, []() { SSDP.schema(httpServer->client()); });
  225. httpServer->begin();
  226. Serial.println(F("Setup complete"));
  227. }
  228. void loop() {
  229. httpServer->handleClient();
  230. if (mqttClient) {
  231. mqttClient->handleClient();
  232. bulbStateUpdater->loop();
  233. }
  234. if (udpServers) {
  235. for (size_t i = 0; i < settings.numGatewayConfigs; i++) {
  236. udpServers[i]->handleClient();
  237. }
  238. }
  239. if (discoveryServer) {
  240. discoveryServer->handleClient();
  241. }
  242. handleListen();
  243. stateStore->limitedFlush();
  244. if (shouldRestart()) {
  245. Serial.println(F("Auto-restart triggered. Restarting..."));
  246. ESP.restart();
  247. }
  248. }