Last active
July 1, 2019 01:49
-
-
Save ma2shita/410490654ced928a3bc9d1d02ec47bce to your computer and use it in GitHub Desktop.
Weather informater for M5Stack + 3G Ext. board
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 <M5Stack.h> | |
#include <string.h> | |
#define TINY_GSM_MODEM_UBLOX | |
#include <TinyGsmClient.h> | |
TinyGsm modem(Serial2); /* Serial2 is Modem of 3G Module */ | |
TinyGsmClient ctx(modem); | |
void modem_enabler(); | |
void print_bottom_menu(); | |
long csq_level_map(int csq); | |
void render_portal(); | |
void render_rain_map(); | |
void render_qr(); | |
void setup() { | |
Serial.begin(115200); | |
M5.begin(); | |
M5.Lcd.clear(BLACK); | |
M5.Lcd.setTextColor(WHITE); | |
M5.Lcd.loadFont("/m5stack_fonts/genshin-p-extralight-16pt", SD); | |
modem_enabler(); | |
render_portal(); | |
} | |
const char* antena_picts[] = { | |
"/m5stack_res/antena_pict0.bmp", | |
"/m5stack_res/antena_pict1.bmp", | |
"/m5stack_res/antena_pict2.bmp", | |
"/m5stack_res/antena_pict3.bmp", | |
"/m5stack_res/antena_pict4.bmp", | |
}; | |
void loop() { | |
M5.update(); | |
if (M5.BtnA.wasReleased()) { | |
render_portal(); | |
} else if (M5.BtnB.wasReleased()) { | |
render_rain_map(); | |
} else if (M5.BtnC.wasReleased()) { | |
render_qr(); | |
} | |
long csq_lv = csq_level_map(modem.getSignalQuality()); | |
M5.Lcd.drawBmpFile(SD, antena_picts[csq_lv], 2, 214); | |
} | |
/* ------------------------------------------------------------*/ | |
void modem_enabler() { | |
M5.Lcd.clear(BLACK); | |
M5.Lcd.setTextColor(WHITE); | |
M5.Lcd.setCursor(0, 0); | |
M5.Lcd.print(F("modem.restart()")); | |
Serial2.begin(115200, SERIAL_8N1, 16, 17); | |
modem.restart(); | |
M5.Lcd.println(F("done")); | |
M5.Lcd.print(F("getModemInfo:")); | |
String modemInfo = modem.getModemInfo(); | |
M5.Lcd.println(modemInfo); | |
M5.Lcd.print(F("waitForNetwork()")); | |
while (!modem.waitForNetwork()) M5.Lcd.print("."); | |
M5.Lcd.println(F("Ok")); | |
M5.Lcd.print(F("gprsConnect(soracom.io)")); | |
modem.gprsConnect("soracom.io", "sora", "sora"); | |
M5.Lcd.println(F("done")); | |
M5.Lcd.print(F("isNetworkConnected()")); | |
while (!modem.isNetworkConnected()) M5.Lcd.print("."); | |
M5.Lcd.println(F("Ok")); | |
M5.Lcd.print(F("My IP addr: ")); | |
IPAddress ipaddr = modem.localIP(); | |
M5.Lcd.print(ipaddr); | |
delay(2000); | |
} | |
/* ref: https://m2msupport.net/m2msupport/atcsq-signal-quality/ */ | |
long csq_level_map(int csq) { | |
if (csq < 1) return 0; | |
if (csq < 10) return 1; | |
if (csq < 15) return 2; | |
if (csq < 20) return 3; | |
if (csq < 31) return 4; | |
if (csq >= 31) return 0; | |
} | |
void print_bottom_menu() { | |
M5.Lcd.setCursor(28, 218); | |
M5.Lcd.print(F("DEMO説明")); | |
M5.Lcd.setCursor(127, 218); | |
M5.Lcd.print(F("雨雲マップ")); | |
M5.Lcd.setCursor(223, 218); | |
//M5.Lcd.print(F("ソースコード")); | |
} | |
void render_portal() { | |
M5.Lcd.clear(BLACK); | |
M5.Lcd.setCursor(0, 1); | |
M5.Lcd.print(F("M5Stack + 3G 拡張ボード DEMO")); | |
M5.Lcd.drawJpgFile(SD, "/m5stack_res/desc.jpg", 0, 20); | |
print_bottom_menu(); | |
} | |
char buf[30*1024] = {0}; /* グローバルの方が確保できる */ | |
void render_rain_map() { | |
M5.Lcd.clear(BLACK); | |
M5.Lcd.setCursor(0, 1); | |
M5.Lcd.print(F("雨雲マップ by YOLP")); | |
M5.Lcd.drawJpgFile(SD, "/m5stack_res/bg_320x190bk.jpg", 0, 20); | |
print_bottom_menu(); | |
M5.Lcd.setCursor(242, 1); | |
M5.Lcd.print(F("取得中...")); | |
/* connect to SORACOM service */ | |
if (!ctx.connect("beam.soracom.io", 8888)) { | |
M5.Lcd.fillRect(242-1, 1-1, 320-242+1, 20, BLACK); /* clean up */ | |
M5.Lcd.setCursor(242, 1); | |
M5.Lcd.print(F("failed(A")); | |
delay(1000); | |
M5.Lcd.fillRect(242-1, 1-1, 320-242+1, 20, BLACK); /* clean up */ | |
return; | |
} | |
Serial.println(F("connected.")); | |
/* send request */ | |
ctx.println("GET / HTTP/1.0"); | |
ctx.println("Host: beam.soracom.io"); | |
ctx.println(); | |
Serial.println("sent."); | |
/* receive response */ | |
while (ctx.connected()) { | |
String line = ctx.readStringUntil('\n'); | |
Serial.println(line); | |
if (line == "\r") { | |
Serial.println("headers received."); | |
break; | |
} | |
} | |
ctx.readBytes(buf, sizeof(buf)); /* body */ | |
ctx.stop(); | |
/* rendering */ | |
size_t buf_s = strlen(buf); | |
M5.Lcd.drawJpg((const unsigned char *)buf, buf_s, 0, 20); | |
buf[0] = '\0'; /* cleanup */ | |
M5.Lcd.fillRect(242-1, 1-1, 320-242+1, 20, BLACK); /* clean up */ | |
M5.Lcd.setCursor(242, 1); | |
M5.Lcd.print(F("完了")); | |
} | |
void render_qr() { | |
M5.Lcd.clear(BLACK); | |
M5.Lcd.setCursor(0, 1); | |
M5.Lcd.print("みーたーなー (^^;;"); | |
M5.Lcd.drawJpgFile(SD, "/m5stack_res/bg_320x190.jpg", 0, 20); | |
M5.Lcd.setCursor(0, 32); | |
M5.Lcd.println("良くたどり着きましたな (^^"); | |
M5.Lcd.println("コードはgistで公開中"); | |
M5.Lcd.drawJpgFile(SD, "/m5stack_res/gist_qr.jpg", 0, 20); | |
print_bottom_menu(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment