Repository navigation
Expand file tree
/
Copy pathHackThisFall_Final.ino
More file actions
185 lines (162 loc) · 4.36 KB
/
Copy pathHackThisFall_Final.ino
File metadata and controls
185 lines (162 loc) · 4.36 KB
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
# include <TinyGPS++.h>
# include <HardwareSerial.h>
# include <LiquidCrystal_I2C.h>
# include <OneWire.h>
# include <DallasTemperature.h>
# include <Wire.h>
# include <MAX30100_PulseOximeter.h>
# include <ArduinoJson.h>
# include <WiFi.h>
# include <PubSubClient.h>
//MQTT
const char* ssid = "Galaxy";
const char* password = "Trinetra";
const char* mqttServer = "ip";
const int mqttPort = 1883;
const char* mqttUser = "test";
const char* mqttPassword = "passwd";
const char* mqttTopic = "trial/esp";5
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("Message arrived in topic: " + String(topic));
Serial.println("Message: " + String((char*)payload));}
//GPS PINS
int RXPin = 16;
int TXPin = 17;
int GPSBaud = 9600;
TinyGPSPlus gps;
HardwareSerial gpsSerial(2);
//Temperature
const int oneWireBus = 4;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16,2);
JsonDocument root;
void setup() {
Serial.begin(115200); // For debugging purposes
gpsSerial.begin(GPSBaud, SERIAL_8N1 , RXPin,TXPin);
lcd.init();
lcd.backlight();
sensors.begin();
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Connect to MQTT broker
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
pinMode(19,INPUT);
}
void loop() {
// Read data from the GPS module
String jsonStr;
if (!client.connected()) {
reconnect();
}
while (gpsSerial.available() > 0)
if (gps.encode(gpsSerial.read()))
{
gpsDisplayInfo();
root["latitude"] = gps.location.lat();
root["longitude"] = gps.location.lng();
root["altitude"] = gps.altitude.meters();
root["hour"] = gps.time.hour();
root["minute"] = gps.time.minute();
root["second"] = gps.time.second();
root["day"] = gps.date.day();
root["month"] = gps.date.month();
root["year"] = gps.date.year();
root["temperature"] = temp();
if (digitalRead(19) != 0){
root["bpm"] = random(85,90);
root["spO2"] = random(98,100);
}
else{
root["bpm"] = random(55,60);
root["spO2"] = random(70,80);
}
serializeJson(root, jsonStr);
Serial.println(jsonStr);
client.loop();
// Publish a string message to the MQTT topic
client.publish(mqttTopic, jsonStr.c_str());
// Wait for a while before publishing again
delay(1000);
}
if (millis() > 1000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
while(true);
}
}
char gpsDisplayInfo()
{
if (gps.location.isValid())
{
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 6);
Serial.print("Altitude: ");
Serial.println(gps.altitude.meters());
}
else
{
Serial.println("Location: Not Available");
}
Serial.print("Date: ");
if (gps.date.isValid())
{
Serial.print(gps.date.month());
Serial.print("/");
Serial.print(gps.date.day());
Serial.print("/");
Serial.println(gps.date.year());
}
else
{
Serial.println("Not Available");
}
Serial.print("Time: ");
if (gps.time.isValid())
{
if (gps.time.hour() < 10) Serial.print(F("0"));
Serial.print(gps.time.hour());
Serial.print(":");
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(":");
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(".");
if (gps.time.centisecond() < 10) Serial.print(F("0"));
Serial.println(gps.time.centisecond());
}
else
{
Serial.println("Not Available");
}
Serial.println();
Serial.println();
delay(1000);
char data = gpsSerial.read();
return data;
}
float temp(){
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
return temperatureC;
}
void reconnect() {
while (!client.connected()) {
Serial.println("Connecting to MQTT broker...");
if (client.connect("ESP32Client", mqttUser, mqttPassword)) {
Serial.println("Connected to MQTT broker");
} else {
Serial.println("Connection failed, retrying in 5 seconds...");
delay(5000);
}
}
}