Skip to content

Instantly share code, notes, and snippets.

@kittipongint
Created March 23, 2017 14:29
Show Gist options
  • Save kittipongint/0d42d9bbf4984c7b8b1846295b8d59f8 to your computer and use it in GitHub Desktop.
Save kittipongint/0d42d9bbf4984c7b8b1846295b8d59f8 to your computer and use it in GitHub Desktop.
Detect temperature and send notification to Line application via LINE Notify.
void Line_Notify(String message);
#include <ESP8266WiFi.h>
#include <DHT.h>
#define DHTPIN D2
// Existing WiFi network
const char* ssid = "xxxxx"; // your wifi name
const char* password = "xxxxx"; // your wifi password
#define LINE_TOKEN "xxxxxxxxxxxxxxx" // 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(9600);
Serial.println("Weather station by kittipongint!");
dht.begin();
// Connect to your WiFi network
WiFi.begin(ssid, password);
Serial.print("Connecting");
// Wait for successful connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
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(600000);
// 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);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
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";
// Send notificatin with LINE Notify
Line_Notify(message + t + "%C2%B0C");
}
void loop() {
read_sensor();
}
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(30);
// Serial.println("-----");
while(client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
//Serial.println(line);
}
// Serial.println("-----");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment