قالب وردپرس درنا توس
Home / Tips and Tricks / How to program an ESP8266 or ESP32 microcontroller over Wi-Fi with MicroPython «Null Byte :: WonderHowTo

How to program an ESP8266 or ESP32 microcontroller over Wi-Fi with MicroPython «Null Byte :: WonderHowTo



The only thing better than programming MicroPython is programming MicroPython over Wi-Fi. So once you've set up MicroPython on a microcontroller and it has its own power source, you don't need to use a data cable to connect to it when you need to interact with it, program it, upload files or collect data.

The MicroPython REPL interface is very simple, making it an attractive option for an ESP8266 or EP32 card, and the WebREPL interface is even more convenient. Once everything is initially set up, you can connect and control the board through the Wi-Fi access point, then program or upload code from the handy web client.

What You Need

To start programming MicroPython on a microcontroller over Wi-Fi, you need a ESP8266 or ESP32 development board with Wi-Fi, like one of these: [19659002] You also need a micro USB cable to connect your board to your Linux, macOS or Windows computer. Although we are going to use the MCU over Wi-Fi, it needs to be set up first, so we need a cable to connect through the serial interface. Then if you want your board to turn on by itself later, you can do this, and the easiest way is with a 5-volt adapter. And to protect the pins of your board, you can purchase an optional breadboard.

In addition to the hardware, you also need the ESP tool on your system. If you've followed our guide to getting started with MicroPython for microcontrollers, you should already have it. If not, go to step 1 to install it.

  • ESPtool (to clear and flash the board)

Step 1: Identify the correct serial port

First we need to find the serial address for the EPS8266, so connect your board to your computer and then run the command to see which serial port it is connected to.

On a Linux computer, such as one using Kali, use the following to find the port of your connected ESP8266. From my results it would be /dev/cu.wchusbserial14630. <19659011> ~~ dmesg | grep tty

/dev/cu.Bluetooth-Incoming-Port
/dev/cu.SOC
/dev/cu.wchusbserial14630
/dev/cu.MALS
/dev/cu.usbserial-14630"19659012// Use the following on a Mac instead.

  ~% ls /dev/cu.*

/dev/cu.Bluetooth-Incoming-Port
/dev/cu.SOC
/dev/cu.wchusbserial14630
/dev/cu.MALS
/dev/cu.usbserial-14630"19659012//With Windows you need to open the Device Manager to find the COM port under the folder "Ports (COM & LPT)". We don't show any of the Windows steps below, so if you are using windows and can't figure out the process, please fill in the comments and we will try to steer you in the right direction. [19659002] Again, the correct serial address for me on Kali or macOS would be /dev/cu.wchusbserial14630 even though there is /dev/cu.usbserial-14630 associated with the ESP8266. If you see two, always use the one with "wch" in it. 

To make sure they are for your ESP8266 , you can unplug the board and run the command again to see the difference. If you plug the board back into another port, you have to run the command again to get the new address.

The address found is the address that we use when giving commands.

Step 2: Download the latest MicroPython binary file [19659004] To get MicroPython on your microcontroller, you must first download the latest firmware based on the board you are using. Visit MicroPython's download page, locate your board and download the file. I use an ESP8266, so I will use it. But if you are using an ESP32 you should download it instead. When you select your board, choose the .bin file for the latest stable release.

Step 3: Clear the board and Flash MicroPython

To make sure there is no hiccups when flashing MicroPython to your MCU, clear first the board. Use the command below to replace my serial port with the serial port of your ESP.

  ~ $ esptool.py --port /dev/cu.wchusbserial14630 erase_flash

esptool.py v2.8
Serial port /dev/cu.wchusbserial14140
To connect....
Detect chip type ... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26 MHz
MAC: 2c: f4: 32: 4b: 07: 83
Upload Stub ...
Run Stub ...
Stub is running ...
Clear Flash (this may take a while) ... 

If you are using an ESP8266 microcontroller then run the following command to flash MicroPython on it. Replace the .bin file at the end with the MicroPython binary you downloaded.

  ~ $ esptool.py --port /dev/cu.wchusbserial14630 --baud 460800 write_flash --flash_size = detect 0 / Users / kali / Downloads / esp8266-20191220-v1.12.bin

