hdsv3.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. HDSv3 functions require the following library
  3. to be included in the main sketch
  4. #include <WiFiManager.h>
  5. #include <ArduinoJson.h>
  6. #include <ESP8266WiFi.h>
  7. #include <ESP8266mDNS.h>
  8. #include <ESP8266WebServer.h>
  9. */
  10. /*
  11. mDNS Probing and Broadcast
  12. */
  13. //Inject zeroconf attr into the MDNS respond (For scanning by ArozOS)
  14. void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) {
  15. //Define the domain of the HDSv2 devices
  16. MDNS.addDynamicServiceTxt(p_hService, "domain", "hds.imuslab.com");
  17. MDNS.addDynamicServiceTxt(p_hService, "protocol", "hdsv3");
  18. //Define the OEM written values
  19. MDNS.addDynamicServiceTxt(p_hService, "uuid", deviceUUID.c_str());
  20. MDNS.addDynamicServiceTxt(p_hService, "name", (String(DEVICE_NAME) + "_" + deviceUUID).c_str());
  21. MDNS.addDynamicServiceTxt(p_hService, "model", DEVICE_NAME);
  22. MDNS.addDynamicServiceTxt(p_hService, "vendor", "imuslab");
  23. MDNS.addDynamicServiceTxt(p_hService, "version", "1.00");
  24. }
  25. void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {
  26. MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback);
  27. }
  28. /*
  29. * mDNS Resolver (new in HDSv3)
  30. */
  31. bool resolveMdnsService(char* service_name, char* protocol, char* desired_host, IPAddress* ip_addr, uint16_t *port_number) {
  32. Serial.println("Sending mDNS query");
  33. int n = MDNS.queryService(service_name, protocol);
  34. Serial.printf("mDNS query got %d results\n", n);
  35. if (n == 0) {
  36. Serial.println("no services found");
  37. } else {
  38. for (int i = 0; i < n; ++i) {
  39. #ifdef DEBUG
  40. Serial.print(i + 1);
  41. Serial.print(": ");
  42. Serial.print(MDNS.hostname(i));
  43. Serial.print(" (");
  44. Serial.print(MDNS.IP(i));
  45. Serial.print(":");
  46. Serial.print(MDNS.port(i));
  47. Serial.println(")");
  48. #endif
  49. if (strcmp(MDNS.hostname(i).c_str(), desired_host) == 0) {
  50. *ip_addr = MDNS.IP(i);
  51. *port_number = MDNS.port(i);
  52. return true;
  53. }
  54. }
  55. }
  56. return false;
  57. }
  58. //Start all HDSv2 required services
  59. void initHds3Services() {
  60. // Connect to Wi-Fi
  61. Serial.println("Starting WiFi Manager");
  62. wifiManager.setClass("invert");
  63. wifiManager.autoConnect(SETUP_WIFI_NAME);
  64. Serial.print("Connected to ");
  65. Serial.println(WiFi.SSID());
  66. Serial.print("IP address:\t");
  67. Serial.println(WiFi.localIP());
  68. //Startup MDNS Responder
  69. MDNS.setHostProbeResultCallback(hostProbeResult);
  70. if (!MDNS.begin((String(DEVICE_NAME) + "_" + deviceUUID).c_str())) {
  71. Serial.println("Error setting up MDNS responder!");
  72. }
  73. //Advertise the port that you are using
  74. MDNS.addService("http", "tcp", LISTENING_PORT);
  75. Serial.println("mDNS responder started");
  76. //Restful API (required)
  77. server.on("/", handle_index);
  78. server.on("/status", handle_status);
  79. server.on("/info", handle_deviceInfo);
  80. server.on("/uuid", handle_uuid);
  81. delay(100);
  82. server.begin();
  83. Serial.println("Restful API server started");
  84. Serial.print("Listening on port: ");
  85. Serial.println(LISTENING_PORT);
  86. }