0
0
Drone-programmingHow-ToBeginner · 4 min read

IoT Project for Water Quality Monitoring: Setup and Code Example

An IoT project for water quality monitoring uses sensors like pH, turbidity, and temperature connected to a microcontroller (e.g., ESP32) that sends data via MQTT protocol to a cloud server or dashboard. This setup allows real-time tracking of water parameters remotely using simple sensor readings and network communication.
📐

Syntax

This project uses the following components and syntax:

  • Sensor reading: Code to read values from water quality sensors like pH or turbidity.
  • Microcontroller setup: Initialize Wi-Fi and MQTT client to connect to the network and broker.
  • MQTT publish: Send sensor data as messages to a topic on the MQTT broker.
cpp
void setup() {
  Serial.begin(115200);
  WiFi.begin("SSID", "PASSWORD");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  mqttClient.setServer("broker.hivemq.com", 1883);
  mqttClient.connect("clientID");
}

void loop() {
  float phValue = readPH();
  char payload[50];
  snprintf(payload, sizeof(payload), "{\"ph\": %.2f}", phValue);
  mqttClient.publish("water/quality", payload);
  delay(60000); // send every 60 seconds
}
💻

Example

This example demonstrates reading a pH sensor value and sending it via MQTT to a public broker every minute.

cpp
#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "YourSSID";
const char* password = "YourPassword";
const char* mqttServer = "broker.hivemq.com";
const int mqttPort = 1883;
const char* mqttTopic = "water/quality";

WiFiClient espClient;
PubSubClient client(espClient);

float readPH() {
  int sensorValue = analogRead(34); // pH sensor connected to GPIO34
  float voltage = sensorValue * (3.3 / 4095.0);
  float ph = 7 + ((2.5 - voltage) / 0.18); // simple calibration
  return ph;
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  client.setServer(mqttServer, mqttPort);
  while (!client.connected()) {
    if (client.connect("ESP32Client")) {
      Serial.println("MQTT connected");
    } else {
      delay(1000);
    }
  }
}

void loop() {
  if (!client.connected()) {
    while (!client.connected()) {
      if (client.connect("ESP32Client")) {
        Serial.println("MQTT reconnected");
      } else {
        delay(1000);
      }
    }
  }
  client.loop();

  float phValue = readPH();
  char payload[50];
  snprintf(payload, sizeof(payload), "{\"ph\": %.2f}", phValue);
  client.publish(mqttTopic, payload);
  Serial.print("Published: ");
  Serial.println(payload);
  delay(60000);
}
Output
WiFi connected MQTT connected Published: {"ph": 7.05} Published: {"ph": 7.02} Published: {"ph": 7.00} ...
⚠️

Common Pitfalls

  • Incorrect sensor calibration: Not calibrating sensors leads to wrong readings.
  • Wi-Fi connection failures: Not handling reconnection causes data loss.
  • MQTT broker issues: Using unreliable brokers or not checking connection status can stop data flow.
  • Power supply problems: Unstable power can reset the microcontroller.
cpp
/* Wrong: No Wi-Fi reconnection handling */
void loop() {
  float phValue = readPH();
  char payload[50];
  snprintf(payload, sizeof(payload), "{\"ph\": %.2f}", phValue);
  client.publish(mqttTopic, payload);
  delay(60000);
}

/* Right: Check and reconnect Wi-Fi and MQTT */
void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.reconnect();
  }
  if (!client.connected()) {
    client.connect("ESP32Client");
  }
  client.loop();
  float phValue = readPH();
  char payload[50];
  snprintf(payload, sizeof(payload), "{\"ph\": %.2f}", phValue);
  client.publish(mqttTopic, payload);
  delay(60000);
}
📊

Quick Reference

  • Sensor types: pH, turbidity, temperature sensors are common for water quality.
  • Microcontrollers: ESP32 or Arduino with Wi-Fi capability.
  • Communication: Use MQTT protocol for lightweight, real-time data transfer.
  • Data handling: Send JSON formatted sensor data for easy parsing.
  • Power: Ensure stable power supply for continuous monitoring.

Key Takeaways

Use sensors like pH and turbidity connected to a Wi-Fi microcontroller for water quality data.
Send sensor readings via MQTT protocol to a broker for real-time remote monitoring.
Always handle Wi-Fi and MQTT reconnections to avoid data loss.
Calibrate sensors properly to get accurate water quality measurements.
Format data as JSON for easy integration with dashboards or cloud services.