This post has been written to aid in the teaching of an Arduino Course at Middlesex University.
The ESP8266 is a cheap chinese wifi module.
It has recently become very popular with the Hobbiest community, mainly because of its price, but also because of its relative ease of use.
In this example, we are using the module to publish data to ThingSpeak
The ESP8266 can be programmed to run C-Code, In essence, it can be made to behave in a similar manor to an arduino, without any external components.
In this example, we are using the ESP8266 with the default ESPRESSIF firmware, which responds to simple commands, for example:
A more complecated command for joining an access point looks like this:
By using the serial port, we can send these commands to the ESP8266 from the arduino.
The ESP8266 is a 3.3v Device, however due to its fairly large current draw, it cannot be powered of the arduinos 3.3v supply.
We therefore need an external supply.
We SHOULD also need level shifters, but They are not 100% nessesary, and I have never used them and as of yet have not ran into problems!
The following schematic should work
You may also find the following diagram helpful:
You will also need to add any sensors you are interested in publishing.
Working, But poor quality code
First I want you to look through the code below.
This code was quick and rough, it is badly structured, and exists within the setup routine.
There is blocking in the espdo function, and many redundancies,
Much better quality code
Now lets look at the final example.
Here I have simplified the code, removed the blocking espdo() function and replaced it with a much nicer esp9266_wait() function
The Connection and data functions are now split, and comments have been added.
Feel free to expand the code below to work on your own projects.