Advertisement
martinius96

Untitled

Jun 21st, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.57 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("Relay 2", 81, Relay2On, Relay2Off);
  44.     relay3 = new Switch("Relay 3", 82, Relay3On, Relay3Off);
  45.     relay4 = new Switch("Relay 4", 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.      }
  58. }
  59.  
  60. void Relay1On() {
  61.     Serial.println("Zapinam switch 1");
  62. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
  63.   http.begin(url);
  64.   http.setAuthorization("admin", "0285");
  65.   httpCode = http.GET();
  66.   if (httpCode == HTTP_CODE_OK) {
  67.  
  68.   }
  69.   http.end();
  70. }
  71.  
  72. void Relay1Off() {
  73.         Serial.println("Vypinam switch 1");
  74. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
  75.   http.begin(url);
  76.   http.setAuthorization("admin", "0285");
  77.   httpCode = http.GET();
  78.   if (httpCode == HTTP_CODE_OK) {
  79.  
  80.   }
  81.   http.end();
  82. }
  83. ///////////////////RELE2
  84. void Relay2On() {
  85.     Serial.println("Zapinam switch 1");
  86. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
  87.   http.begin(url);
  88.   http.setAuthorization("admin", "0285");
  89.   httpCode = http.GET();
  90.   if (httpCode == HTTP_CODE_OK) {
  91.  
  92.   }
  93.   http.end();
  94. }
  95.  
  96. void Relay2Off() {
  97.         Serial.println("Vypinam switch 1");
  98. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
  99.   http.begin(url);
  100.   http.setAuthorization("admin", "0285");
  101.   httpCode = http.GET();
  102.   if (httpCode == HTTP_CODE_OK) {
  103.  
  104.   }
  105.   http.end();
  106. }
  107.  
  108. /////////////RELE3
  109. void Relay3On() {
  110.     Serial.println("Zapinam switch 1");
  111. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
  112.   http.begin(url);
  113.   http.setAuthorization("admin", "0285");
  114.   httpCode = http.GET();
  115.   if (httpCode == HTTP_CODE_OK) {
  116.  
  117.   }
  118.   http.end();
  119. }
  120.  
  121. void Relay3Off() {
  122.         Serial.println("Vypinam switch 1");
  123. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
  124.   http.begin(url);
  125.   http.setAuthorization("admin", "0285");
  126.   httpCode = http.GET();
  127.   if (httpCode == HTTP_CODE_OK) {
  128.  
  129.   }
  130.   http.end();
  131. }
  132.  
  133. ////////////////RELE4
  134. void Relay4On() {
  135.     Serial.println("Zapinam switch 1");
  136. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
  137.   http.begin(url);
  138.   http.setAuthorization("admin", "0285");
  139.   httpCode = http.GET();
  140.   if (httpCode == HTTP_CODE_OK) {
  141.  
  142.   }
  143.   http.end();
  144. }
  145.  
  146. void Relay4Off() {
  147.         Serial.println("Vypinam switch 1");
  148. String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
  149.   http.begin(url);
  150.   http.setAuthorization("admin", "0285");
  151.   httpCode = http.GET();
  152.   if (httpCode == HTTP_CODE_OK) {
  153.  
  154.   }
  155.   http.end();
  156. }
  157.  
  158. boolean connectWifi(){
  159.   boolean state = true;
  160.   int i = 0;
  161.  
  162.   WiFi.mode(WIFI_STA);
  163.   WiFi.begin(ssid, password);
  164.   Serial.println("");
  165.   Serial.println("Connecting to WiFi");
  166.  
  167.   // Wait for connection
  168.   Serial.print("Connecting ...");
  169.   while (WiFi.status() != WL_CONNECTED) {
  170.     delay(500);
  171.     Serial.print(".");
  172.    
  173.   }
  174.  
  175.   if (state){
  176.     Serial.println("");
  177.     Serial.print("Connected to ");
  178.     Serial.println(ssid);
  179.     Serial.print("IP address: ");
  180.     Serial.println(WiFi.localIP());
  181.   }
  182.   else {
  183.     Serial.println("");
  184.     Serial.println("Connection failed.");
  185.   }
  186.  
  187.   return state;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement