This is only a preview of the December 2014 issue of Silicon Chip. You can view 37 of the 112 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "A WiFi Server For $5":
Items relevant to "High-Energy Multi-Spark CDI For Performance Cars":
Items relevant to "A TDR Dongle For Cable Fault-Finding":
Items relevant to "Currawong 2 x 10W Stereo Valve Amplifier, Pt.2":
Purchase a printed copy of this issue for $10.00. |
The $5
WiFi Server
By Geoff Graham
This is something that you will find hard to believe . . . a WiFi
module which includes the 2.4GHz transmitter and receiver, the
aerial, a full TCP/IP protocol stack, a web server and everything
else that you need to add wireless Internet connectivity to your
next Micromite or microcontroller project. And it costs just $5!
L
OW-COST WiFi modules based
on the ESP8266 chip are now
available from the Chinese company
Espressif Systems Inc. These modules
can be purchased from many suppliers
on eBay and other sites on the Internet for prices ranging from $4 to $6;
and that often includes free postage
or freight.
The ESP8266 module connects
to your microcontroller via a serial
link and can be used to serve up web
pages, send data to Internet services
like Twitter, send emails and so on. So
for a very small outlay, you can add a
sophisticated level of communications
to your next project.
The ESP8266 itself is an impressive little chip. It measures just 5 x
5mm and contains a complete WiFi
transceiver including the transmitter,
receiver, integrated TR switch, balun,
low-noise receiver, power amplifier
and matching antenna network. It
meets the requirements of the 802.11
b/g/n standards, including full encryption, so it will easily integrate with
your current network.
The ESP8266 needs very few external components. The reference
design requires just three capacitors,
one resistor, a flash memory chip and
a 24MHz crystal. Many suppliers in
China have fully-built modules using
this design and all you need to do is
connect the module to your microcontroller and supply power.
In addition to the WiFi transceiver,
the chip also houses a 32-bit processor
which is used to run the TCP/IP protocol stack. This includes high level
Internet protocols such a web (HTTP)
server, P2P Wi-Fi Direct, infrastructure
base station mode and softAP mode.
The ESP8266 is intended to capitalise on the Internet-of-Things (IoT)
concept which posits that everything,
from the smallest gadget to the largest
appliance, will in future be connected
to the Internet in some way.
For the ordinary experimenter it
opens up a world where easy connectivity to the Internet is well within
reach.
Micromite in the garden
Fig.1: you should receive something like this if you connect to our “Micromite
Garden Webserver” using the URL: http://garden.geoffg.net It gives you the
time according to the server’s internal clock and the current temperature
and humidity. It is intended as a demonstration of what you can do using the
Micromite and an ESP8266 module.
30 Silicon Chip
As a demonstration of what you can
do with this module we have placed
a Micromite and an ESP8266 module
in a garden. This monitors the current temperature and humidity in the
garden and serves this data as a web
page via WiFi to an access point and
then via a router to the Internet.
It is only a simple example but if
you would like to see the output of
a combined Micromite and ESP8266
siliconchip.com.au
Fig.2: these are the connections to the most
common version of the ESP8266 module
which has an on-board antenna. Note that
not all modules have the Power Down and
Reset pins connected (this mostly applies
to older versions of the module).
This is our “Micromite Webserver In The Garden”. It may not look pretty
but it was only built as a proof of concept. It includes a 28-pin Micromite,
ESP8266 module, a real time clock, DHT22 temperature/humidity sensor and
a USB-Serial bridge. Note that this location is sheltered from the rain.
web server, you can connect to our
demonstration server by entering the
following URL into the address bar
of your browser: http://garden.geoffg.
net You should receive a web page as
shown in Fig.1.
The program running on the Micromite uses a combined temperature/
humidity sensor along with a real-time
clock and is a good demonstration of
how to implement a web server using
the ESP8266 module. The full MMBasic program is available for download
from the SILICON CHIP website and if
you intend to implement a web server
it would make an excellent starting
point.
The Micromite was featured in the
May 2014 issue of SILICON CHIP. In
case you missed that issue, the Micromite is a single 28-pin or 44-pin
microcontroller that runs a powerful
BASIC interpreter. You can control
up to 33 I/O pins and communicate
with other chips using I2C, serial,
SPI and other protocols. In this case,
communication with the ESP8266 is
via standard serial.
Module variations
These modules are tiny but they
pack a punch. They include the
WiFi transmitter and receiver,
antenna switching and matching
network, WiFi 802.11 a/b/n
compatibility, a TCP/IP protocol
stack and a web server. When
connected to a microcontroller you
can serve web pages, send emails
and log data onto “the cloud”. The
larger unit has the antenna on the
PCB while the smaller unit uses an
external antenna. Note: modules
shown larger than life size.
siliconchip.com.au
There are a many versions of the
ESP8266 module in circulation. The
most common is illustrated in Fig.2. It
has eight connections which include
power, serial transmit/receive, a Power
Down pin, a Reset pin and two general purpose input/output pins. This
module includes its own aerial etched
onto the PCB and this feature makes it
quite easy to work with.
The only issue with this module is
that the arrangement of the connecting
pins makes it impossible to plug it into
a standard solderless breadboard. So
you will need to solder wires to the
pins or use jumper leads to connect
to a breadboard.
Another common version is illustrated in Fig.3. This has just four
connecting pins (two for the power
and two for the serial interface). Fortunately, these pins have a 0.1-inch
spacing which is breadboard friendly.
Fig.3: this diagram shows the connections
to the version of the ESP8266 module
without an inbuilt antenna. The antenna
connection is via a miniature U.FL
connector and these are common in WiFi
access points and routers that employ an
externally mounted antenna (you may be
able to salvage these parts from an old
WiFi router or modem).
The module is intended for use with an
external aerial and there is a miniature
U.FL connector on the board for this
purpose (also referred to as an IPEX,
IPAX, IPX, MHF, UMCC or AMC type
connector).
Suitable aerials can be purchased on
eBay and the U.FL connector is common in WiFi access points and routers
that employ externally mounted aerials – so you may be able to salvage a
complete aerial and connector from a
discarded access point. As you would
expect, with a decent antenna, this
module has a far greater range than the
version with a PCB antenna.
There are other variations of the
ESP8266 module with different connecting pads but the above two are the
most common
The good & the bad
The ESP8266 modules on offer are
quite powerful but they do come with
some issues (or perhaps we should say
“challenges”). The ESP8266 chip itself
is mostly documented in Chinese.
December 2014 31
Fig.4: this is how you could connect an ESP8266 module to the COM1
serial port on a 28-pin Micromite. We have shown the Power Down
and Reset pins on the module connected to logic high but if your
module uses these pins, you could connect them to the Micromite so
that it would have more control over the module.
There are some (translated) English
data sheets but in general they are brief
and do not tell the full story.
The firmware (TCP/IP, server, etc)
that runs on the 32-bit processor integrated on the chip is what most users
will interact with and unfortunately
this is also poorly documented.
As a result you will need to be adept
at trawling the Internet and discovering facts and techniques that others
have painfully learnt. Hopefully, what
we have to tell you in the following
pages will also make that task somewhat easier.
You should also refer to the side
panels. One lists some of the more
relevant commands recognised by the
module and the other lists relevant
ESP8266 websites where you can find
much more information.
Connecting up
The ESP8266 requires a well-regulated 3.3V supply and it can draw quite
a lot of current, especially in transmit
mode. Normally, the current requirement varies from 10-70mA when the
module is idling or just receiving but
it can jump to over 200mA during
transmit. So a solid 3.3V 300mA supply is a must.
The module has a serial interface
running at 115,200 baud with the
standard settings (eight bits of data,
one stop bit and no parity). This is a
TTL level signal (idle is voltage high)
and it uses 3.3V signalling levels.
These perfectly match the Micromite’s
COM1 serial port so the two make a
great pair.
Do not try to connect the ESP8266
module to a microcontroller running at
5V or to an RS232 interface without a
level translator. The voltages on these
interfaces will exceed 3.3V and could
easily destroy your module.
Controlling the ESP8266
To control the operation of the
ESP8266 module, the microcontroller
will need to send commands over the
serial link and will receive acknowledgements and data in return.
Communicating is straightforward;
all commands sent from the microcontroller begin with the letters “AT”
and the module replies with “OK” or
“ERROR” or the data that you have
requested (rather like the Hayes modems in the past).
The Tx (transmit) pin on the module
should connect to the Rx (receive) pin
on the microcontroller and Rx on the
module should connect to Tx on the
microcontroller. The Reset and Power
Down pins (if your module has them)
should be connected to the 3.3V supply. The other (general purpose) pins
can be left unconnected.
Fig.4 illustrates a typical connection
for the 28-pin Micromite. Note that
older versions of the module leave
the Power Down or Reset pins uncon-
nected or use them for general purpose
I/O – so you may need to experiment
with these two connections (see the
accompanying panel for a website that
identifies the differences).
If your module does have a Power
Down pin, it is important that this
pin is pulled high by connecting it to
the power (3.3V) pin. If it is left floating, the module will power down by
default.
For the purpose of experimentation
you can connect the module via a USBto-Serial bridge and type in commands
directly from your computer.
First steps
In the following discussion, we will
avoid listing a lot of programming
code. Not only is it boring but it is also
much easier to download a program
than to type it in. The main program
that you will want to refer to is our
example of a web server running on
the Micromite (in the garden!). This
can be downloaded from the SILICON
CHIP website.
In order to get started with the
ESP8266, you need to type in some
commands and observe the result. If
you are using the Micromite, the easiest way to do this is to use the small
program shown in Fig.5. This will turn
your Micromite into a simple terminal
where anything that you type into the
Micromite will be sent to the ESP8266
module and anything it sends back
will be sent out from the console to
your screen.
When you first apply power to the
ESP8266 module, the current should
jump to about 67mA while the module
sniffs the air and then drop down to
10-30mA. Run the terminal program
on your Micromite (or connect in some
other way) and try typing in:
AT
When you hit Enter, the module
should return with OK. This is a big
step and once you are communicating
you can get on with testing how the
module will respond to your commands.
By the way, all commands should be
in upper case, should not contain any
Issues Getting Dog-Eared?
Keep your copies safe with these handy binders
REAL
VALUE
AT
$14.95
PLUS P
&
P
Order now from www.siliconchip.com.au/Shop/4 or call (02) 9939 3295 and quote your
credit card number or mail the order form in this issue. *See website for overseas prices.
32 Silicon Chip
siliconchip.com.au
spaces and are all terminated by the
Enter key. If you enter an invalid command the module will respond with
ERROR, otherwise you should see OK.
The next stage is to tell the module
to act as both a client and access point.
The command is:
AT+CWMODE=3
The module should reply with a
simple OK.
Then you need to reset the module.
Why you need to do this is uncertain
and not documented but we have
found that it is a necessary step.
You reset the module with the command:
AT+RST
You should get an OK followed by
a dump of data half a second later.
You can ignore this data as it just lists
details of the firmware running on the
module.
At this point you can do some useful things. For example, try listing the
WiFi access points in your area. The
command is AT+CWLAP and in return
you will get something like this:
+CWLAP:(0,””,0)
+CWLAP:(3,”BigPond9B8A1D”,-86)
+CWLAP:(2,”wifhubs”,-85)
+CWLAP:(4,”wifhubs_2GEXT”,-77)
+CWLAP:(4,”dlink-281D”,-91)
+CWLAP:(4,”Baas DSL-2890AL”,-90)
In this case the module has sniffed
out the WiFi access points in adjacent
offices as well as the one we want to
connect to.
The first digit after the opening
bracket is the security protocol that
the access point is using (0 = open,
1 = WEP, etc). The second field is the
network name (or SSID) of the access
point and the third field is the signal
strength.
Common Commands Recognised By The ESP8266 Module
AT
Checks that the module is alive. The response should be OK.
AT+RST
Reset the module. It will first respond with OK followed by a dump of information
related to the firmware.
AT+GMR
Returns the version number of the firmware.
AT+CWMODE=n
Sets the mode of operation according to the number n. Where 1 is a WiFi client,
2 is an access point and 3 is both client and access point. Note that as an access
point the module has limited functionality (no DHCP, etc).
AT+CWLAP
Will list the currently available access points in the location.
AT+CWJAP=”SSID”,”passwd”
Will join a network where SSID is the access point name and passwd is the
password. Both must be surrounded by quotes.
AT+CWQAP
Will close the current connection to an access point.
AT+CIFSR
Will return the IP address assigned to the module.
AT+CIPMUX=n
Will configure the module for multiple connections according to the number n.
Where 0 is single connection mode and 1 is multiple connections mode.
AT+CIPSERVER=n,nn
Will start a server. If n is 0 if the server is to be disabled or 1 if it is to be enabled.
nn is the port number. WEB browsers default to port 80. When a connection is
established the module will send “Link” followed by the data offered by the remote
browser.
AT+CIPSEND=n,nn
Will send data to a remote client that has connected to the server. n is the id number of the transport connection (normally zero but it can be some other number if
two clients have connected simultaneously). nn is the length of the data to send
including any terminating carriage return and line feed characters. The module
will prompt with “>” then the microcontroller should send the data followed by
carriage return and line feed.
Connecting to your network
AT+CIPCLOSE=n
Close the connection n (normally this is zero). This is should used after all the
data has been sent to the client.
AT+CWJAP=”SSID”,”passwd”
Note: most commands acknowledge with OK or ERROR. There are many more
commands that the module will recognise. For a full list, consult the links in the
side box.
To connect to your WiFi access
point, use the following command:
where “SSID” is the broadcast name of
your WiFi access point and “passwd”
is the password for access. Note that
there should not be any spaces and the
quotes should be retained as shown.
If the connection is made, the module
will return with OK. If something has
gone wrong, you will receive the message ERROR.
You could experience some difficulty
here as the access point might reject the
connection. Possible reasons include
incorrect case in the SSID or password,
the security set-up of the access point
siliconchip.com.au
(ie, if only certain MAC addresses are
permitted access), the inability of the
module to get an IP address from the
router because the router is not running
DHCP or simply the module being too
far from the access point.
Once you have connected to your
network you can try the command:
AT+CIFSR
This will return with the IP address
that your access point assigned to the
module, eg, 192.168.0.109. Note that
this command is one of only a few that
do not return with OK.
To verify that the module is “on the
air”, you can ping it from your PC. In
the case of a Windows computer, you
can call up a command window and
and enter the ping command and the
IP address. As an example, enter the
following at the command prompt:
ping 192.168.0.109
You should see the ping packets as
they are sent to the module and the
December 2014 33
Reference Links For The ESP8266
Home page of Espressif Systems Inc, the designers of the ESP8266:
https://espressif.com/
An English version of the data sheet for the ESP8266 chip:
https://nurdspace.nl/File:ESP8266_Specifications_English.pdf
Description of the module and the commands that it will accept:
http://www.electrodragon.com/w/Wi07c and https://nurdspace.nl/ESP8266
How to tell if your module has a Power Down and a Reset pin:
http://www.xess.com/blog/esp8266-is-alive/
How to update the firmware:
http://blog.electrodragon.com/cloud-updating-your-wi07c-esp8266-now/
A community forum focused on the ESP8266:
http://www.esp8266.com
Search eBay for sellers of modules based on the ESP8266:
http://www.ebay.com/sch/i.html?&_nkw=ESP8266
response times as the module returns
the pings.
The module will remember all the
previous set-up steps, even when the
power is cycled. So, when you next
apply power, it will automatically
connect to your WiFi access point and
get an IP address.
Most access points/routers will try
to assign the same address each time
your module connects but this is not
guaranteed. To ensure that the address
of the module doesn’t change, you
should go into your router set-up and
assign a fixed IP address to the module
based on its MAC address.
You need to do this because the
module does not have a network name,
only an IP address and by making sure
that this address does not change you
will always be able to find it on your
network by using the IP address.
Starting the web server
The first stage of setting up a web
server is to tell the module to accept
multichannel connections. The command is:
AT+CIPMUX=1
The module should reply with a
simple OK.
You need to then tell the module
to act as a server for port 80, which is
the default port used by web browsers.
This is done with the AT+CIPSERVER
command as follows:
AT+CIPSERVER=1,80
The module will respond with OK
then listen for incoming connections
on that port number. When a remote
computer does make a connection, the
module will send a line containing
34 Silicon Chip
the single word “Link” to the microcontroller.
Following this message, the module
will send a series of other lines representing the data transmitted by the
remote computer when it connected.
This often includes information
identifying the operating system and
browser but the important part is a
line with the word “GET” followed by
a path. The path is the web page that
the browser is requesting. A simple “/”
means that the browser wants the main
WEB page while (for example) “/data.
html” means that the browser wants
the page called “data.html”.
If you only want to offer the one
page, you can ignore the details and
just send your data back. This is done
using the AT+CIPSEND command, as
follows:
AT+CIPSEND=n,nn
where n is the channel number and
nn is the length of the line of text that
will be sent (including the terminating
carriage return and line feed characters). The module will respond with
“>” which is the prompt to send the
actual line of text. When that line has
been transmitted, the module will
return with: SENT OK
The channel number (n) mentioned
above is normally zero but the module
can handle up to four simultaneous
requests and if that happens you will
have to note which request you are
responding to and use the correct
number for n.
This is a typical send operation
AT+CIPSEND=0,38
> <TITLE>Micromite WiFi Server</TITLE>
SEND OK
You may send as many lines as you
wish by using multiple AT+CIPSEND
commands. Each line should be formatted according to the html standard
so that they will display correctly in
the browser.
When you have finished sending
the web page, you should close the
connection with the command:
AT+CIPCLOSE=n
where n is the channel number.
Rather than go into excessive detail,
it will make more sense if you to download the example server code for the
“Micromite in the Garden” from the
SILICON CHIP website. Using the above
discussion as a guide, you can see how
a real web server can be implemented.
Restarting the module
One annoying issue that we did run
into is that the ESP8266 can get confused for no particular reason and stop
communicating. To overcome this, we
used a TI TPS2042B power switch to
control power to the module. If the
module did not respond, the program
would turn off power to the ESP8266
for a short time to clear the fault condition. The MMBasic server program
would then initialise the module.
Note that if you use this technique,
you should also place a 33kΩ resistor
in series with the signal going to the
module’s Rx pin. This is because the
microcontroller will still be driving
its Tx line at 3.3V when the module is
powered down and the resultant current into the module’s Rx input will
destroy it. The resistor will limit this
current to a safe value (100µA).
Alternatively, if you have a module with a Power Down pin, you can
achieve the same result by simply
connecting this pin to an output pin
on the microcontroller and toggling
it low for 200ms when you need to
restart the module.
It’s possible that a more recent version of the firmware for the ESP8266
will fix the bug that causes it to become
non-communicative. However, this
technique will still be useful because
there are many other things that can
go wrong and a brief power down will
rescue the server regardless of what
difficulty it is in.
Beyond a simple web server
You can get quite fancy with your
web server if you want to. You could
format the data in your web page to
include variables that the microconsiliconchip.com.au
troller is monitoring; eg, temperature,
voltages etc.
You can also use html FORM tags
to create check boxes, radio buttons
and push buttons on the web page.
When the user selects these items,
their browser will send a message to
the ESP8266 module which can be
interpreted by your server program
to turn on a valve, unlock a door or
whatever.
Using html links, you can have
hot links in the web page that link to
other pages supplied by your microcontroller or other websites.
Nor are you limited to simply serving web pages. Using the ESP8266
module, your microcontroller can get
the time from the Internet or the current weather forecast. You can connect
to an SMTP server and send emails –
eg, to alert you to some fault condition.
You can also connect to services like
Twitter to upload and log interesting
information – eg, the humidity in your
greenhouse. There are also websites
that specialise in logging data from embedded controllers. A popular service
is http://thingspeak.com which will
store, graph and present your data for
you. Imagine being able to check the
temperature trends in your home-brew
concoction via your smartphone.
And don’t forget that the cost is just
$5 for this amazingly capable module.
Upgrading the ESP8266
Most ESP8266 modules come with
version 00160901 of the firmware.
You can discover the version of your
module with the command: AT+GMR
Some talented individuals on the
Internet have created alternative versions of the firmware which include
bug fixes and extra features such as
the ability to change the baud rate.
One especially valuable feature is the
capability of putting the module into
a transparent mode where any data
sent by the remote computer is fed
straight through to the microcontroller
and vice versa.
This means that you can set up an
ESP8266 module and connect it to the
Micromite’s console. Using telnet, you
can then connect to the Micromite via
WiFi and edit the program running on
it – all from your desktop while the
Micromite and ESP8266 module are
hidden in a cabinet, in your shed or
some other inaccessible location.
An accompanying panel contains
Terminal Program
OPEN “Com1:115200” AS #1
DO
PRINT INPUT$(1, #1);
PRINT #1, INKEY$;
LOOP
Fig.5: this simple program will
turn your Micromite into a
terminal. It will shuffle characters
between its console and COM1 (the
ESP8266 module) and is useful for
experimenting with the module
and verifying how it reacts to
commands.
links to websites that carry upgraded
versions of the firmware. To upgrade
the module, you need to short one of
the GPIO pins to ground and run a
program on your computer that will
transfer the new firmware via the serial link.
The operation is relatively painless and it only takes a minute or so.
About the only downside is that the
new commands and features are often
poorly documented so you will need
to engage in some personal experiSC
mentation.
Enter C
ode :
SCDEC
20
*Conditions apply
Selecte
d Items
Ends
20 O%FF
15th January 2015
S AV E
4D Systems designs, develops and manufactures intelligent graphics solutions
using the latest OLED and LCD technology available, with custom graphics
processors that enable both stand alone and
host dependent solutions suitable for a very
wide range of applications and projects.
4D SYSTEMS
TURNING TECHNOLOGY INTO ART
www.4dsystems.com.au
siliconchip.com.au
.com/4DSystemsAU
.com/4DSystems
December 2014 35
|