Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for remote serial ports. #6

Merged
merged 2 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Command Line Tool
-h, --help show this help message and exit
-v be more verbose
-d DEVICE, --device DEVICE
set local device e.g. '/dev/ttyUSB0' or
set remote device e.g. 'rfc2217://[IP]:[PORT]'
default: '/dev/ttyUSB0'
-f FREQUENCY_RFM1 set the frequency for RFM1
-F FREQUENCY_RFM2 set the frequency for RFM2
-t TOGGLE_INTERVAL_RFM1
Expand Down Expand Up @@ -80,6 +83,25 @@ then the tool will print the defined names
id=16 t=18.700000 h=60 nbat=0 name=Livingroom
id=0 t=17.400000 h=65 nbat=0 name=Kitchen

Using remote serial port with ser2net
-------------------------------------

You can also use ser2net to connect to a remote JeeLink Adapter. This can be useful, if you use
a docker container or if you can not attach a JeeLink adapter to your host running pylacrosse. On your
remote device install ser2net and add the following line to your ser2net.conf:

.. code :: shell

20001:telnet:0:/dev/ttyUSB0:57600 remctl banner

Restart the ser2net daemon and connect to your remote host using pylacrosse command line tool:

.. code :: shell

# pylacrosse -d rfc2217://[REMOTE_IP]]:20001 scan
id=40 t=16.000000 h=69 nbat=0 name=Bedroom
id=16 t=18.700000 h=60 nbat=0 name=Livingroom
id=0 t=17.400000 h=65 nbat=0 name=Kitchen

.. _Jeelink: https://www.digitalsmarties.net/products/jeelink
.. _sketch: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/arduino/36_LaCrosse-LaCrosseITPlusReader.zip
Expand Down
6 changes: 4 additions & 2 deletions pylacrosse/cli_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import codecs
import logging
import os
import pprint
import time
try:
from ConfigParser import (SafeConfigParser, NoOptionError)
Expand Down Expand Up @@ -92,10 +91,13 @@ def led(lacrosse, config, args):
lacrosse.led_config(state)

def main(args=None):
parser = argparse.ArgumentParser('LaCrosse sensor CLI tool.')
parser = argparse.ArgumentParser('LaCrosse sensor CLI tool.', formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-v', action='store_true', dest='verbose',
help='be more verbose')
parser.add_argument('-d', '--device', type=str, dest='device',
help='set local device e.g. \'{0}\' or\n'
'set remote device e.g. \'rfc2217://[IP]:[PORT]\'\n'
'default: \'{0}\''.format(DEFAULT_DEVICE),
default=DEFAULT_DEVICE)
parser.add_argument('-f', type=str, dest='frequency_rfm1',
help='set the frequency for RFM1')
Expand Down
13 changes: 11 additions & 2 deletions pylacrosse/lacrosse.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import logging
import re
import threading
from serial import Serial

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -55,7 +54,7 @@ def __init__(self, port, baud, timeout=2):
self._port = port
self._baud = baud
self._timeout = timeout
self._serial = Serial()
self._serial = SerialPortFactory().create_serial_port(port)
self._callback_data = None

def open(self):
Expand Down Expand Up @@ -241,3 +240,13 @@ def _parse(self, line):
def __repr__(self):
return "id=%d t=%f h=%d nbat=%d" % \
(self.sensorid, self.temperature, self.humidity, self.new_battery)


class SerialPortFactory(object):
def create_serial_port(self, port):
if port.startswith("rfc2217://"):
from serial.rfc2217 import Serial
return Serial()
else:
from serial import Serial
return Serial()