Introduction: ESP8266 for Beginners
Please vote for me in the IOT contest it would mean a lot!
The ESP8266 is an incredible little chip that interfaces with WiFi. And as far as so many people are concerned that is all it can do, but in this Instructable we will look at what is possible with this amazingly versatile chip that can be had for so little money!
Today I will be learning right along with you so if you have any other interesting tid bits to add to the conversation please add them in the comments. And as always please do not struggle on your own, post your issues and the community will attempt to help you though this journey into the invisible world of WiFi interactions.
I invite you to read the entire Instructable as there are several experiments that are done with different components that are not listed in the next section. This allows you to decide what you want to do with this amazing module.
I apologize for the unfocused pictures my camera cannot focus on the objects because they are too close. I will attempt to remedy this in later revisions.
Step 1: Procurement
This is the base equipment you will need to start working with the ESP8266 module.
There are so many ways to get this chip. There is a version with only six pins there's one with 10-ish and then there is one with 20 pins!
The latter version will be what we are after for development and playing with because this allows us to connect to all of the available inputs of the ESP8266.
The particular configuration that I find to be the best is sold by Adafruit, here, they call it a HUZZAH, (no they don't pay me to say that) although it can be had at many different retailers for a ridiculous price range.
I decided to get my product from mouser.com, another distributor of Adafruit products, here, because there were several other things that I wanted to test/play with.
Along with the HUZZAH you will need a special cable, USB to TTL serial, that allows us to connect to USB it can be found here and here for the windows module. There are also ESP8266 modules that have USB connectors on them, just another option available, and in that case you will not need the serial cable but rather the respective USB connector.
Step 2: Lua or Arduino?
There are several ways of getting your programming onto this module and depending on your application and skill level you can choose any of the multitude. We will be entertaining two code interpreters Lua and Arduino.
The first, Lua, "[...]is a powerful, efficient, lightweight, embedded scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description." (Lua: about) if you don't have a computer science degree that doesn't mean much. But all its saying is that its fast, the code is small (for storage on the device), and it feels like C++. This language is not difficult to learn and works really well; however, in the interest of not having to learn a completely new language for this device, there is another possibility.
Thanks to the Hacaverse full of people that know way more than I do, we can use the Arduino IDE and associated language to program the ESP8266. So, in the attempt to create ease of use we will be using this as our means of programming when it comes to this Instructable.
Step 3: Installing ESP8266 Package on Arduino IDE
Assuming you already have the IDE installed, this is a very simple process.
Go to the preferences section under the File tab of the editor and in the additional board manager URL put this:
click O.K. and go to the board manager. This is the top option in the Board: section of the Tools tab and search for ESP2866 and install the community version of the software.
Now when you are ready to load software onto the module you will connect to USB and select Adafruit HUZZA ESP8266 from the Board: menu in the Tools section of the Arduino editor. But we will go over that in more detail later.
Step 4: Putting Code on Your HUZZAH
If you have small fingers, I don't, this will be easy.
If you bought your TTL cable from Adafruit the picture above will suffice to get you up and running.
Now to get code on this thing we will need to press the GPIO0 button, on the right, and the red LED above it should light up. At the same time press the RESET button, on the left, and the red LED should become dim. You are now in bootload mode, which has no time limit on it, and from here you can load code onto the module!!
Step 5: Eperiments
This is a quick links page that will lead you to the various experiments/tutorials that are a part of this Instructable for your convenience.
Step 6: Blink LED
For this we are just going to flash the on board LED for simplicity sake but the same principle applies to doing this on any of the other pins of the module... just substitute the pin number being used(printed on the board). Also if you are using another led DON'T FORGET A RESISTOR OR YOU RISK LOOSING A PIN ON THE HUZZAH.
After putting the Huzzah in bootloader mode (go back 2 steps if you don't know how), open the .ino file, and hit the upload button on the IDE, wait for it...., now the red LED should be blinking.
This experiment does a couple of things: It makes sure the IDE is set up correctly, the TTL cable is correctly attached, and most importantly the HUZZAH is not DOA. these three things are crucial for the rest of the experiments so please post if its not working for you and we will work through it together.
Step 7: Control Shift Register
This, in my opinion is a piece of equipment way underutilized by hobbyist... the Shift Register.
Ill be using the SN74HC595N. This is a Texas Instruments Tri-State 8-bit shift register. the tri-state is very important. it means we can load data in while still displaying the "old" bits from the last transmission.
Attached is the data sheet for the register. This is really easy to understand so don't freak out. On page 4 is the pin-out of the chip this tells what each pin does: all the Q pins are the outputs in the logical order of A-H, the RCLK and SRCLK are the two different clock signals that allow data to be moved around, the OE enables the output or Q pins, and the SER pin is the value that will be put into the register when the clock is pulsed.
The next page to look at is p.13 at the function table. This shows us the effect of each pin on the data stored. Verrry usefull information to understand because this allows you to manipulate the chip for your bidding.
I don't think I want to deal with the SRCLR so I will attach that to the supply voltage to keep it in the high state, but the rest of the ones need to be "mapped" to pins of the HUZZAH module (please write these down). Now attach the Q outputs to LEDs (with resistors of course) and power the chip and lets get playin' with code. The above picture is of all my connections but is just a jumble of spagetti without being able to play with the wires yourself but you get the idea.
To write the code we will use the "mapping" that you wrote down earlier and the chart, on p.8, from the datasheet and away we go!!!
Step 8: Use I2C Device
I2C is a great protocol that allows device to device communications that are easy to establish and take minimal wiring for a lot of devices.
The Adafruit library includes some code that won't work with the ESP... Luckily Sparkfun has some code that we can play with the source code is here.
We need to install the library for the device; just type TSL2561 in the search bar and install the one from SparkFun, because that is where our code is derived from, and create some code... or copy the example code from the git repository. The code can be made to work with the module by adding one line to the example code: after light.begin() line we add Wire.begin([SDA], [SCL]) the two values will be replaced by the pins used on the module.
The example code connects the SDA to pin #2 and SCL to #0. This can be changed in the added line of code.
The LUX values will be printed to the serial output... that's on the top right of the IDE.
Step 9: Use Analog Device
So a single analog pin is all we get with the ESP8266 module... that is interesting, but in the age of I2C and SPI, don't use the latter myself but, perhaps we don't need the analog for as much as we think... but doesn't hurt to know how to use it in case.
The easiest circuit to illustrate this is a voltage divider circuit with a pot, I'll be using a 100K Ohm because that is what I have feel free to do the same, attached to V+ and GND on the outside pins and the middle to the Analog, marked A on the top left of the board, and do analog reads. easy enough.
The code is really simple and prints the values to the serial monitor.
The values are 0 to 1024 so the ADC is 10 bits and has a range of 0-1V, weird range but that's what it is, so you can do the math for the varistor if you want.
Step 10: Connect to Wireless Network
Awesome this thing that we know how to use with sensors and write code with can connect to the internet!!! Yes IOT for the win.
This can be connected to any network as far as you know the ssid and the password for it.
code for this is EXTREMELY easy and awesome since we are using the IDE to program. The example code has a value of ssid and password that you will have to replace with your information in order for it to work.
This code is from Adafruit and demonstrates how to connect to their API, even though we will not be using it later, and uses the wifi to connect to the server; it then tells you some information about the IP of the device and about the server it connects to and closes the connection and goes in a circle.
Step 11: Send Data to Thing Speak Pt. 1 (Installation)
So you want to turn this awesome chip into an IOT project. GREAT CHOICE!
This is made awesomely easy because all we have to do is include the ThingSpeak API which can be downloaded to use on the Arduino IDE platform.
The idea for installing this is exactly the same as the library for the ESP8266. This time you will go to the Include Library menu in the Sketch pull down. Choose the Manage Libraries option and type "thingspeak" in the search box. After installing go back to the Include Library menu and include the newly installed thingspeak library.
Step 12: Send Data to Thing Speak Pt. 2 (API)
So what is an API you may be wondering? This is just a group of functions that allow you to work with someone else's code without having to know the ins and outs of the entire project. But what is a function? You use functions all the time in order to do things in programming without having to type the repetitive code again and again; an example of this would be the delay() function that I am sure we have all used before.
This particular API has these different public functions:
begin(Client & client, IPAddress customIP, unsigned int port)
writeField(unsigned long channelNumber, unsigned int field, int value, const char * writeAPIKey)
setField(unsigned int field, int value)
writeFields(unsigned long channelNumber, const char * writeAPIKey)
writeRaw(unsigned long channelNumber, const char * postMessage, const char * writeAPIKey)
readStringField(unsigned long channelNumber, unsigned int field, const char * readAPIKey)
readFloatField(unsigned long channelNumber, unsigned int field, const char * readAPIKey)
readLongField(unsigned long channelNumber, unsigned int field, const char * readAPIKey)
readIntField(unsigned long channelNumber, unsigned int field, const char * readAPIKey)
readRaw(unsigned long channelNumber, String URLSuffix)
They are varied and there are different versions of these functions to take different types of arguments, C++ problems, but you don't need to worry about that.
A few of the functions I would like to highlight, mainly because we will be using them in our project:
- this will START your session with ThingSpeak and must be used in the setup section of the code
- this is where you STORE your data that will be written to the server
- this allows you to WRITE to the server with your established connection
Step 13: Send Data to Thing Speak Pt. 3 (Code)
On the previous step in the highlighted functions you will notice there are 3 bolded words START, STORE, WRITE. This is the formula we will follow in order to get our data onto thingspeak.
After you have created a Thingspeak account/channel with a couple of data fields, go to the API Keys tab on the channel page. This key is how the API will know which/whose channel to put the data in or read it from.
I have uploaded code for reading a couple of sensors/circuits just comment out for what you have, put your API key in for the variable, and general change the code for your particular purpose and watch it GO. I would invite you to try to write the code on your own first...just to see if you can get it because that's half the fun.
That's it!!! Isn't that exciting how relatively easy that is.
Step 14: ThingSpeak Thoughts
- Visualizations on thingspeak are awesome, there are examples already in your channel, they are written in MATLAB an extremely easy language to learn, especially after C++ and even Python, and they can add sooo much to your ability to understand your data and view possible trends.
- ThingSpeak does only let you post data every 15 seconds or so so if you need it faster then that perhaps open another channel so then you could post every 7.5 seconds or so.
- I have what I am calling a "Heart Beat" channel that all of my IOT projects post to at least once a day so that I know they are still operational and doing well; However that is not an excuse for not checking on projects especially in their infancy stages.
Step 15: Conclusion
Armed with the information that you now posses IOT applications are not as daunting as they perhaps once were. So go ahead begin that project that you had in mind be it gardening, home automation, roaming robot, back pack tracker, Cheer Lights, traffic counter, wild life tracker, and even twitter activated items.
I'd love to see what you come up with... please post pictures/comments about your project(s) that include the ESP8266 module.
Today is the day that you solve a mystery with Technology!