Skip to content

Commit 83383ec

Browse files
committed
Fixed issues with WiFi client
1 parent 517411f commit 83383ec

File tree

6 files changed

+147
-120
lines changed

6 files changed

+147
-120
lines changed

README.md

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,7 @@ Include the library in your sketch and initialize it with your WiFi credentials.
2121
## Methods
2222

2323
- `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.
24+
- `getCurrentColor()`: Get the current CheerLights color from ThingSpeak.
25+
- `showColorName()`: Show the current CheerLights color name (e.g. "red").
26+
- `showColorHex()`: Show the current CheerLights color as a hex value (e.g. 0xFF0000).
27+
- `showRed()`, `showGreen()`, `showBlue()`: Show the RGB values for the current CheerLights color (e.g. 255, 0, 0).

examples/CheerLightsExample/CheerLightsExample.ino

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,61 @@ To learn more about CheerLights, visit https://cheerlights.com.
55
*/
66

77
#include <CheerLights.h>
8-
#include <WiFiClientSecure.h>
8+
#include "secrets.h"
99
#include <Adafruit_NeoPixel.h> // For controlling NeoPixels
1010

1111
#define LED_PIN 6
1212
#define NUM_LEDS 8
1313

14-
// Replace with your network credentials
15-
const char* ssid = "your_SSID";
16-
const char* password = "your_PASSWORD";
17-
18-
CheerLights cheerLights;
14+
CheerLights CheerLights;
1915
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
2016

17+
unsigned long previousMillis = 0;
18+
const long interval = 15000; // 15 seconds
19+
2120
void setup() {
2221
Serial.begin(115200);
23-
cheerLights.begin(ssid, password);
22+
CheerLights.begin(SECRET_SSID, SECRET_PASSWORD);
2423
strip.begin();
2524
strip.show(); // Initialize all pixels to 'off'
2625
}
2726

2827
void loop() {
29-
// Fetch and update the color
30-
cheerLights.getColorHex();
28+
unsigned long currentMillis = millis();
29+
30+
if (currentMillis - previousMillis >= interval) {
31+
previousMillis = currentMillis;
32+
33+
// Get the current CheerLights color
34+
Serial.print("Current CheerLights Color: ");
35+
Serial.println(CheerLights.getCurrentColor());
36+
37+
// Show the color as a hex value
38+
Serial.print("Color Hex: 0x");
39+
Serial.println(CheerLights.showColorHex(), HEX);
40+
41+
// Show the RGB values
42+
uint8_t red = CheerLights.showRed();
43+
uint8_t green = CheerLights.showGreen();
44+
uint8_t blue = CheerLights.showBlue();
3145

32-
// Now get the RGB values
33-
uint8_t red = cheerLights.getRed();
34-
uint8_t green = cheerLights.getGreen();
35-
uint8_t blue = cheerLights.getBlue();
46+
Serial.print("Red: ");
47+
Serial.print(red);
48+
Serial.print(" Green: ");
49+
Serial.print(green);
50+
Serial.print(" Blue: ");
51+
Serial.println(blue);
3652

37-
Serial.print("Current CheerLights Color: ");
38-
Serial.println(cheerLights.getColorName());
53+
// Show the color name
54+
Serial.print("CheerLights Color Name: ");
55+
Serial.println(CheerLights.showColorName());
3956

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));
57+
// Set all pixels to the CheerLights color using the RGB values
58+
for (int i = 0; i < NUM_LEDS; i++) {
59+
strip.setPixelColor(i, strip.Color(red, green, blue));
60+
}
61+
strip.show();
4362
}
44-
strip.show();
4563

46-
delay(15000); // Update every 15 seconds
64+
// Other non-blocking code can go here
4765
}
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
CheerLights KEYWORD1
2-
getColorName KEYWORD2
3-
getColorHex KEYWORD2
2+
getCurrentColor KEYWORD2
3+
showColorName KEYWORD2
4+
showColorHex KEYWORD2
5+
showRed KEYWORD2
6+
showGreen KEYWORD2
7+
showBlue KEYWORD2
48
begin KEYWORD2

src/CheerLights.cpp

Lines changed: 85 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,31 @@ CheerLights::CheerLights() {
1010
void CheerLights::begin(const char* ssid, const char* password) {
1111
Serial.begin(115200);
1212

13-
// Connect to WiFi
13+
// Store WiFi credentials
14+
_ssid = ssid;
15+
_password = password;
16+
17+
// Initial WiFi connection
18+
_connectToWiFi();
19+
}
20+
21+
void CheerLights::_connectToWiFi() {
1422
Serial.print("Connecting to WiFi");
15-
16-
#if defined(ESP8266) || defined(ESP32)
17-
WiFi.begin(ssid, password);
23+
24+
#if defined(ESP8266) || defined(ESP32)
25+
WiFi.begin(_ssid, _password);
1826
while (WiFi.status() != WL_CONNECTED) {
1927
delay(500);
2028
Serial.print(".");
2129
}
2230
#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) {
31+
while (WiFi.begin(_ssid, _password) != WL_CONNECTED) {
2432
delay(5000);
2533
Serial.print(".");
2634
}
2735
#else
2836
// Default WiFi connection method
29-
WiFi.begin(ssid, password);
37+
WiFi.begin(_ssid, _password);
3038
while (WiFi.status() != WL_CONNECTED) {
3139
delay(500);
3240
Serial.print(".");
@@ -41,36 +49,82 @@ void CheerLights::_fetchColor() {
4149
unsigned long currentTime = millis();
4250

4351
if (currentTime - lastUpdate < MIN_UPDATE_INTERVAL) {
52+
Serial.println("Update interval not reached, skipping request.");
4453
return;
4554
}
4655
lastUpdate = currentTime;
4756

57+
// Check WiFi connection and attempt to reconnect if necessary
4858
if (WiFi.status() != WL_CONNECTED) {
49-
Serial.println(F("WiFi not connected"));
50-
return;
59+
Serial.println(F("WiFi not connected, attempting to reconnect..."));
60+
_connectToWiFi();
61+
if (WiFi.status() != WL_CONNECTED) {
62+
Serial.println(F("Failed to reconnect to WiFi."));
63+
return;
64+
}
5165
}
5266

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();
67+
const char* host = "api.thingspeak.com";
68+
const int httpPort = 80;
69+
const char* apiPath = "/channels/1417/field/1/last.txt";
70+
71+
WiFiClient client;
72+
if (!client.connect(host, httpPort)) {
73+
Serial.println(F("Connection to ThingSpeak failed"));
74+
client.stop();
6075
return;
6176
}
6277

63-
_colorName = http.getString();
64-
http.end();
78+
// Create the HTTP GET request
79+
client.print(String("GET ") + apiPath + " HTTP/1.1\r\n" +
80+
"Host: " + host + "\r\n" +
81+
"Connection: close\r\n\r\n");
82+
83+
// Wait for response
84+
unsigned long timeout = millis();
85+
while (client.connected() && !client.available()) {
86+
if (millis() - timeout > 5000) { // 5 seconds timeout
87+
Serial.println(F(">>> Client Timeout!"));
88+
client.stop();
89+
return;
90+
}
91+
}
92+
93+
// Read the response
94+
bool headersEnd = false;
95+
while (client.connected()) {
96+
String line = client.readStringUntil('\n');
97+
line.trim();
98+
99+
if (!headersEnd) {
100+
// Check for end of headers
101+
if (line.length() == 0) {
102+
headersEnd = true;
103+
}
104+
continue;
105+
}
106+
107+
if (line.length() > 0) {
108+
_colorName = line;
109+
break;
110+
}
111+
}
112+
113+
// Read any remaining data
114+
while (client.available()) {
115+
client.read();
116+
}
117+
118+
client.stop();
65119

66120
_colorName.trim();
67121
_colorName.toLowerCase();
68122

69-
// Use progmem for color mapping
123+
// Map the color name to a hex value
70124
static const struct {
71125
const char* name;
72126
uint32_t color;
73-
} colorMap[] PROGMEM = {
127+
} colorMap[] = {
74128
{"red", 0xFF0000},
75129
{"green", 0x00FF00},
76130
{"blue", 0x0000FF},
@@ -88,31 +142,35 @@ void CheerLights::_fetchColor() {
88142

89143
_colorHex = 0x000000; // Default to black
90144
for (const auto& color : colorMap) {
91-
if (_colorName == FPSTR(color.name)) {
92-
_colorHex = pgm_read_dword(&color.color);
145+
if (_colorName.equalsIgnoreCase(color.name)) {
146+
_colorHex = color.color;
93147
break;
94148
}
95149
}
150+
96151
}
97152

98-
String CheerLights::getColorName() {
153+
String CheerLights::getCurrentColor() {
99154
_fetchColor();
100155
return _colorName;
101156
}
102157

103-
uint32_t CheerLights::getColorHex() {
104-
_fetchColor();
158+
String CheerLights::showColorName() {
159+
return _colorName;
160+
}
161+
162+
uint32_t CheerLights::showColorHex() {
105163
return _colorHex;
106164
}
107165

108-
uint8_t CheerLights::getRed() {
166+
uint8_t CheerLights::showRed() {
109167
return (_colorHex >> 16) & 0xFF;
110168
}
111169

112-
uint8_t CheerLights::getGreen() {
170+
uint8_t CheerLights::showGreen() {
113171
return (_colorHex >> 8) & 0xFF;
114172
}
115173

116-
uint8_t CheerLights::getBlue() {
174+
uint8_t CheerLights::showBlue() {
117175
return _colorHex & 0xFF;
118176
}

0 commit comments

Comments
 (0)