-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP2.ino
207 lines (157 loc) · 5.74 KB
/
ESP2.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
#include <HTTPClient.h>
#include <Wire.h>
#include "max32664.h"
#include <PubSubClient.h>
#include <WiFi.h>
#include <ThingSpeak.h>
#define mqttport 1883
char ssid[] = "Sonam gupta bewafa he";
char password[] = "mujhekyametohhuhinhi";
const char* server = "mqtt3.thingspeak.com";
char mqttUserName[] = "FCoPExU9MAUQISkGCBEDCz0";
char mqttClientID[] = "FCoPExU9MAUQISkGCBEDCz0";
char mqttpass[] = "OwEpA4RuSjFFfZNzg6fGoBXa";
int writeChannelID = 2151521;
char writeAPIKey[] = "XVEHO8NYR0DRP5KQ";
WiFiClient client;
PubSubClient mqttClient(server, mqttport, client);
#define CSE_IP "192.168.151.251"
#define CSE_PORT 5089
#define OM2M_ORGIN "admin:admin"
#define OM2M_MN "/~/in-cse/in-name/"
#define OM2M_AE "AE-TEST"
#define OM2M_DATA_CONT "User-Patterns"
#define RESET_PIN 4
#define MFIO_PIN 5
// SCL 22
// SDA 21
#define RAWDATA_BUFFLEN 250
max32664 MAX32664(RESET_PIN, MFIO_PIN, RAWDATA_BUFFLEN);
HTTPClient http;
void mfioInterruptHndlr(){
//Serial.println("i");
}
void enableInterruptPin(){
//pinMode(mfioPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(MAX32664.mfioPin), mfioInterruptHndlr, FALLING);
}
void loadAlgomodeParameters(){
algomodeInitialiser algoParameters;
/* Replace the predefined values with the calibration values taken with a reference spo2 device in a controlled environt.
Please have a look here for more information, https://pdfserv.maximintegrated.com/en/an/an6921-measuring-blood-pressure-MAX32664D.pdf
https://github.com/Protocentral/protocentral-pulse-express/blob/master/docs/SpO2-Measurement-Maxim-MAX32664-Sensor-Hub.pdf
*/
algoParameters.calibValSys[0] = 120;
algoParameters.calibValSys[1] = 122;
algoParameters.calibValSys[2] = 125;
algoParameters.calibValDia[0] = 80;
algoParameters.calibValDia[1] = 81;
algoParameters.calibValDia[2] = 82;
algoParameters.spo2CalibCoefA = 1.5958422;
algoParameters.spo2CalibCoefB = -34.659664;
algoParameters.spo2CalibCoefC = 112.68987;
MAX32664.loadAlgorithmParameters(&algoParameters);
}
#define flexSensorpin 32
#define sound_pin 33
void setup(){
Serial.begin(115200);
pinMode(flexSensorpin,INPUT);
pinMode(sound_pin,INPUT);
// pinMode(piezoelectric_sensor,INPUT);
// pinMode(emg_pin,INPUT);
WiFi.begin(ssid ,password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("Local IP address: ");
Serial.println(WiFi.localIP());
mqttClient.setServer(server, mqttport);
}
void mqttConnect(){
while (!mqttClient.connected()) {
Serial.println("Connecting to MQTT...");
if (mqttClient.connect(mqttClientID, mqttUserName, mqttpass )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(mqttClient.state());
delay(2000);
}
}
}
void mqttPublish(long writeChannelID, char* pubWriteAPIKey,String dataString){
// int index = 0;
// String dataString = "";
// while(fieldArray[index] == 1){
// dataString = "field1="+String(hr)+"&field2="+String(systole)+"&field3="+String(diastole)+"&field4="+String(spo2);
// index++;
// Serial.println(dataString);
String topicString = "channels/" + String( writeChannelID ) + "/publish";
// Serial.println(topicString);
mqttClient.publish(topicString.c_str(), dataString.c_str());
// Serial.println(writeChannelID);
}
void publish_to_om2m(String data,String label)
{
// String label = "Label";
// String data;
String server = "http://" + String() + CSE_IP + ":" + String() + CSE_PORT + String() + OM2M_MN + String() + OM2M_AE + "/" + String() + OM2M_DATA_CONT + "/";
if (label=="Label-1")
server += "Flex_Sensor";
else if(label=="Label-4")
server += "Microphone";
Serial.println(server);
http.begin(server);
http.addHeader("X-M2M-Origin", OM2M_ORGIN);
http.addHeader("Content-Type", "application/json;ty=4");
// http.addHeader("Content-Length", "100");
// data = "[" + String(systole) + ", " + String(diastole) + ", " + String(hr) + ", " + String(spo2) + "]";
String req_data = String() + "{\"m2m:cin\": {"
+
"\"con\": \"" + "[" + data + "]" + "\","
+
"\"lbl\": \"" + label + "\""
// + "\"rn\": \"" + "Entry "+String(i++) + "\","
// +
// "\"cnf\": \"text\""
+
"}}";
Serial.println(req_data);
int code = http.POST(req_data);
http.end();
Serial.println(code);
}
void loop(){
// Serial.println("start in estimation mode");
float VCC = 4.98; // Measured voltage of Ardunio 5V line
float R_DIV = 11000.0; // Measured resistance of 3.3k resistor
// Upload the code, then try to adjust these values to more
// accurately calculate bend degree.
float STRAIGHT_RESISTANCE = 38600.0; // resistance when straight
float BEND_RESISTANCE = 108000.0; // resistance at 90 deg
int flexADC = analogRead(flexSensorpin);
float flexV = flexADC * VCC / 1023.0;
float flexR = R_DIV * (VCC / flexV - 1.0);
// Serial.println("Resistance: " + String(flexR) + " ohms");
// Use the calculated resistance to estimate the sensor's
// bend angle:
// float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE, 0, 90.0);
// Serial.println("Bend: " + String(angle) + " degrees");
// Serial.println();
int flex_sensor_value=analogRead(flexSensorpin);
// Serial.print("Flex Sensor value: ");
// Serial.println(angle);
// Serial.println(flex_sensor_value);
int sound_value=analogRead(sound_pin);
// Serial.print("Electronic Microphone Value: ");
Serial.println(sound_value);
String sound_publish=String(sound_value);
String flex_to_publish=String(flex_sensor_value);
publish_to_om2m(sound_publish,"Label-4");
publish_to_om2m(flex_to_publish,"Label-1");
// publish to mqtt
delay(50);
}