-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThingspeakReadMultipleFields_lcd_TimeBacklite_ESP8266
171 lines (149 loc) · 4.62 KB
/
ThingspeakReadMultipleFields_lcd_TimeBacklite_ESP8266
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <ESP8266WiFi.h>
#include "secrets.h" // Contains your WiFi SSID, password, and ThingSpeak channel ID
#include <ThingSpeak.h>
#include <LCDI2C_Multilingual.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h>
// LCD configuration (adjust address and size if needed)
LCDI2C_Generic lcd(0x27, 16, 2);
// ThingSpeak channel details
unsigned long weatherStationChannelNumber = SECRET_CH_ID_WEATHER_STATION;
WiFiClient client;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600); // NTP server and offset (seconds)
bool backlightState = false;
float field[8] = {0}; // Initialize the array
void setup() {
Serial.begin(115200);
while (!Serial);
lcd.init();
lcd.clear();
timeClient.begin();
ThingSpeak.begin(client);
// Connect to WiFi
Serial.print("Connecting to ");
Serial.println(SECRET_SSID);
WiFi.begin(SECRET_SSID, SECRET_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
setTime(epochTime);
time_t currentTime = now();
int currentHour = hour(currentTime);
int currentMinute = minute(currentTime);
// Backlight control based on time (22:30 - 6:15)
bool shouldBacklightBeOn = (currentHour > BACKLIGHT_ON_HOUR || (currentHour == BACKLIGHT_ON_HOUR && currentMinute >= BACKLIGHT_ON_MINUTE)) &&
(currentHour < BACKLIGHT_OFF_HOUR || (currentHour == BACKLIGHT_OFF_HOUR && currentMinute < BACKLIGHT_OFF_MINUTE));
if (shouldBacklightBeOn != backlightState) {
backlightState = shouldBacklightBeOn;
if (backlightState) {
lcd.backlight();
Serial.println("Backlight ON");
} else {
lcd.noBacklight();
Serial.println("Backlight OFF");
}
}
int statusCode = ThingSpeak.readMultipleFields(weatherStationChannelNumber);
if (statusCode == 200) {
// Read all 8 fields
for (int i = 0; i < 8; i++) {
field[i] = ThingSpeak.getFieldAsFloat(i + 1);
}
// Serial output
Serial.println("--------------------");
Serial.println("Temperature: " + String(field[0]) + " °C");
Serial.println("Atmospheric Pressure: " + String(field[1]) + " mBar");
Serial.println("Humidity: " + String(field[2]) + " %");
Serial.println("PM 2.5: " + String(field[3]) + " ug/m3");
Serial.println("Dew Point: " + String(field[4]) + " °C");
Serial.println("Absolute Humidity: " + String(field[5]) + " g/m3");
Serial.println("Sea Level Pressure: " + String(field[6]) + " mBar");
Serial.println("Altitude: " + String(field[7]) + " m");
Serial.println("--------------------");
// Display on LCD
lcd.clear();
lcd.print("T:");
lcd.print(field[0]);
lcd.print("C H:");
lcd.print(field[2]);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("PM:");
lcd.print(field[3]);
lcd.print(" AP:");
lcd.print(field[1]);
// Blink logic
bool wasBacklightOn = backlightState; // Save the current backlight state
if (field4 > 150) {
for (int i = 0; i < 10; i++) {
lcd.noBacklight();
delay(250);
lcd.backlight();
delay(250);
}
} else if (field4 > 100) {
for (int i = 0; i < 5; i++) {
lcd.noBacklight();
delay(400);
lcd.backlight();
delay(400);
}
} else if (field4 > 50) {
for (int i = 0; i < 2; i++) {
lcd.noBacklight();
delay(550);
lcd.backlight();
delay(550);
}
}
// Reset the backlight state to the previous state
if (!wasBacklightOn) {
lcd.noBacklight();
Serial.println("Backlight OFF (restored)");
} else {
lcd.backlight();
Serial.println("Backlight ON (restored)");
}
delay(10000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Alt:");
lcd.print(field8);
lcd.print("m ");
lcd.print("pSea:");
lcd.print(field7);
lcd.print("mbar");
delay(10000);
Serial.println();
// Repeat first display
lcd.clear();
lcd.print("T:");
lcd.print(field1);
lcd.print("C ");
lcd.print("H:");
lcd.print(field3);
lcd.print("% ");
lcd.setCursor(0, 1);
lcd.print("PM:");
lcd.print(field4);
lcd.print(" ");
lcd.print("AP:");
lcd.print(field2);
lcd.print(" ");
} else {
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
}
Serial.println();
delay(150000); // 2.5 min no need to fetch too often
}