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 <Wire.h>
- #ifndef UNIT_TEST
- #include <Arduino.h>
- #endif
- #include <IRremoteESP8266.h>
- #include <IRsend.h>
- // prototypes
- boolean connectWifi();
- //on/off callbacks
- bool lightOneOn();
- bool lightOneOff();
- // Change this before you flash
- const char* ssid = "YOUR_WIFI_SSID";
- const char* password = "YOUR_WIFI_PASSWORD";
- boolean wifiConnected = false;
- UpnpBroadcastResponder upnpBroadcastResponder;
- Switch *lightOne = NULL;
- // Set Relay Pins, I use GPIO 2 for ESP8266 ESP-01
- int relayOne = 2;
- IRsend irsend(relayOne); // Set the GPIO to be used to sending the message.
- void setup()
- {
- //Serial.begin(115200);
- // Initialise wifi connection
- wifiConnected = connectWifi();
- Serial.print("WiFi Connected");
- if (wifiConnected) {
- upnpBroadcastResponder.beginUdpMulticast();
- // Define your switches here. Max 14
- // Format: Alexa invocation name, local port no, on callback, off callback
- lightOne = new Switch("Projector", 3301, lightOneOn, lightOneOff);
- //Serial.println("Adding switches upnp broadcast responder");
- upnpBroadcastResponder.addDevice(*lightOne);
- //Set relay pins to outputs
- pinMode(relayOne, OUTPUT);
- }
- }
- void loop()
- {
- if (wifiConnected) {
- upnpBroadcastResponder.serverLoop();
- lightOne->serverLoop();
- }
- }
- // Projector ON
- bool lightOneOn() {
- Serial.print("Switch 1 turn on ...");
- irsend.sendNEC(0xCF20D, 32);
- }
- // Projector OFF
- bool lightOneOff() {
- Serial.print("Switch 1 turn off ...");
- irsend.sendNEC(0xC728D, 32);
- delay(1000);
- irsend.sendNEC(0xC728D, 32);
- }
- // connect to wifi – returns true if successful or false if not
- 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 (i > 10) {
- state = false;
- break;
- }
- i++;
- }
- 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