Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Last active March 15, 2024 09:59
Show Gist options
  • Save ma2shita/e78bea1fbecac40f76a099e05520dd32 to your computer and use it in GitHub Desktop.
Save ma2shita/e78bea1fbecac40f76a099e05520dd32 to your computer and use it in GitHub Desktop.
RainRadar by YOLP for M5Stack
/*
* Copyright (c) 2019 Kohei "Max" MATSUSHITA
* Released under the MIT license
* https://opensource.org/licenses/mit-license.php
*/
#include <M5Stack.h>
#define CONSOLE Serial
#define MODEM Serial2 /* Serial2 is Modem of 3G Module */
#include <string.h>
#define TINY_GSM_MODEM_UBLOX
#include <TinyGsmClient.h>
TinyGsm modem(MODEM);
TinyGsmClientSecure ctx(modem);
void modem_enabler();
void render_rain_radar();
void print_top();
void print_bottom();
uint16_t getColor(uint8_t red, uint8_t green, uint8_t blue);
void setup() {
M5.begin();
CONSOLE.begin(115200);
M5.Lcd.clear(BLACK);
M5.Lcd.setTextSize(2);
print_top();
print_bottom();
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setCursor(0, 16);
M5.Lcd.println("## Congrats,");
M5.Lcd.println("## Boot successfully!");
}
void loop() {
if (M5.BtnA.wasReleased()) {
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setCursor(218, 0);
M5.Lcd.print(F("(prog..."));
modem_enabler();
get_and_render_rain_radar();
print_top();
print_bottom();
modem.gprsDisconnect();
}
M5.update();
}
/* ------------------------------------------------------------*/
void modem_enabler() {
M5.Lcd.setTextColor(WHITE);
MODEM.begin(115200, SERIAL_8N1, 16, 17);
M5.Lcd.fillRect(0, 224, 320, 16, BLACK); /* clean up */
M5.Lcd.setCursor(0, 224);
M5.Lcd.println(F("modem.restart()"));
CONSOLE.println(F("modem.restart()"));
modem.restart();
M5.Lcd.fillRect(0, 224, 320, 16, BLACK); /* clean up */
M5.Lcd.setCursor(0, 224);
M5.Lcd.println(F("waitForNetwork()"));
CONSOLE.println(F("waitForNetwork()"));
while (!modem.waitForNetwork()) M5.Lcd.print(".");
M5.Lcd.fillRect(0, 224, 320, 16, BLACK); /* clean up */
M5.Lcd.setCursor(0, 224);
M5.Lcd.println(F("gprsConnect(soracom.io)"));
CONSOLE.println(F("gprsConnect(soracom.io)"));
modem.gprsConnect("soracom.io", "sora", "sora");
M5.Lcd.fillRect(0, 224, 320, 16, BLACK); /* clean up */
M5.Lcd.setCursor(0, 224);
M5.Lcd.println(F("isNetworkConnected()"));
CONSOLE.println(F("isNetworkConnected()"));
while (!modem.isNetworkConnected()) M5.Lcd.print(".");
M5.Lcd.fillRect(0, 224, 320, 16, BLACK); /* clean up */
M5.Lcd.setCursor(0, 224);
M5.Lcd.print(F("MyIP:"));
CONSOLE.print(F("MyIP: "));
IPAddress ipaddr = modem.localIP();
M5.Lcd.println(ipaddr);
CONSOLE.println(ipaddr);
}
char _buf[32*1024] = {0}; /* store for JPG image data */
void get_and_render_rain_radar() {
/* connect to host */
const char *host = "map.yahooapis.jp";
const int port = 443;
int timeout_s = 60;
CONSOLE.print("Connecting:");
CONSOLE.println(host);
if (!ctx.connect(host, port, timeout_s)) {
CONSOLE.println("fail");
return;
}
/* send request */
const char *path = "GET /map/V1/static?appid=<YOUR_CLIENT_ID>&output=jpg&quality=50&width=320&height=208&overlay=type:rainfall%7Cdatelabel:off&mode=map&style=base:simple&z=8&lat=35.6313456&lon=139.7312189 HTTP/1.0";
CONSOLE.println(path);
ctx.println(path);
ctx.print("Host: ");
ctx.println(host);
ctx.println();
Serial.println("sent.");
/* receive response */
while (ctx.connected()) {
String line = ctx.readStringUntil('\n');
CONSOLE.println(line);
if (line == "\r") {
CONSOLE.println("headers received.");
break;
}
}
ctx.readBytes(_buf, sizeof(_buf)); /* body */
ctx.stop();
/* rendering */
size_t _buf_s = strlen(_buf);
CONSOLE.println(_buf_s);
CONSOLE.println(_buf);
M5.Lcd.drawJpg((const unsigned char *)_buf, _buf_s, 0, 16);
_buf[0] = '\0'; /* cleanup */
CONSOLE.println("done.");
}
void print_top() {
M5.Lcd.fillRect(0, 0, 320, 16, getColor(51, 244, 204));
M5.Lcd.setTextColor(BLACK);
M5.Lcd.setCursor(0, 0);
M5.Lcd.print(F("RainRadar by YOLP"));
}
void print_bottom() {
M5.Lcd.fillRect(0, 224, 320, 16, BLACK); /* clean up */
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setCursor(0, 224);
M5.Lcd.print(F("Btn => Retreve RainRadar"));
}
uint16_t getColor(uint8_t red, uint8_t green, uint8_t blue) {
return ((red>>3)<<11) | ((green>>2)<<5) | (blue>>3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment