Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <WiFiUdp.h>
- #include <functional>
- #include "switch.h"
- #include "UpnpBroadcastResponder.h"
- #include "CallbackFunction.h"
- #include <ESP8266HTTPClient.h>
- #include <Arduino.h>
- const char *ssid = "Macgyver";
- const char *password = "0285461937";
- boolean connectWifi();
- void Relay1On();
- void Relay1Off();
- void Relay2On();
- void Relay2Off();
- void Relay3On();
- void Relay3Off();
- void Relay4On();
- void Relay4Off();
- boolean wifiConnected = false;
- int httpCode = 0;
- UpnpBroadcastResponder upnpBroadcastResponder;
- Switch *relay1 = NULL;
- Switch *relay2 = NULL;
- Switch *relay3 = NULL;
- Switch *relay4 = NULL;
- WiFiClient client;
- HTTPClient http;
- void setup()
- {
- Serial.begin(9600);
- // Initialise wifi connection
- wifiConnected = connectWifi();
- if(wifiConnected){
- upnpBroadcastResponder.beginUdpMulticast();
- // Define your switches here. Max 14
- // Format: Alexa invocation name, local port no, on callback, off callback
- relay1 = new Switch("Relay", 80, Relay1On, Relay1Off);
- relay2 = new Switch("Relay 2", 81, Relay2On, Relay2Off);
- relay3 = new Switch("Relay 3", 82, Relay3On, Relay3Off);
- relay4 = new Switch("Relay 4", 84, Relay4On, Relay4Off);
- Serial.println("Adding switches upnp broadcast responder");
- upnpBroadcastResponder.addDevice(*relay1);
- }
- }
- void loop()
- {
- if(wifiConnected){
- upnpBroadcastResponder.serverLoop();
- relay1->serverLoop();
- }
- }
- void Relay1On() {
- Serial.println("Zapinam switch 1");
- String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
- http.begin(url);
- http.setAuthorization("admin", "0285");
- httpCode = http.GET();
- if (httpCode == HTTP_CODE_OK) {
- }
- http.end();
- }
- void Relay1Off() {
- Serial.println("Vypinam switch 1");
- String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
- http.begin(url);
- http.setAuthorization("admin", "0285");
- httpCode = http.GET();
- if (httpCode == HTTP_CODE_OK) {
- }
- http.end();
- }
- ///////////////////RELE2
- void Relay2On() {
- Serial.println("Zapinam switch 1");
- String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
- http.begin(url);
- http.setAuthorization("admin", "0285");
- httpCode = http.GET();
- if (httpCode == HTTP_CODE_OK) {
- }
- http.end();
- }
- void Relay2Off() {
- Serial.println("Vypinam switch 1");
- String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
- http.begin(url);
- http.setAuthorization("admin", "0285");
- httpCode = http.GET();
- if (httpCode == HTTP_CODE_OK) {
- }
- http.end();
- }
- /////////////RELE3
- void Relay3On() {
- Serial.println("Zapinam switch 1");
- String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
- http.begin(url);
- http.setAuthorization("admin", "0285");
- httpCode = http.GET();
- if (httpCode == HTTP_CODE_OK) {
- }
- http.end();
- }
- void Relay3Off() {
- Serial.println("Vypinam switch 1");
- String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
- http.begin(url);
- http.setAuthorization("admin", "0285");
- httpCode = http.GET();
- if (httpCode == HTTP_CODE_OK) {
- }
- http.end();
- }
- ////////////////RELE4
- void Relay4On() {
- Serial.println("Zapinam switch 1");
- String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/on";
- http.begin(url);
- http.setAuthorization("admin", "0285");
- httpCode = http.GET();
- if (httpCode == HTTP_CODE_OK) {
- }
- http.end();
- }
- void Relay4Off() {
- Serial.println("Vypinam switch 1");
- String url = "http://192.168.1.13:83/dev/sps/io/0f7d5586-039c-3ba2-fffffe345eb0bf31/off";
- http.begin(url);
- http.setAuthorization("admin", "0285");
- httpCode = http.GET();
- if (httpCode == HTTP_CODE_OK) {
- }
- http.end();
- }
- boolean connectWifi(){
- boolean state = true;
- int i = 0;
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- Serial.println("");
- Serial.println("Connecting to WiFi");
- // Wait for connection
- Serial.print("Connecting ...");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- if (state){
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- }
- else {
- Serial.println("");
- Serial.println("Connection failed.");
- }
- return state;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement