Created
June 13, 2017 07:29
-
-
Save maxpromer/faa31e003bf43e97765a549a4e5f608a to your computer and use it in GitHub Desktop.
This file contains 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 <WiFi.h> | |
#include <HTTPClient.h> | |
#include "time.h" | |
#define WIFI_STA_NAME "MaxHome3BB" | |
#define WIFI_STA_PASS "xxxxxxxxxx" | |
#define IFTTT_TOKEN "<You IFTTT Token>" | |
#define TIMEZONE 7 | |
int last_twit = -1; | |
bool TwitToTwitter(String msg) { | |
HTTPClient http; | |
http.begin("http://maker.ifttt.com/trigger/post_tweet/with/key/" + String(IFTTT_TOKEN)); | |
http.addHeader("Content-Type", "application/json"); | |
int httpCode = http.POST("{\"value1\":\"" + msg + "\"}"); | |
if (httpCode != 200) { | |
Serial.println("[IFTTT] Error code " + String(httpCode) + " ros : " + http.getString()); | |
} | |
http.end(); | |
return httpCode == 200; | |
} | |
void setup() { | |
Serial.begin(115200); | |
pinMode(LED_BUILTIN, OUTPUT); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(WIFI_STA_NAME); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(WIFI_STA_NAME, WIFI_STA_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); | |
} | |
digitalWrite(LED_BUILTIN, HIGH); | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
configTime(TIMEZONE * -1 * 3600, 0, "ntp.ku.ac.th", "fw.eng.ku.ac.th", "time.uni.net.th"); | |
} | |
void loop() { | |
struct tm timeinfo; | |
getLocalTime(&timeinfo); | |
Serial.print("Now: "); | |
Serial.print(timeinfo.tm_hour); | |
Serial.print(":"); | |
Serial.print(timeinfo.tm_min); | |
Serial.print(":"); | |
Serial.print(timeinfo.tm_sec); | |
Serial.println(); | |
if (last_twit != timeinfo.tm_hour) { | |
last_twit = timeinfo.tm_hour; | |
TwitToTwitter("\u0E02\u0E13\u0E30\u0E19\u0E35\u0E49\u0E40\u0E27\u0E25\u0E32 " + (String)(timeinfo.tm_hour < 10 ? "0" : "") + String(timeinfo.tm_hour) + ":" + (String)(timeinfo.tm_min < 10 ? "0" : "") + String(timeinfo.tm_min) + " \u0E19."); | |
} | |
delay(30000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment