-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathESP32_Heart_Rate_MQTT.ino
329 lines (288 loc) · 9.08 KB
/
ESP32_Heart_Rate_MQTT.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
/* Heart beat monitor for MQTT server
Heart beat rate is sent to MQTT server every 15sec by default.
I recomment to use ThingsBoard to visualize heart beat rate vs co2 level
or temperature or air pressure.
Author: coniferconifer
This program is based on the art by pcbreflux ESP32_AD8232.ino at
https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_AD8232
to get heart beat rate per min.
Source codes enclosed by #ifdef MQTT is coded by coniferconifer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define MQTT //indicates coded by coniferconifer
#ifdef MQTT
//#define VERBOSE
#define LOFFPLUS GPIO_NUM_16 // Setup for leads off detection LO +
#define LOFFMINUS GPIO_NUM_17 //Setup for leads off detection LO -
#define ANALOGIN GPIO_NUM_34 //ADC for AD8232
#define VERSION "20180512"
#include <WiFi.h>
#include <PubSubClient.h>
#include "credentials.h"
char* ssidArray[] = { WIFI_SSID , WIFI_SSID1, WIFI_SSID2};
char* passwordArray[] = {WIFI_PASS, WIFI_PASS1, WIFI_PASS2};
char* tokenArray[] = { TOKEN , TOKEN1, TOKEN2};
char* serverArray[] = {SERVER, SERVER1, SERVER2};
#define MQTTRETRY 1
#define DEVICE_TYPE "ESP32" //
String clientId = DEVICE_TYPE ;
char topic[] = "v1/devices/me/telemetry"; //for Thingsboard
#define NTP1 "time.google.com"
#define NTP2 "ntp.nict.jp"
#define NTP3 "ntp.jst.mfeed.ad.jp"
#define MQTTPORT 1883 //for Thingsboard or MQTT server
#define WARMUPTIME 10000 // 10sec
WiFiClient wifiClient;
PubSubClient client(serverArray[0], MQTTPORT, wifiClient);
// index of WiFi access point
int AP = -1;
#define TIMEZONE 9 //in Japan
#define MAX_TRY 15
int initWiFi() {
int i ;
int numaccesspt = (sizeof(ssidArray) / sizeof((ssidArray)[0]));
Serial.print("Number of Access Point = "); Serial.println(numaccesspt);
for (i = 0; i < numaccesspt; i++) {
Serial.print("WiFi connecting to "); Serial.println(ssidArray[i]);
WiFi.mode(WIFI_OFF);
WiFi.begin(ssidArray[i], passwordArray[i]);
int j;
for (j = 0; j < MAX_TRY; j++) {
if (WiFi.status() == WL_CONNECTED) {
int rssi = WiFi.RSSI();
Serial.printf("RSSI= %d\n", rssi);
configTime(TIMEZONE * 3600L, 0, NTP1, NTP2, NTP3);
Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP());
return (i);
}
delay(500);
Serial.print(".");
}
Serial.println("can not connect to WiFi AP");
}
return (-1);
}
int initWiFi_retry() {
#ifdef VERBOSE
Serial.print("initWiFi_retry() WiFi connecting to "); Serial.println(ssidArray[AP]);
#endif
// Serial.print(" "); Serial.print(passwordArray[AP]);
WiFi.mode(WIFI_OFF);
WiFi.begin(ssidArray[AP], passwordArray[AP]);
int j;
for (j = 0; j < MAX_TRY; j++) {
if (WiFi.status() == WL_CONNECTED) {
#ifdef VERBOSE
Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP());
#endif
configTime(TIMEZONE * 3600L, 0, "time.google.com", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
return (AP);
}
delay(500);
#ifdef VERBOSE
Serial.print(".");
#endif
}
#ifdef VERBOSE
Serial.println(" can not connect to WiFi AP");
#endif
return (-1);
}
boolean mqttflag = false;
void publishToMQTT(float beatspermin) {
// WiFi mode
String payload = "{";
payload += "\"beatspermin\":"; payload += beatspermin ;
payload += "}";
// dont send too long string to MQTT server
// max 128byte
#ifdef VERBOSE
Serial.print("Sending payload: "); Serial.println(payload);
Serial.print("AP = "); Serial.println(AP);
#endif
#ifdef VERBOSE
Serial.print("Reconnecting client to "); Serial.println(serverArray[AP]);
#endif
int mqttloop = 0;
while (1) { // for thingsboard MQTT server
mqttflag = client.connect(clientId.c_str(), tokenArray[AP], NULL);
if (mqttflag == true) break;
Serial.print("-"); delay(500);
mqttloop++;
if (mqttloop > MQTTRETRY) { //there may be something wrong
mqttflag = false;
initWiFi_retry();
break;
}
}
if (mqttflag == true) {
if (millis() > WARMUPTIME) {
if (client.publish(topic, (char*) payload.c_str())) {
#ifdef VERBOSE
Serial.println("Publish ok");
#endif
} else {
#ifdef VERBOSE
Serial.println("Publish failed");
#endif
}
} // WARMUP decision end
} else {
#ifdef VERBOSE
Serial.println("unable to connect to MQTT server");
#endif
}
#ifdef WIFI_POWERSAVE
#ifdef VERBOSE
Serial.println("WiFi turn off");
#endif
WiFi.mode(WIFI_OFF);
#endif
}
void setup() {
Serial.begin(115200);
while (!Serial) {
; // for Leonardo
}
Serial.print("ESP32_Heart_Rate_MQTT: ");
Serial.println( VERSION );
pinMode(LOFFPLUS, INPUT); // Setup for leads off detection LO +
pinMode(LOFFMINUS, INPUT); // Setup for leads off detection LO -
uint64_t chipid;
chipid = ESP.getEfuseMac();
clientId += "-";
clientId += String((uint32_t)chipid, HEX);
Serial.println("clientId :" + clientId);
AP = initWiFi();
if ( AP != -1) { // found WiFi AP
client.setClient(wifiClient);
client.setServer(serverArray[AP], MQTTPORT); // MQTT server for NodeRED or MQTT by Thingsboarxd
}
}
#endif // endof #ifdef MQTT
// heart beat monitor is copied from
// https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_AD8232
// Thanks for sharing
/* Copyright (c) 2017 pcbreflux. All Rights Reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MQTT
#define PLOTT_DATA
// comment out by coniferconifer
#endif
#define MAX_BUFFER 100
uint32_t prevData[MAX_BUFFER];
uint32_t sumData = 0;
uint32_t maxData = 0;
uint32_t avgData = 0;
uint32_t roundrobin = 0;
uint32_t countData = 0;
uint32_t period = 0;
uint32_t lastperiod = 0;
uint32_t millistimer = millis();
float frequency;
float beatspermin = 0;
uint32_t newData;
/*
This is just a homebrew function.
Don't take this for critical measurements !!!
Do your own research on frequencydetection for arbitrary waveforms.
*/
void freqDetec() {
if (countData == MAX_BUFFER) {
if (prevData[roundrobin] < avgData * 1.5 && newData >= avgData * 1.5) { // increasing and crossing last midpoint
period = millis() - millistimer; //get period from current timer value
millistimer = millis();//reset timer
maxData = 0;
}
}
roundrobin++;
if (roundrobin >= MAX_BUFFER) {
roundrobin = 0;
}
if (countData < MAX_BUFFER) {
countData++;
sumData += newData;
} else {
sumData += newData - prevData[roundrobin];
}
avgData = sumData / countData;
if (newData > maxData) {
maxData = newData;
}
/* Ask your Ask your cardiologist
how to place the electrodes and read the data!
*/
#ifdef PLOTT_DATA
Serial.print(newData);
Serial.print("\t");
Serial.print(avgData);
Serial.print("\t");
Serial.print(avgData * 1.5);
Serial.print("\t");
Serial.print(maxData);
Serial.print("\t");
Serial.println(beatspermin);
#endif
prevData[roundrobin] = newData;//store previous value
}
#ifdef MQTT
#define TICK 5 //in msec
#define REPORTTICK 15000 // in msec
long int loopcount = 0;
#endif
void loop() {
newData = analogRead(34);
freqDetec();
if (period != lastperiod) {
frequency = 1000 / (double)period; //timer rate/period
if (frequency * 60 > 20 && frequency * 60 < 120) { // supress unrealistic Data
beatspermin = frequency * 60;
#ifndef MQTT
#ifndef PLOTT_DATA
Serial.print(frequency);
Serial.print(" hz");
Serial.print(" ");
Serial.print(beatspermin);
Serial.println(" bpm");
#endif
#endif
lastperiod = period;
}
}
#ifndef MQTT
delay(5);
#endif
#ifdef MQTT
delay(TICK);
if ((loopcount % (REPORTTICK / TICK) ) == 0) {
if ( beatspermin > 50 && beatspermin < 120) { // supress unrealistic Data
publishToMQTT( beatspermin );
}
// print date and time , heart beat rate
struct tm timeInfo;
getLocalTime(&timeInfo);
Serial.printf("Date: %04d/%02d/%02d %02d:%02d:%02d , ",
timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec);
Serial.printf("%5.1f \n\r", beatspermin);
}
loopcount++;
#endif
}