Examples.ino 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Home Dynamic System v2
  3. * Designed by tobychui
  4. *
  5. * This is an example of making use of all avaible endpoints definations
  6. *
  7. */
  8. #include <ESP8266WiFi.h> // Include the Wi-Fi library
  9. #include <ESP8266WiFiMulti.h> // Include the Wi-Fi-Multi library
  10. #include <ESP8266mDNS.h> // Include the mDNS library
  11. #include <ESP8266WebServer.h> // Include the WebServer library
  12. //Change the properties of the IoT device
  13. const String DeviceName = "HDsv2-Test"; //The name of this IoT device
  14. const int ListeningPort = 12110; //The port where this IoT device listen
  15. //Library Objects
  16. ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
  17. ESP8266WebServer server(ListeningPort); //Create an Web Server on the listening port
  18. //Change the WiFi Settings
  19. void WiFiConfig(){
  20. wifiMulti.addAP("Toby Room Automation", "homedynamicsystem");
  21. //Add more WiFi AP here if nessary
  22. //wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
  23. //wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
  24. }
  25. //Inject zeroconf attr into the MDNS respond (For scanning by ArozOS)
  26. void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) {
  27. //Define the domain of the HDSv2 devices
  28. MDNS.addDynamicServiceTxt(p_hService, "domain","hds.arozos.com");
  29. MDNS.addDynamicServiceTxt(p_hService, "protocol","hdsv2");
  30. //Define the OEM written values
  31. MDNS.addDynamicServiceTxt(p_hService, "uuid",getMacAddress());
  32. MDNS.addDynamicServiceTxt(p_hService, "model","Test Unit");
  33. MDNS.addDynamicServiceTxt(p_hService, "vendor","HomeDynamic Project");
  34. MDNS.addDynamicServiceTxt(p_hService, "version_minor","0.00");
  35. MDNS.addDynamicServiceTxt(p_hService, "version_build","1");
  36. }
  37. void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {
  38. MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback);
  39. }
  40. void setup() {
  41. //Use 115200 baudrate on serial monitor if you want to see what is happening to the device
  42. Serial.begin(115200);
  43. delay(10);
  44. Serial.println('\n');
  45. //Start WiFi Conenction Routines
  46. WiFiConfig();
  47. Serial.println("Connecting ...");
  48. while (wifiMulti.run() != WL_CONNECTED) {
  49. delay(500);
  50. Serial.print('.');
  51. }
  52. Serial.println('\n');
  53. Serial.print("Connected to ");
  54. Serial.println(WiFi.SSID());
  55. Serial.print("IP address:\t");
  56. Serial.println(WiFi.localIP());
  57. //Startup MDNS Responder
  58. MDNS.setHostProbeResultCallback(hostProbeResult);
  59. if (!MDNS.begin(DeviceName)) { // Start the mDNS responder for esp8266.local
  60. Serial.println("Error setting up MDNS responder!");
  61. }
  62. //Advertise the port that you are using
  63. MDNS.addService("http", "tcp", ListeningPort);
  64. Serial.println("mDNS responder started");
  65. //Startup the Web Server Endpoints
  66. delay(100);
  67. server.on("/", handle_index);
  68. server.on("/status", handle_status);
  69. server.on("/eps", handle_endpoints);
  70. server.on("/string", handle_string);
  71. server.on("/integer", handle_integer);
  72. server.on("/float", handle_float);
  73. server.on("/bool", handle_bool);
  74. server.on("/output", handle_output);
  75. server.begin();
  76. Serial.println("HTTP server started");
  77. Serial.print("Listening on port: ");
  78. Serial.println(ListeningPort);
  79. }
  80. //Handlers for Web Server
  81. void handle_index() {
  82. server.send(200, "text/html", "index");
  83. }
  84. void handle_string() {
  85. String value = server.arg("value");
  86. Serial.println(value);
  87. //Use value (String) to do something
  88. server.send(200, "text/html", "OK");
  89. }
  90. void handle_integer() {
  91. String value = server.arg("value");
  92. int number = value.toInt();
  93. Serial.println(number);
  94. //Use number (int) to do something
  95. server.send(200, "text/html", "OK");
  96. }
  97. void handle_float() {
  98. String value = server.arg("value");
  99. int floatNumber = value.toFloat();
  100. Serial.println(floatNumber);
  101. //Use floatNumber to do something else
  102. server.send(200, "text/html", "OK");
  103. }
  104. void handle_bool() {
  105. String value = server.arg("value");
  106. bool result = false;
  107. if (value == "true"){
  108. result = true;
  109. Serial.println("True");
  110. }else{
  111. Serial.println("False");
  112. }
  113. //Use result to do something else
  114. server.send(200, "text/html", "OK");
  115. }
  116. void handle_output() {
  117. Serial.println("Hello World");
  118. server.send(200, "text/html", "OK");
  119. }
  120. //This function return the status of the current device.
  121. //If the name of status matches one of the input endpoints, the value will be used as auto fill
  122. void handle_status() {
  123. server.send(200, "application/json", "{\
  124. \"Global Status\":\"It is working\",\
  125. \"String Input\":\"default value\",\
  126. \"Integer Input\":8,\
  127. \"Float Input\":4.5,\
  128. \"Boolean Input\":true\
  129. }");
  130. }
  131. //This function define all the endpoint accessiable in this device
  132. void handle_endpoints() {
  133. server.send(200, "application/json", "[\
  134. {\
  135. \"Name\": \"String Input\",\
  136. \"RelPath\":\"string\",\
  137. \"Desc\":\"Print out your input string to serial\",\
  138. \"Type\":\"string\",\
  139. \"Regex\":\"/[A-Za-z0-9]/g\"\
  140. },\
  141. {\
  142. \"Name\": \"Integer Input\",\
  143. \"RelPath\":\"integer\",\
  144. \"Desc\":\"Print out your input integer to serial\",\
  145. \"Type\":\"integer\",\
  146. \"Min\":0,\
  147. \"Max\":10\
  148. },\
  149. {\
  150. \"Name\": \"Float Input\",\
  151. \"RelPath\":\"float\",\
  152. \"Desc\":\"Print out your input float to serial\",\
  153. \"Type\":\"float\",\
  154. \"Min\":0,\
  155. \"Max\":10,\
  156. \"Steps\":0.1\
  157. },\
  158. {\
  159. \"Name\": \"Boolean Input\",\
  160. \"RelPath\":\"bool\",\
  161. \"Desc\":\"Print out your input string to \",\
  162. \"Type\":\"bool\"\
  163. },\
  164. {\
  165. \"Name\": \"Print Hello World\",\
  166. \"RelPath\":\"output\",\
  167. \"Desc\":\"Reply your request with Hello World\",\
  168. \"Type\":\"none\"\
  169. }\
  170. ]");
  171. }
  172. //Main Loop
  173. void loop() {
  174. server.handleClient();
  175. MDNS.update();
  176. }