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:
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
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:
mynetwork.send({'Send2': 'TestDevice', 'Command': 123, 'Something': 456})
Out there should be a moteino node called TestDevice waiting to recieve a struct declared by:
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:
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:
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.
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:
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 :)
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.
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!
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:
import logging
logging.basicConfig(level=logging.DEBUG)
at the top of your script