Created
June 12, 2019 15:42
-
-
Save iamgoangle/3019cd824c1474122ef2af6e2c8eea19 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <ESP8266WiFi.h> | |
| #include <ESP8266HTTPClient.h> | |
| #include <BlynkSimpleEsp8266.h> | |
| #include <dht11.h> | |
| // WiFi | |
| //const char *ssid = "<your_ssid>"; | |
| //const char *password = "<your_password>"; | |
| char networkDriver = 'b'; // 'n' = normal 'b' = blynk | |
| char ssid[] = "<your_ssid>"; | |
| char password[] = "<your_password>"; | |
| // Blynk | |
| char auth[] = "<your_blynk_token>"; | |
| // LINE | |
| const String lineHost = "notify-api.line.me"; | |
| const String notifyApi = "/api/notify"; | |
| const String token = "<your_line_notify_token>"; | |
| const int httpsPort = 443; | |
| // Use web browser to view and copy | |
| // SHA1 fingerprint of the certificate | |
| // https://en.wikipedia.org/wiki/Public_key_fingerprint | |
| // https://www.facebook.com/longhackz/posts/1692042657717181/ | |
| const char fingerprint[] PROGMEM = "BF 16 AE 79 D2 AB 71 44 BE D8 E7 55 A2 C7 0B 39 68 DB B5 D2"; | |
| // Constant of timer | |
| const int httpTimeout = 5000; | |
| const unsigned long DELAY_1_S = 1000UL; | |
| uint32_t DELAY_1_MINUTE = DELAY_1_S * 60UL; | |
| unsigned long DELAY_1_HOUR = 3600 * DELAY_1_MINUTE; | |
| // Sensor or I/O | |
| int dht = D4; | |
| int mh = D5; | |
| dht11 DHT11; | |
| void connectNetwork(char options) | |
| { | |
| switch (options) | |
| { | |
| case 'n': | |
| connectWiFi(); | |
| break; | |
| case 'b': | |
| connectBlynk(); | |
| break; | |
| } | |
| } | |
| void connectWiFi() | |
| { | |
| WiFi.mode(WIFI_OFF); // Prevents reconnection issue (taking too long to connect) | |
| delay(1000); | |
| WiFi.mode(WIFI_STA); // Only Station No AP, This line hides the viewing of ESP as wifi hotspot | |
| WiFi.begin(ssid, password); // WiFi connection | |
| while (WiFi.status() != WL_CONNECTED) | |
| { | |
| // Wait for the WiFI connection completion | |
| delay(500); | |
| Serial.println("Waiting for connection"); | |
| } | |
| Serial.println(); | |
| Serial.println("WiFi connected"); | |
| } | |
| void connectBlynk() | |
| { | |
| Blynk.begin(auth, ssid, password); | |
| } | |
| /** | |
| * LineNotify send message to LINE Notify service | |
| */ | |
| void LineNotify(String msg, bool debug) | |
| { | |
| if (WiFi.status() == WL_CONNECTED) | |
| { | |
| WiFiClientSecure httpsClient; | |
| Serial.print("Connecting to ... "); | |
| Serial.println(lineHost); | |
| // certificate pinning | |
| Serial.printf("Using fingerprint ... '%s'\n", fingerprint); | |
| httpsClient.setFingerprint(fingerprint); | |
| httpsClient.setTimeout(httpTimeout); | |
| delay(1000); | |
| if (!httpsClient.connect(lineHost, httpsPort)) | |
| { | |
| Serial.println("Connection failed"); | |
| return; | |
| } | |
| Serial.print("[HTTPS] begin..."); | |
| String LineNotifyMessage = "message=" + msg; | |
| int lineMessageContentLen = String(LineNotifyMessage).length(); | |
| String req = ""; | |
| req += "POST /api/notify HTTP/1.1\r\n"; | |
| req += "Host: " + lineHost + "\r\n"; | |
| req += "Authorization: Bearer " + token + "\r\n"; | |
| req += "User-Agent: line-iot\r\n"; | |
| req += "Connection: close\r\n"; | |
| req += "Content-Type: application/x-www-form-urlencoded\r\n"; | |
| req += "Content-Length: " + String(lineMessageContentLen) + "\r\n"; | |
| req += "\r\n"; | |
| req += LineNotifyMessage; | |
| if (debug) | |
| { | |
| Serial.print("\nRequest payload: "); | |
| Serial.println(req); | |
| } | |
| // Send http request | |
| httpsClient.print(req); | |
| delay(100); | |
| // Checking timeout | |
| // unsigned long timeout = millis(); | |
| // while (httpsClient.available() == 0) | |
| // { | |
| // if (millis() - timeout > httpTimeout) | |
| // { | |
| // Serial.println("Connection timeout !"); | |
| // httpsClient.stop(); | |
| // return; | |
| // } | |
| // } | |
| while (httpsClient.connected()) | |
| { | |
| String line = httpsClient.readStringUntil('\n'); | |
| if (line == "\r") | |
| { | |
| Serial.println("Push LINE Notify successfull!"); | |
| break; | |
| } | |
| } | |
| String line = httpsClient.readStringUntil('\n'); | |
| Serial.println("reply was:"); | |
| Serial.println("=========="); | |
| Serial.println(line); | |
| Serial.println("=========="); | |
| Serial.println("closing connection"); | |
| } | |
| } | |
| void SendTemperature() | |
| { | |
| int dht11Input = DHT11.read(dht); | |
| String tempTxt = "Temperature (C): " + String(DHT11.temperature); | |
| LineNotify(tempTxt, true); | |
| } | |
| void SendHumidity() | |
| { | |
| int dht11Input = DHT11.read(dht); | |
| String h = "Humidity (%25): " + String(DHT11.humidity); | |
| LineNotify(h, true); | |
| } | |
| bool SchedulerInMinutes(int period, unsigned long timeNow) | |
| { | |
| unsigned long p = period * DELAY_1_MINUTE; | |
| return millis() > timeNow + p; | |
| } | |
| bool SchedulerInSecs(int period, unsigned long timeNow) | |
| { | |
| unsigned long p = period * DELAY_1_S; | |
| return millis() > timeNow + p; | |
| } | |
| void setup() | |
| { | |
| Serial.begin(115200); | |
| connectNetwork(networkDriver); | |
| } | |
| unsigned long timeNowMin = 0; | |
| unsigned long timeNowSec = 0; | |
| void loop() | |
| { | |
| // auto scheduler every hour send to LINE Notify | |
| while (SchedulerInMinutes(60, timeNowMin)) | |
| { | |
| timeNowMin = millis(); | |
| SendTemperature(); | |
| SendHumidity(); | |
| } | |
| Blynk.run(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment