Advertisement
martinius96

Untitled

Jun 21st, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include <WiFiUdp.h>
  4. #include <functional>
  5. #include "switch.h"
  6. #include "UpnpBroadcastResponder.h"
  7. #include "CallbackFunction.h"
  8. #include <ESP8266HTTPClient.h>
  9. #include <Arduino.h>
  10. const char *ssid = "Macgyver";
  11. const char *password = "0285461937";
  12. boolean connectWifi();
  13. void Relay1On();
  14. void Relay1Off();
  15. void Relay2On();
  16. void Relay2Off();
  17. void Relay3On();
  18. void Relay3Off();
  19. void Relay4On();
  20. void Relay4Off();
  21. boolean wifiConnected = false;
  22. int httpCode = 0;
  23. UpnpBroadcastResponder upnpBroadcastResponder;
  24. Switch *relay1 = NULL;
  25. Switch *relay2 = NULL;
  26. Switch *relay3 = NULL;
  27. Switch *relay4 = NULL;
  28. WiFiClient client;
  29. HTTPClient http;
  30. void setup()
  31. {
  32. Serial.begin(9600);
  33.  
  34. // Initialise wifi connection
  35. wifiConnected = connectWifi();
  36.  
  37. if(wifiConnected){
  38. upnpBroadcastResponder.beginUdpMulticast();
  39.  
  40. // Define your switches here. Max 14
  41. // Format: Alexa invocation name, local port no, on callback, off callback
  42. relay1 = new Switch("Relay", 80, Relay1On, Relay1Off);
  43. relay2 = new Switch("Light", 81, Relay2On, Relay2Off);
  44. relay3 = new Switch("Relay 2", 82, Relay3On, Relay3Off);
  45. relay4 = new Switch("Relay 3", 84, Relay4On, Relay4Off);
  46. Serial.println("Adding switches upnp broadcast responder");
  47. upnpBroadcastResponder.addDevice(*relay1);
  48. }
  49. }
  50.  
  51. void loop()
  52. {
  53. if(wifiConnected){
  54. upnpBroadcastResponder.serverLoop();
  55.  
  56. relay1->serverLoop();
  57. relay2->serverLoop();
  58. relay3->serverLoop();
  59. relay4->serverLoop();
  60. }
  61. }
  62.  
  63. void Relay1On() {
  64. Serial.println("Zapinam switch 1");
  65. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
  66. http.begin(url);
  67. http.setAuthorization("admin", "0285");
  68. httpCode = http.GET();
  69. if (httpCode == HTTP_CODE_OK) {
  70.  
  71. }
  72. http.end();
  73. }
  74.  
  75. void Relay1Off() {
  76. Serial.println("Vypinam switch 1");
  77. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
  78. http.begin(url);
  79. http.setAuthorization("admin", "0285");
  80. httpCode = http.GET();
  81. if (httpCode == HTTP_CODE_OK) {
  82.  
  83. }
  84. http.end();
  85. }
  86. ///////////////////RELE2
  87. void Relay2On() {
  88. Serial.println("Zapinam switch 1");
  89. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
  90. http.begin(url);
  91. http.setAuthorization("admin", "0285");
  92. httpCode = http.GET();
  93. if (httpCode == HTTP_CODE_OK) {
  94.  
  95. }
  96. http.end();
  97. }
  98.  
  99. void Relay2Off() {
  100. Serial.println("Vypinam switch 1");
  101. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
  102. http.begin(url);
  103. http.setAuthorization("admin", "0285");
  104. httpCode = http.GET();
  105. if (httpCode == HTTP_CODE_OK) {
  106.  
  107. }
  108. http.end();
  109. }
  110.  
  111. /////////////RELE3
  112. void Relay3On() {
  113. Serial.println("Zapinam switch 1");
  114. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
  115. http.begin(url);
  116. http.setAuthorization("admin", "0285");
  117. httpCode = http.GET();
  118. if (httpCode == HTTP_CODE_OK) {
  119.  
  120. }
  121. http.end();
  122. }
  123.  
  124. void Relay3Off() {
  125. Serial.println("Vypinam switch 1");
  126. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
  127. http.begin(url);
  128. http.setAuthorization("admin", "0285");
  129. httpCode = http.GET();
  130. if (httpCode == HTTP_CODE_OK) {
  131.  
  132. }
  133. http.end();
  134. }
  135.  
  136. ////////////////RELE4
  137. void Relay4On() {
  138. Serial.println("Zapinam switch 1");
  139. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
  140. http.begin(url);
  141. http.setAuthorization("admin", "0285");
  142. httpCode = http.GET();
  143. if (httpCode == HTTP_CODE_OK) {
  144.  
  145. }
  146. http.end();
  147. }
  148.  
  149. void Relay4Off() {
  150. Serial.println("Vypinam switch 1");
  151. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
  152. http.begin(url);
  153. http.setAuthorization("admin", "0285");
  154. httpCode = http.GET();
  155. if (httpCode == HTTP_CODE_OK) {
  156.  
  157. }
  158. http.end();
  159. }
  160.  
  161. boolean connectWifi(){
  162. boolean state = true;
  163. int i = 0;
  164.  
  165. WiFi.mode(WIFI_STA);
  166. WiFi.begin(ssid, password);
  167. Serial.println("");
  168. Serial.println("Connecting to WiFi");
  169.  
  170. // Wait for connection
  171. Serial.print("Connecting ...");
  172. while (WiFi.status() != WL_CONNECTED) {
  173. delay(500);
  174. Serial.print(".");
  175.  
  176. }
  177.  
  178. if (state){
  179. Serial.println("");
  180. Serial.print("Connected to ");
  181. Serial.println(ssid);
  182. Serial.print("IP address: ");
  183. Serial.println(WiFi.localIP());
  184. }
  185. else {
  186. Serial.println("");
  187. Serial.println("Connection failed.");
  188. }
  189.  
  190. return state;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement