ESP8266 and MicroPython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
consultant I work in systems integration and software development stuff here in Melbourne when I'm very very lucky I get to do that in Python when I'm less lucky I get to do it in other languages but for fun I've been playing around with a lot of esp8266 stuff and trying to get a feel for this this side of things it's it's a nice change from dealing with enterprise systems and si P and Java and other things that no one really wants to do it's a lot of fun to actually go and play with a chip the size of your thumbnail for a little bit so this talk is about two things it's about esp8266 and it's about micro Python and about running one on the other all right whoo he was heard of these ESP things before this morning heaps of people Wow there was a very similar question at a session at OSD C last year or something and about three people put their hands up and I wasn't one of them I heard about these chips and went wow that is amazing what are they they're a system on a chip as Damian mentioned they're out of Shanghai by a company called expressive and on the inside there are ten silica core and a bunch of Wi-Fi stuff a bunch of I think they're Russians called zip tie bars D capped one and dissolved its packaging in acid and then took a photo with a very good microscope and that's what the dye looks like the giant parking lot on the bottom right hand there is the onboard RAM all the funny little stuff that looks like a petrol refinery or something is the CPU pretty much and those giant things that look like an enormous coils up in the top-left really are enormous coils the Wi-Fi is right there on the chip on the die it's actually kind of crazily beautiful if you look at it closely the way that they've constructed these antennas and and parts right there in the silicon it makes it a really nice platform to work with because it is so integrated it's in one place they first came to prominence as a Wi-Fi controller so a extra component you might add to something that ran on an AVR or something like that neat little unit eight little tiny pins you plug it in you can talk like a does everyone remember Hays 80 command sets for modems you talk them to it and send packets back and forth and so on eventually someone took a closer look at it and realize that it's several times as powerful as the AVR that it was helping if you have a look at the spec sheet it runs a lot faster and that this was a little ludicrous and so people started looking into this as a development platform in its own right and it has proven to be a very good platform a lot of people put a lot of work into that and I think the open source world and espresso both deserve a lot of credit for working well playing nicely with each other it's a bit of a new frontier for espresso I think and it's a bit of a new frontier for hope and sauce to be dealing so directly with Chinese manufacturers and the results have been fantastic um so in short it's the 32-bit core there's only one of them it can run at 80 megahertz or if you like wasting batteries it can run at 160 megahertz and wait for it some Ram the whole time instead or wait for its flash the whole time instead there's only about a hundred and sixty K of RAM on board of various types it was a very very limiting and normally it's coupled with a better for megabyte flash which is where all the actual kind of software happens espresso system manages to control that quite well stuff pages in and out without too much intervention from from the programmer you normally wouldn't buy the actual chip for start you keep losing the things they're very small instead you probably buy a little module like this this is an ESP 12 module there are a whole range of them imaginatively numbered from ESP oh one through to ESP 12 and there might be a 13 out by now they vary in basically the number of pins they have the size of the memory that's typically put on them things like that but they're all basically the same components an esp8266 processor some flash of varying size and try not to get burnt by that one on eBay and often they'll come with a little can over the top and generally a little built-in antenna which is the gold squiggly thing on the left it's a very small package that's only just under an inch long very easy to use there are even smaller packages out there if you happen to have better eyesight than me you could even use one all that's not all that new it's a processor system on a chip etc etc lots of people have been doing that for a few years now none of that's all letting you what's really new is that they can suddenly you can buy them quantity one for five bucks on eBay that's only a minor detail really but it changes the game a lot for hobbyists for beginners for people who might not want to blow a hundred bucks on a prototyping board if you're a big company it's it's really nothing you buy a dev kit it costs you a thousand dollars it's not really a big deal for a hobbyist that pretty much writes you out getting started and you've got to make a pretty big decision if that's something you really want to get into being able to buy one of these things for five bucks on eBay changes that game completely it means that you can use them in any crappy project that you happen to have a sudden whim to do and if that is a robot that picks up tennis balls you can do that and if it's made out of parts you found in the junk box and a piece of plank that's fine and if you then fry the microcontroller because there's basically well I don't think those bypass caps worked on that motor if you then fry the microcontroller it's five bucks and no one has to get upset and that's really good and so you can make lots of fun projects and and play a lot more it makes up for a lot more playful or platformer all right how do you actually program these things the the worst part about any of these these little systems as always you your your exciting package arrives in the mail for zero dollars postage from Shenzhen nor ever and you open it up and you look at this thing and you go right so I need about 10 times as much in components to wire this thing up as I spent on the actual CPU that's slightly perverse in this case it's a lot easier these things talk a really nice serial interface they have basically a couple of dedicated surreal lines that talk rs-232 TTL equivalent if you have a decent sized junk box worth of previous generations of CPU stuff you probably have several of these things they do need 3.3 volts by which they don't mean three point six volts and they certainly don't mean 3.7 volts and if you try three point eight volts it gets a nasty quick and they needed a probably a 200 250 milliamps or you'll get unpredictable results the old unspecified behavior tends to happen if you if the power lines dip as you program the things that's easy to get caught out on because a lot of the five volt to 3.3 volt converters you might have in the junk box tend to crap out at 100 milliamps the things draw a lot less than that in the long term but in the short term they draw lots of little spikes of power as the Wi-Fi chips turn on and off and things like that so that's just something to watch it's it's an extremely depressing way to get started on microcontroller when whenever you go to flash anything it fails and it sometimes may be the easy way if you really don't want to deal with that stuff at all and don't mean had one of these hanging off the side of his laptop is to buy node MCU that's really just an ESP 12 module you can see the DSP 12 module on the end of it soldered onto a little backing board there with tenth inch headers which are like what's used in standard bread boards that you might have lying around and on board is a 3.3 volt converter which is handy and a USB to UART converter so that's really handy too that's all wired up for you there's also a couple of tricky little transistors that mean the thing can automatically reset itself put itself into flash mode all that stuff you can get them for about 10 bucks which you know maybe have one less coffee and by the tender-back thing and then if you if you feel like you have mastered that then move on to buying the ones you can get for less than five bucks the it's a very handy little piece of hardware originally it was kind of marketed as a way to run Lua again as Damian mentioned but there's nothing actually locking that particular chip or that particular module on to that particular language or anything it's really just an esp8266 the one time that module gets annoying is if you actually want to use the UART for yourself because it's kind of tied up directly to the USB port so they're very very handy little units you'll caught of course also need some software you need to somehow push code on to the chip you need to tell it go reset yourself put yourself into flash mode etc there's a handy little lump I think all DSP tool that does that and really all you need to do is grab it from github and run it and it will put binary straight onto the device for you micro Python is now available as a binary that you can just download so you can skip the following steps if your interest is purely into into micro Python and not say I mean to using micro Python as opposed to working on it or changing it or writing new parts of it and stuff like that you can just grab the micro path and binary download it onto the chip but if you are interested in taking the the covers off micro path in a bit you'll need to get a few more things there's a ASP open SDK that's a bunch of C libraries and utilities and so on and so forth that provide a software development kit for the ESP espresso of themselves put out their own SDK but it has more closed source parts this particular version which is again on github has as much as possible open source components with only a few little lumps of of binary blob from espresso that are still required so it's probably not going to be Richard Stallman approved but it's certainly heading towards open source in its in its intent that's fun to build it's an entire GCC toolchain so you download an awful lot of Debian packages and then you run make and then you go make a nice cup of tea because on my laptop it took at least half an hour to actually build the thing when you get back though you've got a cross-platform compiler which can compile code into the ten silica machine code and it's just like GC so you can use all the things you know about GCC if you know things about yes you see it's just a regular replacement for GCC the SDK also includes support for that cool Wi-Fi chipset stuff and a surprisingly good library of things that do things like you know perform WPS and there's it's very simple to put the thing into a mode where it is both an access point and a client both at once which is kind of nifty and I didn't realize you were actually allowed to do that for according to protocol but apparently you are so that's good so you can sit there and be an access point and a client both at once and just see how people want to connect to it there is no operating system though and this can be a little bit weird for those of us who've worked in larger systems or used to the web world and all that there is no operating system underneath so there's no engine x2 dispatch to your processes there's no but there is a file system but it's very small there's no other processes running there there's certainly not a database and that can be a little weird there is a library called Libby SP httpd surprise surprise it's on github which provides a pretty good set of primitives for dealing with HTTP and dealing with WebSockets and things like that and it's quite easy to write a bit of see that actually picks up on an a hardware interrupt so just as a little example of what C code can look like I don't know probably can't read that all that well down the back but that's basically a little HTTP handler the code you require to open a socket listen for HTTP incoming connections allow people to switch up to WebSockets and so on and you just have to write your own details in there to actually make it do something useful however writing C is not everyone's cup of tea especially when you're trying to debug it on a platform that doesn't really have good debugging support at this point I ended up writing a bunch of code on in C and it'll went quite well until the end of that first little rush through the code where you you're feeling really good about everything and everything's flowing well and then you go away and you come back three weeks later and you go what on earth is this I don't even know who wrote this it's got my name all over it get blamed says it was me but I don't remember anything so it's actually I found it very difficult to come back to and then change stuff and so I had a look around at the other languages that are available for the platform no lure is the obvious one it came very early on to the platform and it's quite cool Lua is just slightly terrifyingly close to JavaScript and Python and therefore I find extraordinarily difficult to get my head around precisely where it isn't JavaScript and it isn't Python but it's it's not a bad little language with some technical drawbacks there are some Java scripts available but I've written far too much JavaScript in my life there's fourth that's cool there's Lisp that's very cool and there's basic so if you really feel like going back to the the great tin print hello 20 go to 10 days of programming you can you know which i think is fantastic I mean we're talking about a chip here that is this big and is many times as powerful as the Apple 2 that you may have learned basic on so I think that's cool thanks to this Kickstarter campaign into Damian we now have micro Python and yay whoo for those of us who are Python fans this is a great thing if you like to think in Python and you like to write Python and you like to have code that you can come back to three weeks later and actually remember our worked this is a great thing micro Python is pretty easy to build like I say you can just download it as a binary now but if you need to build it it's right there in github again you can build it and make it relatively simply oh by the way all these slides will be available they're all online so if you want to read the instructions again later I'll make sure the URL to that at the end you can just build micro Python as soon as you write that onto the chip with the ESP tool as Damian showed you you can just connect straight to that device and see a ripple and I still am blown away by how magical that seems being able to connect to this thing the size of your thumbnail and actually say oh wow it's Python I can type dirt I can type help I can compile Python in Python it's very very complete I kept looking at the the implementation going but surely at some point I'll find something I can't do and okay oh this isn't really Python this is a fast but every time I went to do something it just worked I owe as domine mentioned is very very simple you can just type the stuff at the repple if you want and this has got to be the quickest platform I've ever seen to get blinking LEDs upon not admittedly terribly useful in of itself but there are 1600 lines I think 15 of them can do PWM there's one analog input none of this stuff is greatly luxurious if you used to like an AVR platform or something like this it's relatively small number of i/o lines but it's a good start you can make a really good start on a servo driven robot or something like that just by soldering things directly to the pins because the PWM is good enough to drive say servo motors so that's pretty simple the Wi-Fi again as dome invention is very very simple to set up there's a bunch of libraries wrapping it in Python so you can very easily connect up to Wi-Fi and there's the equivalent to be an AP so people can connect to you sockets again about as simple as you can get and there is actually I haven't got a slide for it there is actually a web server project someone's setup which is like a micro web server you're not going to be running Jango on this thing if you wanted to run Jango on this thing that's two rooms over maybe get a bigger computer though you're not going to be running Jango on this thing but you you probably do want to look at it as a webserver it's actually really handy to be able to just serve up your application as a web application straight from the device because many people have phones things like that you can just point the phone straight at the thing loaded as a web page and then it can do a WebSockets or HTTP POST or whatever to control what the hardware is doing I find that a really nice approach how do you actually get software on there I mean it's not an apple - so we don't have to type in the listing every time the ripple is cool but it's a little limiting after a while there are a few ways one of them is to plunk stuff into this build directory modules build directory in which case it gets bytecode compiled and then frozen into the the firmware you upload to the device well you can put it in this scripts directory where it gets frozen into the firmware as source code and uploaded to device but both of those things are a little annoying especially if you don't want to wear out your flash memory too quickly so the alternative is that you use either there's a thing called web ripple which is a TCP or a WebSocket sorry speaking version of the rebel so that you can talk to the ripple over over Wi-Fi or I've got a little utilities programs collection there that I'm starting on which is basically just a bunch of ways you can talk to the thing over that serial line so okay this is actually inspired by Lua tool which comes with no Lua it connect up and types for you into the repple open this file right to this file write this text and thankfully due to pythons wrapper function that's really really simple to implement so it lets you upload files and soon to come download files think of arles all that sort of stuff so that's a handy way to get stuff on there what's files are on there once you reset the device it'll look for a file called boot pi and run that so that's very simple to get into thankfully there is also pip so micro pip provides most of the things you were thinking of from pip so that you can do package management properly and things like that we really don't want to go back to the good old days of copying package files around and having your own version of everything and all that or even go to get sub modules I mean it's an approach but it's it's an annoying approach to have to take so micro PIP is there and it seems to work fairly well as Damien mentioned as well the not all packages will work with micro Python the biggest constraint I think is probably this amount of RAM you have available not much a lot of code is optimized very heavily these days to use as much RAM as possible as often as possible if you're sitting down to write something than you want it to go fast mostly what you do in a big CPU is you pull everything into ramp first right cache everything if you've got a file oh yeah and it's a Giga file upload it into RAM why not I might have to serve it up several times that'll be quicker in the long run right this thing's got 32 gig of ram on it it's fine this thing doesn't so you probably don't want to do that and a lot of libraries make this assumption I should cache things I should do that without ever kind of checking how much ram is actually available so that's a bit of a challenge but the the micro Python packages should hopefully make up for that by basically having the same code with a more memory conserving bin accepting that it'll go a little slower it's an 80 megahertz microcontroller but it will work the other way I didn't mention in that previous list was actually to to write your own modules and I was actually very pleased to say looking around in the micro Python source code there's a whole bunch of C files looking suspicious and called things like you know mod cease and mod machine and things like that that correspond to the things you can import and I was very pleased to say that they're actually fairly understandable C code and so that's worth considering if you've got a need on this if you've got a desire to run Python on this chip but a need to do stuff that has to run quick it's worth thinking about whether you can make the bit that has to run quick as a C library as a mod something in micro Python and of course it's worth considering whether you should be releasing that on micro pythons package management what doesn't it come with it doesn't come with an operating system as I mentioned before that means there's a lot of things you just don't have it doesn't have multi processing so if you're used to the idea that your code has to run in a very linear way and that you'll fork a thread or a process or whatever to handle your web requests you've got a bit of a nasty surprise in store but that being said it does do co-routines there is a you async so as long as you're willing to deal with the fact that your code will run asynchronously and in multiple threads that's okay there's not much memory to play with as I mentioned what I think the biggest thing it's missing at the moment is a way to remotely connect up PDB or equivalent as for remote debugger you can debug on it doing lots of print statements sorry print functions as a Python 3 print functions and lots of stack traces and so on and so forth and that's fine but it's not somewhere I enjoy going so I'd love to see and we'll hopefully get to contribute to a python debugger attachment port basically alright I've got a couple of minutes so I just wanted to mention a little hobbyhorse project this is what got me excited about esp8266 in the first place there's a educational programming purposes for them they're very very cheap they're very very simple they're a great platform for teaching programming on so it was playing around with this idea of how do you teach a programming and one of the things I thought about was getting rid of variables entirely because I found my kids had a lot of trouble getting their heads around variables and I started looking at data flow architectures which don't really have variables who just data flows from place to place I started looking at having a graphical programming language for that and so on I've ended up implementing this on the esp8266 and I implemented it the first time around in C and then I discovered that was far too much like hard work and I wasn't really doing this for hard work so I reimplemented in micro Python in a couple of days and what it does is it offers you're up first a web page with has a whole bunch of JavaScript in it and lets you manipulate these things on the screen and then what it does is it compiles that down to Python and one of the things that micro Python include is the compile function from Python so rather than run a virtual machine or anything like that which is what I had to do and see it just takes this program that you've written by connecting the bot boxes with lines turns it into Python compiles that Python and runs it in Python on a microcontroller and I just think that's fantastic that you can actually get away with doing that on such a tiny tiny platform you can get away with having that kind of meta programming approach and it just works the end result is that the code base is incredibly simple and my favorite thing about it is it means that if you kind of grow out of playing around with the toy graphical programming language there's a very very direct route to go from there to actually programming real Python you can take the thing you drew in this little sketch hey it's a Python program I can just run it directly instead so that's just an example of I guess what I've been working with on this platform all right in summary if you're interested in microcontrollers Internet of Things robotics anything in those general directions well esp8266 is are currently about the cheapest way you can possibly get into that and if you're interested in that stuff micro Python has got to be about the easiest way you can get into that stuff so I think that'd go fantastically together all right
Info
Channel: PyCon AU
Views: 15,939
Rating: undefined out of 5
Keywords: pyconau, pyconau2016, NickMoore
Id: C19fFU-TVWU
Channel Id: undefined
Length: 27min 38sec (1658 seconds)
Published: Mon Aug 15 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.