Skip to content

Commit

Permalink
use keep_alive in examples; add funding.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Oct 5, 2022
1 parent 1ede9ae commit 55cdc39
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
12 changes: 12 additions & 0 deletions .github/FUNDING.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: 2bndy5
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: ["https://www.paypal.me/Brendan884"] # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
3 changes: 2 additions & 1 deletion examples/homie_button_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def on_disconnected(client: MQTT, user_data, rc):
mqtt_client.on_connect = lambda *args: print("Connected to the MQTT broker!")

# connect to the broker and publish/subscribe the device's topics
device.begin()
device.begin(keep_alive=3000)
# keep_alive must be set to avoid the device's `$state` being considered "lost"

# a forever loop
try:
Expand Down
3 changes: 2 additions & 1 deletion examples/homie_clock_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def on_disconnected(client: MQTT, user_data, rc):
mqtt_client.on_connect = lambda *args: print("Connected to the MQTT broker!")

# connect to the broker and publish/subscribe the device's topics
device.begin()
device.begin(keep_alive=3000)
# keep_alive must be set to avoid the device's `$state` being considered "lost"

# a forever loop
try:
Expand Down
11 changes: 6 additions & 5 deletions examples/homie_dht_sensor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
mqtt_client = MQTT(**mqtt_settings, socket_pool=pool)

# create a light_sensor object for analog readings
dht_in = board.A3 # change this accordingly
dht_in = board.A0 # change this accordingly
dht_sensor = adafruit_dht.DHT11(dht_in)
dht_sensor.measure() # update current data for Homie init

# create the objects that describe our device
device = HomieDevice(mqtt_client, "my device name", "lib-dht-sensor-test-id")
dht_node = HomieNode("DHT11", "temperature/humidity sensor")
dht_temperature_property = PropertyFloat(
"temperature", init_value=dht_sensor.temperature, unit="°F"
"temperature", init_value=dht_sensor.temperature * (9 / 5) + 32, unit="°F"
)
dht_humidity_property = PropertyPercent(
"humidity", datatype="integer", init_value=dht_sensor.humidity
Expand All @@ -60,7 +60,8 @@ def on_disconnected(client: MQTT, user_data, rc):
mqtt_client.on_connect = lambda *args: print("Connected to the MQTT broker!")

# connect to the broker and publish/subscribe the device's topics
device.begin()
device.begin(keep_alive=3000)
# keep_alive must be set to avoid the device's `$state` being considered "lost"

# a forever loop
try:
Expand All @@ -73,9 +74,9 @@ def on_disconnected(client: MQTT, user_data, rc):
mqtt_client.loop()
dht_sensor.measure() # update current data
temp = device.set_property(
dht_temperature_property, dht_sensor.temperature
dht_temperature_property, dht_sensor.temperature * (9 / 5) + 32
)
print("Temp:", temp, dht_temperature_property.unit, end=" ")
print("Temp:", temp, dht_temperature_property.unit[-1], end=" ")
humid = device.set_property(dht_humidity_property, dht_sensor.humidity)
print("Humidity:", humid, dht_humidity_property.unit, end="\r")
except MMQTTException:
Expand Down
3 changes: 2 additions & 1 deletion examples/homie_light_sensor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def on_disconnected(client: MQTT, user_data, rc):
mqtt_client.on_connect = lambda *args: print("Connected to the MQTT broker!")

# connect to the broker and publish/subscribe the device's topics
device.begin()
device.begin(keep_alive=3000)
# keep_alive must be set to avoid the device's `$state` being considered "lost"

# a forever loop
try:
Expand Down
4 changes: 2 additions & 2 deletions examples/homie_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def on_disconnected(client: MQTT, user_data, rc):
mqtt_client.on_connect = lambda *args: print("Connected to the MQTT broker!")

# connect to the broker and publish/subscribe the device's topics
device.begin()
device.begin(keep_alive=3000)
# keep_alive must be set to avoid the device's `$state` being considered "lost"

# a forever loop
try:
Expand All @@ -86,7 +87,6 @@ def on_disconnected(client: MQTT, user_data, rc):
now = time.time()
if now - refresh_last > 0.5: # refresh every 0.5 seconds
refresh_last = now
assert mqtt_client.is_connected()
mqtt_client.loop()
except MMQTTException:
print("!!! Connection with broker is lost.")
Expand Down

0 comments on commit 55cdc39

Please sign in to comment.