Fixed LoraWAN message loss, added departure type & better formatting

This commit is contained in:
Filip Znachor 2022-12-07 15:06:49 +01:00
parent 30a93c55a7
commit 51b5714135
2 changed files with 2582 additions and 1448 deletions

File diff suppressed because it is too large Load diff

View file

@ -6,21 +6,21 @@
#include <time.h>
#include "Ubuntu_24px.h"
M5_LoRaWAN LoRaWAN;
String response;
void setup() {
M5.begin();
LoRaWAN.Init(&Serial2, 13, 14);
delay(100);
M5.Lcd.begin();
M5.Lcd.setTextSize(1);
M5.Lcd.setFreeFont(&Ubuntu_24px);
M5.Lcd.drawString("Checking LoraWAN module...", 0, 0);
while (!LoRaWAN.checkDeviceConnect())
;
M5.Lcd.drawString("Connecting to LoraWAN...", 0, 0);
while (!LoRaWAN.checkDeviceConnect());
LoRaWAN.writeCMD("AT?\r\n");
delay(100);
Serial2.flush();
@ -40,8 +40,8 @@ void setup() {
"00bdea85badeed0100bdea85badeed01", // APP KEY
"2" // Upload Download Mode
);
response = LoRaWAN.waitMsg(1000);
Serial.println("MESSAGE: " + response);
Serial.println("MESSAGE: " + LoRaWAN.waitMsg(1000));
// Set ClassC mode
LoRaWAN.setClass("2");
LoRaWAN.writeCMD("AT+CWORKMODE=2\r\n");
@ -50,8 +50,8 @@ void setup() {
// TX Freq
// 868.1 - SF7BW125 to SF12BW125
// 868.3 - SF7BW125 to SF12BW125 and SF7BW250
// 868.5 - SF7BW125 to SF12BW125
// 867.1 - SF7BW125 to SF12BW125
// 868.5 - SF7BW125 to SF12BW125 best
// 867.1 - SF7BW125 to SF12BW125
// 867.3 - SF7BW125 to SF12BW125
// 867.5 - SF7BW125 to SF12BW125
// 867.7 - SF7BW125 to SF12BW125
@ -62,27 +62,25 @@ void setup() {
// 869.525 - SF9BW125 (RX2) | 869525000
LoRaWAN.setRxWindow("869525000");
LoRaWAN.startJoin();
while(true) {
String recvMsg = LoRaWAN.waitMsg(1000);
recvMsg.trim();
if(recvMsg == "+CJOIN:OK") {
Serial.println("Connected!");
M5.Lcd.drawString("Connected!", 0, 30);
delay(500);
break;
}
}
}
// Helper functions
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 };
@ -108,41 +106,130 @@ bool stringInArray(String val, String* arr, int length){
void unixToDate(unsigned long timestamp, char* buffer) {
time_t t = timestamp;
struct tm* timeStruct = localtime(&t);
sprintf(buffer, "%02d:%02d %02d.%02d.%04d", timeStruct->tm_hour, timeStruct->tm_min, timeStruct->tm_mday, timeStruct->tm_mon + 1, timeStruct->tm_year + 1900);
sprintf(buffer, "%02d.%02d.%04d", timeStruct->tm_mday, timeStruct->tm_mon + 1, timeStruct->tm_year + 1900);
}
void unixToTime(unsigned long timestamp, char* buffer) {
time_t t = timestamp;
struct tm* timeStruct = localtime(&t);
sprintf(buffer, "%02d:%02d", timeStruct->tm_hour, timeStruct->tm_min);
}
// Departure stucture
struct departure {
int id;
int type;
String line;
String last_stop;
int departure;
};
// Global departure list
struct departure departures[50];
int departureIndex = 0;
// Global variables
time_t seconds;
int last_update = 0;
int unixtime = 1;
// Recieve from LoraWAN
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;
}
}
void receiveMsg() {
String recvMsg = LoRaWAN.waitMsg(500);
recvMsg.trim();
if(recvMsg.length() != 0 && recvMsg.substring(0, 8) == "OK+RECV:") {
String decodedMsg = decodeMsg(split(recvMsg, ',', 3));
Serial.println(decodedMsg);
if(decodedMsg.substring(0,5) == "TIME|") {
unixtime = (decodedMsg.substring(5,30).toInt()) - last_update*6;
Serial.println(unixtime);
}
addDeparture(decodedMsg);
}
}
// Departure functions
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();
d.type = split(s, '|', 1).toInt();
d.line = split(s, '|', 2);
d.last_stop = split(s, '|', 3);
d.departure = split(s, '|', 4).toInt();
return d;
}
void displayTimeDate(int lines) {
char date[20];
char time[20];
unsigned long timestamp = unixtime + last_update*6;
unixToDate(timestamp, date);
unixToTime(timestamp, time);
M5.Lcd.fillRect(0, lines*30, 320, 30, BLACK);
M5.Lcd.drawString(String(date), 0, lines*30+5);
M5.Lcd.setTextDatum(TR_DATUM);
M5.Lcd.drawString(String(time), 320, lines*30+5);
M5.Lcd.setTextDatum(TL_DATUM);
}
void displayDeparture(struct departure d, int y) {
M5.Lcd.fillRect(0, y*30, 40, 30, BLACK);
uint16_t color = 0xFFFFFF;
if(d.type == 1) color = 0xFFEA; // žlutá: 0xEF53 / 0xFFEB
if(d.type == 2) color = 0x6FF0;
if(d.type == 3) color = 0xFAAD;
M5.Lcd.setTextColor(color, TFT_BLACK);
M5.Lcd.drawString(d.line, 0, y*30);
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.fillRect(40, y*30, 255, 30, BLACK);
M5.Lcd.drawString(d.last_stop, 40, y*30);
String departure = "<1";
if(d.departure >= 10) departure = String(d.departure/10);
if(departure.length() == 1) departure = " " + departure;
M5.Lcd.drawString(departure, 290, y*30);
M5.Lcd.fillRect(290, y*30, 30, 30, BLACK);
M5.Lcd.setTextDatum(TR_DATUM);
M5.Lcd.drawString(departure, 320, y*30);
M5.Lcd.setTextDatum(TL_DATUM);
}
int departureComparation(const void* a,const void* b) {
@ -175,7 +262,6 @@ void addDeparture(String s) {
if(d.line == "" || d.last_stop == "") return;
departures[departureIndex] = d;
departureIndex++;
sortDepartures();
}
void displayDepartures(int lines) {
@ -189,7 +275,6 @@ void displayDepartures(int lines) {
// TODO: Not sure if 999 wouldn't be displayed as 100
if(!stringInArray(lineLastStop, displayed, displayedIndex) && departures[i].departure < 999) {
displayed[displayedIndex] = lineLastStop;
M5.Lcd.fillRect(0, displayedIndex*30, 320, 30, BLACK);
displayDeparture(departures[i], displayedIndex);
displayedIndex++;
}
@ -199,33 +284,21 @@ void displayDepartures(int lines) {
}
}
// Main loop with time counting
time_t seconds;
int last_update = 0;
int unixtime = 1;
void loop() {
String recvMsg = LoRaWAN.receiveMsg();
if(recvMsg.length() != 0) {
String decodedMsg = decodeMsg(recvMsg);
Serial.println(decodedMsg);
if(decodedMsg.substring(0,5) == "TIME|") {
unixtime = (decodedMsg.substring(5,30).toInt()) - last_update*6;
Serial.println(unixtime);
}
addDeparture(decodedMsg);
}
receiveMsg();
seconds = time(NULL);
// TODO: Not sure if time is ticking correctly (check by adding real date & time and keep it running)
if(seconds / 6 > last_update) {
last_update++;
int lines = 7;
sortDepartures();
displayDepartures(lines);
char buffer[20];
unsigned long timestamp = unixtime + last_update*6;
unixToDate(timestamp, buffer);
M5.Lcd.drawString(String(buffer), 0, lines*30);
displayTimeDate(lines);
}
delay(5);
}