Added RTC offset calculation to return absolute time
This commit is contained in:
@@ -31,7 +31,7 @@ jitter = int.from_bytes(config[17:21], byteorder="little")
|
||||
pointers = list(config[21 : 21 + n_devices])
|
||||
|
||||
modem_frequency = 868.0
|
||||
modem_power = 0
|
||||
modem_power = 2
|
||||
client_address = 0x1234
|
||||
server_address = 0x0001
|
||||
|
||||
@@ -90,4 +90,4 @@ client_key = 0x0#0x7ed64cce5b5d8e85
|
||||
port.write(b"k")
|
||||
port.write(struct.pack("<QQ", client_key, server_key))
|
||||
|
||||
#port.write(b"s")
|
||||
port.write(b"s")
|
@@ -13,13 +13,11 @@ bool _is_client;
|
||||
uint8_t configuration_memory[CFGMEM];
|
||||
DeviceBase* devices[N_DEVICES];
|
||||
uint32_t last_server_message_id = 0;
|
||||
uint32_t rtc_offset = 0;
|
||||
|
||||
//void init_mn(bool is_client = true)
|
||||
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;
|
||||
|
||||
EEPROM.setCommitASAP(false); // Don't unnecessarily write to the EEPROM
|
||||
@@ -95,6 +93,9 @@ void initMN()
|
||||
|
||||
initializeDevices();
|
||||
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()
|
||||
@@ -306,8 +307,11 @@ bool receive()
|
||||
switch (message_type)
|
||||
{
|
||||
case MT_Time:
|
||||
rtc_offset = server_message_id - RTC->MODE1.COUNT.reg;
|
||||
|
||||
if (server_message_id > message_id) // Ensure that IDs are sequential
|
||||
message_id = server_message_id;
|
||||
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
@@ -444,5 +448,5 @@ void initRTC() // https://github.com/arduino-libraries/RTCZero/blob/master/src/R
|
||||
|
||||
uint32_t getRTC()
|
||||
{
|
||||
return RTC->MODE1.COUNT.reg;
|
||||
return RTC->MODE1.COUNT.reg + rtc_offset;
|
||||
}
|
||||
|
@@ -75,8 +75,8 @@ void loop()
|
||||
SerialUSB.print("Battery voltage: ");
|
||||
SerialUSB.println(batteryVoltage());
|
||||
|
||||
SerialUSB.print("RTC time: ");
|
||||
SerialUSB.println(RTC->MODE1.COUNT.reg);
|
||||
SerialUSB.print("RTC + offset time: ");
|
||||
SerialUSB.println(getRTC());
|
||||
SerialUSB.println("END");
|
||||
break;
|
||||
|
||||
@@ -155,7 +155,7 @@ void loop()
|
||||
long next_tick_sensors_dt = next_tick_sensors - now_time;
|
||||
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);
|
||||
offset += min_delay;
|
||||
@@ -170,5 +170,4 @@ void power_sleep(uint32_t milliseconds)
|
||||
setLoopPower(OFF);
|
||||
LowPower.sleep(milliseconds);
|
||||
radio.setModeIdle();
|
||||
delay(100);
|
||||
}
|
||||
|
Reference in New Issue
Block a user