Base.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Home Dynamic System v2
  3. * Designed by tobychui
  4. *
  5. * This is the base module for implementing the Home Dynamic v2 Protocol with ArozOS 1.0
  6. * For more examples, see other project folders.
  7. *
  8. */
  9. #include <ESP8266WiFi.h> // Include the Wi-Fi library
  10. #include <ESP8266WiFiMulti.h> // Include the Wi-Fi-Multi library
  11. #include <ESP8266mDNS.h> // Include the mDNS library
  12. #include <ESP8266WebServer.h> // Include the WebServer library
  13. //Change the properties of the IoT device
  14. const String DeviceName = "HDSv2-Base"; //The name of this IoT device
  15. const int ListeningPort = 12110; //The port where this IoT device listen
  16. //Library Objects
  17. ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
  18. ESP8266WebServer server(ListeningPort); //Create an Web Server on the listening port
  19. //Change the WiFi Settings
  20. void WiFiConfig(){
  21. wifiMulti.addAP("Toby Room Automation", "homedynamicsystem");
  22. //Add more WiFi AP here if nessary
  23. //wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
  24. //wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
  25. }
  26. //Inject zeroconf attr into the MDNS respond (For scanning by ArozOS)
  27. void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) {
  28. //Define the domain of the HDSv2 devices
  29. MDNS.addDynamicServiceTxt(p_hService, "domain","hds.arozos.com");
  30. MDNS.addDynamicServiceTxt(p_hService, "protocol","hdsv2");
  31. //Define the OEM written values
  32. MDNS.addDynamicServiceTxt(p_hService, "uuid",getMacAddress());
  33. MDNS.addDynamicServiceTxt(p_hService, "model","Generic");
  34. MDNS.addDynamicServiceTxt(p_hService, "vendor","HomeDynamic Project");
  35. MDNS.addDynamicServiceTxt(p_hService, "version_minor","0.00");
  36. MDNS.addDynamicServiceTxt(p_hService, "version_build","0");
  37. }
  38. void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {
  39. MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback);
  40. }
  41. void setup() {
  42. //Use 115200 baudrate on serial monitor if you want to see what is happening to the device
  43. Serial.begin(115200);
  44. delay(10);
  45. Serial.println('\n');
  46. //Start WiFi Conenction Routines
  47. WiFiConfig();
  48. Serial.println("Connecting ...");
  49. while (wifiMulti.run() != WL_CONNECTED) {
  50. delay(500);
  51. Serial.print('.');
  52. }
  53. Serial.println('\n');
  54. Serial.print("Connected to ");
  55. Serial.println(WiFi.SSID());
  56. Serial.print("IP address:\t");
  57. Serial.println(WiFi.localIP());
  58. //Startup MDNS Responder
  59. MDNS.setHostProbeResultCallback(hostProbeResult);
  60. if (!MDNS.begin(DeviceName)) { // Start the mDNS responder for esp8266.local
  61. Serial.println("Error setting up MDNS responder!");
  62. }
  63. //Advertise the port that you are using
  64. MDNS.addService("http", "tcp", ListeningPort);
  65. Serial.println("mDNS responder started");
  66. //Startup the Web Server Endpoints
  67. delay(100);
  68. server.on("/", handle_index);
  69. server.on("/status", handle_status);
  70. server.on("/eps", handle_endpoints);
  71. server.on("/hello", handle_hello);
  72. server.begin();
  73. Serial.println("HTTP server started");
  74. Serial.print("Listening on port: ");
  75. Serial.println(ListeningPort);
  76. }
  77. //Handlers for Web Server
  78. void handle_index() {
  79. server.send(200, "text/html", "index");
  80. }
  81. void handle_hello() {
  82. Serial.println("Hello World");
  83. server.send(200, "text/html", "OK");
  84. }
  85. void handle_status() {
  86. server.send(200, "application/json", "{\
  87. \"power\":true,\
  88. \"network\":true\
  89. }");
  90. }
  91. void handle_endpoints() {
  92. server.send(200, "application/json", "[{\
  93. \"Name\": \"Hello World\",\
  94. \"RelPath\":\"hello\",\
  95. \"Desc\":\"Print out Hello World in Serial\",\
  96. \"Type\":\"none\"\
  97. }]");
  98. }
  99. //Main Loop
  100. void loop() {
  101. server.handleClient();
  102. MDNS.update();
  103. }