Skip to content

Commit

Permalink
Merge pull request #15 from Ribbit-Network/pr/heartbeat
Browse files Browse the repository at this point in the history
ribbit: Add an heartbeat to detect event loop blocking events
  • Loading branch information
keenanjohnson authored Dec 8, 2022
2 parents 7f9cc57 + 036db0b commit 93cf0a3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async def _main():
import ribbit.golioth as _golioth
import ribbit.coap as _coap
import ribbit.http as _http
import ribbit.heartbeat as _heartbeat

if not in_simulator:
import ribbit.network as _network
Expand All @@ -78,6 +79,8 @@ class Registry:

registry = Registry()

_heartbeat.Heartbeat(in_simulator)

config_schema = []
if not in_simulator:
config_schema.extend(_network.CONFIG_KEYS)
Expand Down
44 changes: 44 additions & 0 deletions modules/ribbit/heartbeat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import time
import logging
import uasyncio as asyncio


class Heartbeat:
def __init__(self, in_simulator):
self._in_simulator = in_simulator
self._logger = logging.getLogger(__name__)

if not self._in_simulator:
self._setup_pixel()
asyncio.create_task(self._loop())

def _setup_pixel(self):
import neopixel
import machine

machine.Pin(21, machine.Pin.OUT, value=1)
neo_ctrl = machine.Pin(33, machine.Pin.OUT)
self._pixel = neopixel.NeoPixel(neo_ctrl, 1)

async def _loop(self):
interval = 200
warn_interval = 300

on = True
px = self._pixel

while True:
if not self._in_simulator:
if on:
px[0] = (4, 2, 0)
else:
px[0] = (0, 0, 0)
on = not on
px.write()

start = time.ticks_ms()
await asyncio.sleep_ms(interval)
duration = time.ticks_diff(time.ticks_ms(), start)

if duration > warn_interval:
self._logger.warning("Event loop blocked for %d ms", duration - interval)

0 comments on commit 93cf0a3

Please sign in to comment.