Skip to content

Commit 517411f

Browse files
committed
Initial Commit
1 parent fa0c2c5 commit 517411f

File tree

6 files changed

+309
-1
lines changed

6 files changed

+309
-1
lines changed

README.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,76 @@
1-
# cheerlights-arduino-library
1+
# CheerLights Arduino Library
2+
3+
An Arduino library to synchronize with the global CheerLights color by fetching the latest color from the CheerLights API.
4+
5+
## Features
6+
7+
- Fetch the latest CheerLights color name.
8+
- Retrieve RGB values for the current color.
9+
- Easy integration with LED strips like NeoPixels.
10+
11+
## IDE Installation
12+
13+
1. Open the Arduino IDE.
14+
2. Select Tools > Manage Libraries.
15+
3. Search for "CheerLights" and install.
16+
17+
## Usage
18+
19+
Include the library in your sketch and initialize it with your WiFi credentials. Use the provided methods to fetch and use the current CheerLights color.
20+
21+
## Methods
22+
23+
- `begin(const char* ssid, const char* password)`: Initialize the library with your WiFi credentials.
24+
- `getColorName()`: Get the current CheerLights color name.
25+
- `getColorHex()`: Get the current CheerLights color as a hex value.
26+
- `getRed()`, `getGreen()`, `getBlue()`: Get the RGB values for the current CheerLights color.
27+
28+
## Example
29+
30+
```cpp
31+
#include <CheerLights.h>
32+
#include <WiFiClientSecure.h>
33+
#include <Adafruit_NeoPixel.h> // For controlling NeoPixels
34+
35+
#define LED_PIN 6
36+
#define NUM_LEDS 8
37+
38+
// Replace with your network credentials
39+
const char* ssid = "your_SSID";
40+
const char* password = "your_PASSWORD";
41+
42+
CheerLights cheerLights;
43+
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
44+
45+
void setup() {
46+
Serial.begin(115200);
47+
cheerLights.begin(ssid, password);
48+
strip.begin();
49+
strip.show(); // Initialize all pixels to 'off'
50+
}
51+
52+
void loop() {
53+
// Fetch and update the color
54+
cheerLights.getColorHex();
55+
56+
// Now get the RGB values
57+
uint8_t red = cheerLights.getRed();
58+
uint8_t green = cheerLights.getGreen();
59+
uint8_t blue = cheerLights.getBlue();
60+
61+
Serial.print("Current CheerLights Color: ");
62+
Serial.println(cheerLights.getColorName());
63+
64+
// Set all pixels to the CheerLights color
65+
for (int i = 0; i < NUM_LEDS; i++) {
66+
strip.setPixelColor(i, strip.Color(red, green, blue));
67+
}
68+
strip.show();
69+
70+
delay(15000); // Update every 15 seconds
71+
}
72+
```
73+
74+
## License
75+
76+
This project is licensed under the MIT License.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
'CheerLightsExample' demonstrates how to use the CheerLights library to fetch and display the current CheerLights color on an Adafruit NeoPixel LED strip.
3+
4+
To learn more about CheerLights, visit https://cheerlights.com.
5+
*/
6+
7+
#include <CheerLights.h>
8+
#include <WiFiClientSecure.h>
9+
#include <Adafruit_NeoPixel.h> // For controlling NeoPixels
10+
11+
#define LED_PIN 6
12+
#define NUM_LEDS 8
13+
14+
// Replace with your network credentials
15+
const char* ssid = "your_SSID";
16+
const char* password = "your_PASSWORD";
17+
18+
CheerLights cheerLights;
19+
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
20+
21+
void setup() {
22+
Serial.begin(115200);
23+
cheerLights.begin(ssid, password);
24+
strip.begin();
25+
strip.show(); // Initialize all pixels to 'off'
26+
}
27+
28+
void loop() {
29+
// Fetch and update the color
30+
cheerLights.getColorHex();
31+
32+
// Now get the RGB values
33+
uint8_t red = cheerLights.getRed();
34+
uint8_t green = cheerLights.getGreen();
35+
uint8_t blue = cheerLights.getBlue();
36+
37+
Serial.print("Current CheerLights Color: ");
38+
Serial.println(cheerLights.getColorName());
39+
40+
// Set all pixels to the CheerLights color
41+
for (int i = 0; i < NUM_LEDS; i++) {
42+
strip.setPixelColor(i, strip.Color(red, green, blue));
43+
}
44+
strip.show();
45+
46+
delay(15000); // Update every 15 seconds
47+
}

keywords.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CheerLights KEYWORD1
2+
getColorName KEYWORD2
3+
getColorHex KEYWORD2
4+
begin KEYWORD2

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=CheerLights
2+
version=1.0.0
3+
author=Hans Scharler <hscharler@gmail.com>
4+
maintainer=Hans Scharler <hscharler@gmail.com>
5+
sentence=Fetch and use the latest CheerLights color.
6+
paragraph=An Arduino library to synchronize with the global CheerLights color by fetching the latest color from the CheerLights API.
7+
category=Communication
8+
url=https://github.com/cheerlights/cheerlights-arduino-library
9+
architectures=*

