Building a Wireless Sensor Network (WSN)

Difficulty Level: Intermediate to Advanced

A Wireless Sensor Network (WSN) consists of spatially distributed autonomous sensors that monitor physical or environmental conditions. This project will guide you through setting up a basic WSN using ESP8266 or ESP32 modules.

Components Required

Setup Instructions

  1. Configure the Sensor Nodes

    Connect your sensors to the ESP modules as follows:

    • DHT11/DHT22: Connect VCC to 3.3V, GND to GND, and Data to a digital pin (e.g., D4).
    • Light Sensor: Connect one terminal to VCC and the other to an analog pin (e.g., A0).

    Write the following code for each sensor node:

    #include <ESP8266WiFi.h>
    #include <DHT.h>
    
    #define DHTPIN D4
    #define DHTTYPE DHT11
    
    DHT dht(DHTPIN, DHTTYPE);
    const char* ssid = "your_SSID";
    const char* password = "your_PASSWORD";
    const char* server = "your_SERVER_IP";
    
    void setup() {
        Serial.begin(115200);
        dht.begin();
        WiFi.begin(ssid, password);
    }
    
    void loop() {
        delay(2000);
        float h = dht.readHumidity();
        float t = dht.readTemperature();
        int lightLevel = analogRead(A0);
        
        if (WiFi.status() == WL_CONNECTED) {
            WiFiClient client;
            if (client.connect(server, 80)) {
                String data = "temperature=" + String(t) + "&humidity=" + String(h) + "&light=" + String(lightLevel);
                client.println("POST /sensor-data HTTP/1.1");
                client.println("Host: " + String(server));
                client.println("Content-Type: application/x-www-form-urlencoded");
                client.print("Content-Length: ");
                client.println(data.length());
                client.println();
                client.println(data);
            }
        }
    }
                    
  2. Set Up the Data Aggregator (Server)

    Set up a simple web server to receive and process sensor data. You can use any backend technology (Node.js, PHP, Python, etc.). Here's a simple example using Node.js:

    const express = require('express');
    const bodyParser = require('body-parser');
    const app = express();
    const port = 3000;
    
    app.use(bodyParser.urlencoded({ extended: true }));
    
    app.post('/sensor-data', (req, res) => {
        console.log('Received data:', req.body);
        res.send('Data received');
    });
    
    app.listen(port, () => {
        console.log(`Server running at http://localhost:${port}`);
    });
                    
  3. Test the System

    Power up the ESP modules and monitor the server logs to see incoming sensor data. You can visualize this data using a simple web frontend or dashboard.

Conclusion

This project demonstrates how to create a simple wireless sensor network using ESP modules to collect data from various sensors. You can expand this network by adding more sensors or features such as data logging, alerts, and real-time monitoring.

Additional Resources