307 lines
7.5 KiB
C++
307 lines
7.5 KiB
C++
#include "M5Core2.h"
|
|
#include <M5GFX.h>
|
|
#include "M5_LoRaWAN.h"
|
|
#include "freertos/queue.h"
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include "Ubuntu_24px.h"
|
|
|
|
|
|
|
|
|
|
M5_LoRaWAN LoRaWAN;
|
|
|
|
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("Connecting to LoraWAN...", 0, 0);
|
|
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
|
|
);
|
|
Serial.println("MESSAGE: " + LoRaWAN.waitMsg(1000));
|
|
|
|
// 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 best
|
|
// 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("0004");
|
|
|
|
// 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 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]) : "";
|
|
}
|
|
|
|
bool stringInArray(String val, String* arr, int length){
|
|
for(int i = 0; i < length; i++){
|
|
if(arr[i] == val) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void unixToDate(unsigned long timestamp, char* buffer) {
|
|
time_t t = timestamp;
|
|
struct tm* timeStruct = localtime(&t);
|
|
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.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) {
|
|
|
|
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.fillRect(0, y*30, 40, 30, 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);
|
|
M5.Lcd.setTextDatum(TR_DATUM);
|
|
M5.Lcd.fillRect(290, y*30, 30, 30, BLACK);
|
|
M5.Lcd.drawString(departure, 320, y*30);
|
|
M5.Lcd.setTextDatum(TL_DATUM);
|
|
|
|
}
|
|
|
|
int departureComparation(const void* a,const void* b) {
|
|
departure* x = (departure*) a;
|
|
departure* y = (departure*) b;
|
|
return x->departure - y->departure;
|
|
}
|
|
|
|
void sortDepartures() {
|
|
qsort(departures, departureIndex, sizeof(departure), departureComparation);
|
|
}
|
|
|
|
void removeDeparture(int pos) {
|
|
for (int i = pos; i < departureIndex-1; i++) {
|
|
departures[i] = departures[i+1];
|
|
}
|
|
departureIndex--;
|
|
}
|
|
|
|
void addDeparture(String s) {
|
|
struct departure d = parseDeparture(s);
|
|
for(int i=0; i<departureIndex; i++) {
|
|
if(departures[i].id == d.id) {
|
|
if(d.line != "") departures[i].line = d.line;
|
|
if(d.type != 0) departures[i].type = d.type;
|
|
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[departureIndex] = d;
|
|
departureIndex++;
|
|
}
|
|
|
|
void displayDepartures(int lines) {
|
|
String displayed[50];
|
|
int displayedIndex = 0;
|
|
for(int i=0; i < departureIndex; i++) {
|
|
if(departures[i].departure < -3) removeDeparture(i);
|
|
departures[i].departure -= 1;
|
|
if(displayedIndex < lines) {
|
|
String lineLastStop = departures[i].line + departures[i].last_stop;
|
|
// TODO: Not sure if 999 wouldn't be displayed as 100
|
|
if(!stringInArray(lineLastStop, displayed, displayedIndex) && departures[i].departure < 999) {
|
|
displayed[displayedIndex] = lineLastStop;
|
|
displayDeparture(departures[i], displayedIndex);
|
|
displayedIndex++;
|
|
}
|
|
}
|
|
}
|
|
for(int i=displayedIndex; i<lines; i++) {
|
|
M5.Lcd.fillRect(0, i*30, 320, 30, BLACK);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
// Main loop with time counting
|
|
|
|
void loop() {
|
|
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);
|
|
displayTimeDate(lines);
|
|
}
|
|
delay(5);
|
|
}
|