Postingan kali ini, kita akan membuat water level monitoring, dengan menggunakan Ultrasonic sensor.
dimana nanti hasil pembacaan berupa : Volume air, ketinggian air dan prosentase pengisian, yang ditampilkan di browser seperti ini.
untuk alat dan bahannya :
1. sensor ultrasonic SR-04
2. esp 8266
3. kabel usb
4. kabel jumper
Rangkaialah rangkaian dengan skema berikut ini :
Tampilan file HTML diatas adalah seperti berikut :
Kemudian masukan sketch ke esp 8266 lolin. dengan sketch berikut ini, modiflah topic sesuai dengan kebutuhan kamu ya,
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define LED 13 // GPIO13 (D7) for LED
#define triggerPin 12 //d6
#define echoPin 14//d5
long durasi;
int tandon, tinggi = 0;
bool ledState = false;
// WiFi settings
const char *ssid = "Rohmad.com"; // Replace with your WiFi name
const char *password = "1122334455"; // Replace with your WiFi password
// MQTT Broker settings
const char *mqtt_broker = "broker.emqx.io"; // EMQX broker endpoint
const char *mqtt_topic = "rokhmad.com/#"; // MQTT topic
const char *mqtt_username = "emqx"; // MQTT username for authentication
const char *mqtt_password = "public"; // MQTT password for authentication
const int mqtt_port = 1883; // MQTT port (TCP)
WiFiClient espClient;
PubSubClient mqtt_client(espClient);
void connectToWiFi();
void connectToMQTTBroker();
void mqttCallback(char *topic, byte *payload, unsigned int length);
void setup() {
Serial.begin(115200);
pinMode (echoPin, INPUT);
pinMode (triggerPin, OUTPUT);
connectToWiFi();
mqtt_client.setServer(mqtt_broker, mqtt_port);
mqtt_client.setCallback(mqttCallback);
pinMode(LED, OUTPUT);
connectToMQTTBroker();
}
void connectToWiFi() {
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to the WiFi network");
}
void connectToMQTTBroker() {
while (!mqtt_client.connected()) {
String client_id = "esp8266-client-" + String(WiFi.macAddress());
Serial.printf("Connecting to MQTT Broker as %s.....\n", client_id.c_str());
if (mqtt_client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Connected to MQTT broker");
mqtt_client.subscribe(mqtt_topic);
// Publish message upon successful connection
} else {
Serial.print("Failed to connect to MQTT broker, rc=");
Serial.print(mqtt_client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void mqttCallback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message received on topic: ");
Serial.println(topic);
Serial.print("Message:");
String message;
for (int i = 0; i < length; i++) {
message += (char) payload[i]; // Convert *byte to string
}
// Control the LED based on the message received
if (message == "on" && !ledState) {
digitalWrite(LED, HIGH); // Turn on the LED
ledState = true;
Serial.println("LED is turned on");
}
if (message == "off" && ledState) {
digitalWrite(LED, LOW); // Turn off the LED
ledState = false;
Serial.println("LED is turned off");
}
Serial.println(message);
Serial.println();
Serial.println("-----------------------");
}
void loop() {
if (!mqtt_client.connected()) {
connectToMQTTBroker();
}
digitalWrite (triggerPin, LOW);
delayMicroseconds(2);
digitalWrite (triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite (triggerPin, LOW);
durasi = pulseIn(echoPin, HIGH);
tinggi = durasi * 0.034 / 2;
delay(1000);
///////////////
float tinggi_max_tangki = 40; //tinggi tangki dlm cm
float jari2 = 1; //jari2 dalam meter
float phi = 3.14; //volume
float tinggi_air = tinggi_max_tangki - tinggi;
float prosentase_tinggi_air = (tinggi_air / tinggi_max_tangki) * 100; //hasil persen
float volume = phi * (jari2 * jari2) * (tinggi_air / 100);
float liter = volume * 1000;
//////////////////
///////////////
if (tandon != tinggi) {
mqtt_client.subscribe("rokhmad.com/tandon");
mqtt_client.publish("rokhmad.com/tandon", String (prosentase_tinggi_air).c_str());
mqtt_client.subscribe("rokhmad.com/liter");
mqtt_client.publish("rokhmad.com/liter", String (liter).c_str());
mqtt_client.subscribe("rokhmad.com/tinggiAir");
mqtt_client.publish("rokhmad.com/tinggiAir", String (tinggi_air).c_str());
mqtt_client.subscribe("rokhmad.com/prosentaseAir");
mqtt_client.publish("rokhmad.com/prosentaseAir", String (prosentase_tinggi_air).c_str());
Serial.println("mqtt uplod");
Serial.print("tinggi air; ");
Serial.println(tinggi_air);
Serial.print("tinggi max air : ");
Serial.println(tinggi_max_tangki);
Serial.print("prosentasetinggi air: ");
Serial.println(prosentase_tinggi_air);
Serial.print("volume air :");
Serial.println(volume);
Serial.print("Liter air: ");
Serial.println(liter);
Serial.print("tinggi ultra sonic: ");
Serial.println(tinggi);
tinggi = tandon;
}
mqtt_client.loop();
}