© Peter Hoddie and Lizzie Prader 2020
P. Hoddie, L. PraderIoT Development for ESP32 and ESP8266 with JavaScripthttps://doi.org/10.1007/978-1-4842-5070-9_1

1. Getting Started

Peter Hoddie1  and Lizzie Prader1
(1)
Menlo Park, CA, USA
 

This chapter takes you through gathering all the hardware and software required for this book and running your first JavaScript application on a microcontroller. Along the way, the chapter also shows how to use the helpful features of xsbug, the JavaScript source-level debugger.

Installing all the software tools and setting up your development environment takes a little time, but once you can run one example you’ll be ready to run any example in this book. You’ll also have everything you need to begin writing your own applications using the Moddable SDK.

Hardware Requirements

The majority of the examples in this book require very little hardware, but you at least need the following:
  • A computer with a USB port (macOS Sierra version 10.12 or later, Windows 7 Pro SP1 or later, or Linux)

  • A Micro USB cable (high-speed, data sync–capable)

  • An ESP32 NodeMCU module or ESP8266 NodeMCU module

Note

All the examples run on the ESP32 or the ESP8266, with the exception that the examples using Bluetooth Low Energy (BLE), as discussed in Chapter 4, run only on the ESP32, because the ESP8266 doesn’t support BLE. If you’re interested in experimenting with the BLE examples in this book, you’ll need to use an ESP32.

The examples were tested with the ESP32 and ESP8266 modules , shown in Figure 1-1.
../images/474664_1_En_1_Chapter/474664_1_En_1_Fig1_HTML.jpg
Figure 1-1

ESP32 (left) and ESP8266 (right)

The examples using sensors and actuators (Chapters 6 and 7) require a few additional components:
  • Tactile button

  • Tri-color LED (common anode)

  • Three 330 Ohm resistors

  • Micro servo

  • TMP36 temperature sensor

  • TMP102 temperature sensor

  • Mini metal speaker (8 Ohm, 0.5W)

  • Jumper wires

These hardware components are shown in Figure 1-2. More information on where you can purchase them is provided in the chapters where they’re discussed.
../images/474664_1_En_1_Chapter/474664_1_En_1_Fig2_HTML.jpg
Figure 1-2

Hardware components for Chapters 6 and 7

The examples that use the Poco renderer (Chapter 9) or the Piu user interface framework (Chapter 10) can be run on the hardware simulator on your computer, but it’s highly recommended that you use an actual display and run them on your ESP32 or ESP8266. If you’re comfortable wiring together components on a breadboard, here’s what you need:
  • An ILI9341 QVGA touch display (Figure 1-3), which is available on eBay and elsewhere online; search for “spi display 2.4 touch” and you should find several inexpensive options. Note that although this display works well, there are many other choices. The Moddable SDK includes built-in support for several other displays of varying cost and quality; see the documentation/displays directory of the Moddable SDK for more information.

  • A breadboard.

  • Male-to-female jumper wires.

../images/474664_1_En_1_Chapter/474664_1_En_1_Fig3_HTML.jpg
Figure 1-3

ILI9341 QVGA touch display

If you’d rather not do the wiring yourself, you can purchase a Moddable One or Moddable Two from the Moddable website. Moddable One is an ESP8266 wired to a capacitive touch screen; Moddable Two is an ESP32 wired to the same touch screen. Both come as ready-to-use development kits in a compact form factor. Figure 1-4 shows a Moddable One.
../images/474664_1_En_1_Chapter/474664_1_En_1_Fig4_HTML.jpg
Figure 1-4

Moddable One

The Moddable SDK also supports ESP32-based development kits with built-in screens. A popular choice is the M5Stack FIRE, shown in Figure 1-5. See the Moddable SDK repository on GitHub for more information about supported development kits.
../images/474664_1_En_1_Chapter/474664_1_En_1_Fig5_HTML.jpg
Figure 1-5

M5Stack FIRE

Software Requirements

You need the following software:
  • Code editor

  • Example code files

  • Moddable SDK

  • Build tools for the ESP32 and/or ESP8266

You can choose whichever code editor you prefer. There are many JavaScript-friendly editors, including Visual Studio Code, Sublime Text 3, and Atom.

The next sections explain how to download the example code files and set up the Moddable SDK and build tools for your device.

Downloading the Example Code

All the examples are available at https://github.com/Moddable-OpenSource/iot-product-dev-book. You can download the example code using the git command line tool.

Note

In this book, commands that you enter on the command line are preceded by a > symbol. This symbol is not part of the command; it’s included only to clarify where each separate command begins.

