-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt2oregonv1.ino
282 lines (230 loc) · 7.23 KB
/
mqtt2oregonv1.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
/*
* mqtt2oregonv1 gateway for ESP8266 or ESP32
*
* (c) 2021 Lars Wessels <[email protected]>
*
* Arduino sketch to emulate an Oregon V1 sensor (433 MHz)
* to transmit temperature readings received via mqtt to be
* displayed on an Oregon compatible weather station device.
*
* Published under MIT license.
*
*/
#include <PubSubClient.h>
#include <ArduinoJson.h>
#if defined(ESP32)
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#define MQTT_BROKER "<mqtt-broker-ip>"
#define MQTT_TOPIC "oregonv1"
#define WIFI_SSID "<your-ssid>"
#define WIFI_PASS "<wlan-password>"
// set output pin for 433MHz transmitter (e.g. FS1000A)
#define TX_PIN 4
// macros to set bits in payload based on 8 nibbles (32 bit)
// http://wmrx00.sourceforge.net/Arduino/OregonScientific-RF-Protocols.pdf
#define SET_ADDR(x) (x & 0x07) << 0
#define SET_CH(x) ((x - 1) & 0x03) << 6
#define SET_TEMP(x) (((uint32_t)x / 100) % 10) << 16 | (((uint32_t)x / 10) % 10) << 12 | (x % 10) << 8
#define SET_CRC(x) (uint32_t)x << 24
// struct for Oregon V1 data frame (excl. checksum)
typedef struct {
uint8_t addr;
uint8_t ch;
float temp;
bool lowbat;
} thn128_t;
// setup wifi and mqtt client
WiFiClient espClient;
PubSubClient mqtt(espClient);
// default data update in mqtt callback function
thn128_t sensor_data = {
.addr = 4, // rolling address
.ch = 1, // channels 1,2,3
.temp = 21.0, // temperature in degrees celcius
.lowbat = false, // true if battery is low
};
// (re)connect to wifi
void connect_wifi() {
delay(100);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(F("."));
}
Serial.print(F("IP "));
Serial.print(WiFi.localIP());
Serial.print(F(" (RSSI "));
Serial.print(WiFi.RSSI());
Serial.println(F(" dBm)."));
}
// called if mqtt messages arrive we've previously subscribed to
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
StaticJsonDocument<256> json; // https://arduinojson.org/v6/assistant/
Serial.print(F("Received message on /"));
Serial.print(topic);
Serial.print(" ");
for (uint8_t i = 0; i < length; i++)
Serial.print((char)payload[i]);
Serial.println();
DeserializationError error = deserializeJson(json, payload);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
} else {
sensor_data.ch = json["channel"];
sensor_data.addr = json["address"];
sensor_data.temp = json["temperature"];
sensor_data.lowbat = json["lowbattery"];
}
}
// connect to mqtt broker with random client id and subscribe
// to sensor topic we want to display on 433MHz weather display
void connect_mqtt() {
static char clientid[16];
static uint8_t mqtt_error = 0;
while (!mqtt.connected()) {
// generate pseudo random client id
snprintf(clientid, sizeof(clientid), "THN128_%x", random(0xffff));
Serial.print(F("Connecting to MQTT Broker "));
Serial.print(MQTT_BROKER); Serial.print(F(" as client "));
Serial.print(clientid); Serial.print(F("..."));
if (mqtt.connect(clientid)) {
Serial.println(F("OK."));
mqtt.subscribe(MQTT_TOPIC);
mqtt_error = 0;
} else {
Serial.print(F("failed("));
Serial.print(mqtt.state());
Serial.println(F("), try again in 5 seconds."));
// no mqtt connection within a minute => restart system
if (mqtt_error++ >= 12) {
Serial.println(F("Auto reboot system..."));
delay(3000);
ESP.restart();
}
delay(5000);
}
Serial.println();
}
}
// calculate checksum for Oregon V1
uint8_t calc_crc(uint32_t data) {
uint16_t crc;
crc = ((data >> 16) & 0xFF) + ((data >> 8) & 0xff) + ((data >> 0) & 0xFF);
crc = (crc >> 8) + (crc & 0xFF);
return (uint8_t)crc;
}
// create payload (8 nibbles)
// (1) rolling address
// (1) channel
// (4) data - temperature
// (2) checksum
uint32_t encode_data(thn128_t *data) {
uint32_t payload;
payload = SET_ADDR(data->addr); // rolling address (4bit)
payload |= SET_CH(data->ch); // channel 1,2,3
if (data->temp > -99 && data->temp < 99) {
if (data->temp < 0) {
payload |= (1UL << 21);
payload |= SET_TEMP((int)(data->temp * -10));
} else {
payload |= SET_TEMP((int)(data->temp * 10));
}
}
if (data->lowbat) // set low battery bit
payload |= (1UL << 23);
payload |= SET_CRC(calc_crc(payload)); // add checksum
return payload;
}
// transmit sync pulse
// leading sync off 4200 ms
// sync pulse 5700 ms
// trailing sync off 5200 ms
void tx_sync() {
digitalWrite(TX_PIN, LOW);
delayMicroseconds(4200);
digitalWrite(TX_PIN, HIGH);
delayMicroseconds(5700);
digitalWrite(TX_PIN, LOW);
delayMicroseconds(5200);
}
// send manchester bit pulse (342 Hz = 2923 micros)
void tx_bit(bool bit) {
digitalWrite(TX_PIN, bit);
delayMicroseconds(1450);
digitalWrite(TX_PIN, !bit);
delayMicroseconds(1475);
}
// send 12 bit preamble
void tx_preamble() {
for (uint8_t i = 0; i < 12; i++) {
delayMicroseconds(1250);
digitalWrite(TX_PIN, HIGH);
delayMicroseconds(1750);
digitalWrite(TX_PIN, LOW);
}
delayMicroseconds(3000);
}
// encode and send 32 bit payload
void tx_data(uint32_t payload) {
for (uint8_t i = 0; i < 32; i++) {
if (payload & (1UL << i))
tx_bit(true);
else
tx_bit(false);
}
}
// transmit data twice including preamble and sync frame
void send_data(thn128_t *data) {
Serial.print(F("Transmit RF433 "));
Serial.printf("(Ch: %d, Addr: %d, Temp: ", data->ch, data->addr);
Serial.print(data->temp);
Serial.println("°C)...");
for (uint8_t i = 0; i < 2; i++) {
tx_preamble();
tx_sync();
tx_data(encode_data(data));
digitalWrite(TX_PIN, LOW);
delay(100);
}
}
void setup() {
pinMode(TX_PIN, OUTPUT);
Serial.begin(115200);
delay(250);
Serial.println();
Serial.print(F("Starting mqtt2oregonv1 gateway..."));
Serial.println();
Serial.print(F("Connecting to SSID "));
Serial.print(WIFI_SSID);
WiFi.mode(WIFI_STA);
connect_wifi();
mqtt.setServer(MQTT_BROKER, 1883);
}
void loop() {
static uint32_t lastLoopRun = 0;
static uint32_t loopSecs = 0;
if (WiFi.status() == WL_CONNECTED) { // check wifi connection status
if (!mqtt.connected())
connect_mqtt();
mqtt.setCallback(mqtt_callback);
mqtt.loop();
} else {
Serial.print(F("Reconnecting to SSID "));
Serial.print(WIFI_SSID);
connect_wifi();
}
if ((millis() - lastLoopRun) >= 1000) {
lastLoopRun = millis();
if ((loopSecs % 30) == 0) { // transmit data every 30 secs.
send_data(&sensor_data);
//sensor_data.temp += 1; // for debugging
} else {
Serial.printf("Waiting %d secs for next RF433 TX...\n", (loopSecs % 30));
}
loopSecs++;
}
}