#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#define D0 13
//#define D1 10
#define statusLED D7
#define TRIGGERPIN D1
#define ECHOPIN D2
char auth[] = "TOKEN"; //Token key
char server[] = "oasiskit.com";
char ssid[] = "Net24g";
char pass[] = "1234567";
int port = 8080;
//char led1 = D0;
//char led2 = D1;
long distance = 0.0;
long alertValueIn = 10.0;
long alertValueOut = 20.0;
String text_In = "";
int cntIn = 0;
BLYNK_WRITE(V2)
{
int pinValue = param.asInt();
Serial.print("Slider ==> ");
Serial.println(pinValue);
alertValueIn = pinValue;
}
BLYNK_WRITE(V4)
{
int pinValue = param.asInt();
Serial.print("Slider ==> ");
Serial.println(pinValue);
alertValueOut = pinValue;
}
void setup()
{
Serial.begin(9600);
Serial.println("กำลังเชื่อมต่อไปที่ Blynk Server");
pinMode(D0, OUTPUT); //กำหนดโหมด ว่าเป็น INPUT หรือ OUTPUT
pinMode(D1, OUTPUT);
pinMode(statusLED, OUTPUT);
analogWriteFreq(500);
analogWriteRange(100);
WiFi.begin(ssid, pass); //เชื่อมต่อ WiFi
Blynk.config(auth, server, port); //กำหนด Token key , ชื่อ Server และ port
Blynk.connect(); //เชื่อมต่อไปยัง Blynk
}
BLYNK_CONNECTED()
{ // ฟังก์ชันนี้ทำงานเมื่อต่อ Blynk ได้
Serial.println("App Blynk ทำงาน!");
Blynk.virtualWrite(V4, alertValueOut);
Blynk.virtualWrite(V2, alertValueIn);
}
void loop()
{
Blynk.run();
distance = readUltrasonic();
Blynk.virtualWrite(V5, distance);
if (distance <= alertValueIn || distance >= alertValueOut)
{
text_In = text_In + " , " + distance;
Blynk.virtualWrite(V1, text_In);
delay(100);
distance = readUltrasonic();
Blynk.virtualWrite(V5, distance);
if (distance <= alertValueIn || distance >= alertValueOut)
{
Blynk.virtualWrite(V3, 1);
// digitalWrite(D7, HIGH);
analogWrite(statusLED, 100);
text_In = text_In + " , " + distance;
Blynk.virtualWrite(V1, text_In);
delay(4000);
}
}
else
{
Blynk.virtualWrite(V3, 0);
// digitalWrite(D7, LOW);
analogWrite(statusLED, 0);
}
if (text_In.length() > 35)
{
text_In = text_In.substring(25, text_In.length());
}
delay(100);
}
long readUltrasonic()
{
long duration, distance;
digitalWrite(TRIGGERPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGERPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGERPIN, LOW);
duration = pulseIn(ECHOPIN, HIGH);
distance = (duration / 2) / 29.1;
Serial.print(distance);
Serial.println("Cm");
return distance;
}
ใช่ได้รึยังครับ