Issue
The script is running fine in thonny on the rasperry zero and does its job to measure and interact with the mqtt server. This is intended to be a script that runs in the background and I added it to /etc/rc.local between the lines fi and exit 0: python3 /home/pi/pisensor/scripts/dht22_mqtt.py & After rebooting the expected messages on the mqtt server did not appear, so I tried running the script on the console. I called it with python3 dht22_mqtt.py in the directory and after some seconds it just looked like it was finished (new line in in console), but had done nothing (no messages to mqtt server and no print messages). No errors were shown that a library is missing or something was wrong. Could it be there is a problem with running an async script like this from console?
Is there a possibility to debug the script when run from console? I tried the -d parameter of python3, but with the same result. I tried pip3 with the dht and the paho-mqtt lib, but it was already installed.
You can have a look at the script here:
from time import sleep
import asyncio
from paho.mqtt import subscribe, publish
from random import randint
from typing import Awaitable, Any
async def run_sequence(*functions: Awaitable[Any]) -> None:
for function in functions:
await function
async def run_parallel(*functions: Awaitable[Any]) -> None:
await asyncio.gather(*functions)
class IOTSensorDHT22:
def __init__(self,
mqtt_server="localhost",
mqtt_port=1883,
sensor_pin=4,
sensor_values_topic_prefix="/sensor/raw/bedroom/",
sensor_interval=60,
sensor_interval_topic="/setting/sensor/inside/power/interval"):
self.messages = None
self.values = None
self.mqtt_server = mqtt_server
self.mqtt_port = mqtt_port
self.sensor_pin = sensor_pin
self.sensor_values_topic_prefix = sensor_values_topic_prefix
self.sensor_interval = sensor_interval
self.sensor_interval_topic = sensor_interval_topic
self.mqtt_client_id = f"mqtt_client_{randint(0, 1000)}"
async def query_interval(self):
new_interval = int(subscribe.simple(self.sensor_interval_topic, hostname=self.mqtt_server).payload)
if new_interval != self.sensor_interval:
print(
f"new interval {new_interval} - old: {self.sensor_interval} - assigning new value: {new_interval != self.sensor_interval}")
self.sensor_interval = new_interval
async def publish_values(self):
print(f"publishing messages: {self.messages}")
publish.multiple(self.messages, hostname=self.mqtt_server)
async def measure(self):
self.values = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, self.sensor_pin)
async def build_messages(self):
messages = (self.build_message("hum", self.values[0]), self.build_message("temp", self.values[1]))
print(messages)
self.messages = messages
def build_message(self, topic_appendix, value):
return self.sensor_values_topic_prefix + topic_appendix, round(value, 1)
def sleep(self):
print(f"going to sleep for {self.sensor_interval}")
sleep(self.sensor_interval)
async def run(self):
while True:
await run_parallel(
run_sequence(self.measure(), self.build_messages(), self.publish_values()),
self.query_interval()
)
self.sleep()
if __name__ is "__main__":
asyncio.run(
IOTSensorDHT22(
mqtt_server="192.168.188.37", #
sensor_pin=12,
sensor_values_topic_prefix="/sensor/raw/bedroom/",
sensor_interval=60,
sensor_interval_topic="/setting/sensor/inside/power/interval/").run())
edit: thanks for the edit help :)
UPDATE - SOLVED:
I wrote if __name__ is "__main__":
while it should have been
if __name__ == "__main__":
My mistake for sure, but it was misleading for me that it worked fine in the IDE (Thonny), but did nothing when run from console.
Solution
You can use ipdb for debugging in the console.
pip install ipdb
then put this in your code at the point you want to debug:
import ipdb
ipdb.set_trace()
then you can just step through your code, like with a debugger from an IDE. for more instructions how to use ipdb see this tutorial.
Answered By - Ruud van den Boomen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.