Since that version is in use, a frequency of 49.8 [Hz] is regularly reported (about once an hour), which might be caused by effectively missing one interrupt.
I suspect that garbage collection (gc) is causing the measurements of a low mains frequency. Therefore, automatic gc was disabled, and replaced by a periodic (immediately after sending a message to the MQTT broker) run of the gc. Still a low frequency is reported a few times per hour. Small variations on this way of working did not result in improvements.
Two relevant portions of the script are shown below. The first one is the ISR, the second one the main part of the task to publish the measurements, which is invoked once every 300 seconds.
Code: Select all
def isr( self, pin ):
# assert pin is self.pin, 'Unexpected interrupt'
tocp = time.ticks_us() # Time of current pulse
self.topp = tocp # Update pulse statistics
self.puls+= 1
if self.puls < self.pulses:
return # Wait till enough pulses seen
self.lopt= time.ticks_diff( tocp, self.tofp )
self.tofp= tocp # Set up for next measurement
self.puls= 0
#
try:
micropython.schedule( self.refupd, None )
except RuntimeError:
error_count+= 1
Code: Select all
if station.isconnected():
pld= _mqtt_pld_hdr # Payload header
for sensor in sensors:
rslt= sensor[1].Poll()
pld+= '"{}":{},'.format( sensor[1].Name,
sensor[2].format(*rslt) )
pld+= '"count":{}}}'.format( report_count )
try:
client.publish( _mqtt_topic, pld.encode() )
except OSError:
print( 'ERROR: MQTT broker connection failed.')
Restart()
else:
print( 'ERROR: Wifi connection broken' )
Restart()
#
report_count+= 1
gc.collect() # Clean up heap