Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Based Smart Sensor System with Wi-Fi and GPS Integration

View and Edit Project In Cirkit DesignerImage of ESP32-Based Smart Sensor System with Wi-Fi and GPS Integration

Circuit Documentation

Summary

This document provides a detailed overview of a circuit involving an ESP32 microcontroller, various sensors, and modules. The circuit is designed to read data from sensors, control an LED, and send data to a cloud server. The components include an ESP32 microcontroller, an MQ-3 alcohol sensor, an MPU6050 accelerometer and gyroscope, an IR sensor, an LED, and a SIM808 GSM GPS GPRS module.

Component List

ESP32 (30 pin)

MQ-3 Breakout

LED: Two Pin (red)

MPU6050 Accelerometer + Gyroscope (Wokwi Compatible)

IR Sensor

SIM808 GSM GPS GPRS Module

SIM800L GSM Module

Wiring Details

ESP32 (30 pin)

MQ-3 Breakout

LED: Two Pin (red)

MPU6050 Accelerometer + Gyroscope (Wokwi Compatible)

IR Sensor

SIM808 GSM GPS GPRS Module

Documented Code

ESP32 Code

#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <WiFi.h> // For WiFi connection
#include <HTTPClient.h> // For HTTP requests

// WiFi credentials
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";

// Cloud server details
const char* serverName = "https://your-cloud-server.com/api"; // Replace with your cloud API endpoint

// Sensors and pins
const int analogPin = 2;  // MQ-3 AO connected to ESP32 D2
const int digitalPin = 23; // MQ-3 DO connected to ESP32 D23
const int ledPin = 13; // Onboard LED
const int irPin = 4; // IR sensor output connected to ESP32 D4

Adafruit_MPU6050 mpu; // MPU6050 instance

void setup() {
  Serial.begin(115200);
  pinMode(digitalPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(irPin, INPUT);

  // Initialize MPU6050
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) delay(10);
  }
  Serial.println("MPU6050 initialized!");

  // Configure MPU6050
  mpu.setAccelerometerRange(MPU6050_RANGE_2_G); // ±2g
  mpu.setGyroRange(MPU6050_RANGE_250_DEG);     // ±250°/s
  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);  // 21 Hz filter

  // Connect to WiFi
  connectToWiFi();
}

void loop() {
  // Read sensor values
  int analogValue = analogRead(analogPin);
  int digitalValue = digitalRead(digitalPin);
  int irValue = digitalRead(irPin);

  // Print sensor values to serial
  Serial.print("Analog Value: ");
  Serial.println(analogValue);
  Serial.print("IR Sensor Value: ");
  Serial.println(irValue);

  // LED control based on digital pin
  digitalWrite(ledPin, digitalValue);

  // Fetch GPS data
  String gpsData = fetchGPSData();

  // If GPS data is valid, send to cloud
  if (!gpsData.isEmpty()) {
    sendToCloud(gpsData, analogValue, irValue);
  }

  delay(1000); // Delay 1 second
}

void connectToWiFi() {
  Serial.print("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected!");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

String fetchGPSData() {
  // Simulated GPS data (replace with actual GPS fetch logic)
  // Sim808 implementation can be added here
  String latitude = "17.385044"; // Replace with parsed latitude
  String longitude = "78.486671"; // Replace with parsed longitude
  String gpsData = "{\"latitude\":\"" + latitude + "\",\"longitude\":\"" + longitude + "\"}";
  return gpsData;
}

void sendToCloud(String gpsData, int alcoholLevel, int irValue) {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin(serverName);

    // Create JSON payload
    String payload = "{\"gps\":" + gpsData + ",\"alcohol_level\":" + String(alcoholLevel) + ",\"ir_value\":" + String(irValue) + "}";
    Serial.println("Payload: " + payload);

    // Set content type and send POST request
    http.addHeader("Content-Type", "application/json");
    int httpResponseCode = http.POST(payload);

    // Handle response
    if (httpResponseCode > 0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
      String response = http.getString();
      Serial.println("Server Response: " + response);
    } else {
      Serial.print("Error in sending POST: ");
      Serial.println(httpResponseCode);
    }

    // Close connection
    http.end();
  } else {
    Serial.println("WiFi not connected