Examples.ino 6.1 KB

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