ESP8266 and MicroPython - Part 1
The first 发布 of Micropython for ESP8266 was delivered a couple of weeks ago. The [文档](http://docs.micropython.org/en/latest/) 遮盖 a lot of ground. This post is providing only a little summary which should get you started.
Until a couple of weeks ago, the pre-built MicroPython binary for the ESP8266 was only available to backers of the Kickstarter campaign. This has changed now and it is available to the public for download.
The easiest way is to use esptool.py for firmware handling tasks. First erase the flash:
and then load the firmware. You may adjust the file name of the firmware binary.
Now reset the 设备. You should then be able to use the REPL (Read Evaluate Print Loop). On Linux there is minicom or picocom, on a Mac you can use screen (eg. screen /dev/tty.SLAB_USBtoUART 115200), and on Windows there is Putty to open a serial connection and get the REPL prompt.
The WebREPL work over a wireless connection and allows easy access to a prompt in your browser. An instance of the WebREPL client is hosted at http://micropython.org/webrepl. Alternatively, you can create a local clone of their GitHub repository. This is necessary if your want to use the command-line tool webrepl_cli.py which is mentionend later in this post.
The public build of the firmware may be different than the firmware distributed to the backers of the Kickstarter campaign. Especially in regard of the available modules, turned on 调试 messages, and alike. Also, the WebREPL may not be started by default.
Connect a LED to pin 5 (or another pin of your choosing) to check if the ESP8266 is working as expected.
You can toogle the LED by changing its 状态 with pin.high() and pin.low().
Various ESP8266 development board are shipped with an onboard photocell or a 灯光 dependent resistors (LDR) connected to the analog pin of your ESP8266 check if you are able to obtain a value.
Make sure that you are familiar with REPL and WebREPL because this will be needed soon. Keep in mind the 密码 for the WebREPL access.
Read the instructions about how to 设置 your wireless connection. Basically you need to upload a boot.py file to the microcontroller and this file is taking care of the connection 设置. Below you find a sample which is more or less the same as shown in the 文档.
Upload this file with webrepl_cli.py or the WebREPL:
If you reboot, you should see your current IP address in the terminal.
First let's create a little consumer for Home Assistant 传感器's 状态. The code to place in main.py is a mixture of code from above and the RESTful API of Home Assistant. If the temperature in the kitchen is higher than 20 °C then the LED connected to pin 5 is switched on.
:::警告
If a module is missing then you need to download it from the MicroPython Library 概述 and upload it to the ESP8266 with webrepl_cli.py manually.
:::
Upload main.py the same way as boot.py. After a reboot (>>> import machine and >>> machine.reboot()) or power-cycling your physical notifier is ready.
If you run into trouble, press "Ctrl+c" in the REPL to stop the execution of the code, enter >>> import webrepl and >>> webrepl.start(), and upload your fixed file.

