Microcontroller projects

Wemos tips and tricks

last updated: 14/02/18

Configure Arduino

Copy the following line:

https://github.com/esp8266/Arduino/releases/download/2.4.0/package_esp8266com_index.json

to "File > Preferences > Additional Boards Manager URLs:".

Go to "Tools > Board:".." > Boards Manager..." and scroll down. Then click install.

Serial

Hardware serial (opened with Serial.begin(9600)) on Wemos D1 mini pro uses UART0 of ESP8266, which is mapped to pins TX (GPIO1) and RX (GPIO3).

Serial1 (opened with Serial1.begin(9600)) uses UART1 which is a transmit-only UART. UART1 TX pin is D4 (GPIO2, LED!!).

If you use serial (UART0) to communicate with hardware, you can't use the Arduino Serial Monitor at the same time to debug your program! The best way to debug is to use Serial1.println() and connect RX of an USB2Serial adapter (FTDI, Profilic, CP210, ch340/341) to D4 and use a terminal program like CuteCom or CleverTerm to listen to D4.

wemos<em>hw</em>serial

Serial (UART0) may be remapped to D8 (TX, GPIO15, 10k Pull-down?) and D7 (RX,GPIO13) by calling Serial.swap(); after Serial.begin();. Calling swap again maps UART0 back to TX and RX.

SPIFFS

The SPI Flash Filing System is designed for SPI flash devices on micro-controller systems with little RAM. It uses statically sized ram buffers (independent of number of files), posix-like API (open, close, read, write, ...) and implements static wear leveling to prolong the life of system flash.

SPIFFS uses a flat structure (no directories). Creating a file with path homie/config.json will create a file called homie/config.json instead of a config.json under directory homie. Write operation might be slow, so it is best used at start up, shut down or when time critical activities are not scheduled.

Here some info on the Wemos boards. Arduino lets you chose the amount of SPIFFS for some boards (newest Arduino and ESP8266 core (2.4!)).

Wemos D1 mini lite v1.0.0 1M bytes (0-512kB SPIFFS)
Wemos D1 mini v3.0 4MB FLASH (1MB or 3MB SPIFFs
Wemos D1 mini pro v1.1.0 16MB FLASH (15MB SPIFFS)
Wemos LOLIN32 Pro v1.0.0 4MB FLASH & 4MB PSRAM
Wemos LOLIN32 lite v1.0.0 4MB FLASH
Wemos LOLIN32 v1.0.0 4MB FLASH

Flash layout

Programming a new sketch will not modify the file system contents. This allows to use file system to store sketch data, configuration files, or content for Web server.

The following diagram illustrates flash layout used in Arduino environment:

|--------------|-------|---------------|--|--|--|--|--|
^              ^       ^               ^     ^
Sketch    OTA update   File system   EEPROM  WiFi config (SDK)

Test size of FLASH

The following example sketch CheckFlashConfig.ino can be found in your core examples (File-Examples-ESP8266-CheckFlashConfig). You see the size in your serial monitor.

Arduino ESP8266 filesystem uploader

Download the plugin from here and unzip the file. Create a folder ESP8266FS in the Arduino tools directory and in this folder a new folder tool Copy the esp8266fs.jar file into the tool folder (Arduino\tools\ESP8266FS\tool\esp8266fs.jar). After a restart you find a menu item ESP8266_Sketch_Data_Upload in the Arduino Tools menu.

Go to sketch directory Sketch > Show Sketch Folder and create a directory named data. Load all your files you want to be in SPIFFS to that folder. After this make sure you have selected your board, port, and closed the serial monitor.

Select Tools > ESP8266 Sketch Data Upload. This starts uploading the files into ESP8266 flash file system (be patient; its done when the IDE status bar displays SPIFFS Image Uploaded).

Test SPIFFS

Here is a code snippet lend from Steve Quinn to read a SPIFFS file (first load up your file (e.g. /homie/config.json) to SPIFFS as described below):

#include <string.h>
#include "FS.h"

bool    spiffsActive = false;
#define TESTFILE "/homie/config.json"

void setup() {
  Serial.begin(115200);
  delay(1000);
  if (SPIFFS.begin()) {    // Start filing subsystem
      Serial.println("SPIFFS Active");
      spiffsActive = true;
  } else {
      Serial.println("Unable to activate SPIFFS");
  }
  delay(1000);
}

void loop() {
  if (spiffsActive) {
    if (SPIFFS.exists(TESTFILE)) {
      File f = SPIFFS.open(TESTFILE, "r");
      if (!f) {
        Serial.print("Unable to open ");
        Serial.print(TESTFILE);        
      } else {
        String s;
        Serial.print("Contents of file ");
        Serial.println(TESTFILE);
        while (f.position()<f.size()) {
          s=f.readStringUntil('\n');
          s.trim();
          Serial.println(s);
        }
        f.close();
      }
      Serial.println();
    } else {
      Serial.print("Unable to find ");
      Serial.println(TESTFILE);
    }
  }
  while (true){
    yield();
  }
}

More infos here http://www.instructables.com/id/Using-ESP8266-SPIFFS/ and here http://esp8266.github.io/Arduino/versions/2.3.0/doc/filesystem.html

External Antenna on Wemos D1 mini pro

To connect an external antenna you have to unsolder the 0Ω resistance near the ceramic antenna and resolder it direction the miniature RF connector (U.FL).

Link to video: