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.

No comments:

Post a Comment