Skip to content

Instantly share code, notes, and snippets.

@katsuyoshi
Last active March 27, 2023 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katsuyoshi/d135a33597adfab70bf2fbc1c9799c9e to your computer and use it in GitHub Desktop.
Save katsuyoshi/d135a33597adfab70bf2fbc1c9799c9e to your computer and use it in GitHub Desktop.
LoRaモジュール評価ボード E220-900T22S(JP)-EV1を使ったLoRa通信送信側 M5AtomS3使用
#include <Arduino.h>
#include <M5Unified.h>
#include <string.h>
#define LORA_RX_PIN 7
#define LORA_TX_PIN 6
#define GPS_RX_PIN 38
#define GPS_TX_PIN 39
#define REC_BUF_SIZE 128
static char gps_rec_buf[REC_BUF_SIZE];
static char gps_rec_buf_idx = 0;
static double rec_time;
static double latitude;
static double longitude;
double to_decimal(double v) {
int a = v;
int b = a / 100;
int c = a % 100;
double d = v - a;
return (double)b + ((double)c + d) / 60.0;
}
bool analizeRmc(char *str) {
int len = strlen(str);
if (len <66) { return false; }
if (str[17] != 'A') { return false; }
char buf[REC_BUF_SIZE];
strncpy(buf, str, REC_BUF_SIZE);
int i = 0;
char *pt;
pt = strtok(buf, ",");
while (pt != NULL) {
//Serial.println(pt);
switch (i) {
case 1:
rec_time = atof(pt);
case 3:
latitude = to_decimal(atof(pt));
break;
case 5:
longitude = to_decimal(atof(pt));
break;
default:
break;
}
if (5 <= i++) {
break;
}
pt = strtok(NULL, ",");
}
USBSerial.printf("lat: %f, log: %f\r\n", latitude, longitude);
return true;
}
void display() {
int w = M5.Display.width();
int h = M5.Display.height();
M5.Display.setColor(BLACK);
M5.Display.fillRect(0, 0, w, h);
M5.Display.setCursor(1, 1);
M5.Display.setTextColor(GREEN);
M5.Display.println("lat:");
M5.Display.setTextColor(WHITE);
M5.Display.printf(" %.5f\r\n", latitude);
M5.Display.setTextColor(GREEN);
M5.Display.println("log:");
M5.Display.setTextColor(WHITE);
M5.Display.printf(" %.5f\r\n", longitude);
}
void setup() {
auto cfg = M5.config();
cfg.external_display.module_display = true;
M5.begin(cfg);
M5.Display.setTextSize(2);
display();
USBSerial.begin(115200);
Serial1.begin(9600, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
Serial2.begin(9600, SERIAL_8N1, LORA_RX_PIN, LORA_TX_PIN);
delay(1000);
Serial1.println("$PUBX,40,GSA,0,0,0,0,0,0*4E");
Serial1.println("$PUBX,40,GLL,0,0,0,0,0,0*5C");
Serial1.println("$PUBX,40,GGA,0,0,0,0,0,0*5A");
Serial1.println("$PUBX,40,GSV,0,0,0,0,0,0*59");
Serial1.println("$PUBX,40,VTG,0,0,0,0,0,0*5E");
Serial1.println("$PUBX,40,RMC,0,3,0,0,0,0*44");
}
void loop() {
if (Serial1.available()) {
int ch = Serial1.read();
if (gps_rec_buf_idx < REC_BUF_SIZE) {
gps_rec_buf[gps_rec_buf_idx++] = ch;
}
USBSerial.write(ch);
// 1行受信
if (ch == '\n') {
gps_rec_buf[gps_rec_buf_idx++] = '\0';
gps_rec_buf_idx = 0;
if (analizeRmc(gps_rec_buf)) {
Serial2.printf("%.2f,%.5f,%.5f\r\n", rec_time, latitude, longitude);
}
display();
}
}
delay(10);
}
[env:ATOMS3]
platform = espressif32@5.2.0
framework = arduino
platform_packages = platformio/framework-arduinoespressif32@^3.20005.220925
board = esp32-s3-devkitc-1
lib_ldf_mode = deep
monitor_speed = 115200
upload_speed = 1500000
board_build.f_cpu = 240000000L
board_build.f_flash = 80000000L
board_build.flash_mode = dio
build_flags =
-DCORE_DEBUG_LEVEL=3
-Iinclude
lib_deps =
m5stack/M5Unified@^0.1.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment