Author Topic: Communication between python and moteino  (Read 3237 times)

Steinarrr

  • NewMember
  • *
  • Posts: 27
  • Country: is
Communication between python and moteino
« on: February 21, 2016, 01:13:29 PM »
Hi people :)

I've been working on a moteino network controlled by a python(2.7) program. To implement this I created a python module and I figured someone might want to use it or build on it.

I want to start by saying I'm certainly no python expert and it wouldn't surprise me if some things from the code could be done in a better or more efficient way.

How the communications are structured:
  • Individual nodes recieve and send structs, each one can have a unique struct but I haven't implemented sending more than one type of struct to a single node
  • The base moteino has no idea what kind of struct it is sending. The base just recieves and sends a string of hex characters. First byte (2 hex characters) are the nodeID that should recieve/just sent and rest is the data ending with a newline.
  • The python module encodes the struct into a hex string and interpets the hex string


Here is an example of its usage:
Code: [Select]
from MoteinoBeta import MoteinoNetwork


class MyNetwork(MoteinoNetwork):
    def __init__(self):
        MoteinoNetwork.__init__(self, port='COM50', baudrate=9600)

    def recieve(self, diction):
        print diction


mynetwork = MyNetwork()
mynetwork.add_device(name='TestDevice', _id=0, structsring="int Command;int Something;")
mynetwork.start_listening()

mynetwork.send({'Send2': 'TestDevice', 'Command': 123, 'Something': 456})

Here is what I have so far: http://pastebin.com/2z3dwWpw

This is still a work in progress and I haven't implemented all of my ideas or datatypes (so far I have only implemented byte and unsigned int) but most of the functionality seems to be working. Still have some testing to do.

I think this could be a cool module but It's hard for me to think and implement outside of my own needs. (for example I think sending different structs to the same node should be implemented but since I haven't needed that so far... you get the idea)

Anyway, I'd love to get some feedback. I don't feel like putting work into making this more presentable if no one will ever need or use this except for me :P

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Communication between python and moteino
« Reply #1 on: February 21, 2016, 02:54:40 PM »
Interesting approach, how do you interface this to the world? Or is this just a python class which you use in whatever project you want as a submodule?

Steinarrr

  • NewMember
  • *
  • Posts: 27
  • Country: is
Re: Communication between python and moteino
« Reply #2 on: February 21, 2016, 09:04:11 PM »
It's a python class that you should be able to use in whatever project you want, In my setup the python program is running on a PC that has a base moteino connected via usb but this should also work on a RasberryPi

My goal was essentially to move the decision making of the network into python because there I have access to more data and can therefore make better decisions. It will also make it a lot easier to add functionality to the network at a later stage.
For example: Device A reports X and therefore device B should do Y unless Device C reports Z.

All communication goes through the python script so Device A just tells python about X instead of telling B directly to do Y and then B checking on C.

One nice thing about this is that the BaseMoteino has no idea what it is sending so there is no need to program the BaseMoteino if you add a device to your network.

In the example I gave I called:
Code: [Select]
mynetwork.send({'Send2': 'TestDevice', 'Command': 123, 'Something': 456})

Out there should be a moteino node called TestDevice waiting to recieve a struct declared by:

Code: [Select]
typedef struct{
  unsigned int Command;
  unsigned int Something;
} Payload;

the python module knows this because I told it that out there is a device called TestDevice with ID=0 that sends and receives that kind of struct, I did this with:

Code: [Select]
mynetwork.add_device(name='TestDevice', _id=0, structsring="int Command;int Something;")

When I called mynetwork.send(...) python prints to the Serial port: '007b00c801' which means: send to device with ID='00' the info '7b00c801'.

the BaseMoteino interprets from hex into bytes and sends it, The TestDevice recieves this info, assumes it contains the struct it expects and casts the info into the struct. It can then extract from the struct like normally.

Regarding how python accepts data from the network I assume each user will overwrite the recieve function like I did but to have some functionality you'd probably want something like:

Code: [Select]
    def recieve(self, diction):
        if diction['Sender'] == 'TestDevice':
            if diction['Command'] == 100:
                print "TestDevice just reported about whatever command 100 means"
                print "It also told us that something is: " + str(diction['Something'])

so diction will contain all the info recieved from the network.
« Last Edit: February 22, 2016, 09:41:34 AM by Steinarrr »

Steinarrr

  • NewMember
  • *
  • Posts: 27
  • Country: is
Re: Communication between python and moteino
« Reply #3 on: April 20, 2016, 09:38:13 PM »
Hi all, although I doubt many are reading this hehe...

Anyway, short update. I have changed the code a lot since my previous post. I also got the chance to test the module in a new environment. (I set up a remote controlled hot tub, that is, Raspberry Pi hosting a website where you could remotely control my parent's hot tub, I'll probably post something about it in the coming days)

Back to the module. I decided to call it moteinopy. I have added it to PyPi so installing is as easy as 'pip install moteinopy'. I also made a GitHub repository for it: https://github.com/Steinarr134/moteinopy.

I have made a good example showing how to send data, and am now working on an example showing how to receive data.
you can check those out at https://github.com/Steinarr134/moteinopy/tree/master/examples

If anyone is interested or has any questions, feel free to ask

-Steinarr

« Last Edit: April 21, 2016, 11:55:12 AM by Felix »

Steinarrr

  • NewMember
  • *
  • Posts: 27
  • Country: is
Re: Communication between python and moteino
« Reply #4 on: October 22, 2016, 10:29:29 AM »
I just released version 0.1.0b0 to pypi. This is the first Beta version of the package. I have also added a bunch of cool functionality.

For example getting a temperature reading could be as easy as:

Code: [Select]
response = mynode.send_and_receive("RequestTemperature")
if response:
    print(response["temperature"])

So if you're going to parse between python and moteino this is what you need (in my opinion)

Feel free to ask if you have questions :)

Roger2016

  • NewMember
  • *
  • Posts: 17
  • Country: us
Re: Communication between python and moteino
« Reply #5 on: October 26, 2016, 10:10:24 AM »
Thanks for posting this!  I'm very interested in giving this a try.  I would like to use this to marry my new Moteino home automation toys to my Legacy X10 stuff.  I've found Python scripts for communicating with the X10 network.  Ultimately I'd like to kill off my ancient X10 stuff, but one step at a time.

Steinarrr

  • NewMember
  • *
  • Posts: 27
  • Country: is
Re: Communication between python and moteino
« Reply #6 on: October 26, 2016, 05:14:23 PM »
Awesome!

Don't be afraid to ask if something is unclear. You can ask here or raise an issue on the github repository, I'll get an email about it both ways...

micca

  • NewMember
  • *
  • Posts: 2
  • Country: us
Re: Communication between python and moteino
« Reply #7 on: August 14, 2017, 12:02:54 AM »
Hi Steinarrr,

I've started to try and use moteinopy, but I'm running into an issue when instantiating the network.

I have a clean up to date install of rasbian jessie lite(from 5.27) on a rpi 3 with moteinopy installed via pip. I have the moteino connected via the GPIO pins with Basesketch 2.3 on it.

It's hanging up here when trying to use the sending data examples.
Code: [Select]
mynetwork = MoteinoNetwork(port='/dev/ttyAMA0', frequency=MoteinoNetwork.RF69_868MHZ, high_power=False, network_id=1, base_id=1, encryption_key='0123456789abcdef')

Here's the trace: https://pastebin.com/gnXa2aWq

Maybe this is something to do with thread blocking? Any ideas?

Thanks, look forward to making use of your module!

RoSchmi

  • NewMember
  • *
  • Posts: 23
  • Country: de
Re: Communication between python and moteino
« Reply #8 on: August 14, 2017, 05:06:57 AM »
Hi,
I didn't analyse your code in-depth but I assume that you use a baudrate of 115200. For me that seems rather high for 3.3 V devices. I would try to start with 9600.

Steinarrr

  • NewMember
  • *
  • Posts: 27
  • Country: is
Re: Communication between python and moteino
« Reply #9 on: August 14, 2017, 09:45:50 AM »
Oh my!, someone other then me is using the module! Awesome!

I haven't used the GPIO serial port that much on the pi. I implemented it on a pi2 but when I upgraded to a pi3 it had all sorts of problems. Eventually I figured out that it was caused by the onboard bluethooth module on the pi3. Appearantly it is connected to the GPIO serial pins. After some frustration and trying to shut the bluethooth module off I eventually just connected an FTDI adapter to a USB port on the pi and used it that way. This has been a long introduction to ask if the bluethooth module might be messing things up.

Reading through the trace I see that the python sketch tries to restart the base as it should but the base never responds with the wakeup sign. I read through the BaseSketch and noticed an error, I forgot to react to the reset sign in the initialization phase. I probably haven't experienced this issue because I keep using FTDI adapters which hardware reset the base whenever the serial port is initialized  ::).

I have added the missing piece of code into the github repository, could you get the newest version and re-upload it to the base?

Regarding RoSchmi's comment about the baudrate; I haven't experienced any problems with using 115200. I chose to have it this high to minimize transit times, using 9600 would add ~60ms to the transit time. Oh and if you want to use lower baudrate I think you have to upgrade to a newer beta version of moteinopy (use pip install moteinopy==1.1.1b6)

Hope this solves it. If not, hit me up and I'll try harder. ;)

Edit:
I just uploaded the current beta version to pip as a new production version so maybe do 'pip install moteinopy --upgrade' for good measures :)

Also,  you can get a better feel for what is happening behind the scenes by putting:
Code: [Select]
import logging
logging.basicConfig(level=logging.DEBUG)
at the top of your script
« Last Edit: August 14, 2017, 10:13:48 AM by Steinarrr »

micca

  • NewMember
  • *
  • Posts: 2
  • Country: us
Re: Communication between python and moteino
« Reply #10 on: August 14, 2017, 08:39:04 PM »
Bluetooth was the big issue. Wasn't aware of the pi3 conflicts.

I followed the instructions felix wrote here https://lowpowerlab.com/guide/mightyhat/pi3-compatibility/, updated moteinopy to 1.1.1 and the base sketch. I am happy to report I am up and running!

Thank you so much for your help! I will let you know if I come across anything.