Skip to content

Commit cac52de

Browse files
committed
Refactored examples and added some helpful methods
1 parent 3700801 commit cac52de

File tree

8 files changed

+153
-21
lines changed

8 files changed

+153
-21
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,24 @@ Include the library in your sketch and initialize it with your WiFi credentials.
2020

2121
## Methods
2222

23-
- `begin(const char* ssid, const char* password)`: Initialize the library with your WiFi credentials.
23+
- `begin(const char* ssid, const char* password)`: Initialize the library with your WiFi credentials to let the library handle the WiFi connection.
24+
- `begin()`: Initialize the library without WiFi credentials (use this if you want to manually connect to the WiFi network and manage the WiFi connection outside of the library).
25+
- `reconnect()`: Reconnect to the WiFi network.
26+
- `isConnected()`: Returns a boolean indicating whether the library is connected to the WiFi network.
2427
- `getCurrentColor()`: Get the current CheerLights color from ThingSpeak channel 1417. Returns a pointer to a constant char array.
2528
- `currentColorName()`: The current CheerLights color name (e.g. "red"). Returns a pointer to a constant char array.
2629
- `currentColorHex()`: The current CheerLights color as a hex value (e.g. 0xFF0000). Returns a uint32_t.
27-
- `currentRed()`, `currentGreen()`, `currentBlue()`: The RGB values for the current CheerLights color (e.g. 255, 0, 0). Returns a uint8_t.
30+
- `currentRed()`, `currentGreen()`, `currentBlue()`: The RGB values for the current CheerLights color (e.g. 255, 0, 0). Returns a uint8_t.
31+
- `hasColorChanged()`: Returns a boolean indicating whether the current CheerLights color has changed since the last call to this method.
32+
33+
## Compatibility
34+
35+
This library supports WiFi-enabled Arduino-compatible boards. It has been tested with the following boards:
36+
37+
- Arduino MKR1000
38+
- Arduino MKR WiFi 1010
39+
- Arduino Uno WiFi Rev2
40+
- Arduino Mega WiFi Rev2
41+
- Arduino Uno R4 WiFi
42+
- ESP8266
43+
- ESP32
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
'CheerLights_Basic' example demonstrates how to use the CheerLights Arduino library to fetch and print the current CheerLights color.
3+
4+
To join the CheerLights community, visit https://cheerlights.com.
5+
*/
6+
7+
// Include the correct WiFi library based on the board
8+
#if defined(ESP8266)
9+
#include <ESP8266WiFi.h>
10+
#elif defined(ESP32)
11+
#include <WiFi.h>
12+
#elif defined(ARDUINO_SAMD_MKR1000)
13+
// For Arduino MKR1000 using WiFi101 library
14+
#include <WiFi101.h>
15+
#elif defined(ARDUINO_SAMD_MKRWIFI1010)
16+
// For Arduino MKR WiFi 1010 using WiFiNINA library
17+
#include <WiFiNINA.h>
18+
#elif defined(ARDUINO_AVR_UNO_WIFI_REV2)
19+
// For Arduino Uno WiFi Rev2
20+
#include <WiFiNINA.h>
21+
#elif defined(ARDUINO_ARCH_SAMD)
22+
// For other SAMD boards
23+
#include <WiFiNINA.h>
24+
#else
25+
#include <WiFi.h>
26+
#endif
27+
28+
// Include the CheerLights library and instantiate the CheerLights object
29+
#include <CheerLights.h>
30+
CheerLights CheerLights;
31+
32+
// Include the secrets file that contains the WiFi credentials
33+
#include "secrets.h"
34+
35+
unsigned long previousMillis = 0;
36+
const long updateInterval = 15000;
37+
38+
void setup() {
39+
// Initialize serial communication
40+
Serial.begin(115200);
41+
42+
// Initialize the CheerLights library
43+
CheerLights.begin(SECRET_SSID, SECRET_PASSWORD);
44+
45+
// Get the current CheerLights color and print it
46+
CheerLights.getCurrentColor();
47+
Serial.print("Current CheerLights Color: ");
48+
Serial.println(CheerLights.currentColorName());
49+
}
50+
51+
void loop() {
52+
unsigned long currentMillis = millis();
53+
54+
// Update the LEDs every updateInterval milliseconds
55+
if (currentMillis - previousMillis >= updateInterval) {
56+
previousMillis = currentMillis;
57+
58+
// Get the current CheerLights color and print it
59+
CheerLights.getCurrentColor();
60+
Serial.print("Current CheerLights Color: ");
61+
Serial.println(CheerLights.currentColorName());
62+
63+
// Print the current CheerLights color as a hex value
64+
Serial.print("Current CheerLights Color Hex: ");
65+
Serial.println(CheerLights.currentColorHex());
66+
67+
// Print the current CheerLights color as RGB values
68+
Serial.print("Current CheerLights Color RGB: ");
69+
Serial.print(CheerLights.currentRed());
70+
Serial.print(", ");
71+
Serial.print(CheerLights.currentGreen());
72+
Serial.print(", ");
73+
Serial.println(CheerLights.currentBlue());
74+
75+
// Check if the color has changed
76+
if (CheerLights.hasColorChanged()) {
77+
Serial.println("Color has changed!");
78+
}
79+
else {
80+
Serial.println("Color has not changed.");
81+
}
82+
Serial.println("--------------------------------");
83+
}
84+
}
File renamed without changes.

