LowPowerLab Forum

Hardware support => Projects => Topic started by: Steinarrr on February 21, 2016, 01:13:29 PM

Title: Communication between python and moteino
Post by: Steinarrr 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:

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.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
Title: Re: Communication between python and moteino
Post by: Felix 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?
Title: Re: Communication between python and moteino
Post by: Steinarrr 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.
Title: Re: Communication between python and moteino
Post by: Steinarrr 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 (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 (https://github.com/Steinarr134/moteinopy/tree/master/examples)

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


Title: Re: Communication between python and moteino
Post by: Steinarrr 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:

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 :)
Title: Re: Communication between python and moteino
Post by: Roger2016 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.
Title: Re: Communication between python and moteino
Post by: Steinarrr on October 26, 2016, 05:14:23 PM

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...
Title: Re: Communication between python and moteino
Post by: micca 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!
Title: Re: Communication between python and moteino
Post by: RoSchmi on August 14, 2017, 05:06:57 AM
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.
Title: Re: Communication between python and moteino
Post by: Steinarrr 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. ;)

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
at the top of your script
Title: Re: Communication between python and moteino
Post by: micca 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.