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

Multi-Gas Detection System with ESP8266, Raspberry Pi Pico W, and MQ Sensors

View and Edit Project In Cirkit DesignerImage of Multi-Gas Detection System with ESP8266, Raspberry Pi Pico W, and MQ Sensors

Circuit Documentation

Summary

The circuit in question is designed to monitor various gases using a set of gas sensors and microcontrollers. It includes two ESP8266 NodeMCU units and a Raspberry Pi Pico W, each interfaced with different gas sensors (MQ-5, MQ-7, and MQ-136) to detect gases like LPG, CO, and H2S respectively. The ESP8266 NodeMCU is responsible for reading the MQ-5 sensor data and transmitting it wirelessly using ESP-NOW protocol. The Raspberry Pi Pico W reads the MQ-7 and MQ-136 sensor data and sends it to a central server via HTTP POST requests. Another ESP8266 NodeMCU acts as a central unit to receive data from both the other NodeMCU and the Raspberry Pi Pico W, and it also hosts a web server to handle incoming HTTP requests.

Component List

ESP8266 NodeMCU

Raspberry Pi Pico W

MQ-5 Gas Sensor

MQ-7 Gas Sensor Breakout

MQ-136 Gas Sensor

Wiring Details

ESP8266 NodeMCU (First Instance)

Raspberry Pi Pico W

MQ-5 Gas Sensor

MQ-7 Gas Sensor Breakout

MQ-136 Gas Sensor

Documented Code

ESP8266 NodeMCU (First Instance) Code

#include <esp_now.h>
#include <WiFi.h>

#define MQ5_PIN A0 // Pin for MQ5

// Structure to hold sensor data
typedef struct struct_message {
  int mq5Value;
} struct_message;

struct_message mq5Data;

void setup() {
  // Initialize serial communication
  Serial.begin(115200);
  
  // Initialize MQ5 pin
  pinMode(MQ5_PIN, INPUT);

  // Initialize Wi-Fi in station mode
  WiFi.mode(WIFI_STA);

  // Initialize ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Register the peer (Central NodeMCU's MAC Address)
  esp_now_peer_info_t peerInfo;
  memcpy(peerInfo.peer_addr, centralNodeMAC, 6); // Central NodeMCU MAC Address here
  peerInfo.channel = 0;
  peerInfo.encrypt = false;

  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }
}

void loop() {
  // Read the MQ5 sensor value
  mq5Data.mq5Value = analogRead(MQ5_PIN);

  // Send data to Central NodeMCU via ESP-NOW
  esp_err_t result = esp_now_send(centralNodeMAC, (uint8_t *) &mq5Data, sizeof(mq5Data));

  if (result == ESP_OK) {
    Serial.println("MQ5 data sent successfully");
  } else {
    Serial.println("Error sending MQ5 data");
  }

  delay(2000); // Wait before the next reading
}

Raspberry Pi Pico W Code

#include <WiFi.h>
#include <HTTPClient.h>

#define MQ7_PIN A1 // CO
#define MQ136_PIN A2 // H2S

const char* ssid = "Your_SSID";  // Replace with your Wi-Fi SSID
const char* password = "Your_PASSWORD";  // Replace with your Wi-Fi password
const char* serverURL = "http://<central_nodemcu_ip>/data";  // Central NodeMCU IP Address

void setup() {
  // Initialize serial communication
  Serial.begin(115200);

  // Initialize pins for MQ7 and MQ136
  pinMode(MQ7_PIN, INPUT);
  pinMode(MQ136_PIN, INPUT);

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

  Serial.println("Connected to Wi-Fi");
}

void loop() {
  // Read sensor values
  int mq7Value = analogRead(MQ7_PIN);
  int mq136Value = analogRead(MQ136_PIN);

  // Create a JSON payload
  String postData = "{\"mq7Value\":" + String(mq7Value) + ",\"mq136Value\":" + String(mq136Value) + "}";

  // Send data to Central NodeMCU via HTTP POST request
  HTTPClient http;
  http.begin(serverURL);  // Specify destination
  http.addHeader("Content-Type", "application/json");  // Specify JSON format
  
  int httpResponseCode = http.POST(postData);  // Send the request

  if (httpResponseCode > 0) {
    String response = http.getString();  // Get the response
    Serial.println("Data sent: " + response);
  } else {
    Serial.println("Error sending data");
  }

  http.end();  // Close the connection

  delay(5000);  // Wait before sending the next reading
}

ESP8266 NodeMCU (Second Instance) Code

#include <esp_now.h>
#include <WiFi.h>
#include <WebServer.h>
#include <ArduinoJson.h>

#define lpgThreshold 300
#define coThreshold 400
#define h2sThreshold 350

// Structure to hold received data from NodeMCU 1
typedef struct struct_message {
  int mq5Value;
} struct_message;

struct_message mq5Data;
WebServer server(80);  // HTTP server

// Function to handle ESP-NOW data reception
void onDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&mq5Data, incomingData, sizeof(mq5Data));
  Serial.print("MQ5 (LPG): ");
  Serial.println(mq5Data.mq5Value);

  if (mq5Data.mq5Value > lpgThreshold) {
    Serial.println("Alert: LPG Gas Detected!");
  }
}

// Function to handle HTTP requests from Raspberry Pi Pico H
void handleGasData() {
  if (server.hasArg("plain")) {
    String body = server.arg("plain");
    DynamicJsonDocument doc(200);
    deserializeJson(doc, body);

    int mq7Value = doc["mq7Value"];
    int mq136Value = doc["mq136Value"];

    Serial.print("MQ7 (CO): ");
    Serial.println(mq7Value);

    Serial.print("MQ136 (H2S): ");
    Serial.println(mq136Value);

    // Check thresholds for alert
    if (mq7Value > coThreshold) {
      Serial.println("Alert: Carbon Monoxide (CO) Detected!");
    }

    if (mq136Value > h2sThreshold) {
      Serial.println("Alert: Hydrogen Sulfide (H2S) Detected!");
    }

    server.send(200, "text/plain", "Data received");
  } else {
    server.send(400, "text/plain", "Invalid request");
  }
}

void setup() {
  // Initialize serial communication
  Serial.begin(115200);

  // Initialize Wi-Fi for HTTP requests
  WiFi.mode(WIFI_AP);
  WiFi.softAP("Central_NodeMCU", "password");  // Set up AP mode
  
  server.on("/data", HTTP_POST, handleGasData);  // Route for receiving data
  server.begin();

  // Initialize ESP-NOW
  WiFi.mode(WIFI_STA);
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error