22
33#define MIN_UPDATE_INTERVAL 5000
44#define TIMEOUT 5000
5+ #define BUFFER_SIZE 128
56
67CheerLights::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) {
2223void 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 (" \n Connected 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\n Host: " );
70+ client.print (host);
71+ client.print (" \r\n Connection: 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
0 commit comments