@@ -10,23 +10,31 @@ CheerLights::CheerLights() {
1010void 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