Arduino Wifi - ESP8266: Schematic and Getting Started Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is the esp8266 it is a little Wi-Fi modu you can pick up and they're around $3 at the moment well I guess that's £180 something like that uh you can pick them up from eBay they're pretty easy to get hold of uh and they're very very popular at the moment uh they communicate over serial so if you're using a microcontroller you can send commands to it and receive information over cereal I'm using software cereal with mine but you can use Hardware cereal as well depending on your firmware on the uh on the board then you'll uh have to set a different board rate for each one but uh mine's currently set to 9600 so this is the setup I've currently got so they need to be powered by 3.3 volts um the pins on them I've heard are 5vt tolerant so I'm not doing any level shifting on the uh the RX and the TX line um but I am powering it from a little 3.3 volt regulator coming straight from my 5 volt pin on the Arduino now the 3 vol 3v3 3 vol3 3.3 volt pin on the Arduino can't really supply enough current but the 5vt PIN can so that's why I'm using that with a regulator I'm also using a small OLED display so that I can see some of the sort of debug information that I'm looking at so let's turn it on and we'll see what it looks like so we've got it started up here you'll notice that it says Loops zero at the at the start there uh and then it says failed so whatever it's doing in the first setup routine has failed and now we've gone to reset so there are certain functions in here which means that it'll go through certain Loops so next it will be asking it to change the Wi-Fi mode uh and it says wi is three so that function is then complete uh it'll then try and connect to a network so there we go it's connected to my home network and it's already got the the time and date that's appeared at the bottom there so it has worked out uh I'll show you the code for it now it's kind of simple I had a lot of problems with this to start with it was failing around uh 70% of the time that it tried to make a request from the server so I've improved that slightly by meaning that it doesn't go through the whole Loop each time that it wants to make a request it'll only go through a few functions so it will reset the board every time and then it will check to see whether it's connected to the the network if it's connected it will display the network there so Wi-Fi is and then it shows you the network and then if that's successful it will then go on to the next part and request the web page if it gets the appropriate response it will then send the information to the website and then collect the information back if it doesn't go through those certain processes it will start again so it will restart from the beginning which means that you don't waste time doing performing actions that you don't need to do so this is the code for the esp8266 I'm using a display with mine so that's why I've included the uh Library here you don't need to do that if you don't have a display to use um but you do need to include software serial um so we've got our objects here we've got the display and we've also got software serial starting here on pins 10 and 11 we've got some Global variables one of which is network and one's password now this is your router identifier and we've also got pass password which is the password Fielder we've got a Boolean which I'm currently not using and we've got a Wi-Fi status variable here now that one's really important it's integral to the how this code Works um we've got a few temporary variables that I'm using to keep track of how many Loops there are and also what the debug messages are in the setup we've got our two serial lines so we've got the one going to the computer and we've got the one going to the esp8266 we've got a large delay at the start of our code here and that's to let the the as866 sort of kick out its uh starting message across serial and we want to sort of ignore that so we'll give it some time to get started in our Loop we've got this section here which is for the display so you can ignore that and then we've got one function to run everything one function to rule them all I guess it's the Run esp8266 function now this just takes the website that we want to contact and the page that we want to get from that website now we already have the information up here for the network and password so that comes into play later on so here's our display function we can ignore and then we have our esp8266 run function that takes in the variables for the website and the page now inside this is a switch statement and statement and this controls sort of the flow of the program so we have uh our Wi-Fi status uh code here code our Wi-Fi status variable here uh and that can be 1 2 3 4 5 6 or zero if it's zero it will reset the module uh just a software reset it might be better to have a hardware reset really if it's if the number is one it will change the Wi-Fi mode um it also checks whether the Wi-Fi mode is currently three so whether it's uh uh able to connect out to the internet and also be connected to so that's Wi-Fi 3 it gives it both different modes um if it is two if the Wi-Fi status is two then it will check for the Wi-Fi status connection so see if it's already connected to the router because this could be going through loops and loops and Loops it I don't want it to try and connect to the router every time because as long as it's connected once it's fine so if it is connected it will jump to number four here and it will go and get the page if it isn't it will connect to Wi-Fi uh once it's got the page it will then unlink l so I'll show you the a couple of the functions and then you'll be able to get an idea of how this is working so the ESP 826 reset function it sends a command across the serial line so it sends at plus rst across our software serial it has a a delay of 5 Seconds there now this is important because the the module itself is running its own code and it's also doing handshake stuff with my router um to do the TCP stuff um so it's important just to give it a bit of time occasionally um so once the serial buff has been filled up with its information we can look for the okay message if it finds okay then we know that it's reset and then we can change the Wi-Fi status to one meaning that it goes onto the next function in the uh the switch statement and that's change Wi-Fi mode again there's various bits here we're just checking what the mode is um and then kicking out a different Wi-Fi status depending on whether it's been successful or not so if it is successful we move on we move to checking on the Wi-Fi network status to see if it's already connected to the Wi-Fi if it is then we go ahead to get the page if it isn't we'll look for connecting to the Wi-Fi so we've got this connect to Wi-Fi function if it's able to connect we move on to getting the page if it's not then we'll start the process over again so getting the page um is a bit of a complicated one really we've got this at plus CIP start now essentially what we need to do is we need to create a connection um on Port 80 to the web page that we want to connect to and then we once that connections established we can then send it command to request a certain page so we do that there and then once that's happened we'll allow the buffer to fill up again and then we'll look for the um return that we expect now my PHP page that I'm using kicks out a star character and that shows me the start of the information that I want to return to the module um so I'll look for that star in the in the return and then we'll split it up based on some other unique characters that I've used ones that I don't normally expect to be in the returns so we've got an at symbol there and a straight line symbol I don't know what that one's called actually um but then it will use a function at the bottom called split to Val and it will use those as delimiters in the end character based on the input string um and so that's how I'm getting information out I'm setting up my variables then after that we go down to link page so if it was successful it goes to Wi-Fi status 5 and then we unlink from that page and then the process starts over again so hopefully this has been a useful piece of code for you it's in the description of the video if you find that you should make any amendments to it please let me know because I'd love to know what you're improving um if you find any mistakes definitely let me know I'd really appreciate that but I found it useful to have all these separate functions so I understand what's going on with the esp8266 it's hard to know what's going on when you're using a library um it's hard to the sort of nuances of what you're doing things take longer if you're requesting larger pages so you might want to play around with the certain delays in this code so it's a good place to start it just means that you're able to learn how this module Works especially over serial so let me know what your experiences are with esp8266 and if this code was any use at all
Info
Channel: David Watts
Views: 245,328
Rating: undefined out of 5
Keywords: Arduino, ESP8266, Getting Started
Id: ayF4Oymf08k
Channel Id: undefined
Length: 9min 30sec (570 seconds)
Published: Wed Apr 15 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.