On macOS/Linux, use the terminal:
> cd ~/Projects
> git clone https://github.com/Moddable-OpenSource/    iot-product-dev-book
On Windows, use the Command Prompt (changing <username> to your username):
> cd C:Users<username>Projects
> git clone https://github.com/Moddable-OpenSource/    iot-product-dev-book

You also need to set the EXAMPLES environment variable to point at your local copy of the examples repository, as follows:

  • On macOS/Linux:
    > export EXAMPLES=~/Projects/iot-product-dev-book
  • On Windows:
    > set EXAMPLES=C:Users<username>Projects    iot-product-dev-book

Setting Up Your Build Environment

Before building and running the examples, follow the instructions in the “Moddable SDK – Getting Started” document in the documentation directory of the Moddable SDK. This document provides step-by-step instructions for installing, configuring, and building the Moddable SDK for macOS, Linux, and Windows, as well as instructions for installing tools you need in order to work with the ESP32 and ESP8266.

Using xsbug

The xsbug debugger provides source-level debugging of JavaScript code running on the XS JavaScript engine. It connects to devices via USB and has a graphical user interface (shown in Figure 1-6) to make it easy to use.
../images/474664_1_En_1_Chapter/474664_1_En_1_Fig6_HTML.jpg
Figure 1-6

xsbug debugger

Similar to other debuggers, xsbug supports setting breakpoints and browsing source code, the call stack, and variables. It also provides real-time instrumentation to track memory usage and to profile application and resource consumption.

When you’re developing for a microcontroller, the build system automatically opens xsbug before launching your application on the target device.

When developing for the desktop simulator, you need to open xsbug yourself, by either double-clicking its application icon or opening it from the command line as follows:
  • On macOS:
    > open $MODDABLE/build/bin/mac/release/xsbug.app
  • On Windows/Linux:
    > xsbug

Important Features for Examples in This Book

This book doesn’t refer to xsbug often because the examples have already been debugged. However, xsbug is an invaluable tool as you create your own applications. The most important xsbug features used in this book are as follows:
  • Machine tabs – Each XS virtual machine connected to xsbug gets its own tab in the upper left of the window (as highlighted by the dashed border in Figure 1-7). Clicking a tab changes the left pane to the machine tab view, where you can view instrumentation, use control buttons, and more.

  • Control buttons – These graphically labeled buttons (highlighted by the dotted border in the figure) at the top of the machine tab view control the virtual machine. From left to right, they are Kill, Break, Run, Step, Step In, and Step Out.

  • Console – It’s often useful to be able to view diagnostic messages during execution of an application. The trace function writes messages to the debug console in the bottom right of xsbug.

../images/474664_1_En_1_Chapter/474664_1_En_1_Fig7_HTML.jpg
Figure 1-7

xsbug machine tabs and control buttons

See the xsbug document in the documentation/xs directory of the Moddable SDK for full documentation of all the features of xsbug.

Running Examples

The examples in the repository for this book are organized by chapter, each one having several examples. To make it faster to build and launch the examples, each chapter has its own host, which contains the software environment needed to run the examples for that chapter; the host is the collection of JavaScript modules, configuration variables, and other software available for your application to use. Because space is very limited in microcontrollers, it isn’t possible to have a single host that contains all the modules used in the examples in this book.

You can think of the host as essentially the base application. The web browser is the host when you run JavaScript in a web browser; Node.js is the host when you run JavaScript on a web server.

Installing the host separately, rather than installing the host and example together, significantly speeds up development, by minimizing the amount of software that needs to be downloaded. Installing a host on your device typically takes between 30 and 90 seconds. Once that’s done, you can install most examples in just a few seconds, because the host already contains the device firmware and JavaScript modules required by the examples.

The next sections walk you through the entire process of installing a host and then an example, starting with helloworld. Note that in the context of this book, installing an application causes the application to then run on the device.

Installing the Host

The first step is to flash the device to install the host. The source code for each chapter’s host is available to read in the host directory , if you’re curious. To use the host, all you really need to know is that it includes all the modules necessary for the corresponding examples.

You use the mcconfig command line tool to flash the device.

mcconfig

The mcconfig command line tool builds and installs applications on microcontrollers or in the simulator. The commands to use to install this chapter’s host on each supported platform are provided here.

On the ESP32, use these commands:
  • On macOS/Linux:
    > cd $EXAMPLES/ch1-gettingstarted/host
    > mcconfig -d -m -p esp32
  • On Windows:
    > cd %EXAMPLES%ch1-gettingstartedhost
    > mcconfig -d -m -p esp32
On the ESP8266, use these commands:
  • On macOS/Linux:
    > cd $EXAMPLES/ch1-gettingstarted/host
    > mcconfig -d -m -p esp
  • On Windows:
    > cd %EXAMPLES%ch1-gettingstartedhost
    > mcconfig -d -m -p esp

Confirming the Host Was Installed

Once the host is installed, it writes the message shown in Figure 1-8 to the debug console.
../images/474664_1_En_1_Chapter/474664_1_En_1_Fig8_HTML.jpg
Figure 1-8

Message from host in xsbug

Installing helloworld

The helloworld example consists of just three lines of JavaScript:
debugger;
let message = "Hello, World";
trace(message + " ");
This example uses two important features:
  • The debugger statement, which halts execution and breaks into xsbug.

  • The trace function, which writes messages to the debug console. Note that trace doesn’t automatically add a newline character ( ) at the end of the message. This enables you to use several trace statements to generate the output of a single line. Be sure to include the newline character at the end of the line so that the text displays properly in xsbug.

You use mcrun to install examples.

mcrun

The mcrun command line tool builds and installs additional JavaScript modules and resources that change the behavior or appearance of Moddable applications on microcontrollers or in the simulator. Both mcconfig and mcrun build scripts and resources. Unlike mcrun, mcconfig also builds native code. In JavaScript terms, mcconfig builds the host.

After you install an example using mcrun, the device reboots. This relaunches the host, which in turn runs the example you installed.

Use the following commands to install the helloworld example. Make sure you change <platform> to the correct platform for your device, either esp32 or esp.
  • On macOS/Linux:
    > cd $EXAMPLES/ch1-gettingstarted/helloworld
    > mcrun -d -m -p <platform>
  • On Windows:
    > cd %EXAMPLES%ch1-gettingstartedhelloworld
    > mcrun -d -m -p <platform>

Finishing Up

You should immediately break into xsbug once the application is installed. Click the Run button to see the message Hello, World written to the debug console, as shown in Figure 1-9.
../images/474664_1_En_1_Chapter/474664_1_En_1_Fig9_HTML.jpg
Figure 1-9

Hello, World written to console in xsbug

If everything goes well, you can move on to the “Conclusion” section of this chapter if you’re working with a bare NodeMCU board. If you want to add a display—which is highly recommended—continue with the “Adding a Display” section instead.

If you ran into issues, see the next section.

Troubleshooting

When you’re trying to install an application, you may experience roadblocks in the form of errors or warnings; this section explains some common issues and how to solve them.

Device Not Connected/Recognized

The error message
error: cannot access /dev/cu.SLAB_USBtoUART
means that the device is not connected to your computer or the computer doesn’t recognize the device. There are a few reasons this can happen:
  • Your device is not plugged into your computer. Make sure it’s plugged in when you run the build commands.

  • You have a USB cable that is power only. Make sure you’re using a data sync–capable USB cable.

  • The computer does not recognize your device. To fix this problem, see the instructions that follow for your operating system.

macOS/Linux

To test whether your computer recognizes your device, unplug the device and enter the following command:
> ls /dev/cu*

Then plug in the device and repeat the same command. If nothing new appears, the device isn’t being seen. Make sure you have the correct VCP driver installed.

If it is seen, you now have the device name and you need to edit the UPLOAD_PORT environment variable. Enter the following command, replacing /dev/cu.SLAB_USBtoUART with the name of the device on your system:
> export UPLOAD_PORT=/dev/cu.SLAB_USBtoUART

Windows

Check the list of USB devices in Device Manager. If your device shows up as an unknown device, make sure you have the correct VCP driver installed.

If your device shows up on a COM port other than COM3, you need to edit the UPLOAD_PORT environment variable. Enter the following command, replacing COM3 with the appropriate device COM port for your system:
> set UPLOAD_PORT=COM3

Incompatible Baud Rate

The following warning message is normal and is no cause for concern:
warning: serialport_set_baudrate: baud rate 921600 may not work
However, sometimes the upload starts but does not complete. You can tell an upload is complete after the progress bar traced to the console goes to 100%. For example:
...................................................... [ 16% ]
...................................................... [ 33% ]
...................................................... [ 49% ]
...................................................... [ 66% ]
...................................................... [ 82% ]
...................................................... [ 99% ]
..                                                    [ 100% ]
There are a few reasons the upload may fail partway through:
  • You have a faulty USB cable.

  • You have a USB cable that does not support higher baud rates.

  • You’re using a board that requires a lower baud rate than the default baud rate that the Moddable SDK uses.

To solve the last two problems, you can change to a slower baud rate as follows:
  1. 1.

    If you’re working with an ESP32, open moddable/tools/mcconfig/make.esp32.mk; if an ESP8266, open moddable/tools/mcconfig/make.esp.mk.

     
  2. 2.

    Find this line, which sets the upload speed to 921,600:

     
UPLOAD_SPEED ?= 921600
  1. 3.

    Set the speed to a smaller number. For example:

     
UPLOAD_SPEED ?= 115200

Device Not in Bootloader Mode

This issue is not uncommon if you’re using certain ESP32-based boards. Status messages stop being traced briefly when you attempt to flash the device, and after several seconds you receive this error message:
A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header
If the device is not in bootloader mode, you cannot flash the device. If you’re using a NodeMCU module, follow these steps every time you flash :
  1. 1.

    Unplug the device.

     
  2. 2.

    Hold down the BOOT button (circled in Figure 1-10).

     
  3. 3.

    Plug the device into your computer.

     
  4. 4.

    Enter the mcconfig command.

     
  5. 5.

    Wait a few seconds and release the BOOT button.

     
../images/474664_1_En_1_Chapter/474664_1_En_1_Fig10_HTML.jpg
Figure 1-10

BOOT button on ESP32

Adding a Display

Although most of the examples in this book don’t require a display, adding a display to the ESP32 or ESP8266 greatly improves the user experience. It enables you to do the following:
  • Show more information than a few blinking lights

  • Create modern user interfaces

  • Add functionality

The examples in this book are designed for a display with 240 x 320 or 320 x 240 resolution (for example, QVGA). These displays are typically between 2.2" and 3.5" in size and were common in early smart phones. Displays of other sizes may be connected to these microcontrollers, but this size is a good match for the capabilities of these microcontrollers.

The following sections show how to connect the ILI9341 QVGA touch display to the ESP32 or ESP8266. If you’re using a development board like the Moddable One or Moddable Two, you can skip to the “Installing helloworld-gui” section.

Connecting a Display to the ESP32

Table 1-1 and Figure 1-11 show how to connect a display to the ESP32.
Table 1-1

Wiring to connect display to ESP32

ILI9341 Display

ESP32

SDO/MISO

GPIO12

LED

3.3V

SCK

GPIO14

SDI/MOSI

GPIO13

CS

GPIO15

DC

GPIO2

RESET

3.3V

GND

GND

VCC

3.3V

T_DO

GPIO12

T_DIn

GPIO13

T_CLK

GPIO14

T_IRQ

GPIO23

T_CS

GPIO18

../images/474664_1_En_1_Chapter/474664_1_En_1_Fig11_HTML.jpg
Figure 1-11

Wiring diagram for connecting display to ESP32

Connecting a Display to the ESP8266

Table 1-2 and Figure 1-12 show how to connect a display to the ESP8266.
Table 1-2

Wiring to connect display to ESP8266

ILI9341 Display

ESP8266

SDO/MISO

GPIO12

LED

3.3V

SCK

GPIO14

SDI/MOSI

GPIO13

CS

GPIO15

DC

GPIO2

RESET

3.3V

GND

GND

VCC

3.3V

T_DO

GPIO12

T_DIn

GPIO13

T_CLK

GPIO14

T_IRQ

GPIO16

T_CS

GPIO0

../images/474664_1_En_1_Chapter/474664_1_En_1_Fig12_HTML.jpg
Figure 1-12

Wiring diagram for connecting display to ESP8266

Installing helloworld-gui

The helloworld-gui example is a version of helloworld that displays text on the screen. If you wired a display to a device yourself, reflashing the device with the helloworld-gui application is a good way to test whether the wiring is correct.

The commands to use are very similar to the ones used to install helloworld. The only difference is the platform identifier. The platform identifier tells the build system to include the proper display and touch drivers. If you’re using a Moddable One, the platform identifier is esp/moddable_one; for a Moddable Two, it’s esp32/moddable_two. If you added a display according to the instructions in the preceding sections, the platform identifier is either esp32/moddable_zero or esp/moddable_zero.

Use the following commands to install the helloworld-gui example. Make sure you change <platform> to the correct platform for your device.
  • On macOS/Linux:
    > cd $EXAMPLES/ch1-gettingstarted/helloworld-gui
    > mcconfig -d -m -p <platform>
  • On Windows:
    > cd %EXAMPLES%ch1-gettingstartedhelloworld-gui
    > mcconfig -d -m -p <platform>
If you specify the correct platform and your wiring is correct, you will see the screen shown in Figure 1-13.
../images/474664_1_En_1_Chapter/474664_1_En_1_Fig13_HTML.jpg
Figure 1-13

Graphical helloworld application

Conclusion

Now that your development environment is set up and you’re familiar with the process of installing this chapter’s examples on your device, you’re ready to try some more examples!

At this point, you have all the materials and skills you need to follow along with Chapters 2 through 10. These chapters are independent of each other, so you can read them in any order. As you start working with the examples in a chapter, be sure to install that chapter’s host or you’ll encounter errors when launching the examples. Once you feel comfortable with the APIs of the Moddable SDK, you can move on to Chapter 11, which covers more advanced topics.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.174.174