Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <IRremoteESP8266.h>
- //#include <IRremoteInt.h>
- //#include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include "fauxmoESP.h"
- #include "credentials.h" //WiFi SSID and password
- #define SERIAL_BAUDRATE 115200
- #define LED 16 // LED on Board
- #define RELAY_PIN 5 //
- #define OPen2Pin 12 // Open Blinds pin
- #define Close2Pin 14 // Close Blinds pin
- #define khz 40 // 40kHz carrier frequency for the Hunter Douglas
- IRsend irsend(4); //
- unsigned int OPen2Signal[80] = {2800, 2800, 1050, 2800, 1050, 1050, 1050, 1050, 1050,
- 2800, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 1050, 1050,
- 2800, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 1050, 1050,
- 2800, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 1050, 1050,
- 2800, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 1050, 1050,
- 2800, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 1050, 1050,
- 2800, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 1050, 1050,
- 2800, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 1050, 1050, 1975
- }; //Open2
- unsigned int Close2Signal[80] = {2800, 2800, 1050, 2800, 1050, 1050, 1050, 2800, 1050,
- 1050, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 2800, 1050,
- 1050, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 2800, 1050,
- 1050, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 2800, 1050,
- 1050, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 2800, 1050,
- 1050, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 2800, 1050,
- 1050, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 2800, 1050,
- 1050, 2800, 2800, 1050, 2800, 1050, 1050, 1050, 2800, 1050, 1975
- }; //Close the lower blinds
- // variables will change:
- int butOpen2State = 0; // variable for reading the pushbutton status
- int butClose2State = 0; // variable for reading the pushbutton status
- fauxmoESP fauxmo;
- // -----------------------------------------------------------------------------
- // Wifi
- // -----------------------------------------------------------------------------
- void wifiSetup() {
- // Set WIFI module to STA mode
- WiFi.mode(WIFI_STA);
- // Connect
- Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
- WiFi.begin(WIFI_SSID, WIFI_PASS);
- // Wait
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(100);
- }
- Serial.println();
- // Connected!
- Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
- }
- void callback(unsigned char device_id, const char * device_name, bool state) {
- Serial.print("Device ID "); Serial.print(device_id);
- Serial.print("Device "); Serial.print(device_name);
- Serial.print(" state: ");
- if (state) {
- Serial.println("ON");
- } else {
- Serial.println("OFF");
- }
- if ( (strcmp(device_name, "relay") == 0) ) {
- // adjust the relay immediately!
- if (state) {
- digitalWrite(RELAY_PIN, HIGH);
- } else {
- digitalWrite(RELAY_PIN, LOW);
- Serial.println("I'm turning it OFF");
- }
- }
- }
- void setup() {
- // Init serial port and clean garbage
- Serial.begin(SERIAL_BAUDRATE);
- Serial.println("FauxMo demo sketch");
- Serial.println("After connection, ask Alexa/Echo to 'turn <devicename> on' or 'off'");
- // Wifi
- wifiSetup();
- // Buttons
- pinMode(OPen2Pin, INPUT);
- pinMode(Close2Pin, INPUT);
- // LED
- pinMode(LED, OUTPUT);
- digitalWrite(LED, LOW);
- pinMode(RELAY_PIN, OUTPUT);
- digitalWrite(RELAY_PIN, LOW);
- //pinMode(LED_BUILTIN, OUTPUT); // This is D0 or 16
- //digitalWrite(LED_BUILTIN, HIGH);
- // Fauxmo
- fauxmo.addDevice("relay");
- fauxmo.addDevice("pixels");
- fauxmo.onMessage(callback);
- }
- void loop() {
- // Since fauxmoESP 2.0 the library uses the "compatibility" mode by
- // default, this means that it uses WiFiUdp class instead of AsyncUDP.
- // The later requires the Arduino Core for ESP8266 staging version
- // whilst the former works fine with current stable 2.3.0 version.
- // But, since it's not "async" anymore we have to manually poll for UDP
- // packets
- fauxmo.handle();
- // read the state of the pushbutton value:
- butOpen2State = digitalRead(OPen2Pin);
- butClose2State = digitalRead(Close2Pin);
- //Serial.println(butOpen2State);
- if (butOpen2State == HIGH) {
- // send IR code:
- //irsend.sendRaw(OPen2Signal, sizeof(OPen2Signal) / sizeof(OPen2Signal[0]), khz);
- irsend.sendRaw(OPen2Signal, 80, 40);
- Serial.println("Open2 Button Pushed");
- digitalWrite(LED, HIGH);
- }
- if (butClose2State == HIGH) {
- // send IR code:
- irsend.sendRaw(Close2Signal, sizeof(Close2Signal) / sizeof(Close2Signal[0]), khz);
- Serial.println("Close2 Button Pushed");
- digitalWrite(LED, LOW);
- }
- /* D0 is the Blue WiFi LED on the ESP Module
- digitalWrite(LED, HIGH);
- delay(1500);
- digitalWrite(LED, LOW);
- delay(1500);
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement