// Blynk credentials
#define BLYNK_TEMPLATE_ID "TMPL3VfmECvkW"
#define BLYNK_TEMPLATE_NAME "xyz"
#define BLYNK_DEVICE_NAME "abc"
#define BLYNK_AUTH_TOKEN "pGf8QlTDm9276HLLqW1TCVWVkQTk-AsP"
#include <WiFi.h>
#include <BlynkSimpleEsp32.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 = "";
// Potentiometer (Pulse Sensor)
const int PulseWire = 34; // Use GPIO 34 (ADC1 channel 6)
// Weight Potentiometer
const int WeightPotPin = 35; // Use GPIO 35 (ADC1 channel 7)
// Gas Sensor (MQ-2)
const int GasSensorPin = 32; // Use GPIO 32 (ADC1 channel 4)
// 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);
Serial.println("Setup started...");
// Connect to WiFi
connectToWiFi();
// Connect to Blynk
Serial.println("Connecting to Blynk...");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
Serial.println("Blynk connected...");
// Initialize Accelerometer
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
// Initialize Temperature Sensor
sensors.begin();
}
void loop() {
Serial.println("Loop started...");
Blynk.run();
// Simulate GPS Data (fallback if GPS not available)
Serial.println("Simulating GPS Data...");
float latitude = 37.7740 + (random(0, 1000) / 100000.0); // Random latitude between 37.7740 and 37.7750
float longitude = -122.4200 + (random(0, 1000) / 100000.0); // Random longitude between -122.4200 and -122.4190
// 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
Serial.println("Reading Potentiometer Data...");
int potValue = analogRead(PulseWire);
int myBPM = map(potValue, 0, 4095, 0, 150); // Map the potentiometer value to a BPM range
// Read Weight Potentiometer Data
Serial.println("Reading Weight Potentiometer Data...");
int weightValue = analogRead(WeightPotPin);
float weight = map(weightValue, 0, 4095, 0, 1000); // Map the potentiometer value to a weight range (0-200 kg)
// Read Gas Sensor Data
Serial.println("Reading Gas Sensor Data...");
int gasValue = analogRead(GasSensorPin);
float gasPPM = map(gasValue, 0, 4095, 0, 10000); // Map the analog value to a gas concentration (example range 0-10000 ppm)
// Read Accelerometer Data
Serial.println("Reading Accelerometer Data...");
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Read Temperature Data
Serial.println("Reading Temperature Data...");
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0); // Fixed function call
// Print data to Serial Monitor
Serial.println("Printing Sensor Data...");
printSensorData(latitude, longitude, myBPM, weight, temperatureC, gasPPM, a);
// Send data to Blynk
Serial.println("Sending Data to Blynk...");
sendDataToBlynk(latitude, longitude, myBPM, weight, temperatureC, gasPPM, a);
// 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(100);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
void printSensorData(float lat, float lon, int bpm, float weight, float tempC, float gasPPM, 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("Weight (kg): ");
Serial.println(weight);
Serial.print("Temperature (C): ");
Serial.println(tempC);
Serial.print("Gas Concentration (PPM): ");
Serial.println(gasPPM);
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 sendDataToBlynk(float lat, float lon, int bpm, float weight, float tempC, float gasPPM, sensors_event_t& accel) {
Serial.println("Sending Data...");
Blynk.virtualWrite(V1, lat); // Latitude
Blynk.virtualWrite(V2, lon); // Longitude
Blynk.virtualWrite(V3, bpm); // Heart Rate
Blynk.virtualWrite(V4, tempC); // Temperature
Blynk.virtualWrite(V5, accel.acceleration.x); // Accelerometer X
Blynk.virtualWrite(V6, accel.acceleration.y); // Accelerometer Y
Blynk.virtualWrite(V7, accel.acceleration.z); // Accelerometer Z
Blynk.virtualWrite(V8, weight); // Weight
Blynk.virtualWrite(V9, gasPPM); // Gas Concentration
// Check if data was sent successfully
if (Blynk.connected()) {
Serial.println("Data sent successfully to Blynk.");
} else {
Serial.println("Error sending data to Blynk.");
}
}