Added client
This commit is contained in:
parent
0c01e24e15
commit
549792e2b6
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
config.py
|
179
client/client.ino
Normal file
179
client/client.ino
Normal file
|
@ -0,0 +1,179 @@
|
|||
#include "M5Core2.h"
|
||||
#include <M5GFX.h>
|
||||
#include "M5_LoRaWAN.h"
|
||||
#include "freertos/queue.h"
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
M5GFX display;
|
||||
M5Canvas canvas(&display);
|
||||
M5_LoRaWAN LoRaWAN;
|
||||
|
||||
String response;
|
||||
|
||||
void setup() {
|
||||
M5.begin();
|
||||
LoRaWAN.Init(&Serial2, 13, 14);
|
||||
delay(100);
|
||||
display.begin();
|
||||
display.setTextSize(1);
|
||||
canvas.setColorDepth(1); // mono color
|
||||
canvas.createSprite(display.width(), display.height());
|
||||
canvas.setTextSize((float)canvas.width() / 160);
|
||||
canvas.setTextScroll(true);
|
||||
//canvas.setFreeFont(&Pixel2_24px);
|
||||
|
||||
display.println("Module Connect.....");
|
||||
while (!LoRaWAN.checkDeviceConnect())
|
||||
;
|
||||
LoRaWAN.writeCMD("AT?\r\n");
|
||||
delay(100);
|
||||
Serial2.flush();
|
||||
|
||||
// Disable Log Information
|
||||
LoRaWAN.writeCMD("AT+ILOGLVL=0\r\n");
|
||||
// Enable Log Information
|
||||
LoRaWAN.writeCMD("AT+CSAVE\r\n");
|
||||
LoRaWAN.writeCMD("AT+IREBOOT=0\r\n");
|
||||
Serial.println("LoraWan Rebooting");
|
||||
delay(1000);
|
||||
|
||||
Serial.println("LoraWan config");
|
||||
// Set Join Mode OTAA.
|
||||
LoRaWAN.configOTTA("00bdea85badeedf1", // Device EUI
|
||||
"00bdea85badeed01", // APP EUI
|
||||
"00bdea85badeed0100bdea85badeed01", // APP KEY
|
||||
"2" // Upload Download Mode
|
||||
);
|
||||
response = LoRaWAN.waitMsg(1000);
|
||||
Serial.println("MESSAGE: " + response);
|
||||
// Set ClassC mode
|
||||
LoRaWAN.setClass("2");
|
||||
LoRaWAN.writeCMD("AT+CWORKMODE=2\r\n");
|
||||
|
||||
// LoRaWAN868
|
||||
// TX Freq
|
||||
// 868.1 - SF7BW125 to SF12BW125
|
||||
// 868.3 - SF7BW125 to SF12BW125 and SF7BW250
|
||||
// 868.5 - SF7BW125 to SF12BW125
|
||||
// 867.1 - SF7BW125 to SF12BW125
|
||||
// 867.3 - SF7BW125 to SF12BW125
|
||||
// 867.5 - SF7BW125 to SF12BW125
|
||||
// 867.7 - SF7BW125 to SF12BW125
|
||||
// 867.9 - SF7BW125 to SF12BW125
|
||||
// 868.8 - FSK
|
||||
LoRaWAN.setFreqMask("0001");
|
||||
|
||||
// 869.525 - SF9BW125 (RX2) | 869525000
|
||||
LoRaWAN.setRxWindow("869525000");
|
||||
LoRaWAN.startJoin();
|
||||
}
|
||||
|
||||
String decodeMsg(String hexEncoded) {
|
||||
if ((hexEncoded.length() & 1) == 0) {
|
||||
char buf[hexEncoded.length() + 1];
|
||||
char tempbuf[((hexEncoded.length() + 1))];
|
||||
hexEncoded.toCharArray(buf, hexEncoded.length() + 1);
|
||||
int i = 0;
|
||||
for (int loop = 2; loop < hexEncoded.length() + 1; loop += 2) {
|
||||
String tmpstr = hexEncoded.substring(loop - 2, loop);
|
||||
sprintf(&tempbuf[i], "%c", strtoul(tmpstr.c_str(), nullptr, 16));
|
||||
i++;
|
||||
}
|
||||
return String(tempbuf);
|
||||
} else {
|
||||
return hexEncoded;
|
||||
}
|
||||
}
|
||||
|
||||
String split(String data, char separator, int index) {
|
||||
int found = 0;
|
||||
int strIndex[] = { 0, -1 };
|
||||
int maxIndex = data.length() - 1;
|
||||
|
||||
for (int i = 0; i <= maxIndex && found <= index; i++) {
|
||||
if (data.charAt(i) == separator || i == maxIndex) {
|
||||
found++;
|
||||
strIndex[0] = strIndex[1] + 1;
|
||||
strIndex[1] = (i == maxIndex) ? i+1 : i;
|
||||
}
|
||||
}
|
||||
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
|
||||
}
|
||||
|
||||
struct departure {
|
||||
int id;
|
||||
String line;
|
||||
String last_stop;
|
||||
int departure;
|
||||
};
|
||||
|
||||
struct departure departures[20];
|
||||
int departure_index = 0;
|
||||
|
||||
struct departure parseDeparture(String s) {
|
||||
struct departure d;
|
||||
d.id = split(s, '|', 0).toInt();
|
||||
d.line = split(s, '|', 1);
|
||||
d.last_stop = split(s, '|', 2);
|
||||
d.departure = split(s, '|', 3).toInt();
|
||||
return d;
|
||||
}
|
||||
|
||||
void displayDeparture(struct departure d) {
|
||||
canvas.print(d.line);
|
||||
canvas.print(" ");
|
||||
canvas.print(d.last_stop);
|
||||
canvas.print(" ");
|
||||
String departure = "<1";
|
||||
if(d.departure >= 10) departure = String(d.departure/10);
|
||||
canvas.print(departure);
|
||||
canvas.println("");
|
||||
}
|
||||
|
||||
void removeDeparture(int pos) {
|
||||
for (int i = pos; i < departure_index-1; i++) {
|
||||
departures[i] = departures[i+1];
|
||||
}
|
||||
departure_index--;
|
||||
}
|
||||
|
||||
void addDeparture(String s) {
|
||||
struct departure d = parseDeparture(s);
|
||||
for(int i=0; i<departure_index; i++) {
|
||||
if(departures[i].id == d.id) {
|
||||
if(d.line != "") departures[i].line = d.line;
|
||||
if(d.last_stop != "") departures[i].last_stop = d.last_stop;
|
||||
if(d.departure != 0) departures[i].departure = d.departure;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(d.line == "" || d.last_stop == "") return;
|
||||
departures[departure_index] = d;
|
||||
displayDeparture(departures[departure_index]);
|
||||
departure_index++;
|
||||
}
|
||||
|
||||
time_t seconds;
|
||||
int last_update = 0;
|
||||
|
||||
void loop() {
|
||||
String recvMsg = LoRaWAN.receiveMsg();
|
||||
if(recvMsg.length() != 0) {
|
||||
String decodedMsg = decodeMsg(recvMsg);
|
||||
Serial.println(decodedMsg);
|
||||
addDeparture(decodedMsg);
|
||||
}
|
||||
seconds = time(NULL);
|
||||
if(seconds / 6 > last_update) {
|
||||
last_update++;
|
||||
canvas.fillSprite(TFT_BLACK);
|
||||
for(int i=0; i<departure_index; i++) {
|
||||
departures[i].departure -= 1;
|
||||
if(departures[i].departure < -3) removeDeparture(i);
|
||||
displayDeparture(departures[i]);
|
||||
}
|
||||
}
|
||||
canvas.pushSprite(0, 0);
|
||||
delay(10);
|
||||
}
|
Loading…
Reference in a new issue