Guest User

ESP8266_Alexa_Projector_IR

a guest
Aug 23rd, 2019
301
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 projOn();
  22. bool projOff();
  23.  
  24. // Change this before you flash
  25. const char* ssid = "WiFi";
  26. const char* password = "Password";
  27.  
  28. boolean wifiConnected = false;
  29.  
  30. UpnpBroadcastResponder upnpBroadcastResponder;
  31.  
  32. Switch *proj = NULL;
  33.  
  34. bool isProjOn = false;
  35.  
  36. // Set Relay Pins
  37. int relayOne = 2;
  38.  
  39. IRsend irsend(relayOne);  // Set the GPIO to be used to sending the message.
  40.  
  41. void setup()
  42. {
  43.  
  44.  
  45.   Serial.begin(115200);
  46.  
  47.   // Initialise wifi connection
  48.   wifiConnected = connectWifi();
  49.   Serial.print("WiFi Connected");
  50.  
  51.   if (wifiConnected) {
  52.     upnpBroadcastResponder.beginUdpMulticast();
  53.  
  54.  
  55.     // Define your switches here. Max 14
  56.     // Format: Alexa invocation name, local port no, on callback, off callback
  57.     proj = new Switch("Projector", 3301, projOn, projOff);
  58.    
  59.  
  60.     //Serial.println("Adding switches upnp broadcast responder");
  61.     upnpBroadcastResponder.addDevice(*proj);
  62.  
  63.     //Set relay pins to outputs
  64.     pinMode(relayOne, OUTPUT);
  65.  
  66.   }
  67. }
  68.  
  69. void loop()
  70. {
  71.   if (wifiConnected) {
  72.     upnpBroadcastResponder.serverLoop();
  73.     proj->serverLoop();
  74.   }
  75. }
  76. // Projector ON
  77. bool projOn() {
  78.   Serial.print("Projector turn on ...");
  79.   irsend.sendNEC(0xCF20D, 32);
  80.   isProjOn = true;
  81.   return isProjOn;
  82. }
  83. // Projector OFF
  84. bool projOff() {
  85.   Serial.print("Projector turn off ...");
  86.   irsend.sendNEC(0xC728D, 32);
  87.   delay(1000);
  88.   irsend.sendNEC(0xC728D, 32);
  89.   isProjOn = false;
  90.   return isProjOn;
  91. }
  92.  
  93. // connect to wifi – returns true if successful or false if not
  94.   boolean connectWifi() {
  95.   boolean state = true;
  96.   int i = 0;
  97.  
  98.   WiFi.mode(WIFI_STA);
  99.   WiFi.begin(ssid, password);
  100.   Serial.println("");
  101.   Serial.println("Connecting to WiFi");
  102.  
  103.   // Wait for connection
  104.   // Serial.print("Connecting ...");
  105.   while (WiFi.status() != WL_CONNECTED) {
  106.     delay(500);
  107.     //Serial.print(".");
  108.     if (i > 10) {
  109.       state = false;
  110.       break;
  111.     }
  112.     i++;
  113.   }
  114.  
  115.   if (state) {
  116.     Serial.println("");
  117.     Serial.print("Connected to ");
  118.     Serial.println(ssid);
  119.     Serial.print("IP address: ");
  120.     Serial.println(WiFi.localIP());
  121.   }
  122.   else {
  123.     Serial.println("");
  124.     Serial.println("Connection failed.");
  125.   }
  126.  
  127.   return state;
  128. }
RAW Paste Data