| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 | /* * Home Dynamic System v2 * Designed by tobychui *  * This is an example of making use of all avaible endpoints definations *  */#include <ESP8266WiFi.h>        // Include the Wi-Fi library#include <ESP8266WiFiMulti.h>   // Include the Wi-Fi-Multi library#include <ESP8266mDNS.h>        // Include the mDNS library#include <ESP8266WebServer.h>   // Include the WebServer library//Change the properties of the IoT deviceconst String DeviceName = "HDsv2-Test"; //The name of this IoT deviceconst int ListeningPort = 12110;        //The port where this IoT device listen//Runtime valuesString stringInput = "default";int integerInput = 0;float floatInput = 2.5;bool booleanInput = true;//Library ObjectsESP8266WiFiMulti wifiMulti;     // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'ESP8266WebServer server(ListeningPort);    //Create an Web Server on the listening port//Change the WiFi Settingsvoid WiFiConfig(){  wifiMulti.addAP("Toby Room Automation", "homedynamicsystem");   //Add more WiFi AP here if nessary  //wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");  //wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");}//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.arozos.com");    MDNS.addDynamicServiceTxt(p_hService, "protocol","hdsv2");    //Define the OEM written values    MDNS.addDynamicServiceTxt(p_hService, "uuid",getMacAddress());    MDNS.addDynamicServiceTxt(p_hService, "model","Test Unit");    MDNS.addDynamicServiceTxt(p_hService, "vendor","HomeDynamic Project");    MDNS.addDynamicServiceTxt(p_hService, "version_minor","0.00");    MDNS.addDynamicServiceTxt(p_hService, "version_build","1");}  void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {  MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback);}void setup() {  //Use 115200 baudrate on serial monitor if you want to see what is happening to the device  Serial.begin(115200);  delay(10);  Serial.println('\n');  //Start WiFi Conenction Routines  WiFiConfig();    Serial.println("Connecting ...");  while (wifiMulti.run() != WL_CONNECTED) {    delay(500);    Serial.print('.');  }  Serial.println('\n');  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(DeviceName)) {             // Start the mDNS responder for esp8266.local    Serial.println("Error setting up MDNS responder!");  }  //Advertise the port that you are using  MDNS.addService("http", "tcp", ListeningPort);  Serial.println("mDNS responder started");  //Startup the Web Server Endpoints  delay(100);  server.on("/", handle_index);  server.on("/status", handle_status);  server.on("/eps", handle_endpoints);  server.on("/string", handle_string);  server.on("/integer", handle_integer);  server.on("/float", handle_float);  server.on("/bool", handle_bool);  server.on("/output", handle_output);    server.begin();  Serial.println("HTTP server started");  Serial.print("Listening on port: ");  Serial.println(ListeningPort);}//Handlers for Web Servervoid handle_index() {  server.send(200, "text/html", "index"); }void handle_string() {  String value = server.arg("value");  Serial.println(value);  stringInput = value;  //Use value (String) to do something  server.send(200, "text/html", "OK"); }void handle_integer() {  String value = server.arg("value");  int number = value.toInt();  Serial.println(number);  integerInput = number;  //Use number (int) to do something  server.send(200, "text/html", "OK"); }void handle_float() {  String value = server.arg("value");  float floatNumber = value.toFloat();  Serial.println(floatNumber);  floatInput = floatNumber;  //Use floatNumber to do something else  server.send(200, "text/html", "OK"); }void handle_bool() {  String value = server.arg("value");  bool result = false;  if (value == "true"){    result = true;    Serial.println("True");  }else{    Serial.println("False");  }  booleanInput = result;  //Use result to do something else  server.send(200, "text/html", "OK"); }void handle_output() {  Serial.println("Hello World");  server.send(200, "text/html", "OK"); }//This function return the status of the current device. //If the name of status matches one of the input endpoints, the value will be used as auto fillvoid handle_status() {  String boolValue = "true";  if (booleanInput == false){    boolValue = "false";  }  server.send(200, "application/json", "{\  \"Global Status\":\"It is working\",\  \"String Input\":\"" + stringInput + "\",\  \"Integer Input\":" + String(integerInput) + ",\  \"Float Input\":" + String(floatInput, 2) + ",\  \"Boolean Input\":" + boolValue + "\}"); }//This function define all the endpoint accessiable in this devicevoid handle_endpoints() {  server.send(200, "application/json", "[\  {\    \"Name\": \"String Input\",\    \"RelPath\":\"string\",\    \"Desc\":\"Print out your input string to serial\",\    \"Type\":\"string\",\    \"Regex\":\"/[A-Za-z0-9]/g\"\  },\  {\    \"Name\": \"Integer Input\",\    \"RelPath\":\"integer\",\    \"Desc\":\"Print out your input integer to serial\",\    \"Type\":\"integer\",\  \"Min\":0,\  \"Max\":10\  },\  {\    \"Name\": \"Float Input\",\    \"RelPath\":\"float\",\    \"Desc\":\"Print out your input float to serial\",\    \"Type\":\"float\",\    \"Min\":0,\    \"Max\":10,\    \"Steps\":0.1\  },\  {\    \"Name\": \"Boolean Input\",\    \"RelPath\":\"bool\",\    \"Desc\":\"Print out your input string to \",\    \"Type\":\"bool\"\  },\  {\    \"Name\": \"Print Hello World\",\    \"RelPath\":\"output\",\    \"Desc\":\"Reply your request with Hello World\",\    \"Type\":\"none\"\  }\]"); }//Main Loopvoid loop() {    server.handleClient();   MDNS.update(); }
 |