Introducing the ESP8266 WiFi Toy (ESPToy)



Content

ESP8266 — a very low-cost and flexible Serial-to-WiFi module that has gained a lot of popularity recently. It allows any microcontroller to have WiFi capability by simply using serial communication. In a previous blog post, I demonstrated how to use ESP8266 combined with an Arduino to set up a very simple web server. This time I built a standalone gadget for prototyping called the ESP8266 Toy (or ESPToy for short). There is also a more serious web server demo in the end that shows how to serve HTML files from a SD card, and using JSON and AJAX to implement prettier web design.

You should also know that there has been a lot of on-going development for this module. For example, there is a Lua-based firmware for ESP8266, which is quite amazing. Also check out the community forum here.

As usual, a video demo first:

New ESPToy 1.2:

Original ESPToy 1.0/1.1: Youtube Video Link


ESP8266 module and ESP8266 Toy are available for purchase at Rayshobby Store.


Features

New ESPToy 1.2:

  • ESP-12 SMD WiFi module, pre-flashed with Lua firmware and a startup demo script.
  • 3.3V 250mA linear regulator.
  • Programming using the on-board mini-USB port.
  • One color (RGB) LED, one pushbutton (used as a general-purpose pushbutton as well as for re-flashing firmware)
  • Additional pin headers for connecting external components and/or breadboard experiments.

IMG_0716server_example


Original ESPToy v1.0/1.1 (retired)

  • ATmega644 @ 3.3V, 16MHz, with CH340G USB-serial converter and Arduino bootloader.
  • 3.3V 500mA linear regulator.
  • Programming in Arduino using the on-board mini-USB port.
  • One color (RGB) LED, one pushbutton, one reset button. (New on version 1.1: one additional button on ESP8266’s GPIO0 pin, useful for firmware upgrade)
  • One light sensor (LDR), one temperature sensor (TDR).
  • One 2×4 pin header to fit ESP8266.
  • Additional pin headers for connecting external components and/or breadboard experiments.

In essence, the ESPToy 1.0/1.1 is a 16MHz Arduino with some handy built-in components for easy prototyping with the ESP8266 WiFi module. Once programmed, the whole assembly can run on battery. There is also a power MOSFET on board to programmably control the power supplied to ESP. This allows you to cut off power to save battery life.

The reason I picked ATmega644 (instead of ATmega328) is that 644 has two hardware Serial interfaces. I am using one for the bootloader and USB communication, the other dedicated for WiFi. This is quite handy and allows you to use the fast baud rates. Also, 644 has twice as much memory as 328, so it’s suitable for building more complex projects.

esptoy_annotatedesptoy_2


Demos

As shown in the video above, I’ve written a few examples to demonstrate the basic features of the ESPToy:

New ESPToy 1.2:

To upload the demo Lua scripts to ESPToy 1.2, download and run the ESPlorer software. The Serial port baud rate is 9600 (default). You can open a demo script, click on ‘Save to ESP’ to save the script as a file to ESP’s internal flash memory space. The script will run right away after uploading. You can also click on ‘doFile’ to re-run the script.

  • Start-up Demo: every ESPToy 1.2 is pre-flashed with a start-up demo. Plug in a mini-USB cable, the blue LED will blink and the WiFi module will start in access point mode, creating a WiFi network with SSID ESPToy-xx. The password is opendoor. Connect to this WiFi network, open a browser, and type in http://192.168.4.1. Use the sliders to change the LED color, and click on ‘Refresh Value’ to read button status and analog pin value.
  • Hello ESPToy!: shows the Serial print function.
  • Blink LED: shows digital pin write, and delay functions.
  • Button Interrupt: shows how to set up interrupt on a digital pin.
  • Hello ESPToy Server: shows a simple HTTP web server.


Original ESPToy v1.0/1.1 (retired)

  • SearchBaud: this comes handy if you forgot the correct baud rate of your ESP8266. It loops through the common baud rates, sends an empty AT command, the detects the correct baud rate by checking the result.
  • SerialCommand: this demo allows you to send AT commands to ESP8266 through a Serial monitor. Use this demo to experiment with all the AT commands available for ESP8266.
  • ScanNetwork: this demo starts the ESP8266 in AP mode, which creates a local WiFi network (the name is ESPxxxx). Use a smart phone or laptop to log onto this network, then open a browser and type in IP adddress 192.168.4.1. You will see a webpage with a list of detected WiFi networks. Type in the ssid and password of the target network, then click on Connect. The page will redirect in about 10 seconds to show the client IP address of the module on the target network. This is pretty standard approach to get your WiFi-enabled gadget to log on to your target network.
  • WebServer: this is a simple web server demo. It serves a html webpage and uses JSON and AJAX to periodically display the analog pin values. The first two analog pins correspond to the light and temperature sensors respectively, so these two values will respond to light and temperature changes. There are also three sliders to set the color of the on-board LED. As I said before, the demo can run on battery, so this can be used as a WiFi-enabled color LED light. Thinking about a simplified version of the Philips Hue? Yup, this is a poor man’s version of that 🙂
  • WebServer_SD: same as above, except this requires an external SD card slot, so that it can serve bigger and multiple html pages from files stored on the SD card.


Preparation

The ESPToy’s source code is available on Github. Check the README file therein to get started.

Serial Port and Driver:
ESPToy uses a CH340G USB-to-Serial converter. For:

On Windows, the Serial Port name is COM? where ? is a number assigned to the USB-serial chip. On Linux, the Serial Port name is /dev/ttyUSB? where ? is a number. On Mac, the Serial Port name is tty.wchxxx.

Programming ESPToy 1.2

Instructions in this section are being re-organized. For now please refer to the ESPToy 1.2 instructions in this blog post.



Programming ESPToy 1.0/1.1 (retired)
Programming the ESPToy can be done through the Arduino IDE (tested with Arduino version 1.0.6). Because ATmega644 is not a board that appears in the stock Arduino, you will need to copy the atmega644 subfolder (from the hardware folder) to the corresponding hardware folder in your Arduino’s installation directory; same with the ESPToy subfolder (from the libraries folder).

Next, launch the Arduino IDE, select Tools->Board->ESPToy, and the correct Serial port from Tools->Serial Port (see below), then select a program from File->Examples->ESPToy, and finally click on Upload

ESPToy Arduino Library:
The RFToy Arduino library can be downloaded from: http://github.com/rayshobby/esptoy. You can either clone this Github repository, or download it as a zip: https://github.com/rayshobby/ESPToy/archive/master.zip

Pin Assignments:

  • D0: ESP power (active LOW, pulled down by default)
  • D3: push-button
  • D12: red LED (pwm)
  • D13: green LED (pwm)
  • D14: blue LED (pwm)
  • A0: light sensor (LDR)
  • A1: temperature sensor (TDR)
  • Serial: USB serial TX/RX
  • Serial1: ESP8266 TX/RX

Spare Pins: D2 (INT2), D4, D15, A2, A3, A4, A5, A6, A7

Power Options:
ESPToy can be powered by USB or an external battery. There is a battery jack that fits a standard 3.7V lithium battery. In addition, there are two battery pins (next to the battery jack) — you can solder wires to connect a 3V AA/AAA battery pack. Note that the external AA/AAA battery should not exceed 3.6V (because it’s not regulated).


Flashing a New Firmware to ESP8266

Programming ESPtoy with Arduino: I strongly recommend you to learn to use Arduino to program ESPtoy, due to its flexibility and memory efficiency. While Lua firmware is easy for beginners, to write advanced programs, it’s much easer to use Arduino. Check the resources in this forum post.

On ESPToy 1.2 and 1.1: there is a pushbutton button (on version 1.1 it’s at the upper-left corner) — it’s connected to ESP8266’s hardware GPIO0 pin. If this button is pressed when powering up ESPToy, the ESP8266 will enter bootloading mode waiting for a new firmware. I recommend using the esptool python program to upload firmware, which is very easy to use. Specifically, in command line, run
./esptool.py write_flash 0x00000 xxxx.bin
where xxxx.bin is the firmware name.

On ESPToy 1.1, use the SerialCommand sketch to prepare ESPToy as a serial relay.


For ESPToy 1.0 (retired): to let ESP8266 enter bootloader, you need to solder a wire from ESP8266’s GPIO0 pin to ground. Specifically, if GPIO0 is pulled to ground, upon power-up ESP8266 will enter bootloader mode waiting for a new firmware from the Serial interface. In contrast, if GPIO0 is pulled up (to VCC), ESP8266 will boot into normal operation mode.

The picture below shows where the GPIO0 pin and a ground pin (any ground pin on the board should work). The wire should be soldered at the back of the circuit board. You can also connect a switch in series on the wire to easily switch between bootloader mode and normal mode.

esp_gpio0

Once this is done, you can flash the SerialCommand sketch to ESPToy, which will make it serve as a Serial relay. I recommend using the esptool python program to upload firmware, which is very easy to use. Specifically, in command line, run
./esptool.py write_flash 0x00000 xxxx.bin
where xxxx.bin is the firmware name.


Purchase Link


Sneak Peak Preview of SquareWear Esimal

Since SquareWear 2.0 and Mini, there will soon be a new member in the SquareWear family. This is a sneak peak preview of SquareWear Esimal — the tiniest SquareWear ever made 🙂

2014-11-14IMG_20141114_123548

How does it differ from the other two members? First, it’s tiny and measure only 1.1″ x 0.7″. It’s designed to be small, low-cost, and suitable for breadboard experiments. It has two rows of 1×11 0.1″-pitch pin headers and can fit directly onto a breadboard. Second, it uses a micro-USB connector, which helps reduce the overall footprint. Although the big sewable pins are gone (so are the built-in buzzer and rechargeable battery), the Esimal keeps the most essential features of SquareWear — it has ATmega328 running at 3.3V, 12MHz, with built-in USB port and USBasp bootloader, light sensor (using a photoresistor), temperature sensor (using a thermistor), general-purpose button, and LED. Overall it will be a very low-cost, breadboard friendly SquareWear, for learning Arduino programming, organizing workshops, and general-purpose microcontroller projects.

IMG_0479

Since this post is a preview, I will not dwell too much on the details. Expect Esimal to be available in a few weeks time!

Introducing RFToy 1.0

Today I am introducing the first version of RFToy — an Arduino-compatible gadget for interfacing with Radio Frequency (RF) modules. First, let me show you a few pictures of RFToy and a video introduction:

IMG_0419IMG_0428IMG_0426
IMG_0427IMG_0431


RFToy is available for purchase at Rayshobby Store.


Features
  • ATmega328p @ 3.3V, 8MHz, with CH340G USB-serial converter and Arduino bootloader.
  • Programming in Arduino using the on-board mini-USB port.
  • One 128×64 OLED display, three tactile buttons.
  • 20mm coin battery holder, and slide switch to select between USB or battery power.
  • Pin headers for plugging in 433/315 MHz RF transmitter and receiver modules, and MOSFET power switches for them.
  • 3.5mm audio jack to output receiver signals to a computer’s line-in port, to monitor RF waves.
  • Pin headers for plugging in nRF24L01 transceiver.
  • Pin headers for connecting external components and/or breadboard experiments.

So in essence, RFToy is a 8MHz Arduino with buttons, OLED display, battery holder. It’s compact (1.5″ x 2.3″) and it’s suitable for a variety of projects involving RF modules.

rftoy_annotated


Demos

As shown in the video above, I’ve written a couple of examples to demonstrate the basic features of the RFToy.

  • RF Recorder: this demo shows how to use RFToy to decode signals from the remote control of a typical wireless power socket, store the decoded signal in EEPROM, and play it back to simulate the remote control. You can store up to 7 different signals, allowing you to control up to 7 power sockets. The demo is based on the RCSwitch library, and it has a basic UI using the OLED display and buttons.
  • Wireless Temperature Sensor: this demo uses a pair of RFToys — one RFToy has a thermistor (connected to analog pin A1) and sends out the temperature reading periodically through its 433MHz transmitter; the other has a 433MHz receiver, and displays the received value to the OLED. This demo is based on the VirtualWire library, and uses the watchdog timer and power-down sleep to save battery life when the sensor is not transmitting data. A variant of this demo is also provided using a pair of nRF24L01 transceivers and the Mirf library.
  • Interfacing with Off-the-Shelf Wireless Sensors: previously I’ve written several blog posts about using Arduino to interface with off-the-shelf wireless temperature, humidity, rain, and soil moisture sensors. Since these sensors all work in the 433MHz frequency band, these demos can all run on RFToy, with sensor values displayed onto the OLED.

With the built-in buttons, display, and Arduino compatibility, there are tons of other projects you can build with RFToy.


User Manual

RFToy is open-source. You can check out its Arduino library code at http://github.com/rayshobby/rftoy, and hardware design files at http://github.com/rayshobby/rftoy-hw. Some technical details are provided below:

Note: the content on this page is published under Creative Commons Attribution-ShareALike (CC BY-SA) 3.0 License. Content reuse is allowed. If you have a project/product based on RFToy, please acknowledge my contribution. The software code and hardware design are published for educational purpose.


Purchase Link


Dead-Simple Driver Installation for USBasp and USBtiny on Windows

Today I came across a surprisingly simple approach to installing USBasp and USBtiny drivers for all versions of Windows — XP, 7, 8, 8.1, whether 32-bit or 64-bit, all inclusive! As you may know, installing open-source drivers such as USBasp and USBtiny have been a great pain on some of the recent Windows OS, due to the enforcement of signed drivers. The typical solution involves rebooting Windows into a mode that disables driver signature enforcement. Even after you’ve done it once, if you boot into the normal mode next time, it may fail to recognize the driver again (reporting it’s not digitally signed). A huge source of frustration.

Anyways, while searching for ‘fully signed USBasp driver’, I came across this tool called Zadig, which can be used to install libusb drivers on all versions of Windows, and it’s digitally signed. Since USBasp and USBtiny are both based on libusb, could it be the right solution? To my great surprise it worked really well — I was able to install both drivers on Windows XP, 7 (32-bit and 64-bit), 8, and 8.1 instantly, without messing with driver signature enforcement at all. I was mostly surprised such a great solution wasn’t documented more widely online.

Instructions
  • Go to http://zadig.akeo.ie/ and download the software (note that Windows XP has a separate link).
  • Plug in your USBasp or USBtiny device. In case your microcontroller uses a USBasp or USBtiny bootloader, enter bootloading mode, and let Windows detect the device (it will report driver not found). If a window pops up asking to search for driver, just close it or click on Cancel.
  • At this point, run Zadig, it should detect the USBasp or USBtiny, or any libusb device that you have. Then in the selection box (see below), choose libusb-win32 (v1.2.6.0), and click on Install Driver, and wait for the installation to complete.

zadig_srceenshot
That’s it! Because the drivers are digitally signed, there is no hassle installing it in Windows 7 64-bit and Windows 8.1.

I will be updating the driver installation instructions for OpenSprinkler 2.1 and SquareWear right away, as they both use USBasp bootloader. Users have often complained that it’s frustrating to install USBasp driver for Windows 7 64-bit and Windows 8.1. Those days are now past!

Sneak Peak Preview of SquareWear 2.3

There is an upcoming MakerJam at Mount Holyoke College and I’ve been commissioned to create a new version of SquareWear, numbered 2.3. Following the suggestions I’ve received in the past, I made the first prototype of SquareWear 2.3:

IMG_0416IMG_0418

Below I list the main changes / improvements:

  • Added Hardware USB-serial Chip (CH340G) : this pretty much follows the same recent change on OpenSprinkler 2.2u. CH340G is a very inexpensive, easy-to-use USB-serial converter. It’s a low-cost replacement of the popular FTDI chip. With a hardware USB-serial chip, SquareWear can now use the same optiboot bootloader as standard Arduinos use. Also, cloud-based Arduino platform, like CodeBender would also work well with SquareWear. Even better, CH340G is supported out of box on Windows 7 and 8, so no more messing with installing USBasp driver, ever!
  • Added Breadboard Pins (dual-purpose): people asked about the possibility of adding breadboard pins, so in this version there are 13 pins on the right edge with standard 0.1″ spacing. These pins are also neatly laid out to serve a second purpose: they match some of the common I2C sensors (particularly MPU6050 6-axis accelerometer) and bluetooth transceiver. This way you can easily plug in sensors and bluetooth transceiver as optional add-ons! The picture on the right above shows how the board looks like with an MPU6050 and bluetooth transceiver plugged in. With this setup, you can easily make a project that involves motion sensor, and even transmit the signal wirelessly to a nearby computer!
  • Upgraded the 3.3V LDO to a bigger chip (SOT-89 packaging) that can provide higher current.
  • Added AT24C128 (16KB) EEPROM: this follows SquareWear Mini, where the added EEPROM can be useful for storing logging data and animation frames.
  • Removed Build-in Rechargeable Coin Cell: I was quite reluctant to make this change, because the built-in rechargeable coin has been one of the main selling points of the original SquareWear. But to make space for the added USB-serial chip, and also to add the breadboard pins, the built-in battery has to go. On the plus side, this makes the design focus on using external LiPo battery, which has higher capacity and the charging current is also suitable increased.

I will look forward to the MakerJam to receive some feedback / comments on the new design.

Announcing OpenSprinkler Firmware 2.1.0 (Major Upgrade)

I am excited to announce that OpenSprinkler Firmware 2.1.0 is officially release. This is a major upgrade that includes a number of new features, including:

  • Automatic Weather-based Water Time Adjustment using real-time weather data obtained from Wunderground (thanks to Rich Zimmerman who introduced the method, the adjustment method is named after him).
  • Improved Program Settings including per-station water time, flexible start times, custom name, per-program weather adjustment control, and up to 14 different programs.
  • Automatic Timezone and DST Detection based on your location. No need to select time zone and mess with DST any more — once you set your location, the firmware can automatically determine your time zone and DST.
  • Improved Station Attributes and Scheduler including station ‘disable’ attribute, ‘activate relay’ attribute, test station feature (replacing the previous manual mode), automatic serialization of overlapping schedules, and the ability to manually start a program on the controller using buttons.
  • Numerous UI Improvements (thanks to Samer’s hard work) including unified mobile interface, export / import configurations, improved visualization of logging data, and the number of supported languages has expanded to 17 (thanks to all who contributed)!

This is a pretty major milestone as it not only addresses the previous limitations but also introduced critical new features including weather-based control. Furthermore, consider all these are implemented on a small microcontroller with only 64KB flash memory and 4KB RAM 🙂 These significant changes are worth making a new video for. So here is the video tutorial for firmware 2.1.0 (it’s a bit long, but gives you a comprehensive overview of the main features);
http://youtu.be/iUrnf4nIuY4?list=UUl5dnj8oj_9dg6KIbudYmOw


Documentation

With this firmware I’ve also written a more detailed user manual, and API documentation. These are available on the Support page of our new website www.opensprinkler.com. In addition, there are a total of 4 tutorial videos that walk you through the hardware installation, WiFi connection, firmware features, and upgrading firmware. Be sure to check them out first.


Upgrade to Firmware 2.1.0

All OpenSprinkler 2.x devices (including 2.0, 2.1, and 2.2) are eligible to upgrade to firmware 2.1.0. Please check the ‘Firmware Update’ instructions on the support page to download and run the firmware updater. OpenSprinkler 2.0 and 2.2 are the easiest as drivers are pretty straightforward to install, and there is no bootloading procedure; OpenSprinkler 2.1 is tricky because the driver installation is more involved, and there is a bootloading procedure you need to follow. In any case, the firmware upgrade tutorial video gives you a quick walk-through of all the steps.

To use the weather feature, you need to apply for a Wunderground API key. Again, instructions can be found on the support page.

Firmware 2.1.0 has gone through internal alpha testing and external beta testing, so it should be pretty stable. For issues and suggestions, please use the forum, or the support page to submit support tickets.


Implementation

When I say ‘all these are implemented on a small microcontroller with only 64KB flash memory and 4KB RAM’, it’s not entirely true — the weather feature and timezone / DST detection are actually implemented using Python scripts hosted at www.opensprinkler.com. Why? Because these require fairly heavy processing power that’s simply beyond the capability of a small microcontroller. So they are implemented by using Python scripts that serve as the ‘middle man’ — retrieving data from weather websites, perform the necessary parsing and computation, and produce the final results to send back to OpenSprinkler. This way the heavy computation is done in the cloud, and OpenSprinkler only needs to poll the server once in a while to update the results. If you are interested in customizing the scripts, you can download the Python scripts from OpenSprinkler Github repository, modify them and host them on your own server. But for most people the default provided script should work pretty well.


Upcoming Features

As this firmware has been rolled out, we are getting excited to decide on the new features for the next round. Some planned features include:

  • Additional station attributes including soil type, slope type, serial group.
  • Support to store programs and station settings onto the microSD card (effectively allowing unlimited programs).
  • Adding firmware support to interface with remote power sockets, so you can use OpenSprinkler to control power line devices like heaters, fan, Christmas lights etc.
  • Support to use sunset and sunrise times for program start times (the sunset and sunrise times are already being detected using the timezone / DST script).
  • Support for flow sensor to monitor water consumption.
  • Cloud support: no more messing with port forwarding.

Suggestions and comments are welcome. Please post them below, or on the forum. Thanks!

os21s


Sneakpeak Preview of OpenSprinkler DIY kit v2.2u

We are getting ready to release the next minor revision of OpenSprinkler DIY kit, numbered 2.2u. This revision is largely the same with the current 2.1u, with three main differences:

  1. MCU clock speed is increased to 16MHz (from 12MHz previous), by using a 16MHz crystal.
  2. Built-in USB-Serial chip is added, by using CH340G, which is a common chip in low-cost USB-serial converters.
  3. With the hardware serial chip, the bootloader is also changed to use Arduino Optiboot, at 115200 bps baud rate and a bootloader size of only 512 bytes.

IMG_0249IMG_0248

The first change above is to increase the processing speed and hopefully make the controller faster at handling complex algorithms and transferring data with the Ethernet controller. The second change above is mainly to solve the issue that it has been increasingly painful to install USBasp driver on Windows 8+. To understand the background, all of these have to do with firmware upgrade — how to reflash the microcontroller with new firmwares. Back in OpenSprinkler 2.0, I used to have a separate ATtiny45 chip on board to function as a USBtinyISP programmer, which can reflash the main MCU (ATmega644). This is not an ideal design because it involves an extra chip that we have to program; also USBtinyISP is not particularly fast. So when I designed OpenSprinkler 2.1, I made the conscious decision to get rid of ATtiny45. Instead, I decided to implement a USBasp bootloader for ATmega644, which can present the MCU itself as a USBasp programmer when a button is pressed on start-up. This is quite appealing because no extra chip is required, and the programming speed is considerably faster. The only caveat is that to use USBasp in Windows, you need to install the open-source USBasp driver. This was not the most pleasant thing to do, but wasn’t a big deal as the driver was fairly easy to install.

That is until when Windows 8 came out, with this feature called driver signature enforcement. It basically means the driver needs to be digitally signed with Microsoft, otherwise it won’t allow you to install the driver. Let’s be honest, for open-source developers, who wants to pay the big bucks to get the driver signed? So suddenly this has created an even bigger barrier for average users. The only way around the issue is to boot Windows 8 into a mode that disables driver signature enforcement. This step turns out to be unnecessarily complicated. I’ve often received comments about how it’s painful to install USBasp driver for Windows 8. I recently made a video to demonstrate how to update OpenSprinkler firmware — in this 11-minute video, 6 minutes were spent purely on explaining how to install USBasp driver for Windows 8. That’s an evidence of how unnecessarily complicated it is!

Anyways, I set out to find a better solution, and was glad that I discovered the CH340 chip. It’s basically a USB-serial chip that is often found in low-cost USB-serial cables / converters. It’s really inexpensive (less than 30 cents) and requires very few peripheral elements (just a crystal and filter caps). With this chip, you can now use the standard serial monitor to debug your program, and the bootloader can also now use the standard Arduino bootloader.

ch340g_closeup

What I like the most about this chip though, is that it does not require driver for Windows 7, 8 and above. What? Is that even possible? Yup, I’ve verified it — Windows 7, 8 and 8.1 all recognized it right away. The fact that it doesn’t require driver just makes it a whole lot easier to upgrade firmware. Windows XP and Mac OS still require driver for it, though, but that’s light years better than installing driver for Windows 8.

You may be wondering: wait a minute, what about the FT232 chip, which has been available on the standard Arduino since the beginning? Isn’t what I am trying to do here already done? Sure, CH340 is basically a replacement for FT232 — both are USB-serial converters. But there is an economic reason to go for it: even at volume quantity like 1000, FT232 costs about $3 to $4 per chip. That compared to 30 cents for CH340? You tell me. Another nice thing about CH340 is that it comes with SOIC-16 packaging, which is very easy to solder even by hand. This makes it more appealing than other low-cost alternatives like PL2303 and CP2102.

OK, I’ve done enough advertisement. I am not in any ways associated with the company that makes this chip, I am just excited, and regret that I didn’t know about it earlier 🙂

First Impression on the ESP8266 Serial-to-WiFi Module


Check my new blog post on the ESP8266 Toy


Continuing from my previous blog post about Hi-Link HLK-RM04 module, I have finally received the ESP8266 Serial-to-WiFi module that I’ve been waiting for. As I said previously, with the popularity of IoT devices, there is an increasing demand for low-cost and easy-to-use WiFi modules. ESP8266 is a new player in this field: it’s tiny (25mm x 15mm), with simple pin connections (standard 2×4 pin headers), and best of all, it’s extremely cheap, less than US$3 from Taobao.com!

IMG_3188IMG_3189IMG_3190

What is Serial-to-WiFi? Simply put, it means using serial TX/RX to send and receive Ethernet buffers, and similarly, using serial commands to query and change configurations of the WiFi module. This is quite convenient as it only requires two wires (TX/RX) to communicate between a microcontroller and WiFi, but more importantly, it offloads WiFi-related tasks to the module, allowing the microcontroller code to be very light-weighted.

There are already a lot of excitements and resources you can find online about ESP8266. I’ve included a few links below:

These are great resources to reference if you need help working with ESP8266. Below I document my own experience. I’ve also bought a few extra and put them available on the Rayshobby Shop for anyone who is interested in buying the module and don’t want to wait for the long shipping time from China 🙂


Check my new blog post on the ESP8266 Toy


Pin Connections. ESP8266 is sold in several different versions. The one I received is the version with 2×4 male pin headers, and PCB antenna. In terms of the form factor, it looks a lot like the nRF24L01 2.4G RF transceiver. Here is a diagram of the pins:
ESP8266_pinout
Connect the top two pins (UTXD, GND) and bottom two pins (VCC, URXD) to the RXD, GND, VCC, TXD pins of a microcontroller. Note that VCC must be no more than 3.6V. The middle four pins are should be pulled up to VCC for normal operation. However, if you need to upgrade the firmware of the module, you need to pull the GPIO0 pin to ground — that way upon booting ESP8266 will wait for a new firmware to be uploaded through serial. This is how you can upgrade the firmware in the future.

A few quick notes for connection:

  • The typical operating voltage is 3.3V (acceptable range is 1.7V to 3.6V). As the module can draw up to 200 to 300mA peak power, make sure the power supply can deliver at least 300mA. For example, the 3.3V line from a USB-serial cable would be barely sufficient, in that case it’s better to use a LDO to derive 3.3V from the 5V line.
  • When using the module with a 5V microcontroller, such as a standard Arduino, make sure to use a level shifter on the URXD pin — a simple resistor-zener level shifter is sufficient. Again, this is to prevent over-voltage.

A schematic will make it clear. See below. In my case, I soldered the components and a matching female 2×4 pin header to a perf-board. This way I can easily plug in and unplug ESP8266. Again, if you are using a 3.3V microcontroller, you can do away with the LDO and zener diode.

esp8266_connIMG_3194

Experiments using a USB-Serial Cable. Before connecting to a microcontroller, it’s a good idea to use a USB-Serial cable (such as the inexpensive PL2303 USB-serial converter) to check out the basic functions of the module. Connect the PL2303 cable with ESP8266 according to the schematic above. Then open a serial monitor (such as gtkterms in Linux and putty in Windows) with 11520 baud rate (my ESP8266 seems to be set to 115200 bard rate; earlier versions use 57600). Then you can use a list of AT commands to talk to ESP8266. The AT commands are pretty well documented on this page. Below are some example input (shown in bold font) and output that show how to reset the module, list available WiFi networks, check the WiFi network it’s connected to, list IP address, and firmware version etc.


AT
OK
AT+RST
OK
ets Jan 8 2013,rst cause:4, boot mode:(3,6)
wdt reset
... ...
chksum 0x46
csum 0x46
ready
AT+CWLAP
+CWLAP:(0,"",0)
+CWLAP:(3,"freefly",-49)
OK
AT+CWJAP?
+CWJAP:"freefly"
OK
AT+CIFSR
192.168.1.130
AT+GMR
00150900
OK

A Simple Demo using Arduino.
Next, I connected ESP8266 to an Arduino. Because Arduino is already using the TX/RX pins for bootloader, make sure to unplug ESP8266 while flashing the Arduino, otherwise you may not be able to upload a sketch successfully. Also, you can’t use TX/RX for printing debugging information, since ESP8266 will be using them to communicate with Arduino. Instead, you can use another pair of pins (e.g. D7 and D8) as software serial pins, and use a PL2303 serial cable to monitor the output. This will help print debugging information.

I’ve also experimented with using software serial to communicate with ESP8266, but that has failed — ESP8266 requires 115200 baud rate, and that’s a little beyond the capability of software serial. So you have to stick with the hardware TX/RX pins.

(Update: the code has been revised on Dec 14, 2014 to improve robustness, particularly for some of the latest ESP8266 firmwares. Right now there is still an issue that if the browser is closed before the transfer is completed, it may leave ESP8266 in the notorious ‘busy s’ mode, the only solution to which is to do a hard reset. If using the module in real products, make sure you have a way to use a microcontroller pin to reset the power of the module, thus providing a way to hard reset the module. It looks like future firmwares may be able to address this in software.)

The demo program first configures ESP8266 to log on to your WiFi network (SSID and password are given as macro defines at the beginning), then it sets ESP8266 as HTTP server with port number 8080, and it listens to incoming request. If you open a browser and type in http://x.x.x.x:8080 where x.x.x.x is the module’s IP address (printed to soft serial pins), you will see the output which is a list of analog pin values, and the page refreshes every 5 seconds. So this is a basic Hello World example that shows how ESP8266 can be configured as an IoT server, responding to incoming requests.

IMG_3193

Challenges. While my initial experiments with ESP8266 have been quite successful, I’ve also encountered minor issues that took me a while to figure out. For example, while the AT commands are well documented, they don’t seem extremely consistent — some commands allow question marks at the end, some don’t. I also see variations of the returned values from running the AT commands: sometimes there is an extra end of line character, sometimes there is none. These basically require a robust software library to handle all possible cases.

Overall I would say ESP8266 is a very promising WiFi module for IoT, particularly open-source IoT gadgets, because of its low cost, compact size, and the community development. It seems the manufacturer has also open source the firmware code, and thus the minor issues can probably be easily fixed through a firmware upgrade.

We have a small number of ESP8266 modules available in stock, and will continue to offer them if there are sufficient interests. Thanks!


Check my new blog post on the ESP8266 Toy


OpenSprinkler Firmware Update Program 2.0 (with Video Tutorial)

We’ve just released a new OpenSprinkler Firmware Update program, with a video tutorial to walk you through the steps of how to upgrade your firmware. Hopefully this will make it easy for users to transition to the upcoming Firmware 2.1.0, which has a number of significant new features and improvements.

The new update program is written in Qt, and does not rely on Java any more. It’s cross-platform just like before. It also supports downloading the latest firmwares from the OpenSprinkler Github repository, and auto-detect of your OpenSprinkler hardware version. If you are a Windows user (especially Windows 8 and 8.1), you will still have to go through the hassle of installing driver. The video tutorial shows you a step-by-step guide of how to install driver.

For those who are interested in modifying the OpenSprinkler firmware code, I am experimenting with CodeBender.cc, which is a cloud-based Arduino platform. It’s really convenient in that it’s essentially a web-based Arduino IDE that runs in a browser; it also make it easy for people to share their code and modifications. I think its convenience will likely lower the barrier of programming, and motivate more users to modify OpenSprinkler firmware code to add custom functionality. I’ve made requests to add OpenSprinkler to their list of supported boards. Hopefully I will hear back from them soon!

First Impression on HLK-RM04 Serial-to-WiFi Module


Check my new blog post on the ESP8266 Serial-to-WiFi module


As the Internet-of-Things (IoT) are becoming more and more popular, there is an increasing demand for low-cost and easy-to-use WiFi modules. For Raspberry Pi and BeagleBone, you can plug in a cheap USB WiFi dongle (like the popular Edimax 7811). These dongles are generally less than $10. But for microcontroller-based devices, the options are much fewer and generally more costly. For example, an Arduino WiFi shield can easily cost $40 to $50; even the more recent CC3000 module costs about $35. If you are puzzled by the big price difference: those cheap WiFi dongles require a USB host system, which is beyond the capability of Arduino. Still, it’s unsettling to realize that a WiFi module or shield would cost more than an off-the-shelf WiFi router, even though the router is significantly more powerful.

Recently I saw a post on Hack A Day alerting us about a new Serial-to-WiFi chip. I immediately ordered two of those. I honestly don’t know what I should expect from these $3 parts, but perhaps it’s a good push towards a new wave competitions on low-cost WiFi solutions. Anyways, while waiting for the shipment to arrive, it reminded me that a while back I actually bought something similar: a Hi-Link HLK-RM04 Serial-to-WiFi module. It’s about $10, so still pretty low-cost. I am glad I didn’t lose it, so I took it out of a pile of electronics and started experimenting with it.

IMG_0222IMG_0221

As you can see, the module is pretty small (about 40mm x 30mm). The picture on the right above shows the components underneath the metal shield. The chip (RT5350F) is a 360MHz MIPS core with built-in WiFi support. The module is quite powerful — at factory default settings it functions as a normal WiFi router. Now, in order to get it to talk to a microcontroller like Arduino, I need to use its Serial-to-WiFi capability. What is that? Well it means using the serial (TX/RX) interface to send and receive Ethernet buffers, and similarly using serial to send commands to the module and query or change its current status. This is quite convenient because first, it only takes two wires (TX/RX) of the microcontroller to talk to the module, second, it moves WiFI-related tasks to the module allowing the Arduino code to be very much light-weighted.


Check my new blog post on the ESP8266 Serial-to-WiFi module


Power-Up. To start the module you just need to provide +5V power. At the first use, the module boots up into a WiFi AP (access point) named Hi-Link-xxxx so you can actually log on to the WiFi network and change settings there. Here is what the homepage looks like (default IP: 192.168.16.254, default log-in: admin/admin):

hlk_rm04_1

Change Settings. For my purpose I need to set the module as a WiFi client so that it can log on to my home network, and allow the Arduino to communicate wirelessly through the module. So I changed the NetMode to WiFi Client – Serial, and put in my home network’s SSID and password. I also changed the Serial baud rate to 57600 (the default is 115200): the lower baud rate can help Arduino to communicate with it more reliably especially if I am going to use software serial. Make sure that the Local/Remote port number is set to 8080 or anything other than 80 (because 80 is reserved for the module’s homepage).

