From d597f50d5349da5e20ff2936b310e52d28bd2a1f Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Sat, 16 Mar 2019 00:38:41 +0100 Subject: [PATCH] timesync fixes --- src/TTN/Nodered-Timeserver.json | 34 ++++++++++++++++----------------- src/main.cpp | 2 +- src/timesync.cpp | 11 +++++++---- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/TTN/Nodered-Timeserver.json b/src/TTN/Nodered-Timeserver.json index f2aac01c..7520e527 100644 --- a/src/TTN/Nodered-Timeserver.json +++ b/src/TTN/Nodered-Timeserver.json @@ -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" ] ] }, diff --git a/src/main.cpp b/src/main.cpp index ced48264..2a7dc259 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 diff --git a/src/timesync.cpp b/src/timesync.cpp index 8442cdb6..e1c8e614 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -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