国土地理院から複数の地図データを取得してM5Stack Core2 for AWSに表示します。
地図データ取得アプリの作成
地図データ取得アプリ「Httpdownload.ino」を次のように作成します。SPIFFSを取得した地図画像を一時的に保存するために使用します。
- 10-13行目で国土地理院からの地図画像のURLを設定します。
- 49行目で指定されたサイズ分画像データを繰り返し取得します。
- 53行目の「readBytes」関数により画像データを入力します。
- 56行目で入力した画像データをSPIFFSに保存します。
- 64行目でSPIFFSに保存した画像データを「M5.Lcd.drawPngFile」関数によりM5Stack Core2 for AWSの画面に表示します。
Httpdownload.ino
#include <M5Core2.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
WiFiMulti wifiMulti;
HTTPClient http;
static uint8_t http_buff[4096];
const char * FILE_PATH = "/map.png";
const char* myUrls[] = {
"https://cyberjapandata.gsi.go.jp/xyz/std/7/113/49.png", "https://cyberjapandata.gsi.go.jp/xyz/std/7/114/49.png",
"https://cyberjapandata.gsi.go.jp/xyz/std/7/113/50.png", "https://cyberjapandata.gsi.go.jp/xyz/std/7/114/50.png"
};
int urlpos = 0;
void setup() {
M5.begin(); //Init M5Core2.
wifiMulti.addAP("id", "pass"); //Storage wifi configuration information.
M5.Lcd.print("\nConnecting Wifi...\n"); //print format output string on lcd.
if (!SPIFFS.begin(true)) {
M5.Lcd.print("SPIFFS Mount Failed");
return;
}
}
void loop() {
M5.Lcd.setCursor(0, 0); //Set the cursor at (0,0).
if ((wifiMulti.run() == WL_CONNECTED)) { // wait for WiFi connection.
M5.Lcd.print("[HTTP] begin...\n");
urlpos ++;
urlpos &= 0x3;
http.begin(myUrls[urlpos]); // configure traged server and url.
M5.Lcd.print("[HTTP] GET...\n");
int httpCode = http.GET(); // start connection and send HTTP header.
if (httpCode > 0) { // httpCode will be negative on error.
M5.Lcd.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) { // file found at server.
M5.Lcd.printf("[HTTP] GET... code: %d\n", httpCode);
WiFiClient *stream = http.getStreamPtr();
int data_size = http.getSize();
int read_size = 0;
SPIFFS.remove(FILE_PATH);
File f = SPIFFS.open(FILE_PATH, "w");
if (f)
{
while (http.connected() && read_size < data_size)
{
size_t sz = stream->available();
if (sz > 0)
{
int rd = stream->readBytes(http_buff, min(sz, sizeof(http_buff)));
read_size += rd;
M5.Lcd.printf("[HTTP] read: %d/%d\n", read_size, data_size);
f.write(http_buff, rd);
}
else
{
delay(1);
}
}
f.close();
M5.Lcd.drawPngFile(SPIFFS, FILE_PATH, 0, 0);
}
else
{
M5.Lcd.print("ERROR: OPEN FILE.");
}
}
} else {
M5.Lcd.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
M5.Lcd.print("connect failed");
}
delay(5000);
M5.Lcd.clear(); //clear the screen.
}
地図データ取得アプリの実行
地図データ取得アプリ「Httpdownload.ino」を実行すると、国土地理院から画像データを次のように取得します。
取得した画像データを次のようにM5Stack Core2 for AWSに表示します。

