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.
Connect your sensors to the ESP modules as follows:
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); } } }
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}`); });
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.
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.