Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Line_Notify(String message);
- #include <ESP8266WiFi.h>
- #include <DHT.h>
- #define DHTPIN 2 //D4
- // Existing WiFi network
- const char* ssid = "Jaidee_iot"; // your wifi name
- const char* password = ""; // your wifi password
- #define LINE_TOKEN "v0EJG1CGNaYf7APFmzuDhSa6qWXvj3NRiEIGsOhoT41" // LINE Notify token
- #define DHTTYPE DHT11 // DHT 11
- //#define DHTTYPE DHT22 // DHT 22 (AM2302)
- //#define DHTTYPE DHT21 // DHT 21 (AM2301)
- DHT dht(DHTPIN, DHTTYPE);
- void setup() {
- Serial.begin(115200);
- Serial.println("Weather");
- dht.begin();
- // Connect to your WiFi network
- WiFi.begin(ssid, password);
- Serial.print("Connecting");
- // Wait for successful connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to: ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.println("");
- }
- void read_sensor() {
- delay(2000);
- // 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();
- // Check if any reads failed and exit early (to try again).
- if (isnan(h) || isnan(t)) {
- Serial.println("Failed to read from DHT sensor!");
- Line_Notify("Failed to read from DHT sensor!");
- return;
- }
- Serial.print("Humidity: ");
- Serial.print(h);
- Serial.print(" %\t");
- Serial.print("Temperature: ");
- Serial.print(t);
- Serial.print(" *C \n");
- String message = "%E0%B8%AD%E0%B8%B8%E0%B8%93%E0%B8%AB%E0%B8%A0%E0%B8%B9%E0%B8%A1%E0%B8%B4%20";
- String message2 = "%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%8A%E0%B8%B7%E0%B9%89%E0%B8%99%20";
- // Send notificatin with LINE Notify
- Line_Notify(message + t + "%C2%B0C " + message2 + h +"%25");
- }
- void loop() {
- read_sensor();
- delay(3600000);
- }
- void Line_Notify(String message) {
- WiFiClientSecure client;
- if (!client.connect("notify-api.line.me", 443)) {
- Serial.println("connection failed");
- 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) + "\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=" + message).length()) + "\r\n";
- req += "\r\n";
- req += "message=" + message;
- client.print(req);
- delay(1000);
- // Serial.println("-----");
- while(client.connected()) {
- String line = client.readStringUntil('\n');
- if (line == "\r") {
- break;
- }
- //Serial.println(line);
- }
- // Serial.println("-----");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement