fauxmo-relay-neopixel.ino
1 // SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 #include <Arduino.h> 6 #include <Adafruit_NeoPixel.h> 7 #include <ESP8266WiFi.h> 8 #include "fauxmoESP.h" 9 10 #define WIFI_SSID "wifi_ssid" 11 #define WIFI_PASS "wifi_pass" 12 13 #define SERIAL_BAUDRATE 115200 14 15 fauxmoESP fauxmo; 16 17 #define RELAY_PIN 13 18 #define NEOPIX_PIN 2 19 Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, NEOPIX_PIN, NEO_GRB + NEO_KHZ800); 20 volatile boolean neopixel_state = false; // off by default! 21 22 uint32_t Wheel(byte WheelPos); // function prototype 23 24 // ----------------------------------------------------------------------------- 25 // Wifi 26 // ----------------------------------------------------------------------------- 27 28 void wifiSetup() 29 { 30 // Set WIFI module to STA mode 31 WiFi.mode(WIFI_STA); 32 33 // Connect 34 Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID); 35 WiFi.begin(WIFI_SSID, WIFI_PASS); 36 37 // Wait 38 while (WiFi.status() != WL_CONNECTED) 39 { 40 Serial.print("."); 41 delay(100); 42 } 43 Serial.println(); 44 45 // Connected! 46 Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str()); 47 } 48 49 void setup() 50 { 51 strip.begin(); 52 strip.setBrightness(20); 53 strip.show(); // Initialize all pixels to 'off' 54 pinMode(RELAY_PIN, OUTPUT); 55 digitalWrite(RELAY_PIN, LOW); 56 57 // Init serial port and clean garbage 58 Serial.begin(SERIAL_BAUDRATE); 59 Serial.println(); 60 Serial.println(); 61 Serial.println("FauxMo demo sketch"); 62 Serial.println("After connection, ask Alexa/Echo to 'turn pixels on' or 'off' or 'turn relay on' or 'off'"); 63 64 // Wifi 65 wifiSetup(); 66 // for gen3 devices or above 67 fauxmo.setPort(80); 68 fauxmo.enable(true); 69 70 // Fauxmo 71 fauxmo.addDevice("relay"); 72 fauxmo.addDevice("pixels"); 73 74 fauxmo.onSetState([](unsigned char device_id, const char *device_name, bool state, unsigned char value) { 75 Serial.printf("[MAIN] %s state: %s\n", device_name, state ? "ON" : "OFF"); 76 77 if ((strcmp(device_name, "pixels") == 0)) 78 { 79 // this just sets a variable that the main loop() does something about 80 if (state) 81 { 82 neopixel_state = true; 83 } 84 else 85 { 86 neopixel_state = false; 87 } 88 } 89 90 if ((strcmp(device_name, "relay") == 0)) 91 { 92 // adjust the relay immediately! 93 if (state) 94 { 95 digitalWrite(RELAY_PIN, HIGH); 96 } 97 else 98 { 99 digitalWrite(RELAY_PIN, LOW); 100 } 101 } 102 }); 103 } 104 uint8_t j = 0; // color swirl incrementer 105 void loop() 106 { 107 fauxmo.handle(); 108 if (neopixel_state) 109 { 110 for (int16_t i = 0; i < strip.numPixels(); i++) 111 { 112 strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); 113 } 114 strip.show(); 115 j++; 116 delay(20); 117 } 118 else 119 { 120 for (int16_t i = 0; i < strip.numPixels(); i++) 121 { 122 strip.setPixelColor(i, 0); 123 } 124 strip.show(); 125 } 126 } 127 128 // Input a value 0 to 255 to get a color value. 129 // The colours are a transition r - g - b - back to r. 130 uint32_t Wheel(byte WheelPos) 131 { 132 if (WheelPos < 85) 133 { 134 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); 135 } 136 else if (WheelPos < 170) 137 { 138 WheelPos -= 85; 139 return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); 140 } 141 else 142 { 143 WheelPos -= 170; 144 return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); 145 } 146 }