Hi @ReneTode,
Thank you for your answer.
I’m sorry, I forgot to mention that my python code is only the beginning of my program and of course the goal of it is to read the serial port incoming data. But as my program fails on the serial.Serial(), I didn’t want to confuse anyone with all my code.
This code does, for now, just open the serial port, read incoming data and close it:
import appdaemon.plugins.hass.hassapi as hass
import serial
#
# RS_TIC App
#
# Args:
#
class RS_TIC(hass.Hass):
def initialize(self):
self.log("RS_TIC")
ser = serial.Serial(
port = '/dev/ttyUSB0',
baudrate = 9600, # 9600 bauds
bytesize = serial.SEVENBITS, # 7bits
parity = serial.PARITY_EVEN, # even parity
stopbits = serial.STOPBITS_ONE, # 1 stop bit
xonxoff = False, # no flow control
timeout = 1
)
for i in range(10):
# read a data line
x = ser.readline()
# clean the string
x = x.decode('utf-8').rstrip()
# split the fields
c=x.split(' ')
# log (for the moment) one of the data
if c[0] == "STINTS":
self.log("%s: %s", c[0], c[1])
ser.close()
In the same meaning, I didn’t put all the log output, but here it is:
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] 00-banner.sh: executing...
-----------------------------------------------------------
Hass.io Add-on: AppDaemon 4
Python Apps and Dashboard using AppDaemon 4.x for Home Assistant
-----------------------------------------------------------
Add-on version: 0.1.2
You are running the latest version of this add-on.
System: HassOS 3.10 (armv7 / raspberrypi3)
Home Assistant version: 0.105.4
Supervisor version: 200
-----------------------------------------------------------
Please, share the above information when looking for help
or support in, e.g., GitHub, forums or the Discord chat.
-----------------------------------------------------------
[cont-init.d] 00-banner.sh: exited 0.
[cont-init.d] 01-log-level.sh: executing...
[cont-init.d] 01-log-level.sh: exited 0.
[cont-init.d] appdaemon.sh: executing...
Looking in links: https://wheels.hass.io/alpine-3.11/armv7/
Collecting pyserial
Downloading https://files.pythonhosted.org/packages/0d/e4/2a744dd9e3be04a0c0907414e2a01a7c88bb3915cbe3c8cc06e209f59c30/pyserial-3.4-py2.py3-none-any.whl (193kB)
Installing collected packages: pyserial
Successfully installed pyserial-3.4
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[cont-init.d] appdaemon.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
[16:18:08] INFO: Starting AppDaemon...
2020-02-16 16:18:12.286131 INFO AppDaemon: AppDaemon Version 4.0.1 starting
2020-02-16 16:18:12.287026 INFO AppDaemon: Python version is 3.8.1
2020-02-16 16:18:12.289747 INFO AppDaemon: Configuration read from: /config/appdaemon/appdaemon.yaml
2020-02-16 16:18:12.291129 INFO AppDaemon: Added log: AppDaemon
2020-02-16 16:18:12.292627 INFO AppDaemon: Added log: Error
2020-02-16 16:18:12.294069 INFO AppDaemon: Added log: Access
2020-02-16 16:18:12.295537 INFO AppDaemon: Added log: Diag
2020-02-16 16:18:12.364495 INFO AppDaemon: Loading Plugin HASS using class HassPlugin from module hassplugin
2020-02-16 16:18:12.468449 INFO HASS: HASS Plugin Initializing
2020-02-16 16:18:12.470376 INFO HASS: HASS Plugin initialization complete
2020-02-16 16:18:12.472845 INFO AppDaemon: Initializing HTTP
2020-02-16 16:18:12.475494 INFO AppDaemon: Using 'ws' for event stream
2020-02-16 16:18:12.522756 INFO AppDaemon: Starting API
2020-02-16 16:18:12.543982 INFO AppDaemon: Starting Admin Interface
2020-02-16 16:18:12.546547 INFO AppDaemon: Starting Dashboards
2020-02-16 16:18:12.589545 INFO HASS: Connected to Home Assistant 0.105.4
2020-02-16 16:18:12.625428 INFO AppDaemon: App 'hello_world' added
2020-02-16 16:18:12.627598 INFO AppDaemon: App 'RS_TIC' added
2020-02-16 16:18:12.631056 INFO AppDaemon: Found 2 total apps
2020-02-16 16:18:12.633555 INFO AppDaemon: Starting Apps with 2 workers and 2 pins
2020-02-16 16:18:12.638662 INFO AppDaemon: Running on port 5050
2020-02-16 16:18:12.722428 INFO HASS: Evaluating startup conditions
2020-02-16 16:18:12.753672 INFO AppDaemon: Got initial state from namespace default
2020-02-16 16:18:14.655105 INFO AppDaemon: Scheduler running in realtime
2020-02-16 16:18:14.669415 INFO AppDaemon: Adding /config/appdaemon/apps to module import path
2020-02-16 16:18:14.683809 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/RS_TIC.py
2020-02-16 16:18:14.717736 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/hello.py
2020-02-16 16:18:14.723074 INFO AppDaemon: Initializing app hello_world using class HelloWorld from module hello
2020-02-16 16:18:14.727920 INFO AppDaemon: Initializing app RS_TIC using class RS_TIC from module RS_TIC
2020-02-16 16:18:15.073192 INFO hello_world: Hello from AppDaemon
2020-02-16 16:18:15.078646 INFO hello_world: You are now ready to run Apps!
2020-02-16 16:18:15.090383 INFO RS_TIC: RS_TIC
2020-02-16 16:18:15.094659 WARNING RS_TIC: ------------------------------------------------------------
2020-02-16 16:18:15.095943 WARNING RS_TIC: Unexpected error running initialize() for RS_TIC
2020-02-16 16:18:15.097078 WARNING RS_TIC: ------------------------------------------------------------
2020-02-16 16:18:15.114836 WARNING RS_TIC: Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/serial/serialposix.py", line 265, in open
self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
FileNotFoundError: [Errno 2] No such file or directory: '/dev/ttyUSB0'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/appdaemon/app_management.py", line 145, in initialize_app
await utils.run_in_executor(self, init)
File "/usr/lib/python3.8/site-packages/appdaemon/utils.py", line 276, in run_in_executor
response = future.result()
File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/config/appdaemon/apps/RS_TIC.py", line 15, in initialize
ser = serial.Serial(
File "/usr/lib/python3.8/site-packages/serial/serialutil.py", line 240, in __init__
self.open()
File "/usr/lib/python3.8/site-packages/serial/serialposix.py", line 268, in open
raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0'
2020-02-16 16:18:15.116039 WARNING RS_TIC: ------------------------------------------------------------
2020-02-16 16:18:15.120777 INFO AppDaemon: App initialization complete
2020-02-16 16:18:22.607342 INFO AppDaemon: New client Admin Browser connected
The log shows that ‘/dev/ttyUSB0’ doesn’t exist in the AppDaemon context while, either with an SSH session, either by examining ‘dmesg’ output or with the supervisor, ‘/dev/ttyUSB0’ really exists.
As I said, in an SSH session, I’m able to see the incoming data with a ‘cat /dev/ttyUSB0’ command. Of course, as the data bytes format is a little bit special, it’s human unreadable, but I can see a continuous flow of garbage data.
There is probably a way to ‘mount’ the serial port in the AppDaemon context… I spent a lot of time searching in the community forum or google but without success.
Finally, @ReneTode, I thank you for your advices for my 1st steps in writing programs, but I omit to mention that ‘I’m a total newbie… in AppDaemon and HA / HassIO’, not in programming which is my full time job since… 1986. That was funny and makes my day
Please don’t be upset, it’s my fault and I very appreciate that people like you takes time to answer to lost people and that you take care about the words you use with guys who want to begin programming in a such complex architecture like HA. That’s nice and not very common in some forums. I thank you very much for that .