Added time & date

This commit is contained in:
Filip Znachor 2022-12-06 23:51:08 +01:00
parent be710a6128
commit 8e7eead1d0
3 changed files with 31 additions and 7 deletions

View file

@ -57,7 +57,7 @@ void setup() {
// 867.7 - SF7BW125 to SF12BW125
// 867.9 - SF7BW125 to SF12BW125
// 868.8 - FSK
LoRaWAN.setFreqMask("0001");
LoRaWAN.setFreqMask("0004");
// 869.525 - SF9BW125 (RX2) | 869525000
LoRaWAN.setRxWindow("869525000");
@ -105,6 +105,12 @@ bool stringInArray(String val, String* arr, int length){
return false;
}
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);
}
// Departure stucture
struct departure {
@ -176,6 +182,8 @@ void displayDepartures(int lines) {
String displayed[50];
int displayedIndex = 0;
for(int i=0; i < departureIndex && displayedIndex < lines; i++) {
if(departures[i].departure < -3) removeDeparture(i);
if(i >= departureIndex) break;
String lineLastStop = departures[i].line + departures[i].last_stop;
departures[i].departure -= 1;
// TODO: Not sure if 999 wouldn't be displayed as 100
@ -185,7 +193,6 @@ void displayDepartures(int lines) {
displayDeparture(departures[i], displayedIndex);
displayedIndex++;
}
if(departures[i].departure < -3) removeDeparture(i);
}
for(int i=displayedIndex; i<lines; i++) {
M5.Lcd.fillRect(0, i*30, 320, 30, BLACK);
@ -196,12 +203,17 @@ void displayDepartures(int lines) {
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);
}
seconds = time(NULL);
@ -210,7 +222,10 @@ void loop() {
last_update++;
int lines = 7;
displayDepartures(lines);
M5.Lcd.drawString(String(last_update), 0, lines*30);
char buffer[20];
unsigned long timestamp = unixtime + last_update*6;
unixToDate(timestamp, buffer);
M5.Lcd.drawString(String(buffer), 0, lines*30);
}
delay(5);
}
}

View file

@ -4,6 +4,10 @@ import config
import json
from time import sleep
from base64 import b64encode
from datetime import datetime
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class LoraDevice:
@ -52,6 +56,10 @@ class DeviceController:
storage = dict()
@staticmethod
def time():
dev.send(f"TIME|{(datetime.now() - datetime(1970, 1, 1)).total_seconds():.0f}")
@staticmethod
def data(data):
send = False
@ -71,4 +79,4 @@ class DeviceController:
return
string = f"{data.id}|{data.line}|{data.last_stop}|{data.get_departure()}"
dev.send(f"{string}\n")
dev.send(f"{string}")

View file

@ -1,6 +1,6 @@
from departures import Departure
from time import sleep
from lora import LoraDevice
from lora import LoraDevice, DeviceController
refetch = 0
@ -9,7 +9,8 @@ LoraDevice.generate_token()
while True:
if refetch == 0:
Departure.fetch("40", 40)
DeviceController.time()
Departure.fetch("40", 10)
refetch = (refetch + 1) % 10
sleep(.1)