Skip to content

Commit 06ec575

Browse files
committed
Fixed string issues, replaced with char
1 parent a1c56a9 commit 06ec575

File tree

4 files changed

+61
-49
lines changed

4 files changed

+61
-49
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +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-
- `getCurrentColor()`: Get the current CheerLights color from ThingSpeak channel 1417.
25-
- `currentColorName()`: The current CheerLights color name (e.g. "red").
26-
- `currentColorHex()`: The current CheerLights color as a hex value (e.g. 0xFF0000).
27-
- `currentRed()`, `currentGreen()`, `currentBlue()`: The RGB values for the current CheerLights color (e.g. 255, 0, 0).
24+
- `getCurrentColor()`: Get the current CheerLights color from ThingSpeak channel 1417. Returns a pointer to a constant char array.
25+
- `currentColorName()`: The current CheerLights color name (e.g. "red"). Returns a pointer to a constant char array.
26+
- `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.

library.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
name=CheerLights
2-
version=1.0.2
2+
version=1.0.3
3+
includes=CheerLights.h
34
author=Hans Scharler <hscharler@gmail.com>
45
maintainer=Hans Scharler <hscharler@gmail.com>
56
sentence=Fetch and use the latest CheerLights color.

src/CheerLights.cpp

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
#define MIN_UPDATE_INTERVAL 5000
44
#define TIMEOUT 5000
5+
#define BUFFER_SIZE 128
56

67
CheerLights::CheerLights() {
7-
_colorName = "black";
8+
strcpy(_colorName, "black");
89
_colorHex = 0x000000;
910
}
1011

@@ -22,25 +23,11 @@ void CheerLights::begin(const char* ssid, const char* password) {
2223
void CheerLights::_connectToWiFi() {
2324
Serial.print("Connecting to WiFi");
2425

25-
#if defined(ESP8266) || defined(ESP32)
26-
WiFi.begin(_ssid, _password);
27-
while (WiFi.status() != WL_CONNECTED) {
28-
delay(500);
29-
Serial.print(".");
30-
}
31-
#elif defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_ARCH_SAMD)
32-
while (WiFi.begin(_ssid, _password) != WL_CONNECTED) {
33-
delay(5000);
34-
Serial.print(".");
35-
}
36-
#else
37-
// Default WiFi connection method
38-
WiFi.begin(_ssid, _password);
39-
while (WiFi.status() != WL_CONNECTED) {
40-
delay(500);
41-
Serial.print(".");
42-
}
43-
#endif
26+
WiFi.begin(_ssid, _password);
27+
while (WiFi.status() != WL_CONNECTED) {
28+
delay(500);
29+
Serial.print(".");
30+
}
4431

4532
Serial.println("\nConnected to WiFi");
4633
}
@@ -76,10 +63,12 @@ void CheerLights::_fetchColor() {
7663
return;
7764
}
7865

79-
// Create the HTTP GET request
80-
client.print(String("GET ") + apiPath + " HTTP/1.1\r\n" +
81-
"Host: " + host + "\r\n" +
82-
"Connection: close\r\n\r\n");
66+
// Create the HTTP GET request without using String
67+
client.print("GET ");
68+
client.print(apiPath);
69+
client.print(" HTTP/1.1\r\nHost: ");
70+
client.print(host);
71+
client.print("\r\nConnection: close\r\n\r\n");
8372

8473
// Wait for response
8574
unsigned long timeout = millis();
@@ -93,32 +82,34 @@ void CheerLights::_fetchColor() {
9382

9483
// Read the response
9584
bool headersEnd = false;
96-
while (client.connected()) {
97-
String line = client.readStringUntil('\n');
98-
line.trim();
85+
char line[BUFFER_SIZE];
86+
while (client.connected() || client.available()) {
87+
int len = client.readBytesUntil('\n', line, sizeof(line) - 1);
88+
line[len] = 0;
89+
90+
char* start = line;
91+
while (*start == ' ' || *start == '\r') start++;
92+
char* end = line + strlen(line) - 1;
93+
while (end > start && (*end == ' ' || *end == '\r')) *end-- = 0;
9994

10095
if (!headersEnd) {
101-
if (line.length() == 0) {
96+
if (strlen(start) == 0) {
10297
headersEnd = true;
10398
}
10499
continue;
105100
}
106101

107-
if (line.length() > 0) {
108-
_colorName = line;
102+
if (strlen(start) > 0) {
103+
strncpy(_colorName, start, sizeof(_colorName) - 1);
104+
_colorName[sizeof(_colorName) - 1] = 0;
109105
break;
110106
}
111107
}
112108

113-
// Read any remaining data
114-
while (client.available()) {
115-
client.read();
116-
}
117-
118109
client.stop();
119110

120-
_colorName.trim();
121-
_colorName.toLowerCase();
111+
// Convert color name to lowercase
112+
for (char* p = _colorName; *p; ++p) *p = tolower(*p);
122113

123114
// Map the color name to a hex value
124115
static const struct {
@@ -142,20 +133,19 @@ void CheerLights::_fetchColor() {
142133

143134
_colorHex = 0x000000; // Default to black
144135
for (const auto& color : colorMap) {
145-
if (_colorName.equalsIgnoreCase(color.name)) {
136+
if (strcasecmp(_colorName, color.name) == 0) {
146137
_colorHex = color.color;
147138
break;
148139
}
149140
}
150-
151141
}
152142

153-
String CheerLights::getCurrentColor() {
143+
const char* CheerLights::getCurrentColor() {
154144
_fetchColor();
155145
return _colorName;
156146
}
157147

158-
String CheerLights::currentColorName() {
148+
const char* CheerLights::currentColorName() {
159149
return _colorName;
160150
}
161151

src/CheerLights.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,33 @@
44
#include <Arduino.h>
55
#include <WiFiClient.h>
66

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+
728
class CheerLights {
829
public:
930
CheerLights();
1031
void begin(const char* ssid, const char* password);
11-
String getCurrentColor();
12-
String currentColorName();
32+
const char* getCurrentColor();
33+
const char* currentColorName();
1334
uint32_t currentColorHex();
1435
uint8_t currentRed();
1536
uint8_t currentGreen();
@@ -20,7 +41,7 @@ class CheerLights {
2041
void _fetchColor();
2142
const char* _ssid;
2243
const char* _password;
23-
String _colorName;
44+
const char* _colorName;
2445
uint32_t _colorHex;
2546
};
2647

0 commit comments

Comments
 (0)