//----------------------------------------Include the NodeMCU ESP8266 Library
#define BLYNK_PRINT Serial
#include <HCSR04.h>
#include <TridentTD_LineNotify.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include "DHT.h"
#include <BlynkSimpleEsp8266.h>
UltraSonicDistanceSensor distanceSensor(D7, D8);
//----------------------------------------
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define DHTPin D2
String t;
#define ON_Board_LED 2 //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router
const char* auth = "FLtkZubxWNY86eDRG7-Xke7o8SSVMINM";
//----------------------------------------SSID dan Password wifi mu gan.
const char* ssid = "Pu_Ley 1_2.4GHz"; //--> Nama Wifi / SSID.
const char* password = "0635499306"; //--> Password wifi .
//----------------------------------------
//----------------------------------------Host & httpsPort
const char* host = "script.google.com";
const int httpsPort = 443;
//----------------------------------------
#define LINE_TOKEN "tS53ncQXmybWsthYxiz4J6Ojm8lWAxarUanRYQ5YdgE"
BlynkTimer timer;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
WiFiClientSecure client; //--> Create a WiFiClientSecure object.
// Timers auxiliar variables
String GAS_ID = "AKfycbwu1avyG5CGdDkB7-rJhMQJ14d0nJA0D1gmR-xFY2oETtB29k6DZ9PM1VkfX1rVktNlhg"; //--> spreadsheet script ID
bool a=0;
bool b=0;
bool c=0;
bool d=0;
bool e=0;
bool f=0;
//============================================ void setup
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(500);
Blynk.begin(auth, ssid, password,"blynk.cloud",80);
dht.begin();
LINE.setToken(LINE_TOKEN);
LINE.notify("Reset");
WiFi.begin(ssid, password); //--> Connect to your WiFi router
Serial.println("");
pinMode(ON_Board_LED,OUTPUT); //--> On Board LED port Direction output
digitalWrite(ON_Board_LED, HIGH); //-->
//----------------------------------------Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
//----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
digitalWrite(ON_Board_LED, LOW);
delay(250);
digitalWrite(ON_Board_LED, HIGH);
delay(250);
//----------------------------------------
}
//----------------------------------------
digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
//----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor
Serial.println("");
Serial.print("Successfully connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
//----------------------------------------
timer.setInterval(1000L,temp_humi);
timer.setInterval(1000L,cm);
timer.setInterval(1000L,blynk_);
client.setInsecure();
}
//==============================================================================
BLYNK_CONNECTED()
{
Blynk.syncAll();
Blynk.virtualWrite(V5,255);
}
void temp_humi()
{
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Computes temperature values in Celsius
float hic = dht.computeHeatIndex(t, h, false);
static char temperatureTemp[7];
dtostrf(hic, 6, 2, temperatureTemp);
// Uncomment to compute temperature values in Fahrenheit
// float hif = dht.computeHeatIndex(f, h);
// static char temperatureTemp[7];
// dtostrf(hif, 6, 2, temperatureTemp);
static char humidityTemp[7];
dtostrf(h, 6, 2, humidityTemp);
Serial.print("Humidity: ");
Serial.println(h);
Serial.print(" %\t Temperature: ");
Serial.println(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t Heat index: ");
Serial.print(hic);
Serial.println(" *C ");
// Serial.print(hif);
// Serial.println(" *F");
Blynk.virtualWrite(V0,t);
Blynk.virtualWrite(V1,h);
if(t>35&&c==0)
{
Serial.println("temp_สูง");
LINE.notify("ค่าอุณหภูมิสูง");
LINE.notify(t);
c=1;
d=0;
}
if(t>25&&t<=35&&d==0)
{
Serial.println("tempปกติ");
LINE.notify("ค่าอุณหภูมิปกติ");
LINE.notify(t);
d=1;
c=0;
f=0;
}
if(t<=25&&f==0)
{
Serial.println("tempต่ำ");
LINE.notify("ค่าอุณหภูมิต่ำ");
LINE.notify(t);
f=1;
}
////////////////////////////////////////
if(h>60&&a==0)
{
Serial.println("OK_a==1");
a=1;
b=0;
LINE.notify("ค่าความชื้นสูง");
LINE.notify(h);
}
if(h>55&&h<=60&&b==0)
{
Serial.println("OK_a==0");
LINE.notify("ค่าความชื้นปกติ");
LINE.notify(h);
a=0;
b=1;
e=0;
}
if(h<=55&&e==0)
{
Serial.println("Hiymidityต่ำ");
LINE.notify("ค่าความชื้นต่ำ");
LINE.notify(h);
e=1;
}
float value =t;
float value2=h;
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);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
}
///////////////////////
/////////////////////////////
void blynk_()
{
if(Blynk.connected())
{
Serial.println("Connectted_to_blynk");
}
if(!Blynk.connected())
{
LINE.notify("Connect_Blynk_fail");
ESP.reset();
}
}
void cm()
{
float i= distanceSensor.measureDistanceCm();
Serial.print(i);
Serial.println("CM");
;
Serial.println("Level");
Blynk.virtualWrite(V3,i);
}
void loop() {
Blynk.run();
timer.run();
}
ลองเอา
#define LINE_TOKEN “tS53ncQXmybWsthYxiz4J6Ojm8lWAxarUanRYQ5YdgE”
ไว้ด้านบนยังครับ
ย้ายแล้วครับยังไม่ได้
มันแจ้งเตือนแค่ตอนที่มันยังไม่ทำcode google sheets
แต่ถ้าทำไปแล้วรอบ1จะส่งไลน์ไม่ได้เลย
น่าจะมีปัญหาตรง code google sheets โค้ดไม่ถูก ไม่มี token ในการเชื่อมต่อ google sheets ถ้าทำได้แล้วอาจจะต้อง loop ให้ส่งใน LINE
ถ้าcodeตัวนี้สามรถส่งค่าไปgoogle sheetได้
ผมลองให้Code google sheetทำงาน1รอบ แต่ยังไม่ส่งไลน์เหมือนกับว่าถ้าcode google sheetทำงานได้1รอบมันจะบล็อคการส่งไลน์ครับ:cold_sweat:
แต่ถ้าบอร์ดReset จะแจ้งในไลน์เพียงแค่ครั้งเดียว
เคยลองใส่whileให้ส่งไลน์ตลอดแล้วยังไม่ได้เหมือนเดิม:joy:
1 Likes
ลองเอา
LINE.setToken(LINE_TOKEN);
ไว้ล่าง
void setup() {
}
ผมว่าชื่อ class น่าจะตีกันในระดับ library ครับ ต้องเข้าไปดูในนั้น