#if defined(ESP32)
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "ESP32"
#elif defined(ESP8266)
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#define DEVICE "ESP8266"
#endif
#include <HTTPClient.h>
// WiFi AP SSID
#define WIFI_SSID "Wokwi-GUEST"
// WiFi password
#define WIFI_PASSWORD ""
// ThingsBoard server URL and device token
#define THINGSBOARD_SERVER "https://demo.thingsboard.io/"
#define DEVICE_TOKEN "uHzVhwA3CUJDvhdKsopN"
// Ultrasonic sensor pins
#define TRIG_PIN_1 2
#define ECHO_PIN_1 1
#define TRIG_PIN_2 4
#define ECHO_PIN_2 3
#define TRIG_PIN_3 6
#define ECHO_PIN_3 5
#define TRIG_PIN_4 8
#define ECHO_PIN_4 7
#define TRIG_PIN_5 10
#define ECHO_PIN_5 9
#define TRIG_PIN_6 12
#define ECHO_PIN_6 11
#define TRIG_PIN_7 14
#define ECHO_PIN_7 13
#define TRIG_PIN_8 16
#define ECHO_PIN_8 15
// Pump pins
#define PUMP1 0
#define PUMP2 45
#define PUMP3 42
#define PUMP4 41
// Led pins
#define GREEN 37
#define RED 35
// Valve pins
#define VALVE1 34
#define VALVE2 33
bool ledVerde = false;
bool ledRojo = false;
bool val1 = false;
bool val2 = false;
//Distance max and min (reverse)
float minDistance = 100.0;
float maxDistance = 1.0;
void setup() {
Serial.begin(115200);
// Setup wifi
WiFi.mode(WIFI_STA);
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to wifi");
while (wifiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
// Setup ultrasonic sensor pins
pinMode(TRIG_PIN_1, OUTPUT);
pinMode(ECHO_PIN_1, INPUT);
pinMode(TRIG_PIN_2, OUTPUT);
pinMode(ECHO_PIN_2, INPUT);
pinMode(TRIG_PIN_3, OUTPUT);
pinMode(ECHO_PIN_3, INPUT);
pinMode(TRIG_PIN_4, OUTPUT);
pinMode(ECHO_PIN_4, INPUT);
pinMode(TRIG_PIN_5, OUTPUT);
pinMode(ECHO_PIN_5, INPUT);
pinMode(TRIG_PIN_6, OUTPUT);
pinMode(ECHO_PIN_6, INPUT);
pinMode(TRIG_PIN_7, OUTPUT);
pinMode(ECHO_PIN_7, INPUT);
pinMode(TRIG_PIN_8, OUTPUT);
pinMode(ECHO_PIN_8, INPUT);
// Setup pump pins
pinMode(PUMP1, OUTPUT);
pinMode(PUMP2, OUTPUT);
pinMode(PUMP3, OUTPUT);
pinMode(PUMP4, OUTPUT);
// Setup led pins
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
// Setup valve pins
pinMode(VALVE1, OUTPUT);
pinMode(VALVE2, OUTPUT);
}
float measureDistance(int trigPin, int echoPin) {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.034) / 2; // Convert to cm
return distance;
}
void loop() {
float distances[8];
float levels[8];
// Measure distances from all eight sensors
distances[0] = measureDistance(TRIG_PIN_1, ECHO_PIN_1);
distances[1] = measureDistance(TRIG_PIN_2, ECHO_PIN_2);
distances[2] = measureDistance(TRIG_PIN_3, ECHO_PIN_3);
distances[3] = measureDistance(TRIG_PIN_4, ECHO_PIN_4);
distances[4] = measureDistance(TRIG_PIN_5, ECHO_PIN_5);
distances[5] = measureDistance(TRIG_PIN_6, ECHO_PIN_6);
distances[6] = measureDistance(TRIG_PIN_7, ECHO_PIN_7);
distances[7] = measureDistance(TRIG_PIN_8, ECHO_PIN_8);
// Calculate levels for each distance
for (int i = 0; i < 8; i++) {
levels[i] = ((maxDistance - distances[i]) / (maxDistance - minDistance)) * 100.0;
if (levels[i] < 0) {
levels[i] = 0;
} else if (levels[i] > 100) {
levels[i] = 100;
}
}
//If the raw and treated water is little
if (levels[0] <= 15 && levels[1] <= 15 && levels[3] <= 15 && levels[4] <= 15 && levels[5] <= 15 && levels[6] <= 15 && levels[7] <= 15){
ledVerde = true;
ledRojo = false;
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
digitalWrite(PUMP1, HIGH);
digitalWrite(PUMP2, LOW);
digitalWrite(PUMP3, LOW);
digitalWrite(PUMP4, LOW);
} //Otherwise if it is enough
else if (levels[0] >= 20 && levels[1] >= 20 && levels[3] <= 15 && levels[4] <= 15 && levels[5] <= 15 && levels[6] <= 15 && levels[7] <= 15){
ledVerde = true;
ledRojo = false;
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
digitalWrite(PUMP1, HIGH);
delay(10000);
digitalWrite(PUMP2, HIGH);
delay(10000);
digitalWrite(PUMP3, HIGH);
delay(10000);
digitalWrite(PUMP4, HIGH);
}
//If the raw water is little and the treated water max
if (levels[0] <= 15 && levels[1] <= 15 && levels[3] >= 98 && levels[4] >= 98 && levels[5] >= 98 && levels[6] >= 98 && levels[7] >= 98) {
ledVerde = true;
ledRojo = false;
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
digitalWrite(PUMP1, HIGH);
digitalWrite(PUMP2, LOW);
digitalWrite(PUMP3, LOW);
digitalWrite(PUMP4, LOW);
}
//If raw and treated water is max
if (levels[0] >= 98 && levels[1] >= 98 && levels[3] >= 98 && levels[4] >= 98 && levels[5] >= 98 && levels[6] >= 98 && levels[7] >= 98) {
ledVerde = false;
ledRojo = true;
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
digitalWrite(PUMP1, LOW);
digitalWrite(PUMP2, LOW);
digitalWrite(PUMP3, LOW);
digitalWrite(PUMP4, LOW);
}
if (levels[8] <= 15) {
digitalWrite(VALVE1, HIGH);
digitalWrite(VALVE2, LOW);
} //
if (levels[8] >= 98) {
digitalWrite(VALVE2, HIGH);
digitalWrite(VALVE1, LOW);
}
// Prepare JSON payload
String jsonPayload = String("{\"device\":\"") + DEVICE +
"\",\"SSID\":\"" + WiFi.SSID() +
"\",\"rssi\":" + WiFi.RSSI() +
",\"nivel1\":" + levels[0] +
",\"nivel2\":" + levels[1] +
",\"nivel3\":" + levels[2] +
",\"nivel4\":" + levels[3] +
",\"nivel5\":" + levels[4] +
",\"nivel6\":" + levels[5] +
",\"nivel7\":" + levels[6] +
",\"nivel8\":" + levels[7] +
",\"bomba1\":" + (PUMP1 ? "false" : "true") +
",\"bomba2\":" + (PUMP2 ? "false" : "true") +
",\"bomba3\":" + (PUMP3 ? "false" : "true") +
",\"bomba4\":" + (PUMP4 ? "false" : "true") +
",\"valvula1\":" + (val1 ? "true" : "false") +
",\"valvula2\":" + (val2 ? "true" : "false") +
",\"ledverde\":" + (ledVerde ? "true" : "false") +
",\"ledrojo\":" + (ledRojo ? "true" : "false") + "}";
Serial.print("Sending data: ");
Serial.println(jsonPayload);
// Check WiFi connection and reconnect if needed
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("Wifi connection lost");
} else {
// Send data to ThingsBoard
HTTPClient http;
http.begin("http://demo.thingsboard.io/api/v1/" DEVICE_TOKEN "/telemetry"); // URL para enviar datos
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(jsonPayload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.println("Response: " + response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); // Cierra la conexión
}
Serial.println("Waiting 1 second");
delay(1000);
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1