-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMEGA.ino
216 lines (185 loc) · 5.08 KB
/
MEGA.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
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
//------------------------------------------------------------------------------------------
const char * SSID_NAME = "SSID_NAME";
const char * SSID_PASS = "SSID_PASS";
const char * TOKEN = "BBFF-xxxxxxxxxxxxxxxxxxxxxxxxx";
const char * SERVER = "industrial.api.ubidots.com";
const char * DEVICE_LABEL = "mega2560";
const char * VARIABLE_LABEL1 = "temperature";
const char * VARIABLE_LABEL2 = "humidity";
const char * VARIABLE_LABEL3 = "bpm";
const char * USER_AGENT = "MEGA2560";
const char * VERSION = "1.0";
const int PORT = 9012;
//------------------------------------------------------------------------------------------
const int PulseWire = 1;
const int LED13 = 13;
int Threshold = 550;
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
PulseSensorPlayground pulseSensor;
void setup() {
Serial.begin(115200);
Serial3.begin(115200);
dht.begin();
lcd.backlight();
lcd.init();
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13);
pulseSensor.setThreshold(Threshold);
if (pulseSensor.begin()) {
Serial.println("We created a pulseSensor Object !");
}
Serial3.println("AT");
if (Serial3.find("Error")) {
Serial.println("ERROR!\nAT Commands system does NOT works correctly.");
return;
}
delay(100);
Serial3.println("AT+CWMODE=3");
delay(100);
Serial3.println("AT+CIPMUX=1");
delay(100);
if (Serial3.find("OK")) {
Serial.println("Connecting to ");
Serial.println(SSID_NAME);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Connecting to: ");
lcd.setCursor(0, 1);
lcd.print(SSID_NAME);
delay(3000);
}
String cmd = "AT+CWJAP=\"";
cmd += SSID_NAME;
cmd += "\"\,\"";
cmd += SSID_PASS;
cmd += "\"";
Serial3.println(cmd);
delay(3000);
Serial3.println("AT+CIPSTATUS=?");
delay(2000);
if ( Serial3.find("OK") ) {
Serial.println("\nConnected.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Connected");
delay(1000);
}
else {
Serial.println("ERROR!\nUnable to connect to the selected access point.");
}
delay(1000);
}
void loop() {
Serial.println("\n========================================");
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
{
lcd.init();
lcd.setCursor(1, 0);
lcd.print("Failed to read");
lcd.setCursor(0, 1);
lcd.print("from DHT sensor!");
return;
}
int myBPM = pulseSensor.getBeatsPerMinute();
int BPM = myBPM - 172;
// AT+CIPSTART=4,"TCP","industrial.api.ubidots.com",9012
String atTcpPortConnectCmd = "AT+CIPSTART=4,\"TCP\",\"";
atTcpPortConnectCmd += SERVER;
atTcpPortConnectCmd += "\",9012";
Serial3.println(atTcpPortConnectCmd);
delay(300);
if ( Serial3.find("OK") ) {
Serial.println("connection to ubidots is established");
lcd.setCursor(15, 0);
lcd.print("*");
}
else {
Serial.println("ERROR!\nUnable to connect to ubidots.");
lcd.setCursor(15, 0);
lcd.print("!");
}
Serial.print("\nTemperature: ");
Serial.print(t);
Serial.println("°C");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println("%");
if (BPM < 0) {
Serial.println("No HeartBeat Happened !");
}
else {
Serial.println("♥ A HeartBeat Happened ! ");
Serial.print("BPM: ");
Serial.println(BPM);
}
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.print(t);
lcd.print((char)223);
lcd.print("C ");
lcd.setCursor(0, 1);
lcd.print("Humidity:");
lcd.print(h);
lcd.print("%");
delay(5000);
if (BPM < 0) {
lcd.setCursor(0, 0);
lcd.print(" No HeartBeat");
lcd.setCursor(0, 1);
lcd.print(" Happened ! ");
}
else {
lcd.setCursor(0, 0);
lcd.print("BPM:");
lcd.print(BPM);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
}
// USER_AGENT/VERSION|POST|TOKEN|DEVICE_LABEL=>VARIABLE_LABEL1:t,VARIABLE_LABEL2:h,VARIABLE_LABEL3:BPM|end
String atGETCmd = USER_AGENT;
atGETCmd += "/";
atGETCmd += VERSION;
atGETCmd += "|POST|";
atGETCmd += TOKEN;
atGETCmd += "|";
atGETCmd += DEVICE_LABEL;
atGETCmd += "=>";
atGETCmd += VARIABLE_LABEL1;
atGETCmd += ":";
atGETCmd += t;
atGETCmd += ",";
atGETCmd += VARIABLE_LABEL2;
atGETCmd += ":";
atGETCmd += h;
atGETCmd += ",";
atGETCmd += VARIABLE_LABEL3;
atGETCmd += ":";
atGETCmd += BPM;
atGETCmd += "|end";
// send data length
Serial3.println("AT+CIPSEND=4," + String(atGETCmd.length() + 4));
delay(3000);
Serial3.println(atGETCmd);
delay(2000);
if ( Serial3.find("OK") ) {
Serial.println("\nData Sent.");
lcd.setCursor(15, 0);
lcd.print("^");
}
Serial3.println("AT+CIPCLOSE=4");
delay(200);
if ( Serial3.find("OK") ) {
Serial.println("Disconnected from ubidots");
}
}