SHARE
TWEET

Untitled

a guest Feb 16th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void Line_Notify(String message);
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <DHT.h>
  5.  
  6. #define DHTPIN 2 //D4
  7.  
  8. // Existing WiFi network
  9. const char* ssid     = "Jaidee_iot"; // your wifi name
  10. const char* password = ""; // your wifi password
  11.  
  12. #define LINE_TOKEN "v0EJG1CGNaYf7APFmzuDhSa6qWXvj3NRiEIGsOhoT41" // LINE Notify token
  13.  
  14. #define DHTTYPE DHT11   // DHT 11
  15. //#define DHTTYPE DHT22   // DHT 22  (AM2302)
  16. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
  17.  
  18. DHT dht(DHTPIN, DHTTYPE);
  19.  
  20. void setup() {
  21.   Serial.begin(115200);
  22.   Serial.println("Weather");
  23.  
  24.   dht.begin();
  25.  
  26.   // Connect to your WiFi network
  27.   WiFi.begin(ssid, password);
  28.   Serial.print("Connecting");
  29.  
  30.   // Wait for successful connection
  31.   while (WiFi.status() != WL_CONNECTED) {
  32.     delay(1000);
  33.     Serial.print(".");
  34.   }
  35.   Serial.println("");
  36.   Serial.print("Connected to: ");
  37.   Serial.println(ssid);
  38.   Serial.print("IP address: ");
  39.   Serial.println(WiFi.localIP());
  40.   Serial.println("");
  41. }
  42.  
  43.  
  44. void read_sensor() {
  45.  
  46.   delay(2000);
  47.   // Reading temperature or humidity takes about 250 milliseconds!
  48.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  49.   float h = dht.readHumidity();
  50.   // Read temperature as Celsius (the default)
  51.   float t = dht.readTemperature();
  52.  
  53.   // Check if any reads failed and exit early (to try again).
  54.   if (isnan(h) || isnan(t)) {
  55.     Serial.println("Failed to read from DHT sensor!");
  56.     Line_Notify("Failed to read from DHT sensor!");
  57.     return;
  58.    }
  59.  
  60.   Serial.print("Humidity: ");
  61.   Serial.print(h);
  62.   Serial.print(" %\t");
  63.   Serial.print("Temperature: ");
  64.   Serial.print(t);
  65.   Serial.print(" *C \n");
  66.  
  67.  
  68.   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";
  69.   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";
  70.   // Send notificatin with LINE Notify
  71.   Line_Notify(message + t + "%C2%B0C " + message2 + h +"%25");
  72.  
  73. }
  74.  
  75. void loop() {
  76.  
  77.   read_sensor();
  78.   delay(3600000);
  79. }
  80.  
  81.  
  82. void Line_Notify(String message) {
  83.   WiFiClientSecure client;
  84.  
  85.   if (!client.connect("notify-api.line.me", 443)) {
  86.     Serial.println("connection failed");
  87.     return;  
  88.   }
  89.  
  90.   String req = "";
  91.   req += "POST /api/notify HTTP/1.1\r\n";
  92.   req += "Host: notify-api.line.me\r\n";
  93.   req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n";
  94.   req += "Cache-Control: no-cache\r\n";
  95.   req += "User-Agent: ESP8266\r\n";
  96.   req += "Content-Type: application/x-www-form-urlencoded\r\n";
  97.   req += "Content-Length: " + String(String("message=" + message).length()) + "\r\n";
  98.   req += "\r\n";
  99.   req += "message=" + message;
  100.  
  101.   client.print(req);
  102.    
  103.   delay(1000);
  104.  
  105.   // Serial.println("-----");
  106.   while(client.connected()) {
  107.     String line = client.readStringUntil('\n');
  108.     if (line == "\r") {
  109.       break;
  110.     }
  111.     //Serial.println(line);
  112.   }
  113.   // Serial.println("-----");
  114. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top