#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include "DHTesp.h"
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "broker.hivemq.com"; // Update with your MQTT broker address
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
Adafruit_MPU6050 mpu;
const int DHT_PIN = 15;
DHTesp dhtSensor;
int analogPin = 34; // Analog pin on ESP32
float val = 3;
// Pins for SOS button and LED light
const int buttonPin = 14; // Change this to the desired pin for the button
const int ledPin = 13; // Change this to the desired pin for the LED light
bool sosActive = false; // State of the SOS
bool lastButtonState = HIGH; // Assume the button is not pressed
// Pin and variable for pressure sensor
int pressurePin = 36;
int pressureValue;
// Pins for GPS potentiometers
const int latitudePotPin = 35; // Change to actual analog pin for latitude
const int longitudePotPin = 32; // Change to actual analog pin for longitude
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
// Handle incoming messages (currently not used)
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32Client")) {
Serial.println("connected");
client.subscribe("esp32/data");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
while (!Serial) delay(20);
setup_wifi(); // Connect to WiFi
Wire.begin();
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
client.setServer(mqtt_server, 1883); // Correct port for MQTT
client.setCallback(callback);
// Setup button and LED pin modes
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
// Setup potentiometer pins
pinMode(latitudePotPin, INPUT);
pinMode(longitudePotPin, INPUT);
}
void publishData() {
sensors_event_t acc, gyr, temp;
mpu.getEvent(&acc, &gyr, &temp);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
val = analogRead(analogPin);
val = val * 20.0 / 4095.0;
// Read the analog value from the potentiometer
pressureValue = analogRead(pressurePin);
// Convert the analog value to a pressure reading (simplified example)
int pressurePsi = map(pressureValue, 0, 1023, 0, 100); // Map the analog value to a pressure range (0-100 psi)
// Read potentiometer values for fake GPS coordinates
int latitudeAnalog = analogRead(latitudePotPin);
int longitudeAnalog = analogRead(longitudePotPin);
// Map analog values to a range suitable for GPS coordinates
float fakeLatitude = map(latitudeAnalog, 0, 4095, -90, 90);
float fakeLongitude = map(longitudeAnalog, 0, 4095, -180, 180);
Serial.println("MPU6050 Data:");
Serial.println("Acceleration on x axis: " + String(acc.acceleration.x));
Serial.println("Acceleration on y axis: " + String(acc.acceleration.y));
Serial.println("Acceleration on z axis: " + String(acc.acceleration.z));
Serial.println("Rotation on x axis: " + String(gyr.gyro.x * 180 / PI));
Serial.println();
Serial.println("DHT22 Data:");
Serial.println("Temperature: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println();
Serial.print("Oxygen: ");
Serial.print(val);
Serial.println(" mg/L");
Serial.println();
// Simulated Pressure Data
Serial.print("Pressure (psi): ");
Serial.println(pressurePsi);
Serial.println();
// Simulated GPS Data
Serial.print("Latitude: ");
Serial.println(fakeLatitude);
Serial.print("Longitude: ");
Serial.println(fakeLongitude);
Serial.println();
// Prepare data as a JSON string (or any other format you prefer)
String payload = "{\"accel_x\":" + String(acc.acceleration.x) +
",\"accel_y\":" + String(acc.acceleration.y) +
",\"accel_z\":" + String(acc.acceleration.z) +
",\"gyro_x\":" + String(gyr.gyro.x * 180 / PI) +
",\"temp\":" + String(data.temperature, 2) +
",\"humidity\":" + String(data.humidity, 1) +
",\"oxygen\":" + String(val) +
",\"pressure\":" + String(pressurePsi) +
",\"latitude\":" + String(fakeLatitude, 6) +
",\"longitude\":" + String(fakeLongitude, 6) +
"}";
// Publish data to MQTT topic
client.publish("esp32/data", payload.c_str());
}
void loop() {
if (!client.connected()) {
reconnect(); // Reconnect MQTT if connection is lost
}
client.loop();
unsigned long now = millis();
if (now - lastMsg > 1200) { // Publish every 1.8 seconds
lastMsg = now;
publishData();
}
// Check if the button is pressed
bool buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
sosActive = !sosActive; // Toggle SOS state
// Toggle LED state based on SOS
digitalWrite(ledPin, sosActive ? HIGH : LOW);
// Prepare SOS message
String sosPayload = sosActive ? "{\"sos\":\"activated\"}" : "{\"sos\":\"deactivated\"}";
// Print SOS to the console
Serial.println(sosActive ? "SOS Activated" : "SOS Deactivated");
// Publish SOS message to MQTT
client.publish("esp32/sos", sosPayload.c_str());
}
lastButtonState = buttonState;
}