/*************************************************************
This is a simple demo of sending and receiving some data.
Be sure to check out other examples!
*************************************************************/
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6agtFUmva"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "J186sriurWqBxNAgoCCgELu7RUTG8HCU"
// #define BLYNK_TEMPLATE_ID "TMPL6agtFUmva"
// #define BLYNK_TEMPLATE_NAME "Quickstart Template"
// #define BLYNK_AUTH_TOKEN "2zGB2stwJKEXYkN-zwIJBuKbI8Qc9ZEJ"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include "DHT.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h> //Google Sheet
#include "ThingSpeak.h" //Thingspeak
#include <BlynkSimpleEsp32.h> //Blynk
#include "TridentTD_LineNotify.h" //Line Notify
#define DHTPIN 27
#define DHTTYPE DHT22
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Line Notify
#define LINE_TOKEN "J3qP2AqYO36KghlT3pet6lnuLJUVmPzpREIejBiUbQ0"
bool sended = false; //ตัวเช็คให้ส่งแบบไม่ผลัด
//Google Sheet
const char* host = "script.google.com";
const int httpsPort = 443;
// Thingspeak
unsigned long myChannelNumber = 2270904; //เลข ID
const char* myWriteAPIKey = "JM9989SUKMHVBG4A"; //API KEY
const int LED1 = 25;
const int LED2 = 26;
BlynkTimer timer;
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
WiFiClientSecure clientSheet; //--> Create a WiFiClientSecure object.
String GAS_ID = "AKfycbzkAVIsd7dB8nINUVvSN1J_CLIPAlbma2HvsdMZH7jHaaDx0HdJxeOBIrimGfOmmp2-";
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V4) //คำสั่งปุ่มกดใน blynk
{
// Set incoming value from pin V0 to a variable
int value = param.asInt(); //ตัวแปรรับค่าจาก V4 คือ HIGH หรือ LOW
// Update state
Blynk.virtualWrite(V2, value); //สั่งให้ledในblynkติด
digitalWrite(LED1,value); //สั่งให้ledในบอร์ดติด
}
BLYNK_WRITE(V5) //คำสั่งปุ่มกดใน blynk
{
// Set incoming value from pin V0 to a variable
int value = param.asInt(); //ตัวแปรรับค่าจาก V4
// Update state
Blynk.virtualWrite(V3, value);//สั่งให้ledในblynkติด
digitalWrite(LED2,value);//สั่งให้ledในบอร์ดติด
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
// Change Web Link Button message to "Congratulations!"
}
// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
// Blynk.virtualWrite(V2, millis() / 1000);
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V1, h);
Serial.print(F("Blynk : "));
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(F("°C "));
if ((t) >= 35.0) {
Blynk.virtualWrite(V3, HIGH);//สั่งให้ledในblynkติด
// digitalWrite(LED2,HIGH);//สั่งให้ledในบอร์ดติด
}else{
Blynk.virtualWrite(V3, LOW);//สั่งให้ledในblynkติด
// digitalWrite(LED2,LOW);//สั่งให้ledในบอร์ดติด
}
}
void setup()
{
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
// Debug console
Serial.begin(115200);
Serial.println(LINE.getVersion());
dht.begin();
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// line Notify
LINE.setToken(LINE_TOKEN);
// --------------> ThingSpeak <--------------------
WiFi.mode(WIFI_STA);
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
delay(5000);
}
Serial.println("\nConnected.");
}
ThingSpeak.begin(client); // Initialize ThingSpeak
Serial.println(F("DHTxx test!"));
// dht.begin();
// --------------> /ThingSpeak <--------------------
dht.begin();
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(3000L, myTimerEvent);
}
void loop()
{
// --------------> Blynk <--------------------
Blynk.run();
timer.run();
// --------------> ฺ/blynk <--------------------
// --------------> ThingSpeak <--------------------
// Wait a few seconds between measurements.
delay(10000);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) ) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// // Compute heat index in Fahrenheit (the default)
// float hif = dht.computeHeatIndex(f, h);
// // Compute heat index in Celsius (isFahreheit = false)
// float hic = dht.computeHeatIndex(t, h, false);
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, h);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
// x = 200 ซึ่งหมายถึงการสำเร็จ หรือ 404 หรือ 500 ซึ่งหมายถึงความล้มเหลวในการส่งข้อมูล.
if (x == 200) {
Serial.println("Channel update successful.");
} else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
Serial.print(F("ThingSpeak : "));
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(F("°C "));
if (((t) >= 35.0) && (sended == false)) {
sended = true;
digitalWrite(LED2,HIGH);//สั่งให้ledในบอร์ดติด
LINE.notify("<======================>");
LINE.notify("แจ้งเตือนอุณหภูมิเกิน 35 องศา");
LINE.notify("อุณหภูมิขณะนี้ : " + String(t) + " องศา");
LINE.notify("ความชื้นขณะนี้ : " + String(h) + " %");
LINE.notify("<==========END=========>");
Serial.println(F("Send message to Line Notify Successful"));
}else{
digitalWrite(LED2,LOW);//สั่งให้ledในบอร์ดติด
sended = false;
}
delay(3000);
// --------------> /ThingSpeak <--------------------
// --------------> /GoogleSheet <--------------------
// WiFi.begin(ssid, pass); //--> Connect to your WiFi router
// Serial.println("");
// //----------------------------------------Wait for connection
// Serial.print("Connecting");
// while (WiFi.status() != WL_CONNECTED) {
// Serial.print(".");
// delay(100);
// }
// Serial.println("");
// Serial.print("Successfully connected to : ");
// Serial.println(ssid);
// Serial.print("IP address: ");
// Serial.println(WiFi.localIP());
// Serial.println();
// clientSheet.setInsecure();
// sendData(t, h);
// --------------> /GoogleSheet <--------------------
}
void sendData(float value, float value2) {
Serial.println("==========");
Serial.print("connecting to ");
Serial.println(host);
//----------------------------------------Connect to Google host
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
//----------------------------------------
//----------------------------------------Proses dan kirim data
float string_temp = value;
float string_humi = value2;
String url = "/macros/s/" + GAS_ID + "/exec?temp=" + string_temp + "&humi=" + string_humi;
// 2 variables
Serial.print("requesting URL: ");
Serial.println(url);
clientSheet.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: BuildFailureDetectorESP32\r\n" + "Connection: close\r\n\r\n");
Serial.println("request sent");
//----------------------------------------
//---------------------------------------
while (clientSheet.connected()) {
String line = clientSheet.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = clientSheet.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp32/Arduino CI successfull!");
} else {
Serial.println("esp32/Arduino CI has failed");
}
Serial.print("reply was : ");
Serial.println(line);
Serial.println("closing connection");
Serial.println("==========");
Serial.println();
//----------------------------------------
}