IOT project one : ESP8266 Line notify เมื่ออุณหภูมิเกินกำหนด

Nutjakpun Chalarut
4 min readFeb 23, 2021

โปรเจคนี้เกิดขึ้นเพราะ มีพี่คนหนึ่งมาปรึกษาเกี่ยวกับอุปกรณ์ที่สามารถรายงานอุณหภูมิตามที่กำหนดได้ และด้วยผมกำลังสนใจ IOT จึงเข้าทางพอดี ผมจึงแนะนำเจ้า ESP8266 และเขียนอุปกรณ์ที่ต้องใช้ให้เขาไปซื้อ มีดังนี้

1. Node MCU : ESP8266

2. DHT11 (เซ็นเซอร์วัดความชื้น และอุณหภูมิ)

3. LCD16x2 หรือ LCD20x4

ESP8266
แจ้งอุณหภูมิผ่านไลน์

Code

#include <TridentTD_LineNotify.h>
#include <ESP8266WebServer.h>
ESP8266WebServer Server(80); // Replace with WebServer for ESP32

#include <EEPROM.h>
#include <TimeLib.h>
const char* ntpServer = “2.th.pool.ntp.org”;
const long gmtOffset_sec = 25200;//+7GMT 7x60x60
const int daylightOffset_sec = 0;

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include “DHT.h”
DHT dht(2, DHT11);

//Variables
int i = 0;
int statusCode;
String ssid2;
String passphrase2;
String st;
String content;
float Temp,HiTemp,LoTemp=45;
float HiHum,LoHum=150;
String Linetoken;
int freqToLine=5;
int hourtoline;
int nfcount;
const int led = 25;
long timebreak;
long timebreak2;
struct tm * timeinfo;

//Function Decalration
bool testWifi(void);
void launchWeb(void);
void setupAP(void);

void setup() {

WiFi.begin(ssid2.c_str(), passphrase2.c_str());

delay(10);
// initialize LCD and turn on LCD backlight
lcd.init();
lcd.backlight();
/*lcd.setCursor(0, 0);
lcd.print(“====================” );
lcd.setCursor(4, 1);
lcd.print(“Start Up!!!” );
lcd.setCursor(0, 2);
lcd.print(“Initailize Program” );
lcd.setCursor(0, 3);
lcd.print(“====================” );*/
// start the DHT
dht.begin();
Serial.begin(115200); //Initialising if(DEBUG)Serial Monitor

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);

char tOutput[5];
char hOutput[5];
dtostrf(t, 2, 1, tOutput);
dtostrf(h, 2, 1, hOutput);

// set cursor to first column, first row
lcd.setCursor(0, 0);
// print message
lcd.print(“T: “);
lcd.setCursor(3, 0);
lcd.print(tOutput);
lcd.setCursor(18, 0);
//lcd.print(“%”);
// set cursor to first column, second row
lcd.setCursor(9,0);
lcd.print(“H: “);
lcd.setCursor(12,0);
lcd.print(hOutput);


Serial.println();
Serial.print(“Connecting to “);
Serial.println(esid);
//lcd.clear();
lcd.setCursor(0, 3);
lcd.print(“Connecting “ );
WiFi.begin(esid.c_str(), epass.c_str());
if (testWifi())
{
Serial.println(“Succesfully Connected!!!”);
Serial.println(“IP: “ + WiFi.localIP().toString());
lcd.setCursor(0, 3);
lcd.print(“IP: “ +WiFi.localIP().toString());
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
LINE.notify(“Start : อุณหภูมิ : “+String(t)+” องศา “ +” และ ความชื้นสัมพัทธ์ : “+String(h)+” %”);
LINE.notify(“LINE Alert : T> “+String(TempNotify)+” องศา ส่ง “ +freq+” นาทีต่อครั้ง”);
LINE.notify(“LINE และเคลียรค่าอุณหภูมิสูงสุด ต่ำสุด “+String(hourline)+”:00 น.”);
//launchWeb();
//time from NTP server
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
delay(3000);
WiFi.softAPdisconnect(true);
return;
}
else
{
lcd.setCursor(0, 3);
lcd.print(“SetupWiFi on” );
Serial.println(“Turning the HotSpot On”);
launchWeb();
setupAP();// Setup HotSpot
}

Serial.println();
Serial.println(“Waiting.”);
Server.on(“/”, handleRoot);

while ((WiFi.status() != WL_CONNECTED))
{
//Serial.print(“.”);
delay(100);
Server.handleClient();
}
}

void loop() {
// Wait a few seconds between measurements.
//digitalWrite(led, HIGH);
delay(2000);
//digitalWrite(led, LOW);
//lcd.clear();
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
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);

char tOutput[5];
char hOutput[5];
dtostrf(t, 2, 1, tOutput);
dtostrf(h, 2, 1, hOutput);

// set cursor to first column, first row
lcd.setCursor(0, 0);
// print message
lcd.print(“T: “);
lcd.setCursor(3, 0);
lcd.print(tOutput);
lcd.setCursor(18, 0);

lcd.setCursor(9,0);
lcd.print(“H: “);
lcd.setCursor(12,0);
lcd.print(hOutput);

if(h > HiHum)HiHum=h;
if(h < LoHum)LoHum=h;
if(t > HiTemp) {
HiTemp=t;
char hiT[5];
dtostrf(HiTemp, 2, 1, hiT);
lcd.setCursor(0,2);
lcd.print(“Hi: “);
lcd.setCursor(4,2);
lcd.print(hiT);
}
if(t < LoTemp) {
LoTemp=t;
char LoT[5];
dtostrf(LoTemp, 2, 1, LoT);
lcd.setCursor(9,2);
lcd.print(“Lo: “);
lcd.setCursor(13,2);
lcd.print(LoT);
}

char buffer[80];
time_t rawtime;
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer,80,” %d %B %Y %H:%M:%S “,timeinfo);
//Serial.println(buffer);
char timeHour[3];
strftime(timeHour,3, “%H”, timeinfo);
//Serial.println(timeHour);
char Year[5];
strftime(Year,5, “%Y”, timeinfo);
//Serial.println(Year);

long minpass = millis()/1000/60;
if(atoi(Year) > 1999 && atoi(timeHour)== hourtoline && minpass >= timebreak2){
LINE.notify(“==== Highest Temperature and Lowest Temperature ====”);
LINE.notify(“อุณหภูมิสูงสุด “+String(HiTemp)+” องศา “ + “อุณหภูมิต่ำสุด “+String(LoTemp)+” องศา”);
LINE.notify(“ความชื้นสัมพัทธ์สูงสุด “+String(HiHum)+” % “ + “ความชื้นสัมพัทธ์ต่ำสุด “+String(LoHum)+” %”);
HiTemp=0;LoTemp=45;HiHum=0;LoHum=150;timebreak2=minpass+100;
}
if(t > Temp){
if(WiFi.status() == WL_CONNECTED && minpass >= timebreak) {
LINE.notify(“Temperature: “+String(t)+” celsius “ + “Humidity: “+String(h)+” %”);
nfcount=nfcount+1;
lcd.setCursor(9,1);
lcd.print(“Sent: “);
lcd.setCursor(15,1);
lcd.print(nfcount);
//Serial.print(“min”);
//Serial.println(minpass);
timebreak = minpass+freqToLine;
//Serial.print(“time”);
//Serial.println(timebreak);
}
}
lcd.setCursor(0, 3);
if(WiFi.status() != WL_CONNECTED) {
Serial.println(ssid2.c_str());
//Serial.println(passphrase.c_str());
WiFi.begin(ssid2.c_str(),passphrase2.c_str());
testWifi();
lcd.print(“Connect Fail!!! “);}
else{lcd.print(“IP: “ +WiFi.localIP().toString());}
}

// — — — — — — — — — — — — — — — — — — — — — — — — Fuctions used for WiFi credentials saving and connecting to it which you do not need to change
bool testWifi(void)
{
int c = 0;
Serial.println(“Waiting for Wifi to connect”);
while ( c < 20 ) {
if (WiFi.status() == WL_CONNECTED)
{
return true;
}
delay(500);
Serial.print(“*”);
c++;
}
Serial.println(“”);
Serial.println(“Connect timed out, opening AP”);
return false;
}

--

--