#include
#include "SSD1306.h"
#include
#include
#include
#include
#include
#include
//Ticker tickerWeatherUpdate;
Ticker ticker1;
Ticker ticker2;
bool WeatherFlag = false;
bool TimeFlag = false;
SSD1306 display(0x3c, 4, 5);
const char* ssid = "Extender-G-9C58";
const char* password = "cipnx4jyu87ue";
//NTP関連
struct tm timeInfo;
char date[20],hour_minute[20];
//天気関連
String now_weather,now_temp,tom_weather,tom_temp;
const String endpoint = "http://api.openweathermap.org/data/2.5/weather?q=takamatsu,jp&APPID=";
//const String endpoint_forecast = "http://api.openweathermap.org/data/2.5/forecast?q=takamatsu,jp&APPID=";
const String key = "894eb4eefcad160cdb231e3de2fde1f4";
//今日の天気を取得する関数
void get_today_weather(){
HTTPClient http;
http.begin(endpoint + key); //URLを指定
int httpCode = http.GET(); //GETリクエストを送信
if (httpCode > 0) { //返答がある場合
String payload = http.getString(); //返答(JSON形式)を取得
Serial.println(httpCode);
Serial.println(payload);
//jsonオブジェクトの作成
String json = payload;
const size_t capacity = JSON_OBJECT_SIZE(13) + 1000;
DynamicJsonDocument doc(capacity);
// Serial.println(json);
// Parse JSON object
DeserializationError error = deserializeJson(doc, json);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
//各データを抜き出し
const char* weather = doc["weather"][0]["main"].as();
const double temp = doc["main"]["temp"].as();
//表示用変数に各要素をセット
now_weather = weather;
now_temp = String(temp-273.15);
}
else {
Serial.println("Error on HTTP request");
}
http.end(); //Free the resources
}
//ticker用の天気データまとめ関数
void get_weather_data(){
get_today_weather();
// get_tomorrow_weather();
}
void show_time(){
time_t t;
struct tm *tm;
static const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};
t = time(NULL);
tm = localtime(&t);
// getLocalTime(&timeInfo);//tmオブジェクトのtimeInfoに現在時刻を入れ込む
sprintf(date, "%04d/%02d/%02d (%s)",tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
wd[tm->tm_wday]);//日付に変換
sprintf(hour_minute, "%02d:%02d:%02d",tm->tm_hour, tm->tm_min, tm->tm_sec);//時間に変換
Serial.println(date);
Serial.println(hour_minute);
display.clear();
//日付を上の方に表示
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, date);
//時間を中央に大きく表示
display.setFont(ArialMT_Plain_24);
display.drawString(20, 20, hour_minute);
display.setFont(ArialMT_Plain_16);
display.drawString(10, 48, "Temp=" + now_temp + "°C");
display.display();
}
void SetTimeFlag(){
TimeFlag = true;
}
void SetWeatherFlag() {
WeatherFlag = true;
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
display.init();//SSD1306を初期化
//Wi-Fi関連
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
configTime(9 * 3600L, 0, "ntp.nict.jp", "time.google.com", "ntp.jst.mfeed.ad.jp");//NTPの設定
//Tickerの設定
ticker1.attach(15, SetTimeFlag);
ticker2.attach(1800, SetWeatherFlag);
get_weather_data();
}
void loop() {
if (TimeFlag) {
show_time();
TimeFlag = false;
}
if (WeatherFlag) {
get_weather_data();
WeatherFlag = false;
}
}