Home-Dynamic-IoT-Module-Protocol-v2U.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include <string>
  4. #define OUTPIN 2
  5. ESP8266WebServer server(80);
  6. //Global variables for module
  7. String CurrentStatus = "ON";
  8. String deviceUuid = "9f4e4ced-4954-4f7c-aacb-cc22df02fda1";
  9. String deviceName = "General Switching Module";
  10. String identifier = "general.std.hd.com.imuslab";
  11. const char* ssid = "Toby Room Automation";
  12. const char* password = "homedynamicsystem";
  13. void setup() {
  14. Serial.begin(9600);
  15. Serial.println("");
  16. WiFi.begin(ssid, password);
  17. Serial.println(deviceName);
  18. Serial.println(identifier);
  19. Serial.println(deviceUuid);
  20. Serial.print("Trying to connect");
  21. while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
  22. delay(500);
  23. Serial.print(".");
  24. }
  25. //Display Wifi Connection Information
  26. Serial.println("WiFi connected");
  27. Serial.println("IP address: ");
  28. Serial.println(WiFi.localIP());
  29. //Declare System Function for NodeMCU
  30. server.on("/", showLicense);
  31. server.on("/uuid", uuid);
  32. server.on("/info", info);
  33. server.on("/on", ON);
  34. server.on("/off", OFF);
  35. server.on("/toggle", toggle);
  36. server.on("/status", Status);
  37. //Declare Output Pins
  38. pinMode(OUTPIN, OUTPUT);
  39. //Start Server
  40. server.begin();
  41. }
  42. void showLicense(){
  43. int size=1000;
  44. char temp[size];
  45. snprintf( temp, size, "<table> <tbody> <tr> <td>Device Type: </td> <td>IMUS General Purpose Module</td> </tr> <tr> <td>Init Type:</td> <td>Static LAN</td> </tr> <tr> <td>Manufacture</td> <td>IMUS Laboratory, Prototype</td> </tr> <tr> <td>License </td> <td>CopyRight IMUS Laboratory, 2018</td> </tr> </tbody> </table> <p>&nbsp;</p>");
  46. server.sendHeader("Access-Control-Allow-Origin","*");
  47. server.send(200, "text/html", temp);
  48. }
  49. void info(){
  50. int size=1000;
  51. char temp[size];
  52. snprintf( temp, size, (deviceName + "_" + identifier).c_str ());
  53. server.sendHeader("Access-Control-Allow-Origin","*");
  54. server.send(200, "text/html", temp);
  55. }
  56. void uuid(){
  57. int size=100;
  58. char temp[size];
  59. snprintf( temp, size, deviceUuid.c_str ());
  60. server.sendHeader("Access-Control-Allow-Origin","*");
  61. server.send(200, "text/html", temp);
  62. }
  63. void ON(){
  64. digitalWrite(OUTPIN,LOW);
  65. Serial.println("Turned ON");
  66. CurrentStatus = "ON";
  67. sendDone();
  68. }
  69. void OFF(){
  70. digitalWrite(OUTPIN,HIGH);
  71. Serial.println("Turned OFF");
  72. CurrentStatus = "OFF";
  73. sendDone();
  74. }
  75. void toggle(){
  76. if (strcmp(CurrentStatus.c_str(),"ON") == 0){
  77. digitalWrite(OUTPIN,HIGH);
  78. Serial.println("Toggled OFF");
  79. CurrentStatus = "OFF";
  80. }else{
  81. digitalWrite(OUTPIN,LOW);
  82. Serial.println("Toggled ON");
  83. CurrentStatus = "ON";
  84. }
  85. sendDone();
  86. }
  87. void Status(){
  88. int size=100;
  89. char temp[size];
  90. snprintf( temp, size, "");
  91. server.sendHeader("Access-Control-Allow-Origin","*");
  92. server.send(200, "text/html", CurrentStatus);
  93. }
  94. void sendDone(){
  95. int size=100;
  96. char temp[size];
  97. snprintf( temp, size, "DONE");
  98. server.sendHeader("Access-Control-Allow-Origin","*");
  99. server.send(200, "text/html", temp);
  100. }
  101. void loop() {
  102. server.handleClient();
  103. }