Using the ESP8266 to build the Internet of Things

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so there's been a great deal of hype over the last couple of years about the internet of things and despite that or more probably because of it there's a lot of confusion about what the Internet of Things actually is and what it means going forward right now our Computing is diffusing out into our environment the desktop is increasingly becoming intended for Niche users it isn't dead but like the main frame it's going to slowly fade behind the scenes back stage developers and designers people like you and me will need it we will continue to need it but despite the prevailing view in Silicon Valley the world is not made of people like us most people are perfectly happy with the desktop going away they never liked computers in the first place so for the rest of the world then well everyday objects are already becoming smarter and in 10 years time every piece of clothing you wear everything you carry with with you will be measuring calculating and weighing your life in 10 years time the world is going to be full of sensors and the distributed computing that powers them right now these cell sensors live in our cell phones all of which come Laden with accelerometers and gyroscopes and magnetometers some either even have um thermometers and pressure and humidity sensors but today's mobile devices the cell phones are transition technology artifacts of a stage of technological progress like the beige box that sat under our desk for 20 years these two shall pass and fade into its own Niche because what you really need to remember is this is not a phone this is just a computer that happens to be able to make phone calls so it's not just our cell phones anymore it's our things things like internate enabled light bulbs weather stations electricity monitors smart thermostats or even cars the Tesla Model S for instance is just a web server with a wheel in each corner and a really big UPS in the middle so there is a problem the commercial iot as we know it today and if you're in Corey's talk this morning his keynote you might have heard about some of those problems the commercial iot has a lot of problems and because of that because what a lot of people can't seem to understand that the internet of things done right isn't just taking an existing thing and putting an internet connection in the thing about two years ago now um I think it was Samsung introduced a smart washing machine others have followed this is uh whirl pool's version released at last year's CES in Las Vegas it both Wi-Fi a and a colored control screen it can be started from an iPhone app and we'll text you or email you when your clothes are done it costs $1,700 as does the smart dryer that goes with it and if you're wondering who want to buy one of these things you're not alone even whpo is not entirely sure they've been quoted as saying we're a little bit of a hammer looking for a nail right now and this is about their own product something they are trying to sell us that's because they haven't got the point they have a thing and they've connect Ed it to the internet and despite the name that's not what the Internet of Things is about the Internet of Things isn't anything to do with connecting things to the internet I no I really think the acronym does isn't the best thing in the world but we're stuck with it now this is not a smart washing machine a smart washing machine would have a single button to turn it on and off when you turned it on it would look at the for NFC tags and all of the clothes you've put in it and figure out what to do by itself and then when it was nearly done it would tell your heating system to turn your radiators on so that you could dry your washing on your radiators you have radiators in this country right yeah you know what I'm talking about you drive your R drive your washing into your radiator so you wouldn't have to buy the smart dryer to go with the smart washing machine only then would it send you a push notification to tell you to come and unload it because we haven't quite got robotics down to that that's s of level yet now that would be smart so despite the fact that having a lot of commercial iot things in my home that's what happens if you run an iot startup every once in a while a lot and like a lot of people right now the working pieces of smart technology in my home are things I've built or modified or hacked on myself because the real problem with the Internet of Things is that all of the things have to talk to one another or it's not a network work and if we're the things that connect the things that's really missing the point and Commercial iot isn't really very good at that right now now most of the internet things I've built make use of this thing an Arduino or a Raspberry Pi but until very recently they didn't come with wireless networking on board and it's not exactly a cheap way to build things especially when you want lots of things which is where the PP 8266 comes in which um Brian Jepson who used to work at make and now works at O'Reilly saids this is inexpensive enough to be very much in the territory of thousands of sensors launched out of a Canon cheap which is yeah really quite decent quote um so this is the ESP 8266 and I wish it had a better name but it doesn't so we're stuck with that too originally it was released as a serial to Wi-Fi Bridge the default firmware uses an at command set to allow you to configure and use it in other words it could provide Wi-Fi access to other microcontrollers like the Arduino but it cost $2 $2 it was so cheap people started to look into the proprietary Chinese written documentation to try and figure out what on Earth could this thing do and what they figured out was is actually an 80 mahz micro controller with full Wi-Fi support both as client and to access point it does 80211 BGN it does supports WPA and WPA 2 it's got a full TCP stack it's got DNS supports it's got gpio it's got i2c It's got i2s SPI fwm support and a 10 bit ADC this is actually a fairly awesome piece of Kit did I mention it was two bucks actually it's less than two bucks if you buy in reasonable quantities it can be had for something like a125 and modules bearing this chip are all over the Place made by various manufacturers this is the esp01 it was the first one to appear and is still probably the most commonly available you can pick this up for two bucks on AliExpress prefering for this I actually logged into AliExpress and checked the price in fact the cheapest I could find it for was $164 per piece with free chipping VI shipping via China post it takes 90 days to get to you but there's still free shipping I bought 100 I'm not sure what I'm going to do with them when I get them but in 90 days I'll probably figure it out the esp01 is fairly Bare Bones you only have very limited access the GP in the Chip And there's a bunch of but so but there are a bunch of other breakout form factors a confusingly large number in fact this is the esp2 module um which has been integrated on like carrier boards it's the adaf fruit huzar this does not cost $164 it cost $9.95 which is still pretty cheap um and it's a lot easier to work with than the spo1 which is something we'll find out in a few moments when we start actually playing with Hardware effectively Computing has become cheap enough that we can throw it away we can build sensors and and for a buck 64 I'm happy to throw them away at the end this is no longer we're treasuring our single Arduino board that we have in the house and we switch between projects we just build the projects silly it inside silicon glue or something and throw it away or keep using it there are nine usable GPO pins um normally pins 6 through 11 are connected to the flash memory in most cases um some modules break out more than others uh for instance the insanely cheap esp01 only really gives you access to gpio0 and gpi2 since gpio0 is the thing that toggles it into boot mode you can't really use GPI Z particularly well so you really only have one GPI open which which is sort of me but hey did I mention $164 anyway it also the the the o one also has some pins tied to high or low so you have to be quite careful about what code you put on it if it's the wrong code you can actually break the module and it's not very good anyway other modules like the Ada of fruit huzar which I showed in the last slide give you access to all the pins as well as being cheap it doesn't at least for a yide chipset use all that much power these are current drawers uh using 3.3 volt power um a temperature about 25 Cel based around a 90% duty cycle um these are probably the lines you want to be most concerned about is putting there about 140 milliamps I actually normally see about 85 milliamps in most operational circumstances but the thing you really should note there is the deep sleep mode 10 milliamps most of that power is power draw is actually from the led the power LED and you can just cut the trace to that and then it drops to pretty much nothing um and in case you're a bit afraid of putting things to sleep if you have a bad experiences with putting microc controllers to sleep in the past this is also designed to wake from deep sleep and transmit package packets in less than 2 milliseconds which is actually pretty cool that means with conservative use of sleep mode and a reasonable duty cycle you can continuously run the module using just solar power all day all night this is I mean that might not sound a huge achievement but this is a 200 milliamp lipo and A5 WT solar panel and that will suffice to run this particular breakout board which is the spark th spark fun thing I'm sure they had lots of fun coming up with that IE name um more or less continuously the availability of good sleep support in a microcontroller is pretty much vital for the if you're going to build distributed internet of things things and the SP 8266 has that so what can you actually do with it well this is a Wi-Fi hat for the Raspberry Pi 0 has anyone actually managed to get a Raspberry Pi Z like wow yeah they're hard to get aren't they um this actually makes use of the sdio support on the pi as because yes the the a266 has sdio support so basically this for four or five bucks you can build Wi-Fi for the pi zero um that doesn't actually block your USB ports which is actually pretty cool so if you actually need heavyduty CPU you can use this and the pi zero and you've got all the power of the pi out in the field despite not actually using this uh the full power of the the chip this is actually in the original at command set mode where it's just used as a serial to Wi-Fi bridge this uses an esperino and a light sensitive diode to measure electricity usage so basically you know you got the little flashing light in your electricity meter when you do a click this is actually just watching for those clicks and measuring your electricity usage which is pretty cool what happened there okay this is an interned inacted world clock with 256 full color LEDs which spell out the current time weather conditions and weather forecasts in words and numbers weather data is stay downloaded from the Weather Underground every 15 minutes and the display is comprised of 256 addressable WS 8212 node pixel LEDs in eight rows of 32 it's all run from an ESP 8266 there's also a little remote uh notification near the door they'll tell you if it's raining outside if you can't look out the window um a lot of iot right now is the why don't we just look out the window stage I'm sure that will get better um this is a DI Weather Station it houses A dht22 and A bmp280 basically it's a battery powered net atmo clone if you've come across the net atmo um it can pretty much operate for about a week for measurements an hour um and measurements are sent bya mqtt to Rasberry Pie House somewhere else in the house and that's actually a fairly common pattern we're starting to see with this a number of disposa cheap 8266 chips scattered around in the field streaming beta data back to Raspberry Pi safely hidden in someone's closet CL it amongst other things that takes the senses off your home network and keeps them on a separate nice Wi-Fi network that you don't have to worry about so adds a little bit more security there's also some insane things that can be done with the chip and most of them are done by Charles Lore this is the es s theet it's an 8 esp8266 i2s based software 10B T EET solution it does its own Manchester encoding decoding framing FCS and only uses two pins on the chip it was announced on April the 1st but the joke was it really wasn't a joke um uh I I bow down to this man it's an massively impressive thing to have done and it's totally useless for all possible actual practical purposes um this is another one of his projects um it's an ESP a26 analog broadcast television interface H an antenna a length of wire in other words up to the gpio 3 RX tune your analog ntsc TV to channel 3 power on the ESP and this is what happens for $2 you can turn your old analog TVs into remote consoles bear in mind this is not entirely legal um just like the just like the Raspberry Pi FM radio hack where he pretty much did attach a um piece of wire to Raspberry Pi um it's not legal um on the other hand it's got a range here about 100 foot so even if you have a ham radio Enthusiast living next door you're probably okay um it's not exactly the internet of things but I had to show it because it's insanely cool and shows up how much grunt the CPU actually had that's a 3D render done in real time on the chip while it serves a web interface to tell the next thing it's going to broadcast via its Wi-Fi that's how much J grunt you actually have to play with so let's get hands on with this thing and this is where this talk will now go absolutely AWOL because I'm about to try and do live coding on Wi-Fi Hardware at oon yay me okay so this is the footprint of the esp01 while it's physically compatible with the uh um a breadboard it's not electrically compatible in other words you plug this into the breadboard short circuits itself and it like the Magic Smoke comes out which is not good don't let the magic smoke out so this is what we actually have to do we connect it up to a um ftdi connector to the the laptop and then we have to pull GPI all low for firmware upload and then push GPI too high for firmware upload and then once the firmware is on the board we disconnect it unplug those wires we plug it in so what I'm going to do to start off with is um once we' got all wired up it's actually really easy to use the community tools remember most of the work done in this is done by the community wasn't done by the manufacturer it's really easy to up use the community tools to upload firmware and the first firmware we're going to look at is the the really simple at command firmware that came with the board to start with um that you will probably never ever use um but it's quite cool so it's a it's a good start so let's have a look at that okay so um if we mirror our display how's that people see that okay so what I'm going to do is I'm going to plug this in here and over here nope two Kim yay over here um so I got to Bear REM mind that's going to take a long time to switch over here we've got one of these uh ESP uh 01 modules and a whole bunch of wires which I wasn't going to actually bother doing like live on stage cuz hey I'm sure you've seen people plug wires in before um so what we'll just do is we will connect the RX to TX the TX to RX don't get them the wrong way around or you'll get very confused why things aren't working um a ground pin and a Power Pin and with a bit of luck yes the light should go on can you see that yes we can see that okay so okay so what we got here is we've got ESP tool. py it's a Python program that's been built to ship firmware up onto the board um it's writing via the serial Port uh the binary firmware onto the board so with a bit of luck it'll actually do that so I'm going to warn you that at some point during this presentation I will almost certainly have to reboot my laptop because one of the things is while the community tools are great they are evolved in very quickly which means they're not exactly all that stable um which means every so often they tend to lock up the serial ports and anyone using USX serial can tell you the only way to get rid of that is to reboot the laptop restart the kernel and everything goes away um there's like four different failure modes which I guess I'll talk about if we ever get to one of the failure modes um so yeah don't be too surprised if I suddenly go like start pulling my hair out reboot the laptop that's a good time for questions very good time for questions so what this is doing is just pushing the firmware up raising into the flash on the chip um with a bit of luck we should get a success message very soon leaving that's good right so then what I want to do is I want to um detach the power here then I want to pull the wires because this is one of the ways you can brick the serial ports so this is pulling the gpio wires apart so that um it doesn't no longer in boot mode and we will Boot It Up and we get a red light and at that point we can talk to um to the thing so just resan serial ports yeah I still have one so we're 115 uh 200 uh BPS if we connect we should get a bunch of junk is that big enough font for everyone yep good wa an error so if I hit a I should get an okay back harah so this is basically it in it it's it's fundamental mode it's um the mode where it is just serial to Wi-Fi and what we're going to do is I'm going to put it into um client mode and then I'm going to reset it we should get a bunch of junk in a second there we go it's ready to go and then I'm going to look for a Wi-Fi network this is where we find out there's no Wi-Fi in this room no yay we have Wi-Fi so these these are all the Wi-Fi um uh APS that can see you can see there's a lot of osone Wi-Fi um so what we're then going to do is AP uh join the access points we're going to join oscon presumably it just picks whichever one it's uh the highest strength and it doesn't have a password so we're good and with a bit of luck we should have an IP address so this is it's doing the DNS dance yay we have an IP address so it's managed to make it onto the network the the next stage I know I'm so happy so the next stage is U anyone that does live coding demos knows exactly how much stress how stressful this is um so we're just going to put it into asynchronous modes so we're going to basically attach a thread to um on the chip to this request and then we are going to start uh a thread to grab the Google homepage okay and we're linked so that means it's made it out out into the world and it's now connected to a server somewhere in the depths of Google um so now what we want to do is we're going to send some characters to Google and we want to get the home play homepage please and we want HTTP 1.1 and I give this a 50% chance of success yeah I didn't think this was going to work okay so effectively what it's doing you can see that you have to play with it it's like there's a lot of commands that have to go back and forth it's not the best thing in the world this is not how you really want to do Wi-Fi because you're going to have to program all of these commands into your microcontroller you're going to have to send all these text strings up to the serial port on your your Arduino or your Raspberry Pi or whatever and then you're going to have to do this negoti what happens when this would finally return which it probably is not going to do let's be let's be honest here it's not going to return anything but what it would finally do is you would get um return packets so basically you would get um chunks of HTML back and then you have to reassemble the web pages back from these chunks of HTML exactly how the browser actually does it um which is not a great fun thing to do on a microcontroller with limited um availability so I'm just going to to give up on this I didn't think it was going to work not really um and we will look at something else in a bit so I'm going to pull this out take that away and uh close that cuz we would need it again actually let's keep that open right uh so so while it's pretty useful and I've seen a lots of people use it for interesting things I really use the at firmware myself because these days the firmware pretty much there's a firmware for pretty much every language Under the Sun uh the First on to come along was uh L firmware is probably the the the most used right now uh but Lura python lisp JavaScript and it's even got fullblown uh Arduino support now which is what I mostly use it in um there's also a basic firmware for anyone that were the hankering for the 198 s so as I said probably amongst the the First Community firmware was uh node MCU it's a lure based firmware and the same people have an esp12 module you can see the thing that's the the the SMD module on top of the the carrier board there uh based board which is also called node MCU which is not confusing in the slightest obviously um this is actually a really good Breco I prefer the E of fruit huzar for various reasons um but nonetheless anyway so I'm an uino guy from way back um so right now I'm mostly using these boards as a cheaper old as the cheapest arduinos on the planet effectively and Arduino sport started out fairly complicated to get around it was like an entire custom build of the IDE you had to install um but now with uh uh 1.6 it's it's purely a module you just install from the board's manager that's it you download the Arduino IDE you pull up the boards manager you search for esp8266 you hit go you're done that's all you have to do um which is really pretty cool Arduino people in the audience yeah so if you can use Arduino you can use one of these um so I was pondering what I was going to do for the real demo as opposed to the demo the little tiny demo um and I was going to you know what I was going to do for the talk and then this happen this is the Amon dash button for The Internet of Things it sounds great in theory and seemingly quite affordable at 20 bucks until you realize that the batter is not rechargeable and only lasts a th000 pushies that's two cents a push for anyone that can't do basic math there's no counter to tell you how many clicks you have left and the heartbreaking thing the thing that will tear at every single open source person's heart in this room is that it's a AAA battery solded to the circuit board yay so today we're going to build our own and my button is going to light up because obviously you know it has to have more features than the close sour close Source competitor um this is the Adafruit huzar um and this is pretty much all you have to do to wire a button up to this thing and make it light up effectively you've got uh one pin to like register the buttons and you've got uh one pin to like the LED um there's you'll see two resistors there one is a a pull down resistor for the button and the other's a resistor in line for the LED uh if you're here for the last talk you you saw the presenter just plugging the LED into the pins of the Arduino yeah don't do that um because basically you you stand the chance of like putting too much current through the LD LED and it goes pop um some most LEDs you can get away with it the Arduino doesn't push too much current through its uh its pins this led on this particular button I'm using which is from spark fun which is a really nice it's a really nice cool button if you need something to light up um that really doesn't cope with excess current very well and blows up at the slightest provocation um so yeah it needs um resistors um at the other end of things if I can make this one contact the Internet it's slightly more this this demo is slightly more reliable um uh Google doesn't like random things coming from conference networks and sometimes I should have picked a different page never mind anyway to make things simple I'm going to hook it into iftt which is not open source but hey we can use any web web endpoint we want and I want to use this one which makes it fine because I want to do it you can use any HTTP web point you like which means you run your Raspberry Pi and you can push things via HTTP back to your Raspberry Pi um the end result can look like this or at least can be made to um this is the whole thing which is a battery a lipo uh management uh board um the Ada huzar the button the resistors can all be put fit in to a 9volt battery box and re which is repurposed with the a ho drill for the button cool thing about that is you can actually use the onoff switch that went with the battery to turn the like connection between the battery and your board on and off so that you can save some extra power um the one we're going to build today isn't going to look like that it's going to look like this because hey it's it's a little black box and that's far too and I didn't bring any drills with me so that's that's a thing um so let's get started the first thing we we like just take a brief look at the hardware two and here we go so let's just go a bit zoom in and as you can see it's pretty much exactly what I had up in the screen you've got an Adafruit hazar it's using um pins 12 and 14 um to connect to the button got your resistors and then you've got this which is the thing that uh whoops that's my hands not any wires you've got these wires which come out and talk back to the laptop um as you can see from that which is that which is the production version you don't need to connect to the laptop that we're just going to get some serial output and um so that we actually know it's working or in the very likely case so we know it's not working and why and therefore I can say at least claim it was doing something um yeah I don't know why I decided to demo Wi-Fi Hardware at oscon but there we go it was a good idea at the time um so let's mirror our displays and jump back into um the Arduino stuff so okay so this is uh some stub code I wrote Because oop this is not stub code that's still a picture of Hardware okay this this is some St code I wrote um because uh seeing someone write code to bounce a button up and down isn't really that interesting um you can see what I do here is I just uh start serial connection I've got a couple of uh pins here one for the button one for the light that lights up um then what we do here is we connect to the Wi-Fi so uh it has its own um esp8266 Wi-Fi Library which operates very much like the arduino's own um so we Wi-Fi begin we wait for it to actually connect to the Wi-Fi when we're connected we grab an IP address we grab an err SSI and we print that up down to the serial Port um simple as that um you can see here one of the things that trips up a lot of people is oh no it's not we do that okay that's fine um so then in the loop what I do is I just read the the current state of the button I also as well as doing the the debouncing via the resistor where I'm doing a software debouncing so basically we just Loop through here look at the button State look at the last button State see if it's changed recently uh for those that don't know what you actually get in buttons you get a lot of flicker so you want to make sure that when people press it down you're going to see a constant value uh from the button rather than like just a little bit of flicker on the thing so this is getting rid of noise so effectively it means that you're not accidentally triggering the um the bottom push every 5 Seconds um so uh let's do a little bit of code then um so what we also need is uh no go away we need we're going to connect it to iftt and for everyone worried really yeah okay so if we go to this I'm just going to show you the recipe no go away um so basically this makes use of the ift's maker Channel if you haven't come across that effectively it's something you can send a random HTTP request to if and they will do something with it so it's if they get a random HTTP request we do something with it in this case I'm actually going to send a notification to my phone um so if it receives the web request called button pressed it will send a push notification to my iPhone that says hey event name button pressed occurred on the maker Channel um you can do a whole bunch of other things you can turn on lights you can you know play your thing uh play music whatever doesn't matter uh if anyone worried about my API K here this is not actually my main account and will be deleted after this presentation so that's not a problem okay so we've got this so what we need to do is we need to um Define which host we want to connect to so we just grab consar host equals um maker. if.com and then we have constar star key equals this long string of random letters here there we go and so we go in here and what we're going to do is we're going to grab a Wi-Fi client and then we want to string which is the URL which is uh trigger uh button pressed with key and then we just URL uh plus equals the key itself URL uh plus equals um uh question mark value you can pass three values um back and one of the things I'm doing is I'm going to C going to pass the um the number of count number of times we push the button um for no particular reason just so to show that how to do it um so then if we client do connect to our host on Port 80 uh then we're going to serial serial print Lin uh we're going to print out I'm going to do a lot of Serial uh prints here just so when it doesn't work um you'll at least see where it didn't work um so that's HTTP slash serial.print Lin host everyone okay with the font size Okay so so uh if uh client. connect uh host 80 uh serial. print re wow I've write write pass Rasberry Pi too many times I automatically instead of started requesting I started writing Raspberry Pi uh requesting URL and so then we go to our clients and we're going to print list uh get uh so string uh get HTTP BL spash plus host plus URL plus string uh HTTP uh 1.1 if anyone sees typos please tell me don't put it after I laugh when it doesn't work um qu and quotes thank you that I've made that that made that one several times I don't know why okay so then we have a client. print uh Lin string host I mean you can see exactly what I'm doing here right which is we're building a um building an HTTP request by hand and obviously normally you'd wrap this away in some Library but here we're just going to do it uh by hand and then we're going to just delay for a second or so and then else serial. print uh print L didn't work and then we're going to return from that so uh then what we need to do is uh while uh clients available string uh line equals clients uh read whoops read read string until we're looking for uh end a line here and we're going to print out the response and then uh serial do print Len and we're done okay so hopefully this compiles wow okay um and what I'll do is I'll just hook up this we got a couple of minutes here you're going to say no aren't you okay okay we're almost there all right um so uh what I need to do is um I pull that uh I'm going to pull uh I'm going to put this in bootloader mode which involves holding down this little button and we get a red light on that and then hopefully what we do is we upload it yes it's uploading okay so effectively what's going to happen is that it'll see the button push I hit the button it'll make an HTTP request off to if and with a bit of luck my phone will then get a push notification um we'll see if not we'll look at the serial terminal and we'll see a whole bunch of things going wrong and we'll see how that works okay so what now I have to do is I can just open the serial monitor I can hit uh reset okay it got a um got a thing and if we push this okay and it did actually go so we'll just do that again here where you can see it you can see that the button pressed event went but if I push here light goes on things happen and there's the button push [Applause] event and that is the how it should work um and there we go so if you're interested in more learning more things with a bit of luck this should come up there we go and here are some URLs that you might want so get your phones out take the pictures uh pretty much this has all of the information that you need to do stuff with this I'm actually writing a book about this stuff for Riley right now but you know it's late it's not going to actually make it was supposed to make it for oom but it didn't um but you can find out what you need there thank you [Applause]
Info
Channel: Alasdair Allan
Views: 33,731
Rating: undefined out of 5
Keywords: ESP8266, OSCON, O'Reilly Media, Microcontroller, IoT, Internet of Things
Id: CjeDkmm0w_w
Channel Id: undefined
Length: 42min 6sec (2526 seconds)
Published: Wed Jun 08 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.