|
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include "Arduino.h"
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <TridentTD_LineNotify.h>
#define interruptPin1 36 //ถ้า = 0 คือตำแหน่งวาวล์เปิด
#define interruptPin2 39 //ถ้า = 0 คือตำแหน่งวาวล์ปิด
#define startvalve 34 //เปิดปิดมอร์เตอร์
#define reverse 35 //D0 กลับทิศทางหมุนวาวล์
#define DHTPIN 32 //D2 อ่านค่าอุณหภูมิ
#define SWITCHLINE 33 //D1 แจ้งเตือนแบตเสื่อม
#define DHTTYPE DHT22 //กำหนดชนิด DHT เป็นแบบ DHT22
#define LINE_TOKEN "6rET1Pusz6dMNU9K2S83465XPLXZWb8mFAHHRgLMpmK" //Token ที่ได้จากการสมัคร ID Line
#define flo 26 //วัดอัตราการไหลของน้ำ
#define presure 27 //วัดแรงดันน้ำ
#define Soil 14 //วัดควชื้นดิน
#define ldr 12 //วัดแสง
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[] = "B322-1RuS3emFSBx-Aa_RjZYUJmH2DS6"; //authen code ของ blink
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "IoT99"; //ชื่อ wifi
char pass[] = "iot0837286389"; //รหัสผ่าน 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), IWOn, FALLING); //กำหนดสั่งให้ switch1 ทำงานที่ function increment เมื่อเกิด interrupt
//attachInterrupt(digitalPinToInterrupt(interruptPin2), IWOff, 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 ทำการเปิดใช้งาน
if (digitalRead(interruptPin1)==0)
valveopen=true;
else
valveopen=false;
}
void loop()
{
Blynk.run(); //สั่งให้ blink ทำงาน
ReadTemp(); //สั่งให้ อ่านอุณหภูมิ
ReadPressure();
}
ICACHE_RAM_ATTR void IWOn(){ //d5=0
if (valveopen==false) {
digitalWrite(reverse,0); //สั่งหยุดการกลับทิศทางหมุน
digitalWrite(startvalve,0); //สั่งหยุดวาวล์
}
valveopen = true; //เปลี่ยนค่า state ของ วาวล์
Serial.println("Valve ON");
}
ICACHE_RAM_ATTR void IWOff(){ //d6=0
if (valveopen==true) {
digitalWrite(reverse,0); //สั่งหยุดการกลับทิศทางหมุน
digitalWrite(startvalve,0); //สั่งหยุดวาวล์
}
valveopen = false; //เปลี่ยนค่า state ของ วาวล์
Serial.println("Valve Off");
}
void ReadPressure() {
const float offset = 0.483;
float V, Ps;
V = analogRead(presure) * 5/4096;
Ps = (V-offset)*400;
Serial.print(Ps,1);
Serial.println("KPa");
}
void ReadAnalog() {
int Val = analogRead(A0)*100/1024;
Serial.println("A0="+String(Val)+" Valve State ="+String(valveopen));
Blynk.virtualWrite(V1, Val);
if (Val > 40) { // ถ้า Analog มากกว่า 80 จะสั่งให้วาวลปิด
if (valveopen==true) {
openvalve("OFF"); }
}
if (Val < 30) { //ถ้า Analog น้อยกว่า 30 จะสั่งให้วาวล์เปิด
if (valveopen==false) {
openvalve("ON"); }
}
}
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");
Blynk.virtualWrite(V3, t12);
Blynk.virtualWrite(V2, h12);
}
if (digitalRead(SWITCHLINE)==0) { //ถ้าแบตเสื่อมให้แจ้งเตือนทาง line
LINE.notify("แบตสมาร์ทวาวล์ตัวที่ 1 เสื่อมแล้วจ้า !!");
}
delay(2000);
}
void openvalve(String state_v) {
if (state_v=="ON") { //ถ้าวาวล์ปิดอยู่จะสั่งเปิด
digitalWrite(reverse,0);
digitalWrite(startvalve,1);
}
if (state_v=="OFF") { //ถ้าวาวล์เปิดอยู่จะสั่งปิด
digitalWrite(reverse,1);
digitalWrite(startvalve,1);
}
}
|
|