examples/CheerLightsExample/CheerLightsExample.ino renamed to examples/CheerLights_NeoPixel_Strip/CheerLights_NeoPixel_Strip.ino

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
'CheerLightsExample' demonstrates how to use the CheerLights Arduino library to fetch and display the current CheerLights color on an Adafruit NeoPixel LED strip.
2+
'CheerLights_NeoPixel_Strip' example demonstrates how to use the CheerLights Arduino library to fetch and display the current CheerLights color on an Adafruit NeoPixel LED strip.
33
4-
To learn more about CheerLights, visit https://cheerlights.com.
4+
To join the CheerLights community, visit https://cheerlights.com.
55
*/
66

77
// Include the Adafruit NeoPixel library and initialize the strip
@@ -44,14 +44,20 @@ unsigned long previousMillis = 0;
4444
const long updateInterval = 15000;
4545

4646
void setup() {
47-
Serial.begin(115200);
47+
// Initialize serial communication
48+
Serial.begin(115200);
49+
50+
// Initialize the CheerLights library
4851
CheerLights.begin(SECRET_SSID, SECRET_PASSWORD);
52+
53+
// Initialize the NeoPixel strip
4954
strip.begin();
5055

5156
// Get the current CheerLights color and set the LEDs to that color
5257
CheerLights.getCurrentColor();
5358
Serial.print("Current CheerLights Color: ");
5459
Serial.println(CheerLights.currentColorName());
60+
Serial.println("--------------------------------");
5561
setLEDColors(CheerLights.currentRed(), CheerLights.currentGreen(), CheerLights.currentBlue());
5662
}
5763

@@ -66,6 +72,7 @@ void loop() {
6672
CheerLights.getCurrentColor();
6773
Serial.print("Current CheerLights Color: ");
6874
Serial.println(CheerLights.currentColorName());
75+
Serial.println("--------------------------------");
6976
setLEDColors(CheerLights.currentRed(), CheerLights.currentGreen(), CheerLights.currentBlue());
7077
}
7178
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID "MySSID" // replace MySSID with your WiFi network name
2+
#define SECRET_PASSWORD "MyPassword" // replace MyPassword with your WiFi password

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ currentRed KEYWORD2
66
currentGreen KEYWORD2
77
currentBlue KEYWORD2
88
begin KEYWORD2
9+
isConnected KEYWORD2
10+
hasColorChanged KEYWORD2
11+
reconnect KEYWORD2

src/CheerLights.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
#include "CheerLights.h"
22

3-
#define MIN_UPDATE_INTERVAL 5000
4-
#define TIMEOUT 5000
5-
#define BUFFER_SIZE 128
6-
73
CheerLights::CheerLights() {
84
strncpy(_colorName, "black", sizeof(_colorName) - 1);
95
_colorName[sizeof(_colorName) - 1] = '\0';
106
_colorHex = 0x000000;
7+
_previousColorHex = 0x000000;
118
}
129

