ตอนแรกที่รันค่าตัวแอพและมอนิเตอร์ยังแสดงผลได้ พอเขียนโค้ดเพื่อไปแจ้งเตือนที่ไลน์เพิ่มก็ไม่แสดงค่าอะไรอีกเลยค่ะ
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPLuJkZnfoS"
#define BLYNK_DEVICE_NAME "Autometic pump"
#define BLYNK_AUTH_TOKEN "UPG49bfoFyJP7R35B6mJAdrtnEkV6O9u"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include<SoftwareSerial.h>
#include <TridentTD_LineNotify.h>
#include <SimpleTimer.h>
SoftwareSerial mySerial(D5,D6);
#define LINE_TOKEN "bmZxI8G4j7Cg63dXY2mgIElHRqbGYYOgfJc9fLqoulr"
char auth[] = "UPG49bfoFyJP7R35B6mJAdrtnEkV6O9u";
char ssid[] = "EEE";
char pass[] = "ern12112542";
SimpleTimer timer;
unsigned char data[4]={};
float distance;
int waterlevel;
const int relay = D7;
int modeSelect = 1;
bool waterPumpStatus;
WidgetLCD LCD(V0);
WidgetLED PUMP(V1);
void setup()
{
pinMode(relay, OUTPUT);
Serial.begin(57600);
mySerial.begin(9600);
Blynk.begin(auth, ssid, pass);
LINE.setToken(LINE_TOKEN);
digitalWrite(relay, LOW);
timer.setInterval(100, sensor);
}
void sensor()
{
do{
for(int i=0;i<4;i++)
{
data[i]=mySerial.read();
}
}while(mySerial.read()==0xff);
mySerial.flush();
if(data[0]==0xff)
{
int sum;
sum=(data[0]+data[1]+data[2])&0x00FF;
if(sum==data[3])
{
distance=(data[1]<<8)+data[2];
if(distance>280)
{
Serial.print("distance=");
Serial.print(distance/10);
waterlevel=(400-(distance/10));
Serial.println("cm");
}
else
{
Serial.println("Below the lower limit");
}
}
else Serial.println("ERROR");
}
Blynk.virtualWrite(V2, waterlevel);
delay(100);
}
void autoPump()
{
sensor();
if(waterlevel>=320)
{
waterPumpStatus = 1;
}
if(waterlevel <=190)
{
waterPumpStatus = 0;
}
}
void loop()
{
checkPump();
checkFunction();
checkLevel();
Blynk.run();
timer.run();
}
void checkPump()
{
if (waterPumpStatus==1)
{
digitalWrite(relay, HIGH);
PUMP.on();
LINE.notify("PUMP ON");
}
else
{
digitalWrite(relay, LOW);
PUMP.off();
LINE.notify("PUMP OFF");
}
}
void checkFunction()
{
if (modeSelect == 2)
{
autoPump();
}
}
void closePump()
{
if (waterPumpStatus == 1)
{
waterPumpStatus = 0;
}
}
BLYNK_WRITE(V4)
{
if (param.asInt())
{
closePump();
modeSelect++;
if (modeSelect >= 3)
modeSelect = 1;
modeLabel();
}
}
BLYNK_WRITE(V3)
{
if (param.asInt())
{
modeSelect = 1;
waterPumpStatus = 1;
modeLabel();
}
else
{
modeSelect = 1;
waterPumpStatus = 0;
modeLabel();
}
}
void modeLabel()
{
if (modeSelect == 1)
{
Blynk.setProperty(V4, "onLabel", "MANUAL");
Blynk.setProperty(V4, "offLabel", "MANUAL");
LINE.notify("MANUAL MODE");
}
else if (modeSelect == 2)
{
Blynk.setProperty(V4, "onLabel", "AUTO");
Blynk.setProperty(V4, "offLabel", "AUTO");
LINE.notify("AUTO MODE");
}
}
void checkLevel()
{
sensor();
if(waterlevel <=320)
{
LCD.clear();
LCD.print(0,0,"Level 3");
LCD.print(0,1,"It's crisis!");
LINE.notify("Level 3");
LINE.notify("It's crisis!");
}
else if((waterlevel >=260) && waterlevel <=280)
{
LCD.clear();
LCD.print(0,0,"Level 2");
LCD.print(0,1,"Will you pump?");
LINE.notify("Level 2");
LINE.notify("Will you pump?");
}
else if((waterlevel >=200) && waterlevel <=220)
{
LCD.clear();
LCD.print(3,0,"Level 1");
LCD.print(0,1,"It's OK.");
LINE.notify("Level 1");
LINE.notify("slightly higher water.Will you pump? ");
}
}