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

Arduino and ESP8266 Based Environmental Monitoring System with GPS Tracking

View and Edit Project In Cirkit DesignerImage of Arduino and ESP8266 Based Environmental Monitoring System with GPS Tracking

Circuit Documentation

Summary

The circuit is designed to interface various sensors and modules with an Arduino UNO and an ESP8266 NodeMCU microcontroller. The sensors include an HC-SR04 Ultrasonic Sensor for distance measurement, a DHT11 Humidity and Temperature Sensor for environmental data, an MQ-5 Gas Sensor for detecting gas concentrations, and a GPS NEO 6M module for location tracking. The ESP8266 NodeMCU is used for Wi-Fi connectivity and data transmission to a server. The Arduino UNO serves as the central processing unit, collecting data from the sensors, processing it, and sending it to the ESP8266 for further communication.

Component List

Wiring Details

HC-SR04 Ultrasonic Sensor

Arduino UNO

DHT11 Humidity and Temperature Sensor

MQ-5 Gas Sensor

ESP8266 NodeMCU

GPS NEO 6M

Documented Code

Arduino UNO Code

#include <DHT.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>

// Define Ultrasonic Pins
#define trigPin 9
#define echoPin 10

// Define DHT Sensor Type and Pin
#define DHTPIN 7
#define DHTTYPE DHT11  

// Create DHT object
DHT dht(DHTPIN, DHTTYPE);

// GPS Setup
SoftwareSerial ss(4, 3);  // RX, TX for GPS
TinyGPSPlus gps;

// Define Gas Sensor Pin
#define gasPin A0

void setup() {
  // Start Serial Communication
  Serial.begin(9600);
  ss.begin(9600);  // GPS Serial

  // Initialize DHT Sensor
  dht.begin();
  
  // Set Ultrasonic Pin modes
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  Serial.println("System Initialized");
}

void loop() {
  // *** Read Ultrasonic Sensor ***
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;  // Convert to cm

  // *** Read Temperature and Humidity ***
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();  // Celsius

  // *** Read Gas Sensor ***
  int gasValue = analogRead(gasPin);  // Read analog value from MQ-135

  // *** Read GPS Data ***
  while (ss.available() > 0) {
    gps.encode(ss.read());
    if (gps.location.isUpdated()) {
      // Display GPS data if available
      Serial.print("Latitude: ");
      Serial.println(gps.location.lat(), 6);
      Serial.print("Longitude: ");
      Serial.println(gps.location.lng(), 6);
    }
  }

  // *** Display Sensor Data ***
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  Serial.print("Gas Value: ");
  Serial.println(gasValue);  // Raw analog value

  delay(2000);  // Delay for 2 seconds before next reading
}

ESP8266 NodeMCU Code

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>

// Replace with your network credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

// MongoDB Atlas API Endpoint (Example)
String serverName = "http://your-atlas-api-url.com/data";

// Variables to hold sensor data
String distance = "";
String temperature = "";
String humidity = "";
String gasValue = "";

void setup() {
  // Start Serial for debugging and communication with Arduino
  Serial.begin(9600);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  // Check if data is available from Arduino
  if (Serial.available() > 0) {
    String sensorData = Serial.readStringUntil('\n');  // Read sensor data
    parseSensorData(sensorData);  // Parse the received data

    // Now send the data to MongoDB Atlas
    if (WiFi.status() == WL_CONNECTED) {
      HTTPClient http;

      // Prepare the data to send
      String postData = "distance=" + distance + "&temperature=" + temperature + "&humidity=" + humidity + "&gas=" + gasValue;

      // Start connection and send HTTP POST request
      http.begin(serverName);  // Specify destination for HTTP POST
      http.addHeader("Content-Type", "application/x-www-form-urlencoded");
      int httpResponseCode = http.POST(postData);

      // Check the response
      if (httpResponseCode > 0) {
        String response = http.getString();
        Serial.println("Response: " + response);  // Print server response
      } else {
        Serial.println("Error on sending POST: " + String(httpResponseCode));
      }
      http.end();  // Close connection
    }
  }

  delay(2000);  // Wait for 2 seconds before next data send
}

void parseSensorData(String data) {
  // Example received data: "Distance:35cm, Temp:28.5C, Humidity:65%, GasValue:300"
  int distanceStart = data.indexOf("Distance:") + 9;
  int distanceEnd = data.indexOf("cm");
  distance = data.substring(distanceStart, distanceEnd);

  int tempStart = data.indexOf("Temp:") + 5;
  int tempEnd = data.indexOf("C");
  temperature = data.substring(tempStart, tempEnd);

  int humStart = data.indexOf("Humidity:") + 9;
  int humEnd = data.indexOf("%");
  humidity = data.substring(humStart, humEnd);

  int gasStart = data.indexOf("GasValue:") + 9;
  gasValue = data.substring(gasStart);
}

This documentation provides an overview of the circuit, including the components used, their wiring connections, and the code