Sinilink_WiFi_Relay.ino 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Home Dynamic System v2
  3. * Designed by tobychui
  4. *
  5. * This is a basic IoT Switch that support single channel ON / OFF function only
  6. * Use this firmware with Wemos D1 R1 board profile
  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 = "Sinilink WiFi Relay"; //The name of this IoT device
  14. const int ListeningPort = 12110; //The port where this IoT device listen
  15. int signalOutputPin = D14; //The pin to activate the mosfet of relay
  16. int buttonPin = D6;
  17. int refPin = D8;
  18. bool poweredOn = true; //The current power state of the switch
  19. int val = 0;
  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","Switch");
  38. MDNS.addDynamicServiceTxt(p_hService, "vendor","HomeDynamic Project");
  39. MDNS.addDynamicServiceTxt(p_hService, "version_minor","0.00");
  40. MDNS.addDynamicServiceTxt(p_hService, "version_build","0");
  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. //Set output pins as OUTPUT and default it to HIGH
  51. pinMode(signalOutputPin, OUTPUT);
  52. digitalWrite(signalOutputPin, HIGH);
  53. //Button input pin
  54. pinMode(buttonPin, INPUT_PULLUP);
  55. //Unknown pin that needed to be HIGH
  56. pinMode(refPin, OUTPUT);
  57. digitalWrite(refPin, HIGH);
  58. pinMode(D9, OUTPUT);
  59. digitalWrite(D9, HIGH);
  60. //Start WiFi Conenction Routines
  61. WiFiConfig();
  62. Serial.println("Connecting ...");
  63. while (wifiMulti.run() != WL_CONNECTED) {
  64. delay(500);
  65. Serial.print('.');
  66. }
  67. Serial.println('\n');
  68. Serial.print("Connected to ");
  69. Serial.println(WiFi.SSID());
  70. Serial.print("IP address:\t");
  71. Serial.println(WiFi.localIP());
  72. //Startup MDNS Responder
  73. MDNS.setHostProbeResultCallback(hostProbeResult);
  74. if (!MDNS.begin(DeviceName)) { // Start the mDNS responder for esp8266.local
  75. Serial.println("Error setting up MDNS responder!");
  76. }
  77. //Advertise the port that you are using
  78. MDNS.addService("http", "tcp", ListeningPort);
  79. Serial.println("mDNS responder started");
  80. //Startup the Web Server Endpoints
  81. delay(100);
  82. server.on("/", handle_index);
  83. server.on("/status", handle_status);
  84. server.on("/eps", handle_endpoints);
  85. server.on("/on", handle_on);
  86. server.on("/off", handle_off);
  87. server.begin();
  88. Serial.println("HTTP server started");
  89. Serial.print("Listening on port: ");
  90. Serial.println(ListeningPort);
  91. }
  92. //Handlers for Web Server
  93. void handle_index() {
  94. server.send(200, "text/html", "");
  95. }
  96. //Handle turning on the switch
  97. void handle_on() {
  98. Serial.println("Turned ON");
  99. digitalWrite(signalOutputPin, HIGH);
  100. poweredOn = true;
  101. server.send(200, "text/html", "OK");
  102. }
  103. //Handle turning off the switch
  104. void handle_off() {
  105. Serial.println("Turned OFF");
  106. digitalWrite(signalOutputPin, LOW);
  107. poweredOn = false;
  108. server.send(200, "text/html", "OK");
  109. }
  110. void handle_status() {
  111. String powerState = "ON";
  112. if (poweredOn == false){
  113. powerState = "OFF";
  114. }
  115. server.send(200, "application/json", "{\"Power\":\"" + powerState + "\"}");
  116. }
  117. void handle_endpoints() {
  118. server.send(200, "application/json", "[{\
  119. \"Name\": \"ON\",\
  120. \"RelPath\":\"on\",\
  121. \"Desc\":\"Switch on the device attached to the switch\",\
  122. \"Type\":\"none\",\
  123. \"AllowRead\":false,\
  124. \"AllowWrite\":true\
  125. },{\
  126. \"Name\": \"OFF\",\
  127. \"RelPath\":\"off\",\
  128. \"Desc\":\"Switch off the device attached to the switch\",\
  129. \"Type\":\"none\",\
  130. \"AllowRead\":false,\
  131. \"AllowWrite\":true\
  132. }\
  133. ]");
  134. }
  135. //Main Loop
  136. void loop() {
  137. //Do stuffs for handling web interface
  138. server.handleClient();
  139. MDNS.update();
  140. //Check if the button is pressed. Toggle the power if yes
  141. val = digitalRead(buttonPin);
  142. if (val == 0){
  143. //Button is down. Toggle power state
  144. if (poweredOn == true){
  145. //Turn if off
  146. digitalWrite(signalOutputPin, LOW);
  147. poweredOn = false;
  148. Serial.println("Turned off using button");
  149. }else{
  150. //Turn it on
  151. digitalWrite(signalOutputPin, HIGH);
  152. poweredOn = true;
  153. Serial.println("Turned on using button");
  154. }
  155. //Sleep for 1 second
  156. delay(1000);
  157. }
  158. }