#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <TinyGPS++.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = ""; // No password needed for Wokwi-GUEST
// ThingSpeak API
const char* server = "http://api.thingspeak.com/update";
String apiKey = "3XKZ8UZ5XOOYXH4W";
// Potentiometer (Pulse Sensor)
const int PulseWire = 36; // Use GPIO 36 (ADC1 channel 0)
// Accelerometer
Adafruit_MPU6050 mpu;
// Temperature Sensor
#define ONE_WIRE_BUS 4 // GPIO 4 for 1-Wire
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// GPS Module
// Use hardware serial port
HardwareSerial ss(2); // Serial2: RX = GPIO16, TX = GPIO17
TinyGPSPlus gps;
void setup() {
Serial.begin(115200);
ss.begin(9600, SERIAL_8N1, 16, 17); // Serial2 with RX = GPIO16, TX = GPIO17
// Connect to WiFi
connectToWiFi();
// Initialize Accelerometer
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
// Initialize Temperature Sensor
sensors.begin();
}
void loop() {
// Simulate GPS Data (fallback if GPS not available)
float latitude = 37.7749; // Example latitude
float longitude = -122.4194; // Example longitude
// Read GPS Data
while (ss.available() > 0) {
gps.encode(ss.read());
}
if (gps.location.isUpdated()) {
latitude = gps.location.lat();
longitude = gps.location.lng();
}
// Read Potentiometer (Pulse Sensor) Data
int potValue = analogRead(PulseWire);
int myBPM = map(potValue, 0, 4095, 0, 150); // Map the potentiometer value to a BPM range
// Read Accelerometer Data
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Read Temperature Data
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
// Print data to Serial Monitor
printSensorData(latitude, longitude, myBPM, temperatureC, a);
// Send data to ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
sendDataToThingSpeak(latitude, longitude, myBPM, temperatureC, a);
} else {
Serial.println("WiFi not connected, retrying...");
connectToWiFi();
}
// Wait before sending the next set of data
delay(20000);
}
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
void printSensorData(float lat, float lon, int bpm, float tempC, sensors_event_t& accel) {
Serial.print("Latitude= ");
Serial.print(lat, 6);
Serial.print(" Longitude= ");
Serial.println(lon, 6);
Serial.print("Heart Rate (BPM): ");
Serial.println(bpm);
Serial.print("Temperature (C): ");
Serial.println(tempC);
Serial.print("Accel X: ");
Serial.print(accel.acceleration.x);
Serial.print(" Accel Y: ");
Serial.print(accel.acceleration.y);
Serial.print(" Accel Z: ");
Serial.println(accel.acceleration.z);
}
void sendDataToThingSpeak(float lat, float lon, int bpm, float tempC, sensors_event_t& accel) {
HTTPClient http;
String url = String(server) + "?api_key=" + apiKey +
"&field1=" + String(lat, 6) +
"&field2=" + String(lon, 6) +
"&field3=" + String(bpm) +
"&field4=" + String(tempC) +
"&field5=" + String(accel.acceleration.x) +
"&field6=" + String(accel.acceleration.y) +
"&field7=" + String(accel.acceleration.z);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.print("Data sent to ThingSpeak, HTTP response code: ");
Serial.println(httpCode);
} else {
Serial.print("Error sending data to ThingSpeak, HTTP response code: ");
Serial.println(httpCode);
}
http.end();
}