src/CheerLights.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include "CheerLights.h"
2+
3+
#define MIN_UPDATE_INTERVAL 5000
4+
5+
CheerLights::CheerLights() {
6+
_colorName = "black";
7+
_colorHex = 0x000000;
8+
}
9+
10+
void CheerLights::begin(const char* ssid, const char* password) {
11+
Serial.begin(115200);
12+
13+
// Connect to WiFi
14+
Serial.print("Connecting to WiFi");
15+
16+
#if defined(ESP8266) || defined(ESP32)
17+
WiFi.begin(ssid, password);
18+
while (WiFi.status() != WL_CONNECTED) {
19+
delay(500);
20+
Serial.print(".");
21+
}
22+
#elif defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_ARCH_SAMD)
23+
while (WiFi.begin(ssid, password) != WL_CONNECTED) {
24+
delay(5000);
25+
Serial.print(".");
26+
}
27+
#else
28+
// Default WiFi connection method
29+
WiFi.begin(ssid, password);
30+
while (WiFi.status() != WL_CONNECTED) {
31+
delay(500);
32+
Serial.print(".");
33+
}
34+
#endif
35+
36+
Serial.println("\nConnected to WiFi");
37+
}
38+
39+
void CheerLights::_fetchColor() {
40+
static unsigned long lastUpdate = 0;
41+
unsigned long currentTime = millis();
42+
43+
if (currentTime - lastUpdate < MIN_UPDATE_INTERVAL) {
44+
return;
45+
}
46+
lastUpdate = currentTime;
47+
48+
if (WiFi.status() != WL_CONNECTED) {
49+
Serial.println(F("WiFi not connected"));
50+
return;
51+
}
52+
53+
HTTPClient http;
54+
http.begin(F("http://api.thingspeak.com/channels/1417/field/1/last.txt"));
55+
int httpCode = http.GET();
56+
57+
if (httpCode != HTTP_CODE_OK) {
58+
Serial.println(F("Failed to fetch color"));
59+
http.end();
60+
return;
61+
}
62+
63+
_colorName = http.getString();
64+
http.end();
65+
66+
_colorName.trim();
67+
_colorName.toLowerCase();
68+
69+
// Use progmem for color mapping
70+
static const struct {
71+
const char* name;
72+
uint32_t color;
73+
} colorMap[] PROGMEM = {
74+
{"red", 0xFF0000},
75+
{"green", 0x00FF00},
76+
{"blue", 0x0000FF},
77+
{"cyan", 0x00FFFF},
78+
{"white", 0xFFFFFF},
79+
{"warmwhite", 0xFDF5E6},
80+
{"oldlace", 0xFDF5E6},
81+
{"magenta", 0xFF00FF},
82+
{"yellow", 0xFFFF00},
83+
{"orange", 0xFFA500},
84+
{"purple", 0x800080},
85+
{"pink", 0xFFC0CB},
86+
{"black", 0x000000}
87+
};
88+
89+
_colorHex = 0x000000; // Default to black
90+
for (const auto& color : colorMap) {
91+
if (_colorName == FPSTR(color.name)) {
92+
_colorHex = pgm_read_dword(&color.color);
93+
break;
94+
}
95+
}
96+
}
97+
98+
String CheerLights::getColorName() {
99+
_fetchColor();
100+
return _colorName;
101+
}
102+
103+
uint32_t CheerLights::getColorHex() {
104+
_fetchColor();
105+
return _colorHex;
106+
}
107+
108+
uint8_t CheerLights::getRed() {
109+
return (_colorHex >> 16) & 0xFF;
110+
}
111+
112+
uint8_t CheerLights::getGreen() {
113+
return (_colorHex >> 8) & 0xFF;
114+
}
115+
116+
uint8_t CheerLights::getBlue() {
117+
return _colorHex & 0xFF;
118+
}

src/CheerLights.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef CHEERLIGHTS_H
2+
#define CHEERLIGHTS_H
3+
4+
#include <Arduino.h>
5+
6+
// Include the correct WiFi library based on the board
7+
#if defined(ESP8266)
8+
#include <ESP8266WiFi.h>
9+
#include <ESP8266HTTPClient.h>
10+
#elif defined(ESP32)
11+
#include <WiFi.h>
12+
#include <HTTPClient.h>
13+
#elif defined(ARDUINO_SAMD_MKR1000)
14+
// For Arduino MKR1000 using WiFi101 library
15+
#include <WiFi101.h>
16+
#include <WiFiClient.h>
17+
#include <HTTPClient.h>
18+
#elif defined(ARDUINO_SAMD_MKRWIFI1010)
19+
// For Arduino MKR WiFi 1010 using WiFiNINA library
20+
#include <WiFiNINA.h>
21+
#include <WiFiClient.h>
22+
#include <HTTPClient.h>
23+
#elif defined(ARDUINO_AVR_UNO_WIFI_REV2)
24+
// For Arduino Uno WiFi Rev2
25+
#include <WiFiNINA.h>
26+
#include <WiFiClient.h>
27+
#include <HTTPClient.h>
28+
#elif defined(ARDUINO_ARCH_SAMD)
29+
// For other SAMD boards
30+
#include <WiFiNINA.h>
31+
#include <WiFiClient.h>
32+
#include <HTTPClient.h>
33+
#else
34+
// Default to WiFi.h and HTTPClient.h
35+
#include <WiFi.h>
36+
#include <HTTPClient.h>
37+
#endif
38+
39+
class CheerLights {
40+
public:
41+
CheerLights();
42+
void begin(const char* ssid, const char* password);
43+
String getColorName();
44+
uint32_t getColorHex();
45+
uint8_t getRed();
46+
uint8_t getGreen();
47+
uint8_t getBlue();
48+
49+
private:
50+
String _colorName;
51+
uint32_t _colorHex;
52+
void _fetchColor();
53+
};
54+
55+
#endif // CHEERLIGHTS_H

0 commit comments

Comments
 (0)