esptool.py v2.8
Serial port /dev/cu.wchusbserial14630
To connect....
Detect chip type ... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26 MHz
MAC: 2c: f4: 32: 4b: 07: 83
Upload Stub ...
Run Stub ...
Stub is running ...
Change baud rate to 460800
Changed.
Configure flash size ...
Flash size automatically detected: 4 MB
Flash parameters set to 0x0040
Compressed 617880 bytes to 402086 ...
Wrote 617880 bytes (402086 compressed) at 0x00000000 in 9.6 seconds (effective 514.5 kbit / s) ...
Hash of data verified.

Departure ...
Hard reset via RTS pin ... 

If you are using an ESP32, run the following command instead. Again replace the .bin file at the end with the MicroPython binary file you downloaded.

  ~ $ esptool.py --chip esp32 --port /dev/cu.wchusbserial14630 write_flash -z 0x1000 / Users / kali / Downloads / esp32 -idf3-20191220-v1.12.bin 

with flashing, you can confirm that it worked with the following command. If the output is comparable, you're good. If you don't see anything, press Enter a few more times to put it into action.

  ~ $ screen /dev/cu.wchusbserial14630 115200

MicroPython v1.12 on 2019-12-20; ESP module with ESP8266
Type "help ()" for more information.
>>> 

Step 4: Download WebREPL

In our other guide we showed how to use REPL (read, evaluate, print), the command line interface for MicroPython on the board. That requires us to stay connected via cable, but there is also WebREPL, which doesn't require wires. You can download WebREPL from GitHub with the command below, or you can go online to the live WebREPL version.

  ~ $ git clone https://github.com/micropython/webrepl.git

Cloning in & # 39; webrepl & # 39; ...
remote: List objects: 186, done.
remote: Total 186 (delta 0), reused 0 (delta 0), reused pack 186
Objects received: 100% (186/186), 75.12 KiB | 1.06 MiB / s, ready.
Solve deltas: 100% (109/109), done. 

Now go to that folder and view the files. You should see the file "webrepl.html". That is what you open to communicate with your plate. But don't open it yet - we need to set up WebREPL on the board first.

  ~ $ cd web repl
~ / webrepl $ ls

FileSaver.js README.md webrepl_cli.py websocket_helper.py
LICENSE term.js webrepl.html 

Step 5: Enable WebREPL in MicroPython

To ensure that you can connect to your ESP8266 or ESP32 board, you must first configure WebREPL. In a new terminal window, use screen again to connect to the board through the serial port. Replace the gate with yours again.

  ~ $ screen /dev/cu.wchusbserial14630 115200

MicroPython v1.12 on 2019-12-20; ESP module with ESP8266
Type "help ()" for more information.
>>> 

Next, let's set up WebREPL with the following command. When prompted, press E on your keyboard and press Enter to enable it during startup.

  >>> import webrepl_setup

WebREPL daemon auto-start status: disabled

Do you want (E) nable or (D) is it running at startup?
(Blank line to stop)
> e 

Next you need to set a password. I use the super strong password "password" as an example (I hope you know I'm kidding), confirm the password and then type Y and Enter to restart WebREPL enabled.

  To enable WebREPL, you must set a password for it
New password (4-9 characters): password
Confirm password: password
Changes are activated after restart
Do you want to restart now? (y / n) y
...
Webrepl started in normal mode

MicroPython v1.12 on 2019-12-20; ESP module with ESP8266
Type "help ()" for more information.
>>> 

Step 6: Connect to MicroPython via WebREPL

On your computer, open your Wi-Fi settings and connect to the MicroPython network. The password to connect is micropythoN (don't forget the capital N at the end).

Then in our / webrepl directory in the other terminal window, open that .html file you found earlier. . In Kali, use:

  ~ / webrepl $ xdg-open webrepl.html 

On macOS just use:

  ~ / webrepl% open webrepl.html 

A browser window should open with the WebREPL client. You can also access the live WebREPL version from the browser, rather than through the terminal. Once the client is opened, click & # 39; Connect & # 39; at the top left and enter the password you set earlier, in my case & # 39; password & # 39 ;.

Make sure you use a strong password

MicroPython's web interface is an incredible way to interact with a board that would otherwise have a serial connection requires. However, best practices apply when it comes to Wi-Fi security.

If you never change the default password and set MicroPython to be programmed that way, someone can take control of your board and embed all sorts of things in it. So make sure to change the default password as soon as you set it. Otherwise, anyone can take control of your MicroPython board.

Want to Make Money as a White Hat Hacker? Jump-start your white-hat hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and receive over 60 hours of training from ethical hacking professionals.

Buy now (96% discount)>

Cover photo by Make Magazin DE / Wikimedia Commons; Screenshots of Retia / Null Byte

Source link