Home / Projects / ESP8266 Projects / ESP8266 IoT Gas Leak Sensor with Arduino Cloud
pcbway

ESP8266 IoT Gas Leak Sensor with Arduino Cloud

In this project, we’ll use the ESP8266 to connect an MQ-135 gas sensor to the Arduino IoT Cloud. This setup will enable you to detect air quality changes and potentially dangerous gas levels, which you can monitor from anywhere in real time through the Arduino Cloud.

Components Needed

  • ESP8266 Development Board (e.g., NodeMCU or Wemos D1 Mini)
  • MQ-135 Gas Sensor (detects gases such as ammonia, sulfide, and benzene steam)
  • Jumper wires
  • Breadboard (optional)
  • USB Cable for ESP8266

Step 1: Setting Up the Arduino IoT Cloud Account

  1. Go to the Arduino IoT Cloud website and create an account if you don’t have one.
  2. Once logged in, navigate to IoT Cloud and select Create New Thing.

Step 2: Setting Up Your IoT Thing and Variables

  1. Name the Thing as “Gas Leak Sensor”.
  2. Add a Variable:
    • Name: GasLevel
    • Type: Float
    • Permission: Read Only
    • Update Policy: On Change
    • Threshold: 0
  3. This variable will hold the gas level value from the MQ-135 sensor, which will be updated on the Arduino Cloud.

Step 3: Connect the ESP8266 to Arduino IoT Cloud

  1. Go to Devices under Arduino Cloud and select Add Device.
  2. Choose Set up a Third Party Device and select ESP8266.
  3. You can also specify which ESP8266 board you are using. I am using WeMos Mini D1 for this example.
  4. Follow the prompts to name your device and generate an API Key.
  5. Once the device is registered, download the device Secret Key and store it safely; this will be needed in the code.

Step 4: Circuit Connection

  1. MQ-135 to ESP8266 Wiring:
    • Connect VCC of the MQ-135 to 3.3V of the ESP8266.
    • Connect GND to GND on the ESP8266.
    • Connect Analog Output (AO) of the MQ-135 to the A0 pin on the ESP8266 (ESP8266 has only one analog pin, A0).

Step 5: Installing Required Libraries

Open the Arduino IDE (make sure it’s the latest version).

  1. Install ArduinoIoTCloud and Arduino_ConnectionHandler libraries:
    • Go to Tools > Manage Libraries…
    • Search for and install the ArduinoIoTCloud library.
    • Repeat for the Arduino_ConnectionHandler library.
  2. If you haven’t yet installed ESP8266 board support:
    • Go to File > Preferences and add http://arduino.esp8266.com/stable/package_esp8266com_index.json to the Additional Board Manager URLs.
    • Go to Tools > Board > Boards Manager…, search for ESP8266 and install it.

Step 6: Coding the ESP8266

Here’s the code to set up the ESP8266 to send gas sensor readings to the Arduino IoT Cloud.

  1. Open a new sketch in Arduino IDE.
  2. Replace SECRET_SSID and SECRET_PASS with your Wi-Fi credentials, and DEVICE_ID and SECRET_KEY with your Arduino Cloud device credentials.
#include "thingProperties.h"

// Analog pin for the MQ-135 sensor
const int mq135Pin = A0;
float gasLevel = 0;

void setup() {
  // Initialize serial and IoT Cloud properties
  Serial.begin(115200);
  delay(1500); 
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  // Wait for connection to the cloud
  while (!ArduinoCloud.connected()) {
    ArduinoCloud.update();
    delay(500);
  }
  Serial.println("Connected to Arduino IoT Cloud.");
}

void loop() {
  ArduinoCloud.update();

  // Read sensor value from MQ-135
  int sensorValue = analogRead(mq135Pin);

  // Convert the sensor value to a meaningful level
  gasLevel = sensorValue * (5.0 / 1023.0);

  // Log the gas level
  Serial.print("Gas Level: ");
  Serial.println(gasLevel);

  // Update cloud variable
  GasLevel = gasLevel;

  delay(1000);  // Adjust delay as needed
}

Step 7: Configuring Thing Properties

Before uploading, you need to configure thingProperties.h for your project:

  1. Open thingProperties.h by selecting Sketch > Show Sketch Folder and finding thingProperties.h.
  2. Set up your setupProperties() function to sync with the cloud variable:
#include <ArduinoIoTCloud.h> 
#include <Arduino_ConnectionHandler.h>

const char DEVICE_ID[] = "your_device_id";
const char SSID[] = SECRET_SSID;
const char PASS[] = SECRET_PASS;

void initProperties() {
  ArduinoCloud.setThingId(DEVICE_ID);
  ArduinoCloud.addProperty(GasLevel, READ, ON_CHANGE, NULL);
}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

Step 8: Uploading the Code

  1. Select your ESP8266 board and correct COM port in Tools.
  2. Upload the code to your ESP8266.
  3. Open the Serial Monitor (set it to 115200 baud rate) to confirm that your ESP8266 connects to Wi-Fi and begins sending gas level readings.

Step 9: Monitoring the Gas Levels on Arduino IoT Cloud

  1. Return to Arduino IoT Cloud and open your Thing.
  2. You should see the GasLevel variable updating in real-time.
  3. Optionally, set up dashboards or notifications for specific thresholds if you want alerts.

Step 10: (Optional) Adding a Visual Dashboard

  1. Under Arduino IoT Cloud, go to Dashboards and select Build Dashboard.
  2. Add a Gauge Widget for real-time monitoring.
  3. Link the GasLevel variable to the gauge.

Summary

You’ve now set up an IoT gas leak sensor with ESP8266 and MQ-135, with real-time monitoring through the Arduino IoT Cloud. This setup can serve as an effective safety tool to monitor potentially hazardous gas levels in your home or workspace.

Check Also

NodeMCU IoT Weather Device

NodeMCU IoT Weather Device

This project demonstrates how to read data from the DHT22 temperature and humidity sensor and …

Leave a Reply

Your email address will not be published. Required fields are marked *