-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Color device example Fixed indentation
- Loading branch information
1 parent
f3fcd0f
commit a7860e5
Showing
5 changed files
with
492 additions
and
227 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 | ||
|
||
#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; | ||
} |
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
Oops, something went wrong.