Arduino IoT Based Monitoring System Project Guide
An
Arduino IoT based monitoring system uses sensors connected to an Arduino board to collect data like temperature or humidity, then sends this data over Wi-Fi using a module like ESP8266 to an online platform for real-time monitoring. This setup allows remote tracking of environmental conditions through a web or mobile app.Syntax
The basic syntax for an Arduino IoT monitoring system includes initializing sensors, connecting to Wi-Fi, reading sensor data, and sending it to a cloud service.
- Setup: Initialize serial communication, sensors, and Wi-Fi module.
- Loop: Read sensor values, format data, and send it to the IoT platform.
arduino
void setup() { Serial.begin(115200); // Start serial communication setupSensors(); // Initialize sensors connectWiFi(); // Connect to Wi-Fi network } void loop() { float sensorValue = readSensor(); // Read sensor data sendDataToCloud(sensorValue); // Send data to IoT platform delay(5000); // Wait 5 seconds before next reading }
Example
This example shows how to read temperature from a DHT11 sensor and send it to ThingSpeak, a popular IoT cloud platform, using an ESP8266 Wi-Fi module.
arduino
#include <ESP8266WiFi.h> #include <DHT.h> #define DHTPIN D4 // Pin where the DHT11 is connected #define DHTTYPE DHT11 // DHT 11 sensor const char* ssid = "your_SSID"; // Your Wi-Fi name const char* password = "your_PASSWORD"; // Your Wi-Fi password const char* host = "api.thingspeak.com"; // ThingSpeak server const char* apiKey = "YOUR_API_KEY"; // ThingSpeak Write API Key DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(115200); delay(10); dht.begin(); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected"); } void loop() { float temp = dht.readTemperature(); if (isnan(temp)) { Serial.println("Failed to read from DHT sensor!"); return; } WiFiClient client; if (!client.connect(host, 80)) { Serial.println("Connection failed"); return; } String url = "/update?api_key=" + String(apiKey) + "&field1=" + String(temp); client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); Serial.print("Temperature sent: "); Serial.println(temp); delay(20000); // Wait 20 seconds before next reading }
Output
WiFi connected
Temperature sent: 24.00
Temperature sent: 24.50
Temperature sent: 25.00
Common Pitfalls
Common mistakes include:
- Incorrect Wi-Fi credentials causing connection failure.
- Not checking if sensor readings are valid (e.g.,
isnan()check for DHT sensors). - Sending data too frequently, which may exceed IoT platform limits.
- Forgetting to initialize sensors or Wi-Fi modules in
setup().
Always add error checks and delays to avoid flooding the server.
arduino
/* Wrong way: No Wi-Fi check and no sensor validation */ void loop() { float temp = dht.readTemperature(); // No isnan check WiFiClient client; client.connect(host, 80); // No check if connected // Sends data immediately without delay } /* Right way: Check Wi-Fi and sensor data before sending */ void loop() { float temp = dht.readTemperature(); if (isnan(temp)) { Serial.println("Sensor error"); return; } if (WiFi.status() == WL_CONNECTED) { WiFiClient client; if (client.connect(host, 80)) { // Send data } else { Serial.println("Connection failed"); } } else { Serial.println("WiFi not connected"); } delay(20000); // Wait before next send }
Quick Reference
- Sensor Initialization: Use sensor libraries and call
begin()insetup(). - Wi-Fi Connection: Use
WiFi.begin(ssid, password)and wait for connection. - Data Sending: Use
WiFiClientto send HTTP GET requests to IoT platforms. - Delays: Add delays (e.g., 20 seconds) to avoid flooding the server.
- Error Handling: Check sensor data validity and Wi-Fi connection status.
Key Takeaways
Use sensors with proper initialization and validation to get accurate data.
Connect Arduino to Wi-Fi using ESP8266 or similar modules for IoT communication.
Send sensor data to cloud platforms like ThingSpeak via HTTP requests.
Add delays and error checks to ensure stable and reliable data transmission.
Verify Wi-Fi credentials and sensor wiring to avoid common connection issues.