Skip to content

Instantly share code, notes, and snippets.

@katsuyoshi
Last active April 13, 2023 14:20
Show Gist options
  • Save katsuyoshi/d261703e01527fe5c0a7244d2a16bab6 to your computer and use it in GitHub Desktop.
Save katsuyoshi/d261703e01527fe5c0a7244d2a16bab6 to your computer and use it in GitHub Desktop.
LoRaモジュール評価ボード E220-900T22S(JP)-EV1を使ったLoRa通信受信側 M5AtomLite使用
#include <Arduino.h>
#include <M5Unified.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <FastLED.h>
#define LED_PIN 27
#define NUM_LEDS 1
#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;
static CRGB leds[NUM_LEDS];
void setLed(CRGB color) {
// change RGB to GRB
uint8_t t = color.r;
color.r = color.g;
color.g = t;
leds[0] = color;
FastLED.show();
}
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);
FastLED.addLeds<WS2811, LED_PIN, RGB>(leds, NUM_LEDS);
FastLED.setBrightness(255 * 15 / 100);
setLed(CRGB::Green);
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;
setLed(CRGB::Red);
send_to_spreadsheet(gps_rec_buf);
setLed(CRGB::Green);
}
}
delay(10);
}
[env:m5stack-atom]
platform = espressif32
board = m5stack-atom
framework = arduino
monitor_speed = 115200
lib_deps =
m5stack/M5Unified@^0.1.4
fastled/FastLED@^3.5.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment