Skip to content

Instantly share code, notes, and snippets.

@myazakky
Created December 27, 2021 11:57
Show Gist options
  • Save myazakky/a844f3b0c8dd756b264396a56a87949e to your computer and use it in GitHub Desktop.
Save myazakky/a844f3b0c8dd756b264396a56a87949e to your computer and use it in GitHub Desktop.
ぶっ飛び時計
#include <M5Stack.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include "time.h"
#include <ArduinoJson.h>
WiFiClient client;
const char* ssid = "";
const char* password = "";
const char* ntpServer = "10.3.0.1";
const int daylightOffset_sec = 0;
const String apiKey = "";
void connect_wifi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void buttobi_time_set(long timezone) {
configTime(timezone, daylightOffset_sec, ntpServer);
}
void setup() {
M5.begin();
M5.Lcd.clear();
M5.Lcd.setTextColor(0xead0, WHITE);
connect_wifi();
buttobi_time_set(9 * 3600);
printMap("Tokyo");
printLocalTime(1, 50, 3);
}
void loop() {
if (M5.BtnA.wasPressed()) {
StaticJsonDocument<284> doc;
String res = buttobi();
deserializeJson(doc, res);
int n = doc["timezone"];
buttobi_time_set(n * 3600);
const char* msg = doc["msg"];
const String city = doc["city"];
printMap(city);
M5.Lcd.setCursor(0, 1);
M5.Lcd.setTextSize(2);
M5.Lcd.print(city);
}
printLocalTime(0, 18, 3);
delay(1000);
M5.update();
}
String buttobi() {
HTTPClient http;
String url = "http://10.8.0.1:39391/";
http.begin(url);
int httpCode = http.GET();
String result;
if (httpCode > 0) {
result = http.getString();
} else {
result = "{\"msg\":\"\", \"zone\": 9, \"city\": \"Tokyo\"}";
}
http.end();
return result;
}
uint8_t buff[200 * 200] = { 0 };
void printMap(const String city) {
HTTPClient http;
String cityp = "&ci=" + city;
String url = "https://image.maps.ls.hereapi.com/mia/1.6/mapview?&z=2&w=320" + cityp + "&apiKey=" + apiKey;
http.begin(url);
int statusCode = http.GET();
if (statusCode == 200) {
int len = http.getSize();
if (len > 0) {
WiFiClient * stream = http.getStreamPtr();
uint8_t* p = buff;
int l = len;
while (http.connected() && (l > 0 || len == -1)) {
size_t size = stream -> available();
if (size) {
int s = ((size > sizeof(buff)) ? sizeof(buff) : size);
int c = stream -> readBytes(p, s);
p += c;
if (l > 0) {
l -= c;
}
}
}
M5.Lcd.drawJpg(buff, len);
}
} else {
M5.Lcd.setCursor(30, 50);
M5.Lcd.println(statusCode);
}
http.end();
}
void printLocalTime(int x, int y, int s)
{
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
M5.Lcd.setTextSize(s);
M5.Lcd.setCursor(x, y);
M5.Lcd.print(&timeinfo, "%H:%M:%S");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment