-
Notifications
You must be signed in to change notification settings - Fork 2
/
WH5300-weatherstation-ESP8266-MQTT-json.ino
342 lines (314 loc) · 9.59 KB
/
WH5300-weatherstation-ESP8266-MQTT-json.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
Created by Johan Lindström <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
Libraries :
- ESP8266 core for Arduino : https://github.com/esp8266/Arduino
- PubSubClient : https://github.com/knolleary/pubsubclient
- DHT : https://github.com/adafruit/DHT-sensor-library
- ArduinoJson : https://github.com/bblanchon/ArduinoJson
*/
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <ArduinoOTA.h>
#define SERIALDEBUG // Comment this out with a // for the final upload.
#define MQTT_VERSION MQTT_VERSION_3_1_1
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
int port = 23;
// Wifi: SSID and password
const char* host = "v-station-webupdate";
const char* WIFI_SSID = "XXXXXX";
const char* WIFI_PASSWORD = "XXXXX";
// MQTT: ID, server IP, port, username and password
const char* MQTT_CLIENT_ID = "v-station";
const char* MQTT_SERVER_IP = "192.168.XXX.XXX";
const uint16_t MQTT_SERVER_PORT = 1883;
const char* MQTT_USER = "XXXX";
const char* MQTT_PASSWORD = "XXXX";
// MQTT: topic
const char* MQTT_SENSOR_TOPIC = "v-station/sensor1"; //this is where all things except rain is reported
const char* MQTT_SENSOR_TOPIC2 = "v-station/sensor2"; //this is where rain is reported
#ifdef SERIALDEBUG
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x) Telnet.print(x)
#define debugln(x) Telnet.println(x)
#endif
WiFiClient wifiClient;
PubSubClient client(wifiClient);
WiFiServer TelnetServer(port);
WiFiClient Telnet;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long interval = 0;
// Set the interval in times you want to skip measuring.
// The weatherstation reports once every 48sec
int pushButton = 14;
boolean intro=0;
boolean dataBuff[500];
int datasent=0;
byte byteArray[15];
boolean ldataBuff[500]; //received bits buffer
int firstcheckdone=0;
int firstraincheckdone=0;
int lp;
// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
}
void handleTelnet(){
if (TelnetServer.hasClient()){
// client is connected
if (!Telnet || !Telnet.connected()){
if(Telnet) Telnet.stop(); // client disconnected
Telnet = TelnetServer.available(); // ready for new client
} else {
TelnetServer.available().stop(); // have client, block new conections
}
}
if (Telnet && Telnet.connected() && Telnet.available()){
// client input processing
while(Telnet.available())
Serial.write(Telnet.read()); // pass through
// do other stuff with client input here
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
debug("INFO: Ansluter till MQTT servern...");
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
debugln("INFO: ansluten");
} else {
debug("ERROR: gick ej ansluta, rc=");
debug(client.state());
debugln("DEBUG: försöker igen om 5 sekunder");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
TelnetServer.begin();
delay(100);
pinMode(pushButton, INPUT);
delay(10);
debugln();
debugln();
debug("INFO: Ansluter till ");
debugln(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
debug(".");
}
debugln("");
debugln("INFO: WiFi ansluten");
debugln("INFO: IP adress: ");
debugln(WiFi.localIP());
// init the MQTT connection
client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
client.setCallback(callback);
MDNS.begin(host);
httpUpdater.setup(&httpServer);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
}
int old,last;
unsigned long dur;
float temp,wSpeed,wGust,rAcum,rAcumold,rAcumpub;
int aux,id,hum,unk,status,dir;
int p=0,b=0,i=0;
static const char* const windDirections[] = {"N","NE","E","SE","S","SW","W","NW"};
int crc8(boolean *BitString,int nBits)
{
char CRC=0;
int i;
boolean DoInvert;
for (i=0; i<nBits; ++i)
{
DoInvert = (BitString[i] ^ (CRC&0x80)>>7);
if(DoInvert){
CRC=CRC^0x18;
}
CRC=CRC<<1;
if(DoInvert){
CRC=CRC|0x01;
}
}
return(CRC);
}
void decode(unsigned char byteArray[8]){
int type=(byteArray[0]&0xF0)>>4;
switch (type){
case 0x0A: //Weather message
//Getting the data
id=((byteArray[0]&0x0F)<<4)+ ((byteArray[1]&0xF0)>>4);
aux=((byteArray[1]&0x0F)<<8)+ ((byteArray[2]&0xFF));
temp=(aux*0.1)-40;
hum=byteArray[3];
aux=byteArray[4];
wSpeed=(aux/3.6);
aux=byteArray[5];
wGust=(aux/3.6);
unk=(byteArray[6]&0xF0)>>4;
aux=((byteArray[6]&0x0F)<<8)+ ((byteArray[7]&0xFF));
rAcum=(((aux/3)*0.01)*25.4);
status=(byteArray[8]&0xF0)>>4;
dir=(byteArray[8]&0x0F)>>1;
break;
default:
debugln("Unknown message: " + String(type));
intro=1;
p=0;
break;
}
if(firstcheckdone==0){
rAcumold=rAcum;
}
if(firstcheckdone==0 && hum != 0){
firstcheckdone=1;
debugln("First check done");
}
if(firstraincheckdone==1){
rAcumold=rAcum;
firstraincheckdone=0;
}
}
void publishData(float temp,int hum,float wSpeed,float wGust,int dir,int status) {
// create a JSON object
// doc : https://github.com/bblanchon/ArduinoJson/wiki/API%20Reference
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
// INFO: the data must be converted into a string; a problem occurs when using floats...
root["temperature"] = String(temp);
root["humidity"] = String(hum);
root["wind"] = String(wSpeed);
root["windgust"] = String(wGust);
root["winddir"] = String(windDirections[dir]);
root["status"] = String(status);
debugln("Temperatur: " + String(temp) + " ºC");
debugln("Luftfuktighet: " + String(hum) + " %");
debugln("Vindhastighet: " + String(wSpeed) + " m/s");
debugln("Vindbyar: " + String(wGust) + "m/s");
debugln("Status bits: " + String(status));
debugln("Vindriktning: " + String(windDirections[dir]));
char data[200];
root.printTo(data, root.measureLength() + 1);
client.publish(MQTT_SENSOR_TOPIC, data, true);
datasent=1;
debugln("Data sent, taking a pause");
intro=1;
p=0;
debugln("p:" + String(p));
}
void publishDatarain(float rAcumpub) {
// create a JSON object
// doc : https://github.com/bblanchon/ArduinoJson/wiki/API%20Reference
StaticJsonBuffer<200> jsonBuffer2;
JsonObject& root2 = jsonBuffer2.createObject();
// INFO: the data must be converted into a string; a problem occurs when using floats...
root2["rain"] = String(rAcumpub);
debugln("Regn senaste 15minuter: " + String(rAcumpub) + " mm");
char data2[200];
root2.printTo(data2, root2.measureLength() + 1);
client.publish(MQTT_SENSOR_TOPIC2, data2, true);
debugln("First raincheck done:");
}
void loop() {
handleTelnet();
int buttonState = digitalRead(pushButton);
if(datasent == 0){
if (buttonState != old && p < 500) {
if((old==1) && (micros() - dur)<=800){
dataBuff[p++]=1;
intro=0;
}else if(old==1){
dataBuff[p++]=0;
intro=0;
}
old=buttonState;
dur=micros();
}else if(p >= 500){
debugln("To much data, restarting loop!");
p=0;
intro=1;
delay(1000);
return;
}
}
if(datasent == 0 && p >= 80){
if((micros() - dur)>50000 && intro==0){
debugln();
debugln("p:" + String(p));
debugln();
/*for(int i = 0; i < p; i++)
{
debug(" " + String(dataBuff[i]) + " |");
}*/
if(p>255){
debugln("To much data, restarting loop!");
p=0;
intro=1;
return;
}
lp=p;
b=0;
for(i=(p-80);i<lp;i++){
byteArray[b]=byteArray[b]*2+dataBuff[i];
if(((i-lp-80)+1)%8==0) {
b++;
byteArray[b]=0;
}
}
debugln();
if(crc8(&dataBuff[p-80],80)==0){ //CRC OK
unsigned long currentMillis = millis();
if(currentMillis - previousMillis2 >= 880000){
firstraincheckdone=1;
decode(byteArray);
}
else{
decode(byteArray);
}
}
else{
debugln("CRC Error");
p=0;
intro=1;
debugln("p after CRC check reset:" + String(p));
return;
}
if (!client.connected()) {
reconnect();
}
client.loop();
httpServer.handleClient();
unsigned long currentMillis = millis();
if(currentMillis - previousMillis2 >= 900000 && hum != 0) {
previousMillis2 = currentMillis;
rAcumpub=rAcum-rAcumold;
publishDatarain(rAcumpub);
}
if(currentMillis - previousMillis >= 1000 && hum != 0){
previousMillis = currentMillis;
publishData(temp, hum, wSpeed, wGust, dir, status);
}
}
}
unsigned long currentMillis2 = millis();
if(currentMillis2 - previousMillis3 >= interval*48000 + 47500 && datasent == 1) {
debugln("Pause done, lets go again!");
debugln("-------------------------------------------------------");
previousMillis3 = currentMillis2;
datasent=0;
}
}