123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- /*
- HDSv3 functions require the following library
- to be included in the main sketch
- #include <WiFiManager.h>
- #include <ArduinoJson.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266WebServer.h>
- */
- /*
- mDNS Probing and Broadcast
- */
- //Inject zeroconf attr into the MDNS respond (For scanning by ArozOS)
- void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) {
- //Define the domain of the HDSv2 devices
- MDNS.addDynamicServiceTxt(p_hService, "domain", "hds.imuslab.com");
- MDNS.addDynamicServiceTxt(p_hService, "protocol", "hdsv3");
- //Define the OEM written values
- MDNS.addDynamicServiceTxt(p_hService, "uuid", deviceUUID.c_str());
- MDNS.addDynamicServiceTxt(p_hService, "name", (String(DEVICE_NAME) + "_" + deviceUUID).c_str());
- MDNS.addDynamicServiceTxt(p_hService, "model", DEVICE_NAME);
- MDNS.addDynamicServiceTxt(p_hService, "vendor", "imuslab");
- MDNS.addDynamicServiceTxt(p_hService, "version", "1.00");
- }
- void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {
- MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback);
- }
- /*
- * mDNS Resolver (new in HDSv3)
- */
- bool resolveMdnsService(char* service_name, char* protocol, char* desired_host, IPAddress* ip_addr, uint16_t *port_number) {
- Serial.println("Sending mDNS query");
- int n = MDNS.queryService(service_name, protocol);
- Serial.printf("mDNS query got %d results\n", n);
- if (n == 0) {
- Serial.println("no services found");
- } else {
- for (int i = 0; i < n; ++i) {
- #ifdef DEBUG
- Serial.print(i + 1);
- Serial.print(": ");
- Serial.print(MDNS.hostname(i));
- Serial.print(" (");
- Serial.print(MDNS.IP(i));
- Serial.print(":");
- Serial.print(MDNS.port(i));
- Serial.println(")");
- #endif
- if (strcmp(MDNS.hostname(i).c_str(), desired_host) == 0) {
- *ip_addr = MDNS.IP(i);
- *port_number = MDNS.port(i);
- return true;
- }
- }
- }
- return false;
- }
- //Start all HDSv2 required services
- void initHds3Services() {
- // Connect to Wi-Fi
- Serial.println("Starting WiFi Manager");
- wifiManager.setClass("invert");
- wifiManager.autoConnect(SETUP_WIFI_NAME);
- Serial.print("Connected to ");
- Serial.println(WiFi.SSID());
- Serial.print("IP address:\t");
- Serial.println(WiFi.localIP());
- //Startup MDNS Responder
- MDNS.setHostProbeResultCallback(hostProbeResult);
- if (!MDNS.begin((String(DEVICE_NAME) + "_" + deviceUUID).c_str())) {
- Serial.println("Error setting up MDNS responder!");
- }
- //Advertise the port that you are using
- MDNS.addService("http", "tcp", LISTENING_PORT);
- Serial.println("mDNS responder started");
- //Restful API (required)
- server.on("/", handle_index);
- server.on("/status", handle_status);
- server.on("/info", handle_deviceInfo);
- server.on("/uuid", handle_uuid);
- delay(100);
- server.begin();
- Serial.println("Restful API server started");
- Serial.print("Listening on port: ");
- Serial.println(LISTENING_PORT);
- }
|