{"id":274,"date":"2019-11-25T21:04:11","date_gmt":"2019-11-25T21:04:11","guid":{"rendered":"https:\/\/r.jander.me.uk\/?p=274"},"modified":"2019-11-26T13:23:19","modified_gmt":"2019-11-26T13:23:19","slug":"ev3-mailboxes-in-python","status":"publish","type":"post","link":"https:\/\/r.jander.me.uk\/index.php\/2019\/11\/25\/ev3-mailboxes-in-python\/","title":{"rendered":"EV3 Mailboxes in Python"},"content":{"rendered":"\n<p>Recently I wanted to enter the Alexa \/ LEGO MINDSTORMS challenge:<\/p>\n\n\n\n<p><a href=\"https:\/\/www.hackster.io\/contests\/alexa-lego-voice-challenge\">https:\/\/www.hackster.io\/contests\/alexa-lego-voice-challenge<\/a><\/p>\n\n\n\n<p>My idea required being able to send EV3 Mailbox messages via Bluetooth between ev3dev and a stock EV3 running the EV3g language &#8211; from Python. I&#8217;m new to Python, I&#8217;m a Perl programmer at heart, so this was somewhat of a learning curve moment. I had to get to grips with Bluetooth (not that difficult as I&#8217;m used the IP networking) and Python at the same time.<\/p>\n\n\n\n<p>I figured that one of the major selling points of Python was its extensive library of support functions, so set to looking for something providing EV3 Mailbox handling. My research wasn&#8217;t as fruitful as I&#8217;d hoped for. I could find various chunks of code but either they were flawed in their behaviour, or much more heavyweight than I wanted. So I decided to jump in feet first and write my own library.<\/p>\n\n\n\n<p>I&#8217;d written a library for App Inventor 2 <a href=\"https:\/\/r.jander.me.uk\/index.php\/2016\/08\/16\/my-first-bt-message\/\">[0]<\/a> <a href=\"https:\/\/r.jander.me.uk\/index.php\/2018\/03\/04\/receiving-bt-mailboxes-from-ev3-by-an-ai2-app\/\">[1]<\/a> <a href=\"https:\/\/r.jander.me.uk\/index.php\/2018\/03\/09\/updated-bt-comms-between-ev3-and-ai2\/\">[2]<\/a> <a href=\"https:\/\/r.jander.me.uk\/index.php\/2016\/08\/25\/update-on-my-bluetooth-ev3-mailbox-testing\/\">[3]<\/a> <a href=\"https:\/\/r.jander.me.uk\/index.php\/2018\/03\/10\/further-ai2-ev3-bluetooth-coding\/\">[4]<\/a> that would encode &amp; decode EV3 Mailbox payloads before, so this wasn&#8217;t too daunting a task. One aspect of Mailbox messages is that there isn&#8217;t an identifier within the payload that identifies the content: String, Float (IEE754 32 bit), Boolean. Normally this is handled in EV3g by expecting a specific type relating to the message name &#8211; i.e. a message called &#8220;status&#8221; would be defined to always be a Boolean, but &#8220;command&#8221; would always be a String &#8211; the code forces the type to remain constant. However, the type of the payload can be deduced to some extent, so I decide that I&#8217;d implement that within the Python class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Payload length = 1 byte =&gt; Boolean<\/li><li>Payload length = 4 bytes<ul><li>Last byte != NULL or NULL in the other bytes =&gt; Float<\/li><li>Otherwise =&gt; String<\/li><\/ul><\/li><li>All other payloads =&gt; String<\/li><\/ul>\n\n\n\n<p>The only issue with the logic above is that really, really, small numbers may get decoded as strings, e.g. &#8220;@@@\\x00&#8221; would get seen as a string, not 5.90052e-39 which is also a valid decode of it. As such I also implemented a .force_float() method which will re-decode the payload.<\/p>\n\n\n\n<p>The git repo for this library can be found at:<\/p>\n\n\n\n<p><a href=\"https:\/\/gitlab.com\/Jander\/ev3-mailbox-python\">https:\/\/gitlab.com\/Jander\/ev3-mailbox-python<\/a><\/p>\n\n\n\n<p>To use it do something like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from ev3mailbox import EV3Mailbox as Mailbox\n\nmessage = EV3Mailbox.encode(\"Name\", \"Message value\", Mailbox.Type.TEXT)\nprint(message)\n\n# Data from Bluetooth\n mailbox = EV3Mailbox.decode(payload)\n print(mailbox)<\/pre>\n\n\n\n<p>Hopefully this will prove useful to others. The code has been released under the GPLv3: <a href=\"https:\/\/www.gnu.org\/licenses\/gpl-3.0.txt\">https:\/\/www.gnu.org\/licenses\/gpl-3.0.txt<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I wanted to enter the Alexa \/ LEGO MINDSTORMS challenge: https:\/\/www.hackster.io\/contests\/alexa-lego-voice-challenge My idea required being able to send EV3 Mailbox messages via Bluetooth between ev3dev and a stock EV3 running the EV3g language &#8211; from Python. I&#8217;m new to Python, I&#8217;m a Perl programmer at heart, so this was somewhat of a learning curve &hellip; <a href=\"https:\/\/r.jander.me.uk\/index.php\/2019\/11\/25\/ev3-mailboxes-in-python\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;EV3 Mailboxes in Python&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[35,26,8,34],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ev3g","tag-lego","tag-mailbox","tag-python"],"_links":{"self":[{"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/comments?post=274"}],"version-history":[{"count":3,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":280,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/274\/revisions\/280"}],"wp:attachment":[{"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/media?parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/categories?post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/r.jander.me.uk\/index.php\/wp-json\/wp\/v2\/tags?post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}