-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(2.4.1) Re-added 2.2.0+ color callbacks
- Loading branch information
1 parent
748949c
commit 1b993b0
Showing
6 changed files
with
134 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
#define ESPALEXA_ASYNC | ||
#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("Color 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters