Arduino Wi-Fi – Part 2


Previously we introduced you to the new ESP8266 Wi-Fi chip from Espressif Systems that adds 802.11b/g/n Wi-Fi to your Arduino projects for less than $4.

We looked at how it uses a modified set of modem-style AT commands to communicate with your Arduino and came up with a simple communications app that lets you talk to the ESP8266-powered ESP-01 Wi-Fi module.

Here we get a little more serious and turn a DHT22 environment sensor, an Arduino Uno board and the ESP-01 module into a standalone Wi-Fi Weather Server.

The circuit

dhtWe’re using the same circuit setup as last time for this server, with just the addition of the DHT22 environment sensor.

Normally, this sensor requires separate power connections, but we’ve coded the Arduino to power the DHT22 through its digital I/O lines, ensuring all we need to do is slot the sensor into the Arduino’s D3 to D6 pins and it’s ready to go.

amsThe DHT22 consumes only a trickle of current, low enough to be supplied direct from the digital I/O lines. By setting I/O pins D6 ‘high’ and D3 ‘low’, we create a teeny-tiny power supply that’s perfect for the DHT22.

The ESP-01 Wi-Fi module requires 3.3VDC power, but the Arduino Uno itself runs off 5VDC, so we’ve added a tiny three-pin AMS1117 3.3VDC regulator, turning the 5VDC into 3.3VDC to power the ESP-01.

overlayThese regulator modules are everywhere on eBay for around $1.50. (Yes, the Arduino Uno does have a 3.3VDC voltage source, but it can’t deliver the current bursts the ESP-01 requires when transmitting, so we’ve gone for the AMS1117 regulator instead.)

Finally, the whole thing is built on a DIY prototype shield — we’ve used one of these before and they’re fantastic for building your own small circuits and adding them neatly to your Arduino board. It’s about $5 on eBay. The overlay diagram should help you figure out how to build your own version.

Web server – minimum parts list

  • Arduino Uno microcontroller board
  • ESP-01 Wi-Fi module
  • AMS1117 3.3VDC regulator module
  • DHT22 environment sensor
  • DIY prototype shield

We purchased these parts from eBay and you should be able to find them for around $20 all-up.

Get the source code

You can grab the WiFiWeather_v3 source code for free from here.

Unzip the file and copy the contents of the libraries subfolder to the same subfolder of your Arduino IDE. Restart the IDE and load up the WiFiWeather.ino source code.

Next, build the circuit, plug the USB cable in to the Arduino, flash the source code and you should be ready to go.

How it works

ssidThe Arduino Uno continually reads data from the DHT22 sensor and combines with the ESP-01 module to form a web server you can read from any web browser.

While you must feed the source code with the Station ID (SSID) code and password for your network before you flash it to the Arduino board (you enter it at the top of the code), the ESP-01 uses dynamic host configuration protocol (DHCP) to automatically find an available IP address on your network.

Once the module has established a network link, it just sits and waits for an incoming connection from another network client. While it waits, the Arduino reads the DHT22 sensor for temperature and humidity data every second.

The Arduino does its communicating over two serial ports — the first one is the standard hardware serial bus on pins D0 and D1 that connects up to your PC’s USB port, which you monitor through the Arduino IDE’s Serial Monitor window.

The problem is, the Arduino only has one genuine serial port and we need two, so we make the second one via the SoftwareSerial library and pins D10 and D11.

This second one is wired up to the ESP-01 to give us the data link between the Arduino and your wireless network.

Read it via web browser

webOnce you’ve built the circuit and flashed the source code, launch the Arduino IDE’s serial monitor (magnifying glass icon, top right) and watch the output.

It’ll first give you the basic boot-up status information about various components, but you’ll also find the IP address the server negotiates to use on your network. Type this IP address into the address bar of any web browser and, within a couple of seconds, you’ll be greeted with the latest temperature and humidity data.

The ESP-01 notes an incoming client, triggering the Arduino to transmit our HTML webpage data and act like a basic web server. In this case, the data includes the readings from the DHT22 sensor.

We’ve kept the LED indicator on pin D9 from last time — when it lights up, you’ll know that a client is hitting the server. It’s optional, though — if you don’t have it, don’t worry.

What’s brilliant about the ESP-01 is that it hides all the complex wireless and wired signal handshaking to the Arduino board, meaning you can build your own web server with not much more than a handful of lines of code.

Other ideas

The use of the DHT22 sensor here really is just to give you a taste of what the ESP-01 is capable of. Combined with an Arduino, the ESP-01 gives you a tiny, low-power computing platform that’s able to communicate with the internet and act as a simple, live web server for any low-speed application.

Combine it with a Lithium-ion battery, charger circuit and small solar panel, for example, and you could make your own completely self-contained outdoor weather station that’s live all day, every day.

Throw in a MicroSD card reader that we’ve looked at previously and you can add up to 32GB of data storage to turn your project into a super-cheap data logger.

What’s even more incredible is that it’s now possible to code the ESP8266 chip in the ESP-01 module directly through the Arduino IDE and create your own tiny webserver the size of an SD card for less than $4.

Everyone is jumping on board the ‘Internet of Things’ (IoT) bandwagon these days and it’s chips like the ESP8266 that is making it possible.

The only things you need are a healthy inquisitive mind and some imagination.

  • Newbie5000

    Hello, Thank you for the project. I am having an issue though…The ESP8266 says “ok” to all setup commands, the “sensor ok”, and the serial monitor spits out good temp and humidity data. But my loop code in the “if(wifi.available()) { ” is not executing. So, can someone point to what I should check? Thanks!