Taillieu.Info

More Than a Hobby..

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /customers/3/2/5/taillieu.info/httpd.www/templates/taillieuinfo_joomla3_rev1/functions.php on line 194

ESP8266 Software

Installation of the required software

The first step is to download and install the Arduino IDE. I explained this in A Beginner's Guide to Arduino. (As of February 7th 2017, the latest stable version of the IDE is 1.8.1.)
 
To program the ESP8266, you'll need a plugin for the Arduino IDE, it can be downloaded from GitHub manually, but it is easier to just add the URL in the Arduino IDE:
  1. Open the Arduino IDE.
  2. Go to File > Preferences.
  3. Paste the URL http://arduino.esp8266.com/stable/package_esp8266com_index.json into the Additional Board Manager URLs field.
    (You can add multiple URLs, separating them with commas.)
  4. Go to Tools > Board > Board Manager and search for 'esp8266'. Select the newest version, and click install. (As of February 7th 2017, the latest stable version is 2.3.0.)
You can check out the official install guide here.

Drivers

If you are using a board with the CH340(G) USB-to-Serial chip, like the NodeMCU, you'll probably have to install the USB drivers for it.
They can be found on GitHub
If you are using a board with the CP2104 USB-to-Serial chip, like the Adafruit Feather HUZZAH board, you'll probably have to install USB drivers as well. You can find them on the Silicon Labs website.
Boards with an FTDI chip should work right out of the box, without the need of installing any drivers.

Examples

You can find all examples used in this article on my GitHub. Just download it as a .ZIP file, unzip it to a convenient location, and you're good to go!

ESP8266 Hardware

Deciding on what board to buy

ESP8266 is just the name of the chip, many companies have designed their own boards that use this chip, so there are many different ESP8266 boards on the market. If you don't know the difference between all these different models, you might have a hard time deciding on what board to buy.
 
The easiest (and fastest) way to get an ESP8266 board is to buy one from a well-known electronics shop like Adafruit or SparkFun, but if you want it cheap, you should check out Ebay or other sites where you can order them directly from China.

Development boards

Some boards have all kinds of features on-board to help developing ESP8266 hardware and software: for example, a USB to Serial converter for programming, a 3.3V regulator for power, on-board LEDs for debugging, a voltage divider to scale the analog input ...
If you're a beginner, I would definitely recommend a development board. It's easier to get started if you don't have to worry about all these things.

Bare-bones AI Thinker boards

If you want to add an ESP8266 to a small project, or if you want a cheaper* board, you might want to buy a board that doesn't have these features. In that case, you can buy one of the many ESP-## modules developed by AI Thinker. They contain just the ESP8266 and the necessary components to run it. 
To program the board, you'll need an external USB-to-Serial converter.
With some modules, you get an on-board antenna (PCB or ceramic) and an LED, some boards have just an antenna connector, or no LEDs at all. They also differ in physical size, and flash memory size. An important thing to notice, is that some boards do not break out all GPIO pins. For example, the ESP-01 only has 2 I/O pins available (apart from the TX and RX pins), while other modules like the ESP-07 or ESP-12 break out all available I/O pins.
 
(*) The board itself is cheaper, but you'll have to spend more on external parts.

Overview

Here's a table with some of the most popular ESP8266 development boards and their features:
 
BoardGPIO3.3V VregUSB-to-SerialAuto-ResetAuto-ProgramFlashADC rangeExtra
SparkFun ESP8266 Thing 11 + - + ±* 512KB (4Mb) 0-1V Battery charger, crypto element, temperature sensor, light sensor
SparkFun ESP8266 Thing - Dev Board 11 + + + + 512KB (4Mb) 0-1V  
Node MCU 11 + + + + 4MB (32Mb) 0-3.3V  
Adafruit Feather HUZZAH with ESP8266 11 + + + + 4MB (32Mb) 0-1V Battery charger
Adafruit HUZZAH ESP8266 Breakout 11 + - - - 4MB (32Mb) 0-1V 5V-tolerant RX and Reset pins
ESP-## 4 - 11 - - - - 512KB (4Mb) – 4MB (32Mb) 0-1V Small and cheap
You can find the full list of ESP-## modules here.
 
As you can see, both the Node MCU and the Adafruit Feather HUZZAH are solid choices.
 
(*) When auto-program on the SparkFun ESP8266 Thing is enabled, you can't use the Serial Monitor.

Getting the hardware ready

There are two main categories of ESP8266 boards: development boards with a USB interface (USB-to-Serial convertor) on-board, and boards without a USB connection.

Development boards with a USB interface

For example: NodeMCU, SparkFun ESP8266 Thing - Dev Board, SparkFun Blynk Board, Adafruit Feather HUZZAH with ESP8266 Wi-Fi ...
 
These boards will show up in Device manager (Windows) or in lsusb (Linux) as soon as you plug them in.
They have a 3.3V regulator on-board, and can be programmed over USB directly, so you don't need any external components to get it working.
The only thing you may need to do, is solder on some headers.

Bare-bones boards and boards without a USB interface

This category has 2 sub-categories: boards with a 3.3V regulator on-board, and boards with just the ESP8266 and a flash memory chip, without 3.3V regulator. If your board doesn't have a 5V to 3.3V regulator, buy one separately. You could use an LM1117-3.3 for example. The on-board 3.3V regulator of most Arduino boards is not powerful enough to power the ESP.

To program the board, you'll need a USB-to-Serial converter. The FTDI FT232RL is quite popular, because it can switch between 5V and 3.3V. It is essential that the USB-to-Serial converter you buy operates at 3.3V. If you buy a 5V model, you will damage the ESP8266.

Connecting the USB-to-Serial converter

  1. Connect the ground (GND) of the USB-to-Serial converter to the ground of the ESP8266.
  2. Connect the RX-pin of the USB-to-Serial converter to the TXD pin of the ESP8266. (On some boards, it's labelled TX instead of TXD, but it's the same pin.)
  3. Connect the TX-pin of the USB-to-Serial converter to the RXD pin of the ESP8266. (On some boards, it's labelled RX instead of RXD, but it's the same pin.)
  4. If your ESP8266 board has a DTR pin, connect it to the DTR pin of the USB-to-Serial converter. This enables auto-reset when uploading a sketch, more on that later. 

Enabling the chip

If you're using a bare-bone ESP-## board by AI Thinker, you have to add some resistors to turn on the ESP8266, and to select the right boot mode. 
  1. Enable the chip by connecting the CH_PD (Chip Power Down, sometimes labeled CH_EN or chip enable) pin to VCC through a 10KΩ resistor.
  2. Disable SD-card boot by connecting GPIO15 to ground through a 10KΩ resistor.
  3. Select normal boot mode by connecting GPIO0 to VCC through a 10KΩ resistor.
  4. Prevent random resets by connecting the RST (reset) pin to VCC through a 10KΩ resistor.
  5. Make sure you don't have anything connected to GPIO2 (more information in the next chapter).

Adding reset and program buttons

If your ESP8266 board doesn't have a reset button, you could add one by connecting a push button to between the RST pin and ground.
 
To put the chip into programming mode, you have to pull GPIO0 low during startup. That's why we also need a program button. Because it's possible to use GPIO0 as an output, we can't directly short it to ground, that could damage the chip. To prevent this, connect 470Ω resistor in series with the switch. It's important that this resistance is low enough, otherwise, it will be pulled high by the 10KΩ resistor we added in the previous paragraph.

Connecting the power supply

If the ESP8266 module you have doesn't have a 3.3V voltage regulator on board, you have to add one externally. You could use an LM1117-3.3 for example.
  1. Connect the first pin of the regulator to ground.
  2. Place a 10µF capacitor between pin 2 (Vout) and ground. Watch the polarity!
  3. Place a 10µF capacitor between pin 3 (Vin) and ground.
  4. Connect pin 2 to the 3.3V or VCCof the ESP8266.
  5. Connect pin 3 to a 5V power source, a USB port, for example.
 

VReg.pngESP8266-2.png

Before you begin ...

There's a few things you have to look out for when using an ESP8266: The most important thing is that it runs at 3.3V, so if you connect it to a 5V power supply, you'll kill it. Unlike some 3.3V Arduino or Teensy boards, the ESP8266's I/O pins are not 5V tolerant, so if you use a 5V USB-to-Serial converter, or 5V sensors etc. you'll blow it up. 
A second thing to keep in mind is that the ESP8266 can only source or sink 12mA per output pin, compared to 20-40mA for most Arduinos.
The ESP8266 has one analog to digital converter, but it has a strange voltage range: 0 - 1V, voltages above 1V might damage the board.
 
One last thing to keep in mind is that the ESP8266 has to share the system resources and CPU time between your sketch and the Wi-Fi driver. Also, features like PWM, interrupts or I²C are emulated in software, most Arduinos on the other hand, have dedicated hardware parts for these tasks.
For most applications however, this is not too much of an issue.

A Beginner's Guide to the ESP8266

Some time ago, I wrote a Beginner's Guide to Arduino that seems to be very popular, so I decided to create a follow-up: A Beginner's Guide to the ESP8266. That's right, a tutorial on how to use the world's most popular $3 Wi-Fi board.
 
This is going to be a very in-depth tutorial, covering some networking concepts as well. If you're a beginner, and just want to go straight to the more exciting Wi-Fi part, feel free to do so, I included short TL;DR's in the longer, more technical parts.
 
A short overview of what I'll cover in this article: 
  1. What is an ESP8266? A short overview of what an ESP8266 is, and what you can do with it
  2. Deciding on what board to buy: There's loads of different ESP8266 available these days, finding the one that's best for you can be hard
  3. Installing the software: you need to install some software to program the ESP8266, and maybe a USB driver
  4. Setting up the hardware: some modules and boards need some external components
  5. The ESP8266 as a microcontroller: the ESP8266 can be used as a normal microcontroller, just like an Arduino
  6. Network protocols: Before we start using the Wi-Fi capabilities of the ESP8266, I'll teach you some of the network protocols involved
  7. Setting up a Wi-Fi connection: That's probably why you're reading this, right?
  8. Name resolution: Find the ESP8266 on your local network using mDNS
  9. Setting up a simple web server: This enables you to add web pages to the ESP8266, and browse them from your computer or phone
  10. Setting up an advanced web server: a more advanced server with a real file system that allows you to upload new files over Wi-Fi
  11. OTA - uploading programs over Wi-Fi: You don't have to upload programs over USB, you can use Wi-Fi instead
  12. Wirelessly controlling your RGB lighting: Change the color of your LED strips using your phone or computer
  13. Getting the time: Connect to a time server using NTP and sync the ESP's clock
  14. Monitoring sensors: log the temperature in your living room, save it in flash memory and show it in a fancy graph in your browser
  15. Getting email notifications: Turn on a notification light when you've got unread emails
  16. Advanced features: use DNS, captive portals, Wi-Fi connector libraries, OSC ...
 
This guide expects some basic knowledge of microcontrollers like the Arduino. If that's something you're not already familiar with, I'd recommend you to read my Beginner's Guide to Arduino first, it covers a lot of the basics that I won't go into in this article.
I really want to focus on the ESP8266-specific things, like Wi-Fi and other network protocols, the ESP's hardware, software, IoT, etc ...

What is an ESP8266?

The ESP8266 is a System on a Chip (SoC), manufactured by the Chinese company Espressif. It consists of a Tensilica L106 32-bit micro controller unit (MCU) and a Wi-Fi transceiver. It has 11 GPIO pins* (General Purpose Input/Output pins), and an analog input as well. This means that you can program it like any normal Arduino or other microcontroller. And on top of that, you get Wi-Fi communication, so you can use it to connect to your Wi-Fi network, connect to the Internet, host a web server with real web pages, let your smartphone connect to it, etc ... The possibilities are endless! It's no wonder that this chip has become the most popular IOT device available. 
 
There are many different modules available, standalone modules like the ESP-## series by AI Thinker, or complete development boards like the NodeMCU DevKit or the WeMos D1. Different boards may have different pins broken out, have different Wi-Fi antennas, or a different amount of flash memory on board.
 
(*) The ESP8266 chip itself has 17 GPIO pins, but 6 of these pins (6-11) are used for communication with the on-board flash memory chip.

Programming

There are different ways to program the ESP8266, but I'll only cover the method using the Arduino IDE. This is really easy for beginners, and it's a very familiar environment if you've used Arduino boards before. 
Just keep in mind that it's not limited to this option: there's also an official SDK available to program it in real C, this is very useful if you want to optimize your code or do some advanced tricks that aren't supported by the Arduino IDE. Another possibility is to flash it with a LUAinterpreter, so you can upload and run LUA scripts. Or maybe you're more familiar with Python? Then you should check out the MicroPython firmware to interpret MicroPython scripts. I'm sure there's other languages available as well, so just do a quick Google search and write your code in the language of your choice.

Requirements

You'll need a couple of things in order to follow this guide:
  • An ESP8266 board
  • A computer that can run the Arduino IDE (Windows, Mac or Linux)
  • A USB-to-Serial converter, it is very important that you use a 3.3V model*
  • A USB cable
  • A 3.3V power supply or voltage regulator*
  • A Wi-Fi network to connect to
(*) Your board may already include these. More information can be found in the next chapter.

Using ESP8266 SPIFFS

Introduction

Did you know each ESP8266 allows you to partition the system flash such that you can use it to store both code and support a file system?

This filing system can be used to store infrequently changing data such as; web pages, configurations, sensor calibration data etc.

This short Instructable details how to use SPIFFs, configure your Arduino IDE to upload files direct to your ESP8266 and figure out how much flash your device has.

.

What parts do I need?
System entry requirements;

  1. Your Arduino IDE has been set up to programme the ESP8266 (Setting Up the Arduino IDE to Program the ESP8266-01)
  2. An ESP8266-01 programming device (Practical Circuit Construction With Strip Board, Step 9 onwards)
  3. An ESP8266-01

.

What software do I need?

  • Arduino IDE 1.6.9

.

What skills do I need?
To set the system up, use the source code (provided) you will need the following;

  1. Knowledge of Arduino and it's IDE,
  2. Some knowledge of the ESP8266,
  3. Some Patience.

.

Topics covered

  1. So what is SPIFFS,
  2. Installing the Arduino ESP8266 filesystem uploader,
  3. How can I tell how much flash memory my ESP8266 has?
  4. How to upload files to your ESP8266 SPIFFS,
  5. How to test your SPIFFS and see it in action,
  6. Conclusion,
  7. References used.

.

Note 1 : Actually the ESP8266 flash also supports OTA updates, however the scope of this Instructable is limited to that of the SPIFFS functionality.

Note 2 : Whilst the examples given uses the ESP8266-01 it works equally well with the ESP8266-07, ESP8266-12, ESP8266-12E, ESP8266-13, as implied by the Instructable image.

Step 1: So What Is SPIFFS

SPIFFS

Which stands for SPI Flash Filing System was designed for SPI flash devices on constrained embedded microprocessor systems with little RAM.

What SPIFFS does:

  • Specifically designed for low ram usage
  • Uses statically sized ram buffers, independent of number of files
  • Posix-like api: open, close, read, write, seek, stat, etc
  • Implements static wear leveling to prolong the life of system flash

What SPIFFS does not do:

  • SPIFFS does not support directories and uses a flat structure. Creating a file with path tmp/myfile.txt will create a file called tmp/myfile.txt instead of a myfile.txt under directory tmp.
  • It is not a real time stack. One write operation might take much longer than another. So is best used at start up, shut down or when time critical activities are not scheduled.
  • Presently, it does not detect or handle bad blocks.

.

Step 2: Installing the Arduino ESP8266 Filesystem Uploader

To install the Arduino ESP8266 filesystem uploader carry out the following;

  1. Go here and download a copy of 'ESP8266FS-0.2.0.zip,
  2. Unzip the file,
  3. Drop the esp8266fs.jar file into Arduino tool directory (the path will look something like [home_dir]\Arduino\tools\ESP8266FS\tool\esp8266fs.jar),
    • In my case (as in pic 1 above);
    • C:\Users\Win 7\Documents\Arduino\tools\ESP8266FS\tool
    • I needed to create;
    • tools\ESP8266FS\tool\
    • Under;
    • C:\Users\Win 7\Documents\Arduino\
  4. Restart Arduino IDE.

.

If you have got it correct you will see a new menu item 'ESP8266 Sketch Data Upload' under under 'Tools' as in pic 2 above.

Step 3: How Can I Tell How Much Flash Memory My ESP8266 Has?

Given most of these ESP8266 devices are sourced from Chinese suppliers who are less than forthcoming in releasing clear and concise product data, one way to figure out how big your flash is to read the chip details (assuming they haven't been scrubbed) and carrying out an internet search. I did this for two of my ESP8266-01s;

  • BergMicro 25Q80ASSIG 1602 : 8M/bit
  • Winbond 25Q80DVSIG 1626 : 8M/bit

Here the former and latter are 1M byte. The problem with this method of identification is it simply won't work with the ESP8266 variants which house all the active components under an RF shielding can such as the ESP8266-07 and ESP8266-12E etc.

Consequently, I found the most reliable and quickest method was to programme the device with the CheckFlashConfig.ino' sketch and review the output from the serial monitor as in Pic 1 above.

.

.

You can download the full Arduino ESP8266 Core from here;

and the CheckFlashConfig Source from here;

Alternatively, copy given below.

.

 

Step 4: How to Upload Files to Your ESP8266 SPIFFS

Carryout the following to upload your files to the ESP8266 SPIFFS;

  1. Ensure you have a sub-directory within your sketch directory named 'data',
  2. Place the files you wish to upload into the 'data' directory,
  3. From 'Tools' menu, select the correct ESP8266 device and choose the 'Flash Size' with the SPIFFS you require ie. '1M (256K SPIFFS)'. See pics 1 and 2 above,
  4. Ensure the serial monitor dialogue box is closed,
  5. Prepare the ESP8266 device for upload;
    1. Hold down 'Flash',
    2. Pulse 'Reset'
    3. Release 'Flash',
  6. From Tools menu select 'ESP8266 Sketch Data Upload' See pic 3 top of image,
  7. Once upload is complete. Arduino IDE message window will show 100% upload. See pic 3 bottom of image.

.

Note 1 : If an error message such as the one in Pic 4 occurs indicating 'SPIFFS Upload failed!'. Try unplugging/re-plugging your device's USB to serial adapter (FTDI) and repeating steps 1...7.

Note 2 : SPIFFS content can be uploaded independently of programme code.

Note 3 : If 'Flash Size' is changed, then programme code and SPIFFS will need to be reloaded.

.

Step 5: How to Test Your SPIFFS and See It in Action

To see a working example of SPIFFs carryout the following;

  1. Download the 'SPIFFsUsage.ino' sketch below,
  2. Unzip it and drop it into your Arduino sketches directory
    • Mine is here; C:\Users\Win 7\Documents\Arduino\My Progs
    • You will note there is a sub-directory named 'data' within which is a text filenamed 'testfile.txt'
  3. Select the flash geometry of your target system and the code space/SPIFFs size split you require (Tools->Flash Size),
  4. Ensure the serial monitor dialogue box is closed,
  5. Upload the text file to your ESP8266 device as described above in Step 4,
  6. Open the serial monitor dialogue box and set the baud rate to 115200,
  7. Upload 'SPIFFsUsage.ino' to ESP8266 device.

Once you have completed the upload, the code will execute and your serial monitor dialogue will display text as in pic 1 above.

It serves to show the contents of 'testfile.txt';

  1. Has been successfully uploaded,
  2. Located in the file system,
  3. Opened for read,
  4. Read until the end of file has been reached,
  5. Closed,
  6. Opened for append,
  7. A single line written to the file,
  8. The file has been re-read, displaying the recently appended line to the end of the file.

.

Note : If the ESP8266 is powered down or reset, upon each successive execution a new line is automatically appended and displayed.

 

Step 6: Conclusion

Use of SPIFFS is a great way to store infrequently changing data though it should not be considered for applications requiring many write accesses as it will eventually cause flash to fail. A typical worked example of how long flash is likely to last is given here.

Using the on-board filing system could save on I/O count where SPI SD card functionality is being considered for an I/O constrained device such as the ESP8266-01 where it is a very simple way to add secondary store without the need for 4 /I/O lines.

Step 7: References Used

SPIFFS (SPI Flash File System)

SPIFFS WiKi

FAQ

Integrate spiffs

Configure spiffs

Using spiffs

Performance and Optimizing

SPIFFS GIT HUB Home page

Arduino ESP8266 filesystem uploader

Arduino plugin which packs sketch data folder into SPIFFS filesystem image, and uploads the image to ESP8266 flash memory. Tested with the following Arduino IDE versions: 1.6.5-r2, 1.6.6

GITHUB URL

Source

Latest

but works with Arduino IDE 1.6.12

Arduino Forum SPIFFs example

Arduino CORE

Arduino GIT HUB Core

CheckFlashConfig Source

Worked example of flash wear

More detail on SPIFFS

.

WeMos D1 mini pins and diagram

Diagram

WeMos D1 mini

Pin

in Function ESP-8266 Pin
TX TXD TXD
RX RXD RXD
A0 Analog input, max 3.3V input A0
D0 IO GPIO16
D1 IO, SCL GPIO5
D2 IO, SDA GPIO4
D3 IO, 10k Pull-up GPIO0
D4 IO, 10k Pull-up, BUILTIN_LED GPIO2
D5 IO, SCK GPIO14
D6 IO, MISO GPIO12
D7 IO, MOSI GPIO13
D8 IO, 10k Pull-down, SS GPIO15
G Ground GND
5V 5V -
3V3 3.3V 3.3V
RST Reset RST
 

*All IO have interrupt/pwm/I2C/one-wire supported(except D0)

d1 mini shematics

wemos d1 mini shematics

Programming

The D1 mini has a micro USB for auto programming. Also you can programming it using OTA

Warnings

All IO is work at 3.3V.

Source wemos.cc