timesync fixes

This commit is contained in:
Verkehrsrot 2019-03-16 00:38:41 +01:00
parent a88ea16e3a
commit d597f50d53
3 changed files with 25 additions and 22 deletions

View File

@ -126,22 +126,6 @@
]
]
},
{
"id": "f4c5b6de.f95148",
"type": "function",
"z": "449c1517.e25f4c",
"name": "Time_Sync_Ans",
"func": "/* LoRaWAN Timeserver\n\nconstruct 6 byte timesync_answer from gateway timestamp and node's time_sync_req\n\nbyte meaning\n0 sequence number (taken from node's time_sync_req)\n1..4 current second (from epoch time 1970)\n5 1/250ths fractions of current second\n\n*/\n\n let buf = new ArrayBuffer(6);\n let timestamp = (+new Date(msg.payload.metadata.gateways[0].time));\n \n var seconds = Math.floor(timestamp/1000);\n var fractions = (timestamp % 1000) / 4;\n var seqno = msg.payload.payload_raw[0];\n\n new DataView(buf).setUint8(0, seqno);\n new DataView(buf).setUint32(1, seconds);\n new DataView(buf).setUint8(5, fractions);\n\n msg.payload = new Buffer(new Uint8Array(buf));\n \n return msg;",
"outputs": 1,
"noerr": 0,
"x": 400,
"y": 280,
"wires": [
[
"49e3c067.e782e"
]
]
},
{
"id": "dac8aafa.389298",
"type": "json",
@ -219,7 +203,23 @@
"y": 200,
"wires": [
[
"f4c5b6de.f95148"
"595229b0.5df3d8"
]
]
},
{
"id": "595229b0.5df3d8",
"type": "function",
"z": "449c1517.e25f4c",
"name": "Time_Sync_Ans",
"func": "/* LoRaWAN Timeserver\n\nconstruct 6 byte timesync_answer from gateway timestamp and node's time_sync_req\n\nbyte meaning\n0 sequence number (taken from node's time_sync_req)\n1..4 current second (from epoch time 1970)\n5 1/250ths fractions of current second\n\n*/\n\n let timestamp = (+new Date(msg.payload.metadata.gateways[0].time));\n let timestamp_server = (+new Date(msg.payload.metadata.time));\n \n if ((timestamp < timestamp_server) && (timestamp > 0)) {\n \n var seconds = Math.floor(timestamp/1000);\n var fractions = (timestamp % 1000) / 4;\n var seqno = msg.payload.payload_raw[0];\n\n let buf = new ArrayBuffer(6);\n new DataView(buf).setUint8(0, seqno);\n new DataView(buf).setUint32(1, seconds);\n new DataView(buf).setUint8(5, fractions);\n\n msg.payload = new Buffer(new Uint8Array(buf));\n \n return msg;\n}\n\nelse\n\nreturn null;",
"outputs": 1,
"noerr": 0,
"x": 380,
"y": 280,
"wires": [
[
"49e3c067.e782e"
]
]
},

View File

@ -187,7 +187,7 @@ void setup() {
// set low power mode to off
#ifdef HAS_LOWPOWER_SWITCH
pinMode(HAS_LOWPOWER_SWITCH, OUTPUT);
digitalWrite(HAS_LOWPOWER_SWITCH, HIGH);
digitalWrite(HAS_LOWPOWER_SWITCH, LOW);
strcat_P(features, " LPWR");
#endif

View File

@ -155,15 +155,18 @@ void process_timesync_req(void *taskparameter) {
ESP_LOGD(TAG, "waiting %dms", 1000 - time_offset_msec);
vTaskDelay(pdMS_TO_TICKS(1000 - time_offset_msec));
setTime(time_to_set);
// sync timer pps to top of second
if (ppsIRQ)
timerWrite(ppsIRQ, 10000); // fire interrupt
if (ppsIRQ) {
timerRestart(ppsIRQ); // reset pps timer
CLOCKIRQ(); // fire clock pps interrupt
}
setTime(time_to_set);
timeSource = _lora;
timesyncer.attach(TIME_SYNC_INTERVAL * 60,
timeSync); // set to regular repeat
ESP_LOGI(TAG, "Timesync finished, time adjusted by %lld ms", time_offset.count());
ESP_LOGI(TAG, "Timesync finished, time adjusted by %lld ms",
time_offset.count());
} else
ESP_LOGI(TAG, "Timesync finished, time not adjusted, is up to date");
} else