Program all the things – How to develop IoT devices using MicroPython



hello everyone and welcome to PyCon and i as a software developer and maker she enjoys developing and facilitating workshops thank you so much Anna for that and she's an organizer of notebooks note what's a you a co-organizer of RIS banality meeting groups and an author of make JavaScript robotics so it's a pleasure to introduce you Anna thank you so much now over to you thank you all [Applause] right so you've already seen some of what micro PI thing can do so my talk is really much much more simple it's how you would get started with developing IOT devices using micropipette so just some more background about me so I'm a senior nodejs developer console keynote so I apologize if some of my pythons is not quite idiomatic but I do teach Python i I teach a Saturday robotics class with kids and I run workshops using Arduino 'z and various other devices to teach robotics and Internet of Things both with micro Python and with Arduino IDE so I'm a bit of a maker hobbyist and the project that I'm going to be drawing some examples on today is just the one that's pictured there unfortunately the robot did not survive in my suitcase very well the trip from Brisbane so it may or may not work but what it is is it's a little pet that is there to remind me when I need to do exercise and it kind of nags me and then I can press a button and let it know that I've actually done the exercise so a very very simple IOT demo device so I thought I'd talk through a bit of the process that I go through when I'm developing my hobbyist devices so I guess the first thing you do is kind of look at the requirements you know what you're what you want your project to do and then to choose the architecture select a language and the development tools as well as the hardware based on those requirements so for a lot of the really simple sort of projects like this is pet for example the architecture is not going to be very complex it's going to be one or two devices probably not not very intelligent devices mostly just gathering data from sensors or with some very simple actuators like some LEDs or something like that most of the work is going to be done by a cloud service or an app so that will effect a choice of hardware so once you've done that pick some hardware I'll talk a little bit about the hardware they've chosen for this project write some programs to read from the sensors control the actuators and then develop the communication between what's essentially a very dumb device with the applications that running in the cloud that are actually doing all of the the processing and the analytics on their data so a lot of IOT is really there to to get some actionable insights out of your data so analyze that data do something with it so in my case it'll be analyzing how much I've exercised and then work out how frequently it needs to nag me to actually get me to exercise so microphone Damien's already covered this but the reason I chose to use micro Python was that I was teaching a lot of classes using Arduino idea and I was already using esp8266 based microcontrollers and I found that because the students that I was teaching had already been exposed to Python that micro Python was a really intuitive sort of leap for them so trying to teach them some of these concepts using C++ C in Arduino IDE was was quite challenging so I started using micro Python for my classes and then I thought well why don't I start using micro Python for my own projects because it's really making things a lot simpler and more intuitive a lot faster to develop so Damian's already covered what micro Python is basically version of Python for constrained environments particularly microcontrollers and the thing that also really appealed to me is it's open source and there's a lot of really great documentation and libraries available for it so selecting hardware there is a lot of hardware that's supported by micro Python so these are some those of some of the ports the hardware that I chose to use for this project is the esp8266 and the reason that I chose that is just because in the hobbyist community it's pretty ubiquitous a lot of the projects that you used to build on say an Arduino with with the shield like it an Ethernet or a Wi-Fi shield a lot of people are now using esp8266 based microcontrollers it's because they're so widely available they're really well documented and you know they're really quite inexpensive as well when it comes to the esp8266 microcontroller there are lots and lots of different dev boards out there that you can choose these are the three that I typically go to for my projects so there's the Adafruit further huzzah esp8266 the we must D one many and the node MCU version one microcontroller and these are all very very similar development boards they all have obviously onboard Wi-Fi because the esp8266 based boards they all have up to four mega flash the modern versions of them will have forming the flash 80 megahertz processor they all operate on 3.3 volt logic which is a little bit different than say if you're coming from the arduino world which is typically 5-volt logic so you have to sort of adjust some of your circuits and programs to to cater for that and they have a decent number of digital i/o pins up to 11 some I think one of these devices has nine and one is 10 one is 11 they only have one analog one ADC pin which is a bit of a limitation if you're doing stuff with a lot of different analog sensors but for a lot of my really simple projects I really only want to deal with one sensor at a time and then you can also connect more sensors using something like an i2c bus so you can work around it or you could connect an external ADC if you wanted to so I found these to be really great for hobbyists projects and for developing prototypes because you've you've got you know a decent number of pins exposed there and they're all designed to plug straight into a breadboard to make it really easy to work with they do also all support ITC and SPI officially there's only software ITC on these boards but you know that's good enough for prototyping and the board that I'm using today is the Adafruit further hazhar purely because it's got an onboard lithium battery charger which just makes it really easy if you're developing a small standalone device you can plug the battery straight in it charges through the micro USB cable so that makes it really useful for prototyping without having to worry about power requirements so this is what the pin layout looks like on that particular board I apologize is probably not very readable but this is pretty indicative of the others you've got a number of i/o pins down one side and on the other side you've got things like your 5 volt and your ground and that sort of thing so the typical process that I use when I'm working on developing a project first of all I work out the interactions so Internet of Things devices you know the ones that I build anyway it tends to be very interactive and they're designed for demonstrating or for working with students so I think about the way that I want it to work and what what I wanted to achieve so for this particular device I it's very simple in terms of the circuit and the code but really the interactions are the important thing it needs to be kind of cute and it needs to have personality so that will actually engage with me and make me want to do my exercise so once you've designed the way that you want to it to feel and look I guess then you work out what components you need and what board that you need and then the process that I use is they typically design my circuit which is just plugging in the different components which pins I want them to go on and then write a program for each individual component and get that working independently so sort of a modular approach so for this particular project which has a buzzer a button and some LEDs as well as a screen it would be developing the programs for each one of those and then having functions for the you know what I want to display on each one or how I want it to interact for each one and then work on developing the program that actually does the communication up with my cloud service or whatever I'm running on the backend and then finally use the repple to interactively tweak so one of the great things about micro Python is it does have that interactive read eval print loop so I can go in and I can work out exactly what timings I need or exactly what colors I want to display on the LEDs that sort of thing without having to say an Arduino we'd have to go and compile it and upload it it would be a very slow process being able to do that pretty much instantaneously really cuts down on the prototyping time when it comes to designing those and developing those interactions and behavior so for this particular project that you can see here this is the circuit so it's really simple it's only a few components there's a 12 ring neopixel there's an O LED screen there's a button and there's a piezo buzzer that just beeps so that I'm forced to pay attention to it so what I do is I'll just step you through basically what I did for each one of those components and how I put it together so the first thing I did actually was I developed the shell for it so this is something that I just threw together in a 3d modeling program so this is what it looks like on the inside it's not a telephone it's it's the neopixel and you can see the 3d model for the case on the outside so this is kind of the shell here that's got the OLEDs screen on the inside with appearing as eyes and on the inside we've got the near pixel and the button and the buzzer so before you actually put it all together though you need to make sure that you've kind of hooked everything up and prepared the board because you don't want to have to be doing things that involve having to unplug everything once you've got in the case so this is all coming pretty much from the documentation for micro path and so I won't talk through a lot of detail but it's really really simple to get it up and running you pretty much just install whatever drivers you need to talk to the board USB serial drivers make sure you've got the ESP tool installed on your system and then you can use that ESP tool to erase the flash if you've used it before so I quite often recycle these from various projects so they might have had some arduino ide code in them that might have had Lua node MCU environment on them so flash it to make sure everything's nice and clean and then upload the latest version of microwave which you can download from the github pages from the releases so yeah really simple once you've done that you're pretty much ready to go then connecting to the board I prefer to use a command line I usually see a screen from my Mac to connect so just a matter of connecting at the 1 1 5 200 board rate and then when I'm doing things interactively what I'll do is I'll develop the code in say sublime text and then I just paste it across into my terminal using ctrl e to enter paste mode with micro Python and I'll demonstrate this in a minute talk through it first and then if I write longer longer files that I don't want to paste in I upload them using the am py tool which is from Adafruit which I find really useful particularly for loading up libraries onto the board when I'm working with students so I tend to use the web Ruppel so that's the web ripple there you can pretty much connect to the boards so if you've got it set up as an access point you can connect directly to it or if you have it set up to connect to your Wi-Fi network you can just go to the IP address and the port is eight to six six access it by the Weber Apple which just gives you a nice interface like this that lets you type in code so that's your Apple on the left hand side and it gives you a really simple way of uploading the files as well to the board if you are using that though originally it used to be enabled by default but now if it's not for I guess obvious security reasons so if you do want to use it you just need to make sure you import the Weber apples setup and then enable it on boot or if you prefer just import it as you need it and start it and stop it directly from the repple so when you do set up your device at first it will be set up as with an ad-hoc Wi-Fi network and the default password is micro Python with capital N it's a really good idea to change that so especially if you're coming to something like this so that someone doesn't hack into device while you're in the middle of a demo it has happened so yes so yeah this is how you do that you just set it up in the main dot py and upload that aren't your your board and yeah then you could at least got a different password probably can still hack into it but it might take them a bit longer so that's getting set up developing programs so I typically start with the simplest thing so in this particular example it's the button so a button just needs two wires I've got mine connected up on pin 12 just ground in pin 12 and it's super super easy to get this working on micro pythons so you pretty much important the machine library or did you just the pin classes all we need in this case set it up using the pin class on the pin that you want so pin 12 till it's an input mode and I've also set it up with the additional parameter to tell it to use the built-in pull-up resistor on the board because I didn't want to have an extra resistor in my circuit don't have a lot of space in there and once you've done that you can just get the button value using button dot value so if you want to do something in response to that button press or the simplest way to do it is just to pull the value of the button so just setting up a loop taking a reading it's always a good idea to take a couple of readings to compare and then yeah pretty much you can just use that to work out when the button is pressed and when it's released so you can do a really similar thing with analog sensors I don't having analog sensor in this particular example but if you are using your analog sensor you just use machined or ADC instead of using a pin so very very simple the only gotcha on these particular boards the esp8266 is is that you can only have a 1 volt voltage on that pin so if you are using a sensor that you've Sayed taken from an Arduino kit or something like that you need to make sure that you have set it up with a voltage divider so that you don't destroy your board ok so that's a button a buzzer also very simple two wires I've got mine plugged into pin 14 and again really really simple stuff this one's a bit different because rather than just being a digital reading on or off this is actually an output device and I'm writing to it so you can write to a digital pin using high and low or on and off but you can also write using PWM pulse width modulation so for something like piezo buzzer if you want to play different tones you'd set it up so you still import the pin class but also import PWM I've set up just a couple of different notes these are just the frequencies of those notes and then set it up on pin 14 in output mode and then it's as simple as just using the frequency so here's our dot freak to play the time and then finally DN it because as soon as you initialize it it will start making so this is not one that you want to do using the ripple necessarily because it's just kind of be squealing the whole time and when you have a whole class of kids doing that it gets a bit so yeah make sure you have that D in it there in the end one of the the components is really really popular with the students that I teach and that I love putting in my devices are neopixels so this is a two fruits marketing term for the ws2812 be light source which are pretty much just extremely brightly colored addressable LEDs and they are awesome they come in lots of different form factors you can get them in strips by the meter or you get them as individual components you can buy them in these rings or in the bar form and these can be in theory they need five volts but you can dry them off 3.3 volts which is great because you don't need an external battery if you're using only a small number of pixels so I've found pretty much out to about thirty or forty pixels is usually fine on just coming off the microcontroller pin up once you get beyond that you want to add some batteries so you can find out all about them on the Adafruit website but they're really really easy that pretty much designed to run off a single pin so you've got your power source and your ground but then you connect a single digital i/o pin to any number of pixels and you can chain them together as well so each ring will have an out pin as well that you can chain onto additional rings so if you've got a project that needs to have lots of pixels you connect them that way now there is a built in near pixel class in mica Python which is why I really like using these it's very very simple so you can just import the new pixel class and the pin class so you set up the pin first as an output pin and then you set up the neopixel class to use that pin and you tell her the number of pixels that you want to control and then it's as simple as just sending a couple of RGB values red green blue values to a pin to a pixel within that so this is an example of just some really simple code that I wrote that will you know actually iterate through the pixels and for the near pixel at that pixel number it'll set it to a particular color so it's just going to set them all to be particular color or clear them by setting the values all to zero and obviously you can do much more interesting things you can set different colors you can have animations and that sort of thing so those are all built-in libraries one of the really cool things about micro Python ecosystem is that there are lots of third-party libraries as well and I found the ones that Adafruit develops to be particularly cool they already had a lot of libraries that I was using in Arduino sort of sea land and some of them have been ported across to micro Python so one that I was using for the screen and the eyes here is the SSD 1306 a LED library and pretty much getting that working you just go and grab the MPI file from the repository and then upload it to the board using a mpy and this is an example this screen is an i2c component so it's an example of using i2c so you import the library set up the IPC component use an ITC class and just for backwards compatibility with arduino so not sure how many of you are familiar with working with arduino but typically on arduino analog pins four and five are the pins that are used for ITC and although technically you can use any of the pins on the esp8266 for ITC because it's software ITC by convention pretty much everyone uses 4 & 5 so you set up your LED display using that ITC clasp just telling it the dimensions of display and then you're ready to display so very very simple to get up and running with third-party libraries as well but once you've got all those bits and pieces all kind of doing something what you want to do is to be able to pull it all together and to actually get it talking to something in the cloud I mean that's Internet of Things so we need to have the Internet component by far the simplest way to get that up and running is just using HTTP POST requests so run a really simple web server on the esp8266 and talk to that so this is an example and I'll actually do this as a demo hopefully it'll work an example of a basic web server based on one that was sort of I think it might have been one of the Adafruit examples so you pretty much have the HTML that you want to display and we've set up the NIR pixels and we've got that color white function that we saw just before and then to kind of pull this all together and make it a network enabled device we just use socket modules so pretty much just a loop that reads the requesters they come in and is looking for just a particular string in that request so it's looking for an ear pixel on or near pixel off and will simply respond by turning the new pixels on or off just to a single color in this case based on whether or not that's in the string so let me just show you that working live demo hopefully this will work all right this is the one I've got connected I apologize I have way too many channels open alright so this one let's just check it's still alive yep see if I can zoom in on that a bit kids alright so this has this is the Myka Python repple and to run that web server code what I'll do is I'll just go grab it I've got it set up I did have it I just find it that looks like it there so all I do is I'll just I could upload that as a file but I'm just gonna copy and paste it straight into the ruffball there it is so hit ctrl e and you go into paste mode paste that in and then ctrl D oh no I forgot to import the library let me just go and check yeah let me just go grab that from yeah that's why you should not do live demos already are hand machined up in I probably edited this just before yeah thank you all right let's try this okay all right so now that's running as a webserver and what I will do is I'll just go and I'll join that access point just right there and if I go to a browser let's just open this one 6h now by default it will be on 1 & 2 1 6 8 4.1 yes good so I should in theory be able to hit the on and off button yeah and it's not working these would be read live demos yes so anyway that does work when the robot is not broken oh no time it's not okay that could be why show it again oh yeah control dude thank you yeah okay try this again just so this is how you connect Joey Jody fingers crossed oh let me try it one more time right yes that's why I was changing them to blue and I must have somehow messed my code up should have stuck with the red all right I thought it looked angry sorry okay that's that's how you do a web server I find that basic HTTP web server kind of approach works really well for demos and for prototyping but if you want to integrate with kind of the wider Internet of Things world you should probably use MQTT or something like MQTT so very simple messaging this is an example of a really simple MQTT program that pretty much sends the button events to a server so this is what I would use to notify that yes I have indeed done my exercise so you import the UM TTT which is built-in and you can there's some examples you can find that do stuff with Jason and putting in adjacent file done dramatically riding it and loading it in on start I highly recommend you do that I couldn't fit it on one slide though so I've got rid of all of that but it works the same way so it has all of your configuration in terms of which server and which channel which topic you're using an MQTT so it's just pub/sub so in this case it's like my button code from before I'm getting the button value getting a second reading comparing that to work out if I've actually got the button pressed and if it is that I'm publishing that button press event so let me just have a quick quick go and see if this can actually run very very quickly get rid of that all right so that would be need to actually make sure I'm running an MQTT service I'm running mosquito here on my local laptop and in another terminal I would need to have a mosquito client that's subscribing to the button events see that and then finally I need to have my mqtt program running here and then let me go back to that all the window hopefully when I press the button yeah it's getting pressed events there so this is not very impressive because like this is kind of what's happening on the back end but you get the idea you could have it storing the database or doing something cool so there's that and it's super as you can see it's super easy much easier than the web server to get right alright so there are lots of other languages you can use for doing this so Lua Arduino it's pre node.js for example but I really like using micro Python because the code is really clean and intuitive excellent for teaching students are already using Python and as I said that interactive repple you can't beat that for just working out what you're doing in terms of the interactions and the other really cool thing I think is going to be big is edge computing that's the idea where you do your processing as close to the data collection point as possible at the edges of your networks so on the board or on a gateway device and we all know that python is awesome for doing data processing and that sort of thing so I think there's really going to be great applications for edge computing with micro Python I've run out of time so this is we can find out more my blog on Twitter or yeah the micro Python documentation is awesome so thank you very much thank you Anna that was a wonderful talk and I'm sure your students enjoy it as much as we did thank you there's a ten minutes break now till the next one starts

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *