# data[].attributes.body
เก

# data[].attributes.body
#define BLYNK_TEMPLATE_ID "TMPL6NVtMNfsB"

#define BLYNK_TEMPLATE_NAME "thanatron"

#define BLYNK_AUTH_TOKEN "YneuMb4hW6PwHr9rkn1VHyG6nUbuu8We"

#include <WiFi.h>

#include <BlynkSimpleEsp32.h>

// ====== WiFi ======

const char* ssid = "electronic5";

const char* password = "1234567890";

// ====== Ultrasonic Sensor ======

#define TRIG_PIN 5

#define ECHO_PIN 18

long duration;

float distance;

// ====== Blynk Timer ======

BlynkTimer timer;

// ฟังก์ชันวัดระยะทาง

void sendSensor() {

digitalWrite(TRIG_PIN, LOW);

delayMicroseconds(2);

digitalWrite(TRIG_PIN, HIGH);

delayMicroseconds(10);

digitalWrite(TRIG_PIN, LOW);

duration = pulseIn(ECHO_PIN, HIGH);

distance = duration * 0.034 / 2; // คำนวณเป็น cm

Serial.print("Distance: ");

Serial.print(distance);

Serial.println(" cm");

// ส่งค่าไปที่ Blynk (Virtual Pin V1)

Blynk.virtualWrite(V1, distance);

}

void setup() {

Serial.begin(115200);

pinMode(TRIG_PIN, OUTPUT);

pinMode(ECHO_PIN, INPUT);

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);

// ส่งค่าขึ้นทุกๆ 1 วินาที

timer.setInterval(1000L, sendSensor);

}

void loop() {

Blynk.run();

timer.run();

}

# data[].attributes.body
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6LC7XmK1z"
#define BLYNK_TEMPLATE_NAME "Theerayut Inwza2"
#define BLYNK_AUTH_TOKEN "KaEcVJqCjJU5oQpWeIw2Pl_SWKCZ4Ke-"

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

BlynkTimer timer;

char ssid[] = "electronic24";
char pass[] = "1234567890";

#define trigPin 12
#define echoPin 13
#define led1 25
#define led2 26
#define sw 16
#define vr 33

bool sw1State = HIGH;
bool led2State = LOW;

#define VPIN10 V10   // vr
#define VPIN11 V11   // switch
#define VPIN13 V13   // distance

BLYNK_CONNECTED() {
  Blynk.syncVirtual(VPIN10);
  Blynk.syncVirtual(VPIN11);
  Blynk.syncVirtual(VPIN13);
}

BLYNK_WRITE(VPIN11) {

  int value = param.asInt();
  if (value == 1) {
    led2State = !led2State;
    digitalWrite(led2, led2State);
    Blynk.virtualWrite(VPIN11, led2State);
  }

}

void senser1() {
  int vr1 = analogRead(vr);
  Blynk.virtualWrite(VPIN10, vr1);
}

void senser2() {
  long duration;
  float distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  Blynk.virtualWrite(VPIN13, distance);
  Serial.print("distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  if (distance < 20) {
    digitalWrite(led1, HIGH);
  } else {
    digitalWrite(led1, LOW);
  }

}

void setup() {
  Serial.begin(115200);
  pinMode(vr, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(sw, INPUT);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(500L, senser1);
  timer.setInterval(500L, senser2);
}

void loop() {
  Blynk.run();
  timer.run();
  }
}