· 5 years ago · Dec 09, 2020, 02:56 AM
1#include <DHT.h> // library for getting data from DHT
2#include <ESP8266WiFi.h> //Librarry connecting ESP8266 to connect with Wifi
3#include <TridentTD_LineNotify.h>
4#include <AsyncDelay.h>
5
6// ***********************************************Thingspeak Credentials*****************
7String apiKey = "TP5UB7FE1OYZCMJF"; //Write API key of your ThingSpeak channel
8const char* server = "api.thingspeak.com"; // API for thingspeak
9//****************************************************************************************
10
11const char* ssid = "BDMS_EQMT";
12const char* pass = "Bhh@4007";
13
14#define DHTPIN D4 //pin where the DHT22 is connected
15DHT dht(DHTPIN, DHT22);
16//#define DHTTYPE DHT22 // DHT 22 (AM2302)
17#define LINE_TOKEN1 "btWWxg5tc04BlNjyphlNMa04OU82haxDkI8b7TSxXeg"
18#define LINE_TOKEN2 "01fsPQBfCRqmeq87DsFmWq09MZyzlE7cVgRVI8rWk0H"
19AsyncDelay temperature_notify_delay; //เพิ่มส่วนนี้
20AsyncDelay humidity_notify_delay; //เพิ่มส่วนนี้
21int notify_time = 100000; //เพิ่มส่วนนี้ เวลาที่ต้องการทำซ้ำ
22
23
24WiFiClient client;
25void setup()
26{
27Serial.begin(115200); // Serial monitor Baudrate
28delay(10);
29//******************PowerSupply to the Sensor**********************
30//pinMode(D1, OUTPUT);
31//pinMode(D3, OUTPUT);
32//digitalWrite(D1,HIGH);
33//digitalWrite(D3,LOW);
34//delay(1000);
35//*********************************************************
36dht.begin();
37
38Serial.println("Trying to Connect with ");
39Serial.println(ssid);
40WiFi.begin(ssid, pass); // Connecting ESP8266 with Internet enabled Wifi with above mentioned credentials
41while (WiFi.status() != WL_CONNECTED)
42{
43// If the connection was unsuccesfull, it will try again and again
44delay(500);
45Serial.print(".");
46}
47// Connection succesfull
48Serial.println("");
49Serial.println("WiFi connected");
50}
51void loop()
52{
53
54float h = dht.readHumidity() +2.3 ; // Reading Temperature form DHT sensor
55float t = dht.readTemperature() +0.5; // Reading Humidity form DHT sensor
56if (isnan(h) || isnan(t))
57{
58Serial.println("Failed to read from DHT sensor!");
59return;
60}
61// Connecting to the Thingspeak API and Posting DATA
62if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
63{
64// Format of DATA Packet "Write API Key&field1=Temperature data&field2=Humidity Data"
65String postStr = apiKey;
66postStr +="&field2=";
67postStr += String(t);
68postStr +="&field3=";
69postStr += String(h);
70postStr += "\r\n\r\n";
71
72client.print("POST /update HTTP/1.1\n");
73client.print("Host: api.thingspeak.com\n");
74client.print("Connection: close\n");
75client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
76client.print("Content-Type: application/x-www-form-urlencoded\n");
77client.print("Content-Length: ");
78client.print(postStr.length());
79client.print("\n\n");
80client.print(postStr);
81client.flush();
82Serial.println(postStr);
83
84Serial.print("Temperature: ");
85Serial.print(t);
86Serial.print(" degrees Celcius, Humidity: ");
87Serial.print(h);
88Serial.println("%.");
89Serial.println("Data has been sussecfully sent to Thingspeak.");
90}
91client.stop();
92
93Serial.println("Waiting to initiate next data packet...");
94
95// thingspeak needs minimum 15 sec delay between updates
96delay(10000);
97
98if (t > 29&&temperature_notify_delay.isExpired()|| t < 15&&temperature_notify_delay.isExpired()) {
99 String LineText;
100 String string1 = "อุณหภูมิห้องยา Chemo อยู่นอกช่วงที่กำหนด ";
101 String string2 = " °C";
102
103 LineText = string1 + t + string2;
104 Serial.print("Line ");
105 Serial.println(LineText);
106 //LINE.notify(LineText);
107 LINE.setToken(LINE_TOKEN1);LINE.notify(LineText);LINE.notifySticker("รบกวนตรวจสอบความผิดปกติ",1,406);
108 LINE.setToken(LINE_TOKEN2);LINE.notify(LineText);LINE.notifySticker("รบกวนตรวจสอบความผิดปกติ",1,406);
109 temperature_notify_delay.start(notify_time, AsyncDelay::MILLIS); //เพิ่มส่วนนี้ เริ่มนับถอยหลัง
110 }
111
112 if (h > 75&&humidity_notify_delay.isExpired()|| h < 45&&humidity_notify_delay.isExpired()) {
113 String LineText;
114 String string3 = "ความชื้นห้องยา Chemo อยู่นอกช่วงที่กำหนด ";
115 String string4 = " % RH";
116 LineText = string3 + h + string4;
117 Serial.print("Line ");
118 Serial.println(LineText);
119 //LINE.notify(LineText);
120 LINE.setToken(LINE_TOKEN1);LINE.notify(LineText);LINE.notifySticker("รบกวนตรวจสอบความผิดปกติ",1,406);
121 LINE.setToken(LINE_TOKEN2);LINE.notify(LineText);LINE.notifySticker("รบกวนตรวจสอบความผิดปกติ",1,406);
122 humidity_notify_delay.start(notify_time, AsyncDelay::MILLIS); //เพิ่มส่วนนี้ เริ่มนับถอยหลัง
123 }
124
125}