-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP8266-Climate-Display-PubSub.ino
304 lines (273 loc) · 7.98 KB
/
ESP8266-Climate-Display-PubSub.ino
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* This sketch means to be run on a WeMos D1 Mini with OLED Shield
*
* It connects to the topics from the mqtt broker used in
* http://bitluni.net/solar-powered-weather-station/
* (i used voltage instad of light sensor, so this display shows voltage)
*
* As i have multiple WiFi AccessPoints around the house, i set three here in this
* sketch. You can remove two of them or integrate the WiFiManger library.
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <PubSubClient.h>
// OLED specific
#define OLED_RESET 0 // GPIO0
Adafruit_SSD1306 display(OLED_RESET);
/************************* CONFIG *****************************************************/
// MQTT broker settings
// IP address of the mqtt broker
#define HOST "xxx.xxx.xxx.xxx"
#define PORT 1883
// credentials needed for connection
#define USERNAME "username"
#define PASSWORD "password"
// Topics, change if yours are different
#define TOPIC_TEMPERATURE "weatherStation/temperature"
#define TOPIC_VOLTAGE "weatherStation/voltage"
#define TOPIC_HUMIDITY "weatherStation/humidity"
#define TOPIC_BAROMETRIC "weatherStation/barometric"
// Multi WiFi
const char* ssid1 = "SSID_1";
const char* ssid2 = "SSID_2";
const char* ssid3 = "SSID_3";
const char* password1 = "PASSWORD_1";
const char* password2 = "PASSWORD_2";
const char* password3 = "PASSWORD_3";
/************************* CONFIG END *************************************************/
// Multiple WiFi connections
ESP8266WiFiMulti multi;
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
PubSubClient mqtt(client);
double currentTemp = 0.0;
double currentVoltage = 0.0;
double currentHum = 0.0;
double currentBaro = 0.0;
const int timeout = 5;
// Define some display states
int state = 0;
#define STATE_TEMP 0
#define STATE_HUM 1
#define STATE_BARO 2
// how long to wait until switching state
#define WAIT_SECONDS 5
// millis of last switch
long lastSwitch = 0;
// ICONS
const unsigned char temp_icon [] = {
0x08, 0x00, 0x14, 0x00, 0x14, 0x00, 0x14, 0xF8, 0x14, 0x00, 0x14, 0xF8, 0x14, 0x00, 0x14, 0xF8,
0x14, 0x00, 0x14, 0x00, 0x22, 0x00, 0x41, 0x00, 0x41, 0x00, 0x41, 0x00, 0x22, 0x00, 0x1C, 0x00
};
const unsigned char humidity [] = {
0x00, 0x80, 0x01, 0xC0, 0x01, 0xC0, 0x03, 0x60, 0x03, 0x60, 0x06, 0x30, 0x06, 0x30, 0x0E, 0x38,
0x1C, 0x1C, 0x18, 0x0C, 0x30, 0x06, 0x30, 0x06, 0x18, 0x0C, 0x1C, 0x1C, 0x0F, 0xF8, 0x07, 0xF0
};
const unsigned char pressure [] = {
0x00, 0x3C, 0x00, 0x47, 0x00, 0xC1, 0x00, 0x83, 0x01, 0x02, 0x04, 0xFE, 0x08, 0x00, 0x1F, 0xFC,
0x08, 0x00, 0x24, 0x00, 0x40, 0x00, 0xFF, 0xE8, 0x40, 0x04, 0x2F, 0xFE, 0x00, 0x04, 0x00, 0x08
};
void barocallback(double p) {
currentBaro = p;
}
void humcallback(double h) {
currentHum = h;
}
void tempcallback(double t) {
currentTemp = t;
}
void lipocallback(double v) {
currentVoltage = v;
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// we know there will be only double data as payload, so ...
double dValue = atof((char *) payload);
if(strcmp(topic, TOPIC_TEMPERATURE) == 0)
tempcallback(dValue);
else if(strcmp(topic, TOPIC_VOLTAGE) == 0)
lipocallback(dValue);
else if(strcmp(topic, TOPIC_HUMIDITY) == 0)
humcallback(dValue);
else if(strcmp(topic, TOPIC_BAROMETRIC) == 0)
barocallback(dValue);
}
/*
* Draw values on display depending on the current state
*/
void drawValues() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
int decimals = 2;
switch(state) {
case STATE_TEMP:
if((currentTemp >= 0 || currentTemp > -10) && currentTemp < 10)
display.print(" ");
if(currentTemp < -10)
decimals = 1;
display.println(currentTemp, decimals);
display.print(" ");
display.print(char(167));
display.print("C");
display.drawBitmap(0,display.height()-16, temp_icon, 16, 16, WHITE);
break;
case STATE_HUM:
if(currentHum < 10)
display.print(" ");
display.println(currentHum);
display.print(" %");
display.drawBitmap(0,display.height()-16, humidity, 16, 16, WHITE);
break;
case STATE_BARO:
int h = (int) currentBaro / (int)100;
if(h < 10000)
display.print(" ");
if(h < 1000)
display.print(" ");
if(h < 100)
display.print(" ");
if(h < 10)
display.print(" ");
display.println(h);
display.print(" hPa");
display.drawBitmap(0,display.height()-16, pressure, 16, 16, WHITE);
break;
}
display.setCursor(29,41);
display.setTextSize(1);
display.print(currentVoltage);
display.print("V");
display.display();
}
void setup() {
Serial.begin(9600);
multi.addAP(ssid1, password1);
multi.addAP(ssid2, password2);
multi.addAP(ssid3, password3);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 64x48)
// Clear the buffer.
display.clearDisplay();
display.dim(1);
display.setTextWrap(false);
// show "welcome" screen
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(14,0);
display.print("ESP");
display.setCursor(8,17);
display.print("8266");
display.setTextSize(1);
display.setCursor(11,40);
display.print("Climate");
display.display();
delay(2000);
display.clearDisplay();
display.setTextWrap(true);
Serial.println("Connecting Wifi...");
display.setCursor(0,4);
display.print("Connecting Wifi");
display.display();
delay(500);
display.setTextWrap(false);
int fake = 3;
while(fake != 0) {
display.setCursor(23,display.height()-8);
display.setTextColor(BLACK);
display.print("...");
display.display();
delay(400);
display.setTextColor(WHITE);
display.setCursor(23,display.height()-8);
display.print(".");
display.display();
delay(250);
display.print(".");
display.display();
delay(250);
display.print(".");
display.display();
delay(500);
fake--;
}
int c = 0;
while(multi.run() != WL_CONNECTED || c >= timeout) {
Serial.println("next wifi connect try in 1s");
delay(1000);
c++;
}
if(c >= timeout)
while(1); // WDT reset
// remove 3 dots (if there are some)
display.setCursor(23,display.height()-8);
display.setTextColor(BLACK);
display.print("...");
display.setTextColor(WHITE);
display.setCursor(5,display.height()-8);
display.print("connected");
display.display();
delay(500);
Serial.println("");
Serial.println("WiFi connected");
Serial.print("WiFi SSID: ");
Serial.println(WiFi.SSID());
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
mqtt.setServer(HOST, PORT);
mqtt.setCallback(callback);
display.clearDisplay();
display.setCursor(8,15);
display.print("fetching");
display.setCursor(20,24);
display.print("data");
display.display();
delay(1500);
}
void reconnect() {
// Loop until we're reconnected
while (!mqtt.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqtt.connect("ESP8266-OLED", USERNAME, PASSWORD)) {
Serial.println("connected");
// ... and resubscribe
mqtt.subscribe("weatherStationTest/#");
} else {
Serial.print("failed, rc=");
Serial.print(mqtt.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
// make sure we're connected
if (!mqtt.connected()) {
reconnect();
}
mqtt.loop();
// show the values
drawValues();
long now = millis();
if(lastSwitch == 0)
lastSwitch = now;
int waited = now - lastSwitch;
// time to switch display values?
if(waited >= WAIT_SECONDS * 1000) {
lastSwitch = now;
state++;
if(state > 2)
state = 0;
}
}