// pre declare function
void Line_Notify1(String message);
void think_speak(String message);
// braud rate คือสัญญาณ(*สัญญาณ ไม่ใช่ bit)ที่ส่งได้ใน 1 วินาที ต้อง set ให้ตรงกับ arduino
const int braudRate = 9600;
// data seprator คือข้อมูลที่ส่งจาก arduino เเต่ละชุดเเบ่งด้วยตัวอักษรอะไร
const char dataSeperator = '\n';
#include <WiFi.h>
#include <WiFiClientSecure.h>
#define WIFI_SSID "oppo" // ใส่ชื่อ Wifi
#define WIFI_PASSWORD "hacker2345" // ใส่รหัส Wifi
#define LINE_TOKEN_PIR "YyG8Dxa8OMKIx5qViyXrWOQe0jXhUx7MPqiWeUuWtD7" // ใส่ token ของตนเอง
String message1 = "%E0%B8%A1%E0%B8%B5%E0%B8%9C%E0%B8%B9%E0%B9%89%E0%B8%9A%E0%B8%B8%E0%B8%81%E0%B8%A3%E0%B8%B8%E0%B8%81!"; // ใส่ข้อความแจ้งเตือน
uint32_t time1, time2;
void setup() {
// put your setup code here, to run once:
Serial.begin(braudRate);
}
void loop() {
delay(1000);
// put your main code here, to run repeatedly:
/*
if(Serial.available()>0){
// ready to read rx
String data = Serial.readStringUntil(dataSeperator);
// print debug
Serial.print("raw data =>");
Serial.print(data);
*/
String data = "12.3,3.14";
int comma_at = data.indexOf(',');
if(comma_at != -1){
// have comma in data
//data => tem,hum,volt จบด้วย \n
float tem=data.substring(0 , comma_at) .toFloat();
float hum=data.substring(comma_at+1 ) .toFloat();
Serial.println(tem);
Serial.println(hum);
}else{
// not have comma in data
Serial.println("dont have comma in data");
}
//}else{
// //not ready to read rx
// Serial.println("not ready to read RX");
//}
}
///////////////////////////////////////
//function
void Line_Notify1(String message) {
WiFiClientSecure client;
client.setInsecure(); // ถ้าเป็น ESP32 เวอร์ชั่น 1.0.4 ให้ปิดส่วนนี้ไว้
if (!client.connect("notify-api.line.me", 443)) {
Serial.println("connection failed");
delay(2000);
return;
}
String req = "";
req += "POST /api/notify HTTP/1.1\r\n";
req += "Host: notify-api.line.me\r\n";
req += "Authorization: Bearer " + String(LINE_TOKEN_PIR) + "\r\n";
req += "Cache-Control: no-cache\r\n";
req += "User-Agent: ESP8266\r\n";
req += "Content-Type: application/x-www-form-urlencoded\r\n";
req += "Content-Length: " + String(String("message=" + message1).length()) + "\r\n";
req += "\r\n";
req += "message=" + message1;
// Serial.println(req);
client.print(req);
delay(20);
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
}
void think_speak(String message){
}