Skip to content

Instantly share code, notes, and snippets.

@katsuyoshi
Last active April 13, 2023 14:20
Embed
What would you like to do?
LoRaモジュール評価ボード E220-900T22S(JP)-EV1を使ったLoRa通信受信側 M5AtomLite使用
#include <Arduino.h>
#include <M5Unified.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define LORA_M0_PIN 21
#define LORA_M1_PIN 25
#define LORA_RX_PIN 23
#define LORA_TX_PIN 19
// change here for your environment
#define SSID "... your wifi ssid .."
#define PASSWORD "...your wifi password ..."
#define URL "... google spread sheet app script deply url ..."
#define REC_BUF_SIZE 128
char gps_rec_buf[REC_BUF_SIZE];
static char gps_rec_buf_idx = 0;
static double rec_time;
static double latitude;
static double longitude;
void setLoraMode(int mode) {
pinMode(LORA_M0_PIN, OUTPUT);
pinMode(LORA_M1_PIN, OUTPUT);
digitalWrite(LORA_M0_PIN, mode & 0x1 != 0);
digitalWrite(LORA_M1_PIN, mode & 0x2 != 0);
}
static void send_to_spreadsheet(char *str) {
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 0:
rec_time = atof(pt);
case 1:
latitude = atof(pt);
break;
case 2:
longitude = atof(pt);
break;
default:
break;
}
pt = strtok(NULL, ",");
i++;
}
sprintf(buf, "{\"time\":%f,\"latitude\":%f,\"longitude\":%f}", rec_time, latitude, longitude);
Serial.println(buf);
HTTPClient http;
http.begin(URL);
http.addHeader("Content-Type", "application/json");
int status_code = http.POST((uint8_t*)buf, strlen(buf));
if( status_code == 200 ){
Serial.println("success");
}
http.end();
}
void setup() {
M5.begin();
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, LORA_RX_PIN, LORA_TX_PIN);
setLoraMode(0);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
Serial.println(WiFi.localIP());
}
void loop() {
if (Serial2.available()) {
int ch = Serial2.read();
if (gps_rec_buf_idx < REC_BUF_SIZE) {
gps_rec_buf[gps_rec_buf_idx++] = ch;
}
Serial.write(ch);
// 1行受信
if (ch == '\n') {
gps_rec_buf[gps_rec_buf_idx++] = '\0';
gps_rec_buf_idx = 0;
send_to_spreadsheet(gps_rec_buf);
}
}
delay(10);
}
[env:m5stack-atom]
platform = espressif32
board = m5stack-atom
framework = arduino
monitor_speed = 115200
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