Showing posts with label MicroPython. Show all posts
Showing posts with label MicroPython. Show all posts

Friday, January 19, 2018

Some IoT Device Platforms

I've been involved in IoT development for about five years. One year ago, I posted the following photo of some IoT device platforms:

Here's what I wrote at that time:

C.H.I.P. - for those who want a desktop interface (which you don't need for IoT devices). Would be better if it used HDMI display instead of composite.

Onion Omega 2+ - just got it, maybe similar to C.H.I.P.

Raspberry Pi 0 - relatively large power demand; missed the boat by not having built-in wifi.

TinyDuino - overpriced; wifi optional/extra (and very expensive).

Intel Edison - small power demand; great hardware and software; overpriced.

RedBear Duo - software is slow to mature; overpriced.

ESP8266 - this is the powerhouse behind the IoT revolution!

ESP32 (prototype) - this is the future.

 

One year later...

C.H.I.P. is now in breadboard-friendly form factor, but at $16.

Omega 2+ is very stable, but is not breadboard-friendly. It has not made it into a lot of commercial products.

There is now the Pi Zero W, which has wifi and BLE, and still $5!

TinyDuino and RedBear Duo haven't changed much.

Intel Edison is no longer in production.

ESP8266 is still going strong and can be found in more and more places.

ESP32 is now commonly available - the future is here!

 

There are other IoT device platforms, like the BeagleBone, the Particle Photon and Electron, and the various boards from Pycom. We also have new transport mediums, too, like LoRa and SigFox. From a hardware standpoint, IoT developers have a banquet of choices!

Things are good on the software side, too. Most of these boards can be programmed in multiple languages. Over the last five years, whole IoT ecosystems (middleware, backend, and "analytics pipeline") have evolved! It is a great time to be involved in IoT development!

Sunday, November 5, 2017

MicroPython on the ESP8266 - Part 2

In this entry we access MicroPython on the ESP8266 using WebREPL instead of serial REPL. WebREPL is a web interface that will allow us to run commands like we did with the serial REPL. In addition, it will allow us to move files from our computer to the ESP8266 and back!

In this short post, we will:

  1. Setup WebREPL
  2. Connect to WebREPL
  3. Upload Files to the ESP8266
  4. Download Files from the ESP8266
  5. Write a Program to Run when ESP8266 Boots


Download the WebREPL Client and Set the ESP8266 to Use It
WebREPL will be run from the desktop or notebook computer, it is not hosted on the ESP8266 itself like we did in a previous tutorial. Download and unzip the WebREPL client from Github at https://github.com/micropython/webrepl. We will see how to use it momentarily.

From the serial REPL, run the following command:

import webrepl_setup

Set a password like ABC123, then accept option to restart.



Connect to WebREPL
When the ESP8266 restarts, it creates its own network - it will have a name like MicroPython-178fee. Connect to that network. The password will be micropythoN with a capital-N

Open the HTML file called webrepl-master/webrepl.html in a browser. In the top-left corner, click the "Connect" to the ESP8266. Enter the password chosen above (ABC123) and you should be at the >>> prompt!

As you try entering commands into WebREPL, you'll see them echoed in the serial REPL - if it is still open.



Upload Files to the ESP8266
Now let us test the file upload feature...

  1. On your desktop or notebook computer, create a file called Test2.py that contains one line:
    print("Hello, from Test2.py!")
    Save the file someplace convenient.
  2. Click the "Choose File" button, choose the Test2.py file, and click "Send to device button"
  3. To make sure it arrived,
    import os
    os.listdir()
    and we see that the Test2.py was uploaded to the root directory! We run it:
    exec(open('Test2.py').read())
    and get the expected result.



Download Files from the ESP8266

Notice that there is an additional file... webrepl_cfg.py. Let's transfer it from the ESP8266 to our computer.

In the WebREPL, type that name in the text input next to "Get a file" and click "Get from device" button. The file will be downloaded. Open it with a text editor or an IDE and it will read:

PASS = 'ABC123'


Write a Program to Run when ESP8266 Boots
As discussed in Part 1, programs that we create will not automatically run when the ESP8266 boots, unless that file is named /main.py. We can use the file upload feature of WebREPL to create this file.

On your desktop or notebook computer, create a file called main.py and enter the following code:

1
2
3
4
5
6
7
8
9
def toggle(p):
    p.value(not p.value())

import time
import machine
pin = machine.Pin(2, machine.Pin.OUT)
while True:
    toggle(pin)
    time.sleep_ms(500)

This program is the same as was used in Part 1, except now it is a stored in a file. Upload the file to the ESP8266 using WebREPL, then reboot (for example by pressing the RST button on the ESP8266). The LED will start blinking!

So, there it is - how to move files between your primary computer and an ESP8266 running MicroPython.

MicroPython on the ESP8266 - Part 1

The ESP8266 can be programmed in several different languages - great! Besides the C++-ish language we've been using, the ESP8266 can be coded in Python, BASIC, Lua, JavaScript, and others. This post demonstrates installing MicroPython on a NodeMCU, getting to the REPL, and trying some different commands.

MicroPython is an interpreted language, so programs will not run as fast as they would when written in a compiled language. With Arduino, there is not only an IDE, but also a large collection of libraries. With MicroPython, there is no solid IDE and there aren't nearly as many libraries. However, MicroPython for the ESP8266 does support WiFi, GPIO, I2C, SPI, etc. Further, the documentation is excellent, and there are excellent tutorials available. The post is based on that documentation.

Before we start, there are several things to know. First, since Python is an interpreted language, a common way to interact with MicroPython is through a specific shell called REPL: read-evaluate-print-loop. This is an interactive environment that allows us to run single lines of code, or blocks of code. Second, Python does not use braces to delimit code blocks like C, C++, JavaScript, etc. Instead, it uses the tab character to indicate lines of code are within a block.

In this post, we will do the following:

  1. Get and Deploy the MicroPython Firmware
  2. Use the Serial Port to Get to the REPL
  3. Examine the File System
  4. Run a Simple Python Program Stored in the File System


Get and Deploy the Firmware
In order to run MicroPython, a special firmware must first be deployed to the ESP8266. We also need a program to actually perform the flashing.

Step 1: Download the firmware from this URL: http://micropython.org/download#esp8266 The file I chose was esp8266-20171101-v1.9.3.bin and it was saved in the Downloads folder. There is no need to upzip this file - instead it will be flashed to the ESP8266.

The remainder of the steps will be performed in the Mac OS X Terminal, and it assumes that Python is installed.

Step 2: Install esptool.py, which actually performs the flash. Open a Terminal window and run:

pip install esptool

Note: if you already have esptool.py installed, you can upgrade to the latest version using:
pip install esptool --upgrade

Step 3: Get the port the ESP8266 is attached to by running this command from the terminal:

ls -l /dev/tty.*
Mine is on /dev/tty.SLAB_USBtoUART

Step 4: Erase the flash memory:

esptool.py --port /dev/tty.SLAB_USBtoUART erase_flash

Step 5: Finally, deploy the new firmware:

esptool.py --port /dev/tty.SLAB_USBtoUART --baud 460800 write_flash --flash_size=detect 0 Downloads/esp8266-20171101-v1.9.3.bin


Getting to the REPL using the Serial Port
There are two ways to get to the REPL: wired using the serial port, or wireless using webREPL. We cover only the wired approach here.

On Mac OSX, use the following:

screen /dev/tty.SLAB_USBtoUART 115200
Then press Enter a few times until you get the ">>>" REPL prompt.

One command to try is:

print("Hello, world!")

Another thing to try is simple arithmetic:

1+2

To find out the version of MicroPython running on the ESP8266, enter the following two commands:

import sys
print(sys.version)
The version I have is 3.4.0.

An interesting series of commands to try is this:

import machine
pin = machine.Pin(2, machine.Pin.OUT)
pin.value()
pin.off()
pin.value()
pin.on()
pin.value()

These commands reveal that the built-in LED on the NodeMCU is "wired backwards", but that it can indeed be controlled via REPL.

Functions are defined as follows:

def toggle(p):
    p.value(not p.value())

Press Enter several times to finish the function definition.

Here is a simple program to call this toggle function once every 500 ms:

import time
import machine
pin = machine.Pin(2, machine.Pin.OUT)
while True:
    toggle(pin)
    time.sleep_ms(500)

Press Enter several times, and the LED will start blinking!

To exit this loop, press Control-C, which will generate a KeyboardInterrupt.


The File System
When the firmware is installed, a small filesystem is created. To start using it,

import os

To get the current directory,

os.getcwd()
which will return '/' - the root.

To get a list of all files and subdirectories in the current directory, use\

os.listdir()
which returns
['boot.py']

We will discuss this file shortly.

To make a directory:

os.mkdir("MyDirectory")

Move into that directory:

os.chdir("MyDirectory")

Now let's make a file while we're in that directory:

f = open("MyFile.txt", "w")
f.write("Hello, world!")
f.close()
Just after the write command is executed, the number "13" is displayed, indicating that 13 characters were written to MyFile.txt.

Now if we list directory contents using os.listdir(), we see:

['MyFile.txt']

Now open the file, read the content, then close it:

f = open("MyFile.txt")
f.read()
f.close()
and 'Hello, world!' is displayed.

Files are removed as follows:

os.remove('MyFile.txt')

And os.listdir() returns []. We move up one level with

os.chdir('..')

Remove the directory with

os.rmdir('MyDirectory')

Let's examine boot.py using

f = open('boot.py')
f.read()
f.close()
Here's the result:
# This file is executed on every boot (including wake-boot from deepsleep)\n#import esp\n#esp.osdebug(None)\nimport gc\n#import webrepl\n#webrepl.start()\ngc.collect()\n

Replacing the '\n' with newlines shows:

# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import gc
#import webrepl
#webrepl.start()
gc.collect()

The majority of the lines in this file have been commented-out, but notice the reference to WebREPL, we'll save that for a later blog post!


Saving and Running a Simple Python Program
Now we create a simple one-line MicroPython program and execute it!

f = open("Test.py", "w")
f.write('print("Hello, world!")')
f.close()

To run this program, do the following:

exec(open("Test.py").read())

We get the expected output. Note that this method of running Python programs does not necessarily make global and local variables available to Test.py.

Finally, to exit: press Control-a then Control-\ and in response to the "Really quit and kill all your windows [y/n]" prompt, press y.

If the ESP8266 is rebooted, Test.py will NOT run. As it goes, the ESP8266 running MicroPython will first execute code in boot.py, then it will execute main.py, if that file exists. We will demonstrate this in the next blog post.

So now we know how to manually create a Python program, but it is terribly inconvenient. This points to the problem of how to conveniently enter and run MicroPython programs on our ESP8266. We will (partially) solve that problem in the next post!