-
Notifications
You must be signed in to change notification settings - Fork 1
/
pubSubTest.ino
164 lines (134 loc) · 3.9 KB
/
pubSubTest.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
//new
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "secrets.h"
#include <ArduinoJson.h>
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#define REPORTING_PERIOD_MS 1000
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
float BPM, SpO2, fahrenheit;
PulseOximeter pox;
uint32_t tsLastReport = 0;
int outputpin= A0;
int bufferValue = 1;
// Find this awsEndpoint in the AWS Console: Manage - Things, choose your thing
// choose Interact, its the HTTPS Rest endpoint
const char* awsEndpoint = AWS_IOT_ENDPOINT;
BearSSL::X509List client_crt(AWS_CERT_CRT);
BearSSL::PrivateKey client_key(AWS_CERT_PRIVATE);
BearSSL::X509List rootCert(AWS_CERT_CA);
WiFiClientSecure wiFiClient;
void msgReceived(char* topic, byte* payload, unsigned int len);
PubSubClient pubSubClient(awsEndpoint, 1883, msgReceived, wiFiClient);
void onBeatDetected()
{
Serial.println("Beat Detected!");
}
void setupPox()
{
Serial.print("Initializing pulse oximeter..");
if (!pox.begin()) {
Serial.println("FAILED");
for (;;);
} else {
Serial.println("SUCCESS");
pox.setOnBeatDetectedCallback(onBeatDetected);
}
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
}
void printPox()
{
Serial.print("BPM: ");
Serial.println(BPM);
Serial.print("SpO2: ");
Serial.print(SpO2);
Serial.println("%");
Serial.println("*********************************");
Serial.println();
}
void setup() {
Serial.begin(115200);
pinMode(16, OUTPUT);
Serial.println();
Serial.println("ESP8266 AWS IoT Example");
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
WiFi.waitForConnectResult();
Serial.print(", WiFi connected, IP address: ");
Serial.println(WiFi.localIP());
setupPox();
// get current time, otherwise certificates are flagged as expired
setCurrentTime();
wiFiClient.setClientRSACert(&client_crt, &client_key);
wiFiClient.setTrustAnchors(&rootCert);
}
unsigned long lastPublish;
int msgCount;
void loop() {
pubSubCheckConnect();
pox.update();
BPM = pox.getHeartRate();
SpO2 = pox.getSpO2();
fahrenheit = temperature();
if (millis() - lastPublish > 1000) {
StaticJsonDocument<200> doc;
printPox();
doc["BPM"] = BPM;
doc["SpO2"] = SpO2;
doc["Temprature"] = fahrenheit;
char jsonBuffer[512];
serializeJson(doc, jsonBuffer); // print to client
pubSubClient.publish("outTopic", jsonBuffer);
lastPublish = millis();
}
}
void msgReceived(char* topic, byte* payload, unsigned int length) {
Serial.print("Message received on ");
Serial.print(topic);
Serial.print(": ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void pubSubCheckConnect() {
if ( ! pubSubClient.connected()) {
Serial.print("PubSubClient connecting to: ");
Serial.print(awsEndpoint);
while ( ! pubSubClient.connected()) {
Serial.print(".");
pubSubClient.connect(THINGNAME);
}
Serial.println(" connected");
//pubSubClient.subscribe("inTopic");
}
//pubSubClient.loop();
}
float temperature()
{
int analogValue = analogRead(outputpin);
float millivolts = [(analogValue/1024.0) * 3300]-bufferValue;
float celsius = millivolts/10;
fahrenheit = ((celsius * 9)/5 + 32);
Serial.print(" in Farenheit= ");
Serial.println(fahrenheit);
return fahrenheit;
}
void setCurrentTime() {
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(10);
Serial.print(".");
now = time(nullptr);
}
Serial.println("");
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
}