Skip to content

Commit 55cdc39

Browse files
committed
use keep_alive in examples; add funding.yaml
1 parent 1ede9ae commit 55cdc39

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

.github/FUNDING.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: 2bndy5
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: ["https://www.paypal.me/Brendan884"] # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

examples/homie_button_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ def on_disconnected(client: MQTT, user_data, rc):
8686
mqtt_client.on_connect = lambda *args: print("Connected to the MQTT broker!")
8787

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

9192
# a forever loop
9293
try:

examples/homie_clock_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def on_disconnected(client: MQTT, user_data, rc):
8181
mqtt_client.on_connect = lambda *args: print("Connected to the MQTT broker!")
8282

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

8687
# a forever loop
8788
try:

examples/homie_dht_sensor_test.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
mqtt_client = MQTT(**mqtt_settings, socket_pool=pool)
3131

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

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

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

6566
# a forever loop
6667
try:
@@ -73,9 +74,9 @@ def on_disconnected(client: MQTT, user_data, rc):
7374
mqtt_client.loop()
7475
dht_sensor.measure() # update current data
7576
temp = device.set_property(
76-
dht_temperature_property, dht_sensor.temperature
77+
dht_temperature_property, dht_sensor.temperature * (9 / 5) + 32
7778
)
78-
print("Temp:", temp, dht_temperature_property.unit, end=" ")
79+
print("Temp:", temp, dht_temperature_property.unit[-1], end=" ")
7980
humid = device.set_property(dht_humidity_property, dht_sensor.humidity)
8081
print("Humidity:", humid, dht_humidity_property.unit, end="\r")
8182
except MMQTTException:

examples/homie_light_sensor_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def on_disconnected(client: MQTT, user_data, rc):
5454
mqtt_client.on_connect = lambda *args: print("Connected to the MQTT broker!")
5555

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

5960
# a forever loop
6061
try:

examples/homie_simpletest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ def on_disconnected(client: MQTT, user_data, rc):
7676
mqtt_client.on_connect = lambda *args: print("Connected to the MQTT broker!")
7777

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

8182
# a forever loop
8283
try:
@@ -86,7 +87,6 @@ def on_disconnected(client: MQTT, user_data, rc):
8687
now = time.time()
8788
if now - refresh_last > 0.5: # refresh every 0.5 seconds
8889
refresh_last = now
89-
assert mqtt_client.is_connected()
9090
mqtt_client.loop()
9191
except MMQTTException:
9292
print("!!! Connection with broker is lost.")

0 commit comments

Comments
 (0)