1310
void CheerLights::begin(const char* ssid, const char* password) {
14-
Serial.begin(115200);
15-
1611
// Store WiFi credentials
1712
_ssid = ssid;
1813
_password = password;
@@ -21,34 +16,44 @@ void CheerLights::begin(const char* ssid, const char* password) {
2116
_connectToWiFi();
2217
}
2318

19+
void CheerLights::begin() {
20+
_ssid = nullptr;
21+
_password = nullptr;
22+
}
23+
24+
bool CheerLights::reconnect() {
25+
if (WiFi.status() == WL_CONNECTED) {
26+
return true;
27+
}
28+
29+
_connectToWiFi();
30+
return WiFi.status() == WL_CONNECTED;
31+
}
32+
2433
void CheerLights::_connectToWiFi() {
25-
Serial.print("Connecting to WiFi");
34+
if (_ssid == nullptr || _password == nullptr) {
35+
return;
36+
}
2637

2738
WiFi.begin(_ssid, _password);
2839
while (WiFi.status() != WL_CONNECTED) {
2940
delay(500);
30-
Serial.print(".");
3141
}
32-
33-
Serial.println("\nConnected to WiFi");
3442
}
3543

3644
void CheerLights::_fetchColor() {
3745
static unsigned long lastUpdate = 0;
3846
unsigned long currentTime = millis();
3947

4048
if (currentTime - lastUpdate < MIN_UPDATE_INTERVAL) {
41-
Serial.println("Update interval not reached, skipping request.");
4249
return;
4350
}
4451
lastUpdate = currentTime;
4552

4653
// Check WiFi connection and attempt to reconnect if necessary
4754
if (WiFi.status() != WL_CONNECTED) {
48-
Serial.println(F("WiFi not connected, attempting to reconnect..."));
4955
_connectToWiFi();
5056
if (WiFi.status() != WL_CONNECTED) {
51-
Serial.println(F("Failed to reconnect to WiFi."));
5257
return;
5358
}
5459
}
@@ -59,7 +64,6 @@ void CheerLights::_fetchColor() {
5964

6065
WiFiClient client;
6166
if (!client.connect(host, httpPort)) {
62-
Serial.println(F("Connection to ThingSpeak failed"));
6367
client.stop();
6468
return;
6569
}
@@ -75,7 +79,6 @@ void CheerLights::_fetchColor() {
7579
unsigned long timeout = millis();
7680
while (client.connected() && !client.available()) {
7781
if (millis() - timeout > TIMEOUT) {
78-
Serial.println(F(">>> Client Timeout!"));
7982
client.stop();
8083
return;
8184
}
@@ -136,6 +139,7 @@ void CheerLights::_fetchColor() {
136139
for (const auto& color : colorMap) {
137140
if (strcasecmp(_colorName, color.name) == 0) {
138141
_colorHex = color.color;
142+
_previousColorHex = _colorHex;
139143
break;
140144
}
141145
}
@@ -165,3 +169,11 @@ uint8_t CheerLights::currentGreen() {
165169
uint8_t CheerLights::currentBlue() {
166170
return _colorHex & 0xFF;
167171
}
172+
173+
bool CheerLights::isConnected() {
174+
return WiFi.status() == WL_CONNECTED;
175+
}
176+
177+
bool CheerLights::hasColorChanged() {
178+
return _colorHex != _previousColorHex;
179+
}

src/CheerLights.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include <Arduino.h>
55
#include <WiFiClient.h>
66

7+
#define MIN_UPDATE_INTERVAL 5000
8+
#define TIMEOUT 5000
9+
#define BUFFER_SIZE 128
10+
711
// Include the correct WiFi library based on the board
812
#if defined(ESP8266)
913
#include <ESP8266WiFi.h>
@@ -29,20 +33,24 @@ class CheerLights {
2933
public:
3034
CheerLights();
3135
void begin(const char* ssid, const char* password);
36+
void begin();
37+
bool reconnect();
3238
const char* getCurrentColor();
3339
const char* currentColorName();
3440
uint32_t currentColorHex();
3541
uint8_t currentRed();
3642
uint8_t currentGreen();
3743
uint8_t currentBlue();
38-
44+
bool isConnected();
45+
bool hasColorChanged();
3946
private:
4047
void _connectToWiFi();
4148
void _fetchColor();
4249
const char* _ssid;
4350
const char* _password;
4451
char _colorName[32];
4552
uint32_t _colorHex;
53+
uint32_t _previousColorHex;
4654
};
4755

4856
#endif // CHEERLIGHTS_H

0 commit comments

Comments
 (0)