|
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
This example runs directly on ESP8266 chip.
Note: This requires ESP8266 support package:
https://github.com/esp8266/Arduino
Please be sure to select the right ESP8266 module
in the Tools -> Board menu!
Change WiFi ssid, pass, and Blynk auth token to run 
Feel free to apply it to any other example. It's simple!
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <TridentTD_LineNotify.h>
#define interruptPin1 D5
#define interruptPin2 D6
#define startvalve D7
#define reverse D8 //D3 กลับทิศทางหมุนวาวล์
#define DHTPIN D4 //D2 อ่านค่าอุณหภูมิ
#define SWITCHLINE D3 //D1 แจ้งเตือนแบตเสื่อม
#define DHTTYPE DHT22 //กำหนดชนิด DHT เป็นแบบ DHT22
#define LINE_TOKEN "u7lUxrRWqXH70hrywpD7d5oAApFlCuE06L4TGUcPEJz" //Token ที่ได้จากการสมัคร ID Line
DHT dht(DHTPIN, DHTTYPE); //SET ตัวแปร dht ให้อ่านค่าจาก sensor
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "RSoM4pfv3Rrd2TOJeT8LalmXmxXfHOEy"; //authen code ของ blink
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "CISCOAdminOffice"; //ชื่อ wifi
char pass[] = ""; //รหัสผ่าน wifi
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
//valve open / close;
boolean valveopen = false; //สถานะวาวล์เปิด = true ปิด = false
BLYNK_WRITE(V5) //ถ้า Timer ของ Blink ทำงานให้สั่งเปิดวาวล์
{
// You'll get HIGH/1 at startTime and LOW/0 at stopTime.
// this method will be triggered every day
// until you remove widget or stop project or
// clean stop/start fields of widget
Serial.print("Got a value: ");
Serial.println(param.asInt());
if(param.asInt() == 0)
{
digitalWrite(startvalve, LOW);
}
if(param.asInt() == 1)
{
digitalWrite(startvalve, HIGH);
}
}
BLYNK_WRITE(V4) //ถ้า Timer ของ Blink ทำงานสั่งให้ปิดวาวล์
{
// You'll get HIGH/1 at startTime and LOW/0 at stopTime.
// this method will be triggered every day
// until you remove widget or stop project or
// clean stop/start fields of widget
Serial.print("Got a value: ");
Serial.println(param.asInt());
if(param.asInt() == 0)
{
digitalWrite(reverse, LOW);
}
if(param.asInt() == 1)
{
digitalWrite(reverse, HIGH);
digitalWrite(startvalve,HIGH);
}
}
void setup()
{
// Debug console
Serial.begin(9600); //กำหนด Buardrate ของ serial port
pinMode(startvalve,OUTPUT); //กำหนด pin Smartvale เป็น output
pinMode(reverse,OUTPUT); //กำหนด pin กลับทิศทาง เป็น output
digitalWrite(startvalve, LOW); //สั่ง ปิดวาวล์
digitalWrite(reverse, LOW); //สั่ง ปิดการกลับทิศทางหมุน
pinMode(SWITCHLINE,INPUT_PULLUP); //กำหนด swith แจ้งเตือนแบต เป็น input แบบ pull up
pinMode(interruptPin1, INPUT_PULLUP); //กำหนด Switch smart valve เป็น input แบบ pull up
pinMode(interruptPin2, INPUT_PULLUP); //กำหนด switch smart valve เป็น input แบบ pull up
attachInterrupt(digitalPinToInterrupt(interruptPin1), increment, FALLING); //กำหนดสั่งให้ switch1 ทำงานที่ function increment เมื่อเกิด interrupt
attachInterrupt(digitalPinToInterrupt(interruptPin2), increment, FALLING); //กำหนดสั่งให้ switch2 ทำงานที่ function increment เมื่อเกิด interrupt
dht.begin(); //สั่งให้ dht ทำการเปิด
Serial.println(LINE.getVersion()); //อ่านค่า version ของ line
// กำหนด Line Token
LINE.setToken(LINE_TOKEN);
Blynk.begin(auth, ssid, pass); //สั่งให้ blink ทำการเปิดใช้งาน
}
void loop()
{
Blynk.run(); //สั่งให้ blink ทำงาน
ReadTemp(); //สั่งให้ อ่านอุณหภูมิ
}
ICACHE_RAM_ATTR void increment(){
digitalWrite(startvalve,0); //สั่งหยุดวาวล์
digitalWrite(reverse,0); //สั่งหยุดการกลับทิศทางหมุน
valveopen = !valveopen; //เปลี่ยนค่า state ของ วาวล์
}
void ReadTemp() {
float t12 = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f12 = dht.readTemperature(true);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h12 = dht.readHumidity();
int Val = 0;
bool dht12Read = true;
// Check if any reads failed and exit early (to try again).
if (isnan(h12) || isnan(t12) || isnan(f12)) {
Serial.println("Failed to read from DHT12 sensor!");
dht12Read = false;
}
if (dht12Read){
// Compute heat index in Fahrenheit (the default)
float hif12 = dht.computeHeatIndex(f12, h12);
// Compute heat index in Celsius (isFahreheit = false)
float hic12 = dht.computeHeatIndex(t12, h12, false);
// Compute dew point in Fahrenheit (the default)
//float dpf12 = dht.dewPoint(f12, h12);
// Compute dew point in Celsius (isFahreheit = false)
//float dpc12 = dht.dewPoint(t12, h12, false);
Serial.print("DHT12=> Humidity: ");
Serial.print(h12);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t12);
Serial.print(" *C ");
Serial.print(f12);
Serial.print(" *F\t");
Serial.print(" Heat index: ");
Serial.print(hic12);
Serial.print(" *C ");
Serial.print(hif12);
Serial.print(" *F");
Serial.print(" Dew point: ");
//Serial.print(dpc12);
Serial.print(" *C ");
//Serial.print(dpf12);
Serial.println(" *F");
Val = analogRead(A0)*100/1024;
Serial.println("A0="+String(Val)+" Valve State ="+String(valveopen));
Blynk.virtualWrite(V3, t12);
Blynk.virtualWrite(V2, h12);
Blynk.virtualWrite(V1, Val);
if (Val > 80) { // ถ้า Analog มากกว่า 80 จะสั่งให้วาวลปิด
if (valveopen==1) {
openvalve("OFF"); }
} else if (Val < 30) { //ถ้า Analog น้อยกว่า 30 จะสั่งให้วาวล์เปิด
if (valveopen==0) {
openvalve("ON"); }
}
}
if (digitalRead(SWITCHLINE)==0) { //ถ้าแบตเสื่อมให้แจ้งเตือนทาง line
LINE.notify("แบตสมาร์ทวาวล์ตัวที่ 1 เสื่อมแล้วจ้า !!");
}
}
void openvalve(String state) {
if (valveopen==false) { //ถ้าวาวล์ปิดอยู่จะสั่งเปิด
digitalWrite(startvalve,1);
digitalWrite(reverse,0);
}
if (valveopen==true) { //ถ้าวาวล์เปิดอยู่จะสั่งปิด
digitalWrite(startvalve,1);
digitalWrite(reverse,1);
}
}
|
|