hlk_rm04_2

Receiving Data. Now after the module boots up, it appears as a device on my home network (for example, 192.168.1.147). If I type in 192.168.1.147 in a browser, I will still see the same homepage as above. However, now I can communicate with the module’s serial interface through port 8080. To see what’s going on, I used a PL2303 USB-serial converter: connect the serial converter’s +5V, GND, TXD, RXD to the WiFi module’s +5V, GND, RX, TX (note TXD->RX and RXD->TX), then I opened a serial monitor at 57600 baud rate (putty in Windows or gtkterms in Linux). Now type in http://192.168.1.147:8080 in a browser, I get the following output on the serial monitor:

GET / HTTP/1.1
Host: 192.168.1.147:8080
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4

Cool, this means the Ethernet buffer has been received through the serial RX pin. If I can then send something back through the serial TX pin, like acknowledgement plus HTML text, the data will be transferred back to the browser. This is basically how HTTP GET command works.

A Simple Arduino Example. To send data back through the serial TX pin, I used an Arduino to set up a simple example. To get reliable communication, I used Arduino’s hardware RX/TX (0/1) pins. The drawback of this is that when you program the Arduino you need to temporarily disconnect the WiFi module from these two pins, otherwise they will interfere with the uploading process. Here is a picture of the setup:
hlk_rm04_3

Next, I wrote an Arduino program to print out the analog values of A0 to A5. This is actually modified from the WebServer example in the Arduino Ethernet Shield library:

void setup() {
  Serial.begin(57600);
}

void loop() {
  boolean has_request = false;
  if (Serial.available()) {
    while(Serial.available()) {char c = Serial.read();}
    has_request = true;
  }
  if (has_request) {
    Serial.println("HTTP/1.1 200 OK");
    Serial.println("Content-Type: text/html");
    Serial.println("Connection: close");  // the connection will be closed after completion of the response
    Serial.println("Refresh: 5");  // refresh the page automatically every 5 sec

    String sr = "\n";
    sr += "

Basically receiving/sending Ethernet buffers is done through reading/writing into serial. Now open a browser and type in http://192.168.1.147:8080, I see the following:

analog input 0 is 521
analog input 1 is 503
analog input 2 is 498
analog input 3 is 510
analog input 4 is 525
analog input 5 is 548

Pretty cool, isn’t it! 🙂 If I have a SD card connected to Arduino, I can also serve files, such as Javascripts or logging data, from the SD card.

Blinking LEDs. By analyzing the received buffer, I can also implement various HTTP GET commands. For example, the program below sets the LED blinking speed by using http://192.168.1.147:8080/blink?f=5 where f sets the frequency.

void setup() {
  Serial.begin(57600);
  pinMode(13, OUTPUT);
}

int f = 0, pos;
void loop() {
  boolean has_request = false;
  String in = "";
  if (Serial.available()) {
    in = "";
    while (true) {  // should add time out here
      while (Serial.available() == false) {}
      in += (char)(Serial.read());
      if (in.endsWith("\r\n\r\n")) {
        has_request = true;  break;
      }
    }
  }
  if (has_request) {
    int i1 = in.indexOf("GET /blink?f="), i2;
    if (i1 != -1) {
      i2 = in.indexOf(" ", i1+13);
      f = in.substring(i1+13, i2).toInt();
    }
    Serial.println("HTTP/1.1 200 OK\nContent-Type: text/html\nConnection: close");
    String sr = "\n";
    sr += "\n";
    sr += "LED frequency: ";
    sr += f;
    sr += "Hz.";
    Serial.print("Content-Length: ");
    Serial.print(sr.length());
    Serial.print("\r\n\r\n");
    Serial.print(sr);
    has_request = false;
  }
  if (f>0) {
    static unsigned long t = millis();
    if (millis() > t + 1000/f) {
      digitalWrite(13, 1-digitalRead(13));
      t = millis();
    }
  }
}

More on Changing Settings. Instead of using the WiFi module’s homepage to configure settings, you can also change settings by sending serial commands to the module directly. This can be done by pulling the RST pin on the module low for a couple of seconds, which switches the chip in AT command mode. Then you can send (through serial) any of the listed AT commands to change parameters such as SSID and password. This is a nice way to add some automation to the configuration process. Details can be found in the module’s datasheet.

Drawbacks. The main drawback of a Serial-to-WiFi module is that the data transfer speed is quite low. I tested transferring a file stored on SD card and am only getting 5 to 7 KB/s. This means to transfer a 1MB file it will take 3 minutes. Clearly not a good solution for bulk data. Another drawback is that if you need to restart the WiFi module it generally takes 35 to 50 seconds to boot up, that can be an issue for the impatient. Fortunately you likely don’t need to reboot the WiFi module frequently.

Overall the HLK-RM04 Serial-to-WiFi module is a reasonable and low-cost solution for adding WiFi to Arduino and similar microcontroller boards. At the price of $10, there is no much more you can ask for 🙂