Rabu, 13 Juli 2022

contoh simulasi iot menggunakan wokwi dan blynk untuk penerapan deteksi ketinggian air pada waduk / pintu air

source code iot untuk mengukur ketinggian air menggunakan simulasi wokwy dan blynk


kode.ino 

#define BLYNK_AUTH_TOKEN "CwaUOQKEPtoken-isisesuaitokenanda"
#define BLYNK_TEMPLATE_ID "TMPisiIdAnda_"
#define BLYNK_DEVICE_NAME "HCSR04"
char auth[] = "CwaUOQKEPtoken-isisesuaitokenanda";
#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>



#define Echo 15
#define Trig 4
#define Merah 14
#define Kuning 12
#define Hijau 13


char ssid[] = "Wokwi-GUEST";
char pass[] = "";

BlynkTimer timer;

float distance = 0;
float ketinggian = 0;


void ukur_jarak(){
  digitalWrite(Trig,LOW);
  delay(2);
  digitalWrite(Trig, HIGH);
  delay(10);
  digitalWrite(Trig, LOW);

  int duration = pulseIn(Echo, HIGH);
  distance = duration * 0.034 / 2;
  ketinggian = 350 - distance ;

  Serial.print("Jarak: ");
  Serial.print(distance);
  Serial.println(" CM");

  Serial.print("Ketinggian Air : ");
  Serial.print(ketinggian);
  Serial.println(" CM");
 
}

void kirim_blynk(){
Blynk.virtualWrite(V0, ketinggian);
Blynk.virtualWrite(V4, ketinggian);
  if (distance < 100){
  Blynk.virtualWrite(V1, 0);
  Blynk.virtualWrite(V2, 0);
  Blynk.virtualWrite(V3, 1);
}
else if (distance >= 100 && distance <= 150){
  Blynk.virtualWrite(V1, 0);
  Blynk.virtualWrite(V2, 1);
  Blynk.virtualWrite(V3, 0);
}
else if (distance > 150){
  Blynk.virtualWrite(V1, 1);
  Blynk.virtualWrite(V2, 0);
  Blynk.virtualWrite(V3, 0);
}

}

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(Merah, OUTPUT);
  pinMode(Kuning, OUTPUT);
  pinMode(Hijau, OUTPUT);
  timer.setInterval(2000L,kirim_blynk);
}

void loop() {
   
  digitalWrite(Merah, LOW);
  digitalWrite(Hijau, LOW);
  digitalWrite(Kuning, LOW);


  ukur_jarak();
  int ketinggian = 350 - distance ;
  Serial.print("Jarak: ");
  Serial.print(distance);
  Serial.println(" CM");

  Serial.print("Ketinggian Air : ");
  Serial.print(ketinggian);
  Serial.println(" CM");

  if( distance > 100 && distance < 150)
  {
    Serial.println("Waspada");
    digitalWrite(Kuning, HIGH);
  }
  else if (distance > 0 && distance < 100)
  {
    Serial.println("Bahaya");
    digitalWrite(Merah, HIGH);
  }
  else
  {
    Serial.println("Aman");
    digitalWrite(Hijau, HIGH);
  }
  Blynk.run();
  timer.run();
  delay(500);
}

source code diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-esp32-devkit-v1", "id": "esp", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -141.2,
      "left": -238.67,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -64.2,
      "left": -237.67,
      "attrs": { "color": "yellow" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": 30.13,
      "left": -232.67,
      "attrs": { "color": "limegreen" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -108.2,
      "left": -187.67,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": -32.54,
      "left": -186.01,
      "attrs": { "value": "220" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": 61.13,
      "left": -191.01,
      "attrs": { "value": "220" }
    },
    { "type": "wokwi-hc-sr04", "id": "ultrasonic1", "top": -90.2, "left": 129, "attrs": {} }
  ],
  "connections": [
    [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    [ "esp:GND.2", "led3:C", "black", [ "h0" ] ],
    [ "led3:C", "led2:C", "black", [ "v3.07", "h-17", "v-97.33" ] ],
    [ "led2:C", "led1:C", "black", [ "v0.07", "h-11.33", "v-78" ] ],
    [ "led1:A", "r1:1", "green", [ "v0" ] ],
    [ "led2:A", "r2:1", "green", [ "v0" ] ],
    [ "led3:A", "r3:1", "green", [ "v0" ] ],
    [ "r3:2", "esp:D13", "green", [ "v0" ] ],
    [ "r2:2", "esp:D12", "green", [ "v0.07", "h9.34", "v151.33" ] ],
    [ "r1:2", "esp:D14", "green", [ "v0.4", "h22.34", "v218.67", "h115.33" ] ],
    [ "ultrasonic1:GND", "esp:GND.1", "black", [ "v0" ] ],
    [ "ultrasonic1:VCC", "esp:3V3", "red", [ "v0" ] ],
    [ "ultrasonic1:ECHO", "esp:D15", "blue", [ "v0" ] ],
    [ "ultrasonic1:TRIG", "esp:D4", "white", [ "v0" ] ]
  ]
}

Source code library.txt
# Wokwi Library List # See https://docs.wokwi.com/guides/libraries Blynk HCSR04 ultrasonic sensor

Project Libraries

    Installed Libraries
  • Blynk
  • HCSR04 ultrasonic sensor

Tidak ada komentar:

Posting Komentar