In the previous article, we discussed reading data from water quality sensors based on Arduino. For details, you can read the historical articles.
However, due to the limitations of Arduino, it cannot upload data to the network. Therefore, this article presents a method for configuring a water quality sensor based on ESP8266 for data reporting.
Preparation
- ESP8266
- Water Quality Sensor
- MQTT Server
Wiring Connection
<span>VCC</span>
—<span>3.3v</span>
<span>GND</span>
—<span>GND</span>
<span>Signal Line</span>
—<span>A0</span>
(Analog Pin)
Code
#include<ESP8266WiFi.h>
#include<PubSubClient.h>
#include<ArduinoJson.h>
// WiFi Configuration
const char* ssid = "Your WiFi Name";
const char* password = "WiFi Password";
// MQTT Configuration
const char* mqtt_server = "MQTT Server Address";
const int mqtt_port = 1883;
const char* mqtt_user = "admin";
const char* mqtt_password = "admin";
const char* topic = "shuizhi"; // Subscription Topic
// Sensor Configuration
const int TDS_PIN = A0; // ESP8266 can only use A0 for analog input
const float VREF = 3.3; // Sensor operating voltage
const float K = 0.4; // Calibration coefficient
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("esp8266-SZ", mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
String getWaterQuality(float tds) {
if (tds <= 9) return "Excellent"; // 0-9 mg/L Pure Water
else if (tds <= 60) return "Excellent"; // 10-60 mg/L Spring Water, Mineral Water
else if (tds <= 100) return "Moderate"; // 60-100 mg/L Purified Water
else if (tds <= 300) return "Moderate"; // 100-300 mg/L Ordinary Tap Water
else return "Poor"; // >300 mg/L Possible Contamination
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Read and calculate TDS value
int analogValue = analogRead(TDS_PIN);
float voltage = analogValue * VREF / 1024.0;
float tdsValue = (133.42 * pow(voltage, 3) - 255.86 * pow(voltage, 2) + 857.39 * voltage) * K;
// Get water quality level
String quality = getWaterQuality(tdsValue);
// Build JSON data
StaticJsonDocument<200> doc;
doc["TDS"] = round(tdsValue); // Round to nearest integer
doc["DJ"] = quality;
char jsonBuffer[512];
serializeJson(doc, jsonBuffer);
// Publish MQTT message
client.publish(topic, jsonBuffer);
Serial.print("Published: ");
Serial.println(jsonBuffer);
delay(5000); // 5 seconds interval
}
Code Function Description:
- Uploads data to the
<span>shuizhi</span>
topic every 5 seconds. - Processes data into categories: Excellent, Moderate, Poor
Data Format Description:
The sent data is in JSON format, as follows:
{"TDS":21,"DJ":"Excellent"}
The former is the reported TDS data, and the latter is the current data level.

Additionally, this is currently a single data point. If you have multiple devices, such as using <span>DHT11</span>
to collect temperature and humidity data, you can concatenate them into a complete JSON data, such as:
{"humi":58,"temp":36,"TDS":500,"DJ":"Poor"}

Integrating with HA or Mini Program
Since the data is reported based on MQTT, we can integrate the data with HA or WeChat Mini Programs.



Conclusion
In the future, we will update other types of sensors to facilitate hardware enthusiasts’ learning and research. Of course, if you have better methods, please leave your wonderful comments in the comment section.
BREAK AWAYPrevious Recommendations01Water Quality Detection Based on Arduino02MQTT Mini Program Fully Open Source!03Mastering MQTT in One Article (Based on ESP8266 DHT11 MQTT MySQL Implementation)For more exciting articles, feel free to follow us