Added RTC offset calculation to return absolute time

This commit is contained in:
Terra 2022-04-24 22:06:06 +02:00
parent d1c052ac90
commit 3c53f94c1b
5 changed files with 27 additions and 22 deletions

View File

@ -31,7 +31,7 @@ jitter = int.from_bytes(config[17:21], byteorder="little")
pointers = list(config[21 : 21 + n_devices]) pointers = list(config[21 : 21 + n_devices])
modem_frequency = 868.0 modem_frequency = 868.0
modem_power = 0 modem_power = 2
client_address = 0x1234 client_address = 0x1234
server_address = 0x0001 server_address = 0x0001
@ -90,4 +90,4 @@ client_key = 0x0#0x7ed64cce5b5d8e85
port.write(b"k") port.write(b"k")
port.write(struct.pack("<QQ", client_key, server_key)) port.write(struct.pack("<QQ", client_key, server_key))
#port.write(b"s") port.write(b"s")

View File

@ -13,13 +13,11 @@ bool _is_client;
uint8_t configuration_memory[CFGMEM]; uint8_t configuration_memory[CFGMEM];
DeviceBase* devices[N_DEVICES]; DeviceBase* devices[N_DEVICES];
uint32_t last_server_message_id = 0; uint32_t last_server_message_id = 0;
uint32_t rtc_offset = 0;
//void init_mn(bool is_client = true) //void init_mn(bool is_client = true)
void initMN() void initMN()
{ {
if (batteryVoltage() < 3.5 && !USBDevice.connected()) // Shut off below this voltage, down to 3.1 V should be OK if the regulator has a dropout of 400 mV and conducts fully if its output is below 3.3 V
LowPower.deepSleep();
//_is_client = is_client; //_is_client = is_client;
EEPROM.setCommitASAP(false); // Don't unnecessarily write to the EEPROM EEPROM.setCommitASAP(false); // Don't unnecessarily write to the EEPROM
@ -95,6 +93,9 @@ void initMN()
initializeDevices(); initializeDevices();
initRTC(); initRTC();
if (batteryVoltage() < 3.5 && !USBDevice.connected()) // Shut off below this voltage, down to 3.1 V should be OK if the regulator has a dropout of 400 mV and conducts fully if its output is below 3.3 V
LowPower.deepSleep();
} }
void test() void test()
@ -306,8 +307,11 @@ bool receive()
switch (message_type) switch (message_type)
{ {
case MT_Time: case MT_Time:
rtc_offset = server_message_id - RTC->MODE1.COUNT.reg;
if (server_message_id > message_id) // Ensure that IDs are sequential if (server_message_id > message_id) // Ensure that IDs are sequential
message_id = server_message_id; message_id = server_message_id;
return true; return true;
break; break;
} }
@ -444,5 +448,5 @@ void initRTC() // https://github.com/arduino-libraries/RTCZero/blob/master/src/R
uint32_t getRTC() uint32_t getRTC()
{ {
return RTC->MODE1.COUNT.reg; return RTC->MODE1.COUNT.reg + rtc_offset;
} }

View File

@ -75,8 +75,8 @@ void loop()
SerialUSB.print("Battery voltage: "); SerialUSB.print("Battery voltage: ");
SerialUSB.println(batteryVoltage()); SerialUSB.println(batteryVoltage());
SerialUSB.print("RTC time: "); SerialUSB.print("RTC + offset time: ");
SerialUSB.println(RTC->MODE1.COUNT.reg); SerialUSB.println(getRTC());
SerialUSB.println("END"); SerialUSB.println("END");
break; break;
@ -155,7 +155,7 @@ void loop()
long next_tick_sensors_dt = next_tick_sensors - now_time; long next_tick_sensors_dt = next_tick_sensors - now_time;
long min_delay = min(next_tick_device_dt, next_tick_sensors_dt); long min_delay = min(next_tick_device_dt, next_tick_sensors_dt);
if (min_delay > 1000 && !USBDevice.connected()) if (min_delay > 1000 && !USBDevice.connected() && message_id > 1000000000) // Listen until the time is initialized
{ {
power_sleep(min_delay); power_sleep(min_delay);
offset += min_delay; offset += min_delay;
@ -170,5 +170,4 @@ void power_sleep(uint32_t milliseconds)
setLoopPower(OFF); setLoopPower(OFF);
LowPower.sleep(milliseconds); LowPower.sleep(milliseconds);
radio.setModeIdle(); radio.setModeIdle();
delay(100);
} }

View File

@ -90,14 +90,15 @@ class MultiNode:
print(f"Hash doesn't match! Expected {hash_function.digest()} got {data_hash}") print(f"Hash doesn't match! Expected {hash_function.digest()} got {data_hash}")
return return
if not rx_id in self.devices: if not tx_id in self.devices:
self.devices[rx_id] = {"last_message_id": msg_id} self.devices[tx_id] = {}
self.decode_packet(self.devices[rx_id], data) self.devices[tx_id]["last_message_id"] = msg_id
self.print_data() self.decode_packet(self.devices[tx_id], data)
# RSSI cahnges from -13 to -27 on change from +20 to +2 dBm TX setting
# print(f"{tx_id} #{msg_id}: {decode_packet(data):.3f} V, {payload.rssi} dB(?) RSSI, {payload.snr} dB(?) SNR {(time.clock_gettime_ns(0)) / 1e9}") # print(f"{tx_id} #{msg_id}: {self.decode_packet(self.devices[rx_id], data)} V, {payload.rssi} dB(?) RSSI, {payload.snr} dB(?) SNR {(time.clock_gettime_ns(0)) / 1e9}")
# print(f"{tx_id} #{msg_id}: {data.hex()} {self.decode_packet(data)}, {payload.rssi} dB(?) RSSI, {payload.snr} dB(?) SNR {(time.clock_gettime_ns(0)) / 1e9}") # print(f"{tx_id} #{msg_id}: {data.hex()} {self.decode_packet(data)}, {payload.rssi} dB(?) RSSI, {payload.snr} dB(?) SNR {(time.clock_gettime_ns(0)) / 1e9}")
self.print_data()
def send_packet(self, target: int, data): def send_packet(self, target: int, data):
assert len(data) < 256 assert len(data) < 256
@ -121,17 +122,19 @@ class MultiNode:
def print_data(self): def print_data(self):
for device_id in self.devices: for device_id in self.devices:
device = self.devices[device_id] device = self.devices[device_id]
print(f'{device_id}:') print(f'Node {device_id} @ ID {device["last_message_id"]}:')
if "status" in device: if "status" in device:
print(f'\t{device["status"]["battery"]} V {device["status"]["temperature"]} °C') print(f'\t{device["status"]["battery"]:.3f} V {device["status"]["temperature"]:.1f} °C')
if "sensors" in device: if "sensors" in device:
for sensor in device["sensors"]: for sensor in device["sensors"]:
if sensor["value"] == math.nan: if sensor["value"] == math.nan:
print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: ERROR') print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: ERROR')
else: else:
print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: {sensor["value"]} {self.sensor_type_table[sensor["type"]]}') print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: {sensor["value"]:.4f} {self.sensor_type_table[sensor["type"]]}')
print()
if __name__ == "__main__": if __name__ == "__main__":

3
TODO
View File

@ -1,5 +1,4 @@
- Add password protection to configuration - Add password protection to configuration
- Webserver - Webserver
- More sensor types - More sensor types
- Control of node outputs - Control of node outputs
- Power saving features