Skip to content

Commit

Permalink
(2.2.0) Added Color device support
Browse files Browse the repository at this point in the history
Added Color device example
Fixed indentation
  • Loading branch information
Aircoookie committed Jan 1, 2019
1 parent f3fcd0f commit a7860e5
Show file tree
Hide file tree
Showing 5 changed files with 492 additions and 227 deletions.
96 changes: 96 additions & 0 deletions examples/EspalexaColor/EspalexaColor.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* This is a basic example on how to use Espalexa with RGB color devices.
*/
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

#include <Espalexa.h>

// prototypes
boolean connectWifi();

//callback function prototype
void colorLightChanged(uint8_t brightness, uint32_t rgb);

// Change this!!
const char* ssid = "...";
const char* password = "wifipassword";

boolean wifiConnected = false;

Espalexa espalexa;

void setup()
{
Serial.begin(115200);
// Initialise wifi connection
wifiConnected = connectWifi();

if(wifiConnected){
espalexa.addDevice("Light", colorLightChanged);

espalexa.begin();

} else
{
while (1) {
Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
delay(2500);
}
}
}

void loop()
{
espalexa.loop();
delay(1);
}

//the color device callback function has two parameters
void colorLightChanged(uint8_t brightness, uint32_t rgb) {
//do what you need to do here, for example control RGB LED strip
Serial.print("Brightness: ");
Serial.print(brightness);
Serial.print(", Red: ");
Serial.print((rgb >> 16) & 0xFF); //get red component
Serial.print(", Green: ");
Serial.print((rgb >> 8) & 0xFF); //get green
Serial.print(", Blue: ");
Serial.println(rgb & 0xFF); //get blue
}

// 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 > 40){
state = false; break;
}
i++;
}
Serial.println("");
if (state){
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("Connection failed.");
}
return state;
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Espalexa
version=2.1.2
version=2.2.0
author=Christian Schwinne
maintainer=Christian Schwinne
sentence=Library to control an ESP module with the Alexa voice assistant
Expand Down
8 changes: 6 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ Now compatible with both ESP8266 and ESP32!

#### What does this do similar projects like Fauxmo don't already?

It allows you to set a ranged value (e.g. Brightness) additionally to standard on/off control.
You can say "Alexa, turn the light to 75%".
It allows you to set a ranged value (e.g. Brightness, Temperature) additionally to standard on/off control.
For example, you can say "Alexa, turn the light to 75% / 21 degrees".
Alexa finally added color support to the local API! You can see how to add color devices in the EspalexaColor example.
Then, you can say "Alexa, turn the light to Blue". Color temperature (white shades) is also supported, but still a WIP.

If you just need On/Off (eg. for a relay) I'd recommend [arduino-esp8266-alexa-wemo-switch](https://github.com/kakopappa/arduino-esp8266-alexa-wemo-switch) instead.

Additionally, it's possible to add up to a total of 20 devices.
Each device has a brightness range from 0 to 255, where 0 is off and 255 is fully on.
You can get a percentage from that value using `espalexa.toPercent(brightness)`

#### How do I install the library?

Expand Down
Loading

0 comments on commit a7860e5

Please sign in to comment.