Misc additional updates

pull/193/head
Jeff Lehman 3 months ago
parent 08724ab20d
commit 9a00eab8af

@ -32,7 +32,11 @@ enum cmd_t {
cmd_add,
cmd_ack,
cmd_time, // Time is sent across the network calling cmd_time on the receiving device
cmd_time_req, // Device is requesting that gateway send time to it.
};
enum ping_t {
ping_request,
ping_reply
};
enum
@ -48,7 +52,7 @@ enum
event_lora2,
event_internal
};
// Interface type that is the network master
// Interface type that is the time source
enum TmNetIf {
TMIF_NONE,
TMIF_LORA,

@ -59,7 +59,7 @@ void sendESPNowNbr(uint8_t);
void sendESPNowPeers();
void sendESPNow(uint8_t);
void sendTimeSerial();
crcResult handleLoRa();
void handleLoRa();
void handleMQTT();
void handleOTA();
@ -162,7 +162,6 @@ void beginFDRS()
DBG("Address:" + String(UNIT_MAC, HEX));
#ifdef USE_LORA
begin_lora();
scheduleFDRS(asyncReleaseLoRaFirst, FDRS_LORA_INTERVAL);
#endif
#ifdef USE_WIFI
begin_wifi();
@ -202,10 +201,17 @@ void handleCommands()
break;
case cmd_time:
if(theCmd.param > MIN_TS) {
#ifdef USE_ESPNOW
recvTimeEspNow(theCmd.param);
recvTimeEspNow(theCmd.param);
#endif // USE_ESPNOW
}
else if(theCmd.param == 0) {
#ifdef USE_ESPNOW
sendTimeESPNow(incMAC);
#endif // USE_ESPNOW
}
break;
case cmd_time_req:

@ -207,7 +207,7 @@ void pingback_espnow()
{
DBG("Ping back to sender");
SystemPacket sys_packet;
sys_packet.cmd = cmd_ping;
sys_packet = { .cmd = cmd_ping, .param = ping_reply };
if (!esp_now_is_peer_exist(incMAC))
{
#ifdef ESP8266
@ -471,7 +471,7 @@ void sendESPNow(uint8_t address)
}
void recvTimeEspNow(uint32_t t) {
// Process time if there is no master set yet or if LoRa is the master or if we are already the time master
// Process time if there is no time source set yet or if LoRa is the time source or if we are already the time source
if(timeSource.tmNetIf <= TMIF_ESPNOW ) {
DBG("Received time via ESP-NOW from 0x" + String(incMAC[5], HEX));
if(timeSource.tmNetIf < TMIF_ESPNOW) {
@ -487,7 +487,7 @@ void recvTimeEspNow(uint32_t t) {
}
}
else {
DBG("ESP-NOW 0x" + String(incMAC[5], HEX) + " is not time master, discarding request");
DBG("ESP-NOW 0x" + String(incMAC[5], HEX) + " is not time source, discarding request");
}
return;
}
@ -518,12 +518,12 @@ esp_err_t sendTimeESPNow() {
}
// Send the time to a specific node
esp_err_t sendTimeESPNow(uint8_t addr) {
esp_err_t sendTimeESPNow(uint8_t *addr) {
esp_err_t result = ESP_FAIL;
SystemPacket sys_packet = { .cmd = cmd_time, .param = now };
DBG1("Sending time to ESP-NOW address 0x" + String(addr));
result = sendESPNow(&addr, &sys_packet);
DBG1("Sending time to ESP-NOW address 0x" + String(addr[5],HEX));
result = sendESPNow(addr, &sys_packet);
return result;
}

@ -168,7 +168,8 @@ void getSerial() {
}
else if(obj.containsKey("cmd")) { // SystemPacket
cmd_t c = doc[0]["cmd"];
if(c == cmd_time) {
uint32_t p = doc[0]["param"];
if(c == cmd_time && p > MIN_TS) {
if(timeSource.tmNetIf < TMIF_SERIAL) {
timeSource.tmNetIf = TMIF_SERIAL;
timeSource.tmSource = TMS_NET;
@ -189,6 +190,9 @@ if(setTime(doc[0]["param"])) {
DBG2("Did not set time from incoming serial.");
}
}
else if(c == cmd_time && p == 0) {
// Received a request for us to send time via serial -- not implemented yet.
}
else {
DBG2("Incoming Serial: unknown cmd: " + String(c));
}

@ -233,7 +233,7 @@ void fetchNtpTime() {
delay(10);
}
if(i < 800) {
DBG("Took " + String(i * 10) + "ms to get NTP response from " + String(timeServer) + ".");
DBG2("Took " + String(i * 10) + "ms to get NTP response from " + String(timeServer) + ".");
NTPFetchFail = 0;
// We've received a packet, read the data from it
FDRSNtp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

@ -52,7 +52,7 @@ unsigned long lastRtcCheck = 0;
unsigned long lastRtcTimeSetMin = 0;
// function prototypes
crcResult sendTimeLoRa();
void sendTimeLoRa();
void printTime();
esp_err_t sendTimeESPNow();
bool setTime(time_t);
@ -274,9 +274,11 @@ void checkDST() {
// Periodically send time to ESP-NOW or LoRa nodes associated with this gateway/controller
void sendTime() {
if(validTime() && timeSource.tmSource > TMS_NONE) { // Only send time if it is valid
if(validTime()) { // Only send time if it is valid
DBG1("Sending out time");
#if defined(USE_WIFI) || defined(USE_ETHERNET)
sendTimeSerial();
#endif
sendTimeLoRa();
sendTimeESPNow();
}
@ -292,7 +294,9 @@ bool setTime(time_t currentTime) {
}
now = currentTime;
slewSecs = now - previousTime;
DBG1("Time adjust " + String(slewSecs) + " secs");
if(slewSecs > 2) {
DBG1("Time adjust " + String(slewSecs) + " secs");
}
// time(&now);
localtime_r(&now, &timeinfo); // write to timeinfo struct
@ -366,7 +370,9 @@ void adjTimeforNetDelay(time_t newOffset) {
if(newOffset < UINT32_MAX && validTime()) {
now = now + newOffset - previousOffset;
previousOffset = newOffset;
DBG1("Time adj by " + String(newOffset) + " secs");
if(newOffset > 2) {
DBG1("Time adj by " + String(newOffset) + " secs");
}
}
if(timeSource.tmSource == TMS_NET && newOffset > 10) {
DBG("Time off by more than 10 seconds!");

Loading…
Cancel
Save