Advertisement
Guest User

ESP8266_Alexa_Projector_IR

a guest
Aug 23rd, 2019
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 <Wire.h>
  9.  
  10. #ifndef UNIT_TEST
  11. #include <Arduino.h>
  12. #endif
  13. #include <IRremoteESP8266.h>
  14. #include <IRsend.h>
  15.  
  16.  
  17. // prototypes
  18. boolean connectWifi();
  19.  
  20. //on/off callbacks
  21. bool lightOneOn();
  22. bool lightOneOff();
  23.  
  24. // Change this before you flash
  25. const char* ssid = "YOUR_WIFI_SSID";
  26. const char* password = "YOUR_WIFI_PASSWORD";
  27.  
  28. boolean wifiConnected = false;
  29.  
  30. UpnpBroadcastResponder upnpBroadcastResponder;
  31.  
  32. Switch *lightOne = NULL;
  33.  
  34. // Set Relay Pins, I use GPIO 2 for ESP8266 ESP-01
  35. int relayOne = 2;
  36.  
  37. IRsend irsend(relayOne);  // Set the GPIO to be used to sending the message.
  38.  
  39. void setup()
  40. {
  41.  
  42.  
  43.   //Serial.begin(115200);
  44.  
  45.   // Initialise wifi connection
  46.   wifiConnected = connectWifi();
  47.   Serial.print("WiFi Connected");
  48.  
  49.   if (wifiConnected) {
  50.     upnpBroadcastResponder.beginUdpMulticast();
  51.  
  52.  
  53.     // Define your switches here. Max 14
  54.     // Format: Alexa invocation name, local port no, on callback, off callback
  55.     lightOne = new Switch("Projector", 3301, lightOneOn, lightOneOff);
  56.    
  57.  
  58.     //Serial.println("Adding switches upnp broadcast responder");
  59.     upnpBroadcastResponder.addDevice(*lightOne);
  60.  
  61.     //Set relay pins to outputs
  62.     pinMode(relayOne, OUTPUT);
  63.  
  64.   }
  65. }
  66.  
  67. void loop()
  68. {
  69.   if (wifiConnected) {
  70.     upnpBroadcastResponder.serverLoop();
  71.     lightOne->serverLoop();
  72.   }
  73. }
  74. // Projector ON
  75. bool lightOneOn() {
  76.   Serial.print("Switch 1 turn on ...");
  77.   irsend.sendNEC(0xCF20D, 32);
  78.  
  79. }
  80. // Projector OFF
  81. bool lightOneOff() {
  82.   Serial.print("Switch 1 turn off ...");
  83.   irsend.sendNEC(0xC728D, 32);
  84.   delay(1000);
  85.   irsend.sendNEC(0xC728D, 32);
  86.  
  87. }
  88.  
  89. // connect to wifi – returns true if successful or false if not
  90.   boolean connectWifi() {
  91.   boolean state = true;
  92.   int i = 0;
  93.  
  94.   WiFi.mode(WIFI_STA);
  95.   WiFi.begin(ssid, password);
  96.   Serial.println("");
  97.   Serial.println("Connecting to WiFi");
  98.  
  99.   // Wait for connection
  100.   // Serial.print("Connecting ...");
  101.   while (WiFi.status() != WL_CONNECTED) {
  102.     delay(500);
  103.     //Serial.print(".");
  104.     if (i > 10) {
  105.       state = false;
  106.       break;
  107.     }
  108.     i++;
  109.   }
  110.  
  111.   if (state) {
  112.     //  Serial.println("");
  113.     //  Serial.print("Connected to ");
  114.     //  Serial.println(ssid);
  115.     // Serial.print("IP address: ");
  116.     //  Serial.println(WiFi.localIP());
  117.   }
  118.   else {
  119.     // Serial.println("");
  120.     //Serial.println("Connection failed.");
  121.   }
  122.  
  123.   return state;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement