Hawkes Blog
Windows reboot | Linux be root
Hawkes Blog
PyMyGW a simple mysensors Gateway

PyMyGW is a simple Gateway between the MySensors Network and OpenHab written in python inspired by wbcode/ham

PyMyGW a simple mysensors Gateway

Development stopped.

FYI I'd stopped the development of the gateway because I switched to an ESP8266 MQTT GW. I will leave the blogpost online, just for the records.

Update

PyMyGW now supports MQTT as an alternative to the OpenHab RestAPI

What`s that

  • a simple SerialGateway for the MySensors Serial Protocol Version 1.4
  • offers basic functionality for the MySensors Network
  • offers IDs for new Nodes (stored in a sqlite DB)
  • push Sensor Data to OpenHab via RestApi
  • or publish to an MQTT Broker
  • written in python
  • inspired by wbcode/ham

Source

the source is available via Github


Requirement

  • an arduino running the MySensors Serial sketch Version 1.4 connected via usb/serial on a linux host
  • an openhab installation (not required for testing, but it would be usefull)
  • at least on openhab item (atm only NumberItems are supported)
  • some MySensors Nodes
  • python pip installed

Installation

git clone https://github.com/thehawkes/pymygw.git
cd pymygw
git checkout v0.1
pip install -r requirements.txt

Configuration

Usually it should be enough to change OpenbabAPI URL and the SerialPort in the config.py

OpenhabAPI = 'http://adugw.home:8080/rest/items'
SerialPort = '/dev/ttyACM0'

Start the Gateway

cd <<installdirectory>>
python pymygw.py

the gateway creates a logfile in <<installdirectory>>/pymygw.log

a typical startup looks like:

 1 2014-10-16 17:27:55,768 [DEBUG] Found 3_0 in DB: typ = V_TEMP, openhab = Temperature_Schlafzimmer
 2 2014-10-16 17:27:55,773 [DEBUG] Found 1_0 in DB: typ = V_HUM, openhab = Luftfeuchtigkeit_Wohnzimmer
 3 2014-10-16 17:27:55,778 [DEBUG] Found 1_1 in DB: typ = V_TEMP, openhab = Temperature_Wohnzimmer
 4 2014-10-16 17:27:55,804 [INFO] serial started up on /dev/ttyACM0, Baud 115200, Timeout 5
 5 2014-10-16 17:27:55,809 [INFO] serial is writeable
 6 2014-10-16 17:28:00,819 [INFO] command send: 0;0;3;0;2;Get Version
 7 
 8 2014-10-16 17:28:00,852 [INFO] incoming message: 0;0;3;0;14;Gateway startup complete.
 9 2014-10-16 17:28:00,874 [DEBUG] Message Length: 6, Message: ['0', '0', '3', '0', '14', 'Gateway startup complete.']
10 2014-10-16 17:28:00,889 [DEBUG] Parsed Message:
11                                 NodeID: 0,
12                                 ChildID: 0,
13                                 MessageType: INTERNAL,
14                                 SubType: I_GATEWAY_READY,
15                                 Payload: Gateway startup complete.
16 2014-10-16 17:28:00,902 [DEBUG] Processing Internal Message
17 2014-10-16 17:28:00,917 [INFO] incoming message: 0;0;3;0;2;1.4
18 2014-10-16 17:28:00,930 [DEBUG] Message Length: 6, Message: ['0', '0', '3', '0', '2', '1.4']
19 2014-10-16 17:28:00,941 [DEBUG] Parsed Message:
20                                 NodeID: 0,
21                                 ChildID: 0,
22                                 MessageType: INTERNAL,
23                                 SubType: I_VERSION,
24                                 Payload: 1.4
25 2014-10-16 17:28:00,957 [DEBUG] Processing Internal Message
26 2014-10-16 17:28:08,088 [INFO] incoming message: 0;0;3;0;9;read: 1-1-0 s=1,c=1,t=0,pt=7,l=5:23.3
27 2014-10-16 17:28:08,095 [DEBUG] Message Length: 6, Message: ['0', '0', '3', '0', '9', 'read: 1-1-0 s=1,c=1,t=0,pt=7,l=5:23.3']
28 2014-10-16 17:28:08,101 [INFO] Skipping debug message: 0;0;3;0;9;read: 1-1-0 s=1,c=1,t=0,pt=7,l=5:23.3
  • 1-3: reads known values from the db
  • 4-5: open the serial port
  • 6- : communication with the arduino serial GW (request sketch version)

the debug messages can be disabled in config.py


a typical set(update) request:

Sensor 1 on Node 1 is a DHT22 connected to an arduino Nano Clone

2014-10-16 17:28:08,111 [INFO] incoming message: 1;1;1;0;0;23.3
2014-10-16 17:28:08,117 [DEBUG] Message Length: 6, Message: ['1', '1', '1', '0', '0', '23.3']
2014-10-16 17:28:08,124 [DEBUG] Parsed Message:
                            NodeID: 1,
                            ChildID: 1,
                            MessageType: SET,
                            SubType: V_TEMP,
                            Payload: 23.3
2014-10-16 17:28:08,131 [DEBUG] Processing Set/Req Message
2014-10-16 17:28:08,137 [DEBUG] Message in set: {'sep': ';', 'ack': '0', 'nodeid': '1', 'messagetype': 'SET', 'subtype': 0, 'payload': '23.3', 'childid': '1'}
2014-10-16 17:28:08,145 [DEBUG] Node 1_1 found in nodes: {'typ': u'V_TEMP', 'openhab': u'Temperature_Wohnzimmer'}
2014-10-16 17:28:08,151 [DEBUG] Try to push value to openhab: {'sep': ';', 'ack': '0', 'nodeid': '1', 'messagetype': 'SET', 'subtype': 0, 'payload': '23.3', 'childid': '1'}
2014-10-16 17:28:08,285 [DEBUG] Openhab Items: {u'All': u'GroupItem', u'Schlafzimmer': u'GroupItem', u'update_greent': u'SwitchItem', u'Temperature': u'GroupItem', 'cache': datetime.datetime(2014, 10, 16, 17, 28, 8, 283984), u'Humidity': u'GroupItem', u'Luftfeuchtigkeit_Wohnzimmer': u'NumberItem', u'Date': u'DateTimeI
tem', u'Wohnzimmer': u'GroupItem', u'Temperature_Wohnzimmer': u'NumberItem', u'Temperature_Schlafzimmer': u'NumberItem'}
2014-10-16 17:28:08,292 [DEBUG] Openhab put url http://adugw.home:8080/rest/items/Temperature_Wohnzimmer/state with data 23.3
2014-10-16 17:28:08,447 [INFO] Openhab updated successfully:
                            Node: 1,
                            Sensor: 1
                            Openhab: Temperature_Wohnzimmer
                            Value: 23.3

the openhab part would be missing if the node is not glued to an openhab item yet

in this case, the node is known and the openhab item was updated via the restapi


The Webinterface

PyMyGW offers a simple webinterface on Port 5000 to glue a sensor to an openhab item

all available nodes are listed, gathered from the db + the glued openhab item

if no openhab item is configured, it offers a dropdown with all known openhab items

the openhab items are pulled via the openhab restapi on the PyMyGW startup and be cached for 5 mins (default)

edit config.py to change the port and/or cache timeout

WebPort = 5000
OpenhabCacheTimeout = 300

logoutput while deleting and glueing a node

2014-10-16 18:15:04,348 [INFO] Webinterface Action delete, Sensor 1_0, Openhab Luftfeuchtigkeit_Wohnzimmer
2014-10-16 18:15:04,355 [INFO] Would delete (u'delete', u'1_0', u'Luftfeuchtigkeit_Wohnzimmer')
2014-10-16 18:15:04,473 [DEBUG] Found 3_0 in DB: typ = V_TEMP, openhab = Temperature_Schlafzimmer
2014-10-16 18:15:04,484 [DEBUG] Found 1_0 in DB: typ = V_HUM, openhab = None
2014-10-16 18:15:04,487 [DEBUG] Found 1_1 in DB: typ = V_TEMP, openhab = Temperature_Wohnzimmer
2014-10-16 18:15:04,496 [INFO] Updated Sensor 1_0 to Openhab None
2014-10-16 18:15:15,024 [INFO] Webinterface Action update, Sensor 1_0, Openhab Luftfeuchtigkeit_Wohnzimmer
2014-10-16 18:15:15,033 [INFO] Would update (u'update', u'1_0', u'Luftfeuchtigkeit_Wohnzimmer')
2014-10-16 18:15:15,151 [DEBUG] Found 3_0 in DB: typ = V_TEMP, openhab = Temperature_Schlafzimmer
2014-10-16 18:15:15,160 [DEBUG] Found 1_0 in DB: typ = V_HUM, openhab = Luftfeuchtigkeit_Wohnzimmer
2014-10-16 18:15:15,167 [DEBUG] Found 1_1 in DB: typ = V_TEMP, openhab = Temperature_Wohnzimmer
2014-10-16 18:15:15,170 [INFO] Updated Sensor 1_0 to Openhab Luftfeuchtigkeit_Wohnzimmer

conclusion

the gateway is far from perfect but I use it with 2 nodes for ~3 weeks without any problems

the push to openhab is tested with a DHT22 and a DS18B20

I used one of my Raspberryi Pis to run PyMyGW and Openhab on the same host

feel free to add a comment or open a github issue if you have any problems/suggestions etc.

MfG Hawkes


  • 1
  • 2