ESP8266 and MicroPython - Part 1
+ + + ++
diff --git a/atom.xml b/atom.xml index e5e9163793..2e5c97f4bd 100644 --- a/atom.xml +++ b/atom.xml @@ -4,7 +4,7 @@
+The public build of the firmware may be different than the firmware distributed to the backers of the campaign. Especially in regard of the [available modules](http://docs.micropython.org/en/latest/esp8266/py-modindex.html), turned on debug 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. + +```python +>>> import machine +>>> pin = machine.Pin(5, machine.Pin.OUT) +>>> pin.high() +``` + +You can toogle the LED by changing its state with `pin.high()` and `pin.low()`. + +Various ESP8266 development board are shipped with an onboard photocell or a light dependent resistors (LDR) connected to the analog pin of your ESP8266 check if you are able to obtain a value. + +```python +>>> import machine +>>> brightness = machine.ADC(0) +>>> brightness.read() +``` + +Make sure that you are familiar with REPL and WebREPL because this will be needed soon. Keep in mind the password for the WebREPL access. + +Read the [instructions](http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/network_basics.html) about how to setup your wireless connection. Basically you need to upload a `boot.py` file to the microcontroller and this file is taking care of the connection setup. Below you find a sample which is more or less the same as shown in the [documentation](http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/network_basics.html#configuration-of-the-wifi). + +```python +def do_connect(): + import network + + SSID = 'SSID' + PASSWORD = 'PASSWORD' + + sta_if = network.WLAN(network.STA_IF) + ap_if = network.WLAN(network.AP_IF) + if ap_if.active(): + ap_if.active(False) + if not sta_if.isconnected(): + print('connecting to network...') + sta_if.active(True) + sta_if.connect(SSID, PASSWORD) + while not sta_if.isconnected(): + pass + print('Network configuration:', sta_if.ifconfig()) +``` + +Upload this file with `webrepl_cli.py` or the WebREPL: + +```bash +$ python webrepl_cli.py boot.py 192.168.4.1:/boot.py +``` + +If you reboot, you should see your current IP address in the terminal. + +```bash +>>> Network configuration: ('192.168.0.10', '255.255.255.0', '192.168.0.1', '192.168.0.1') +``` + +First let's create a little consumer for Home Assistant sensor's state. The code to place in `main.py` is a mixture of code from above and the [RESTful API](/developers/rest_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 is it from [MicroPython Library overview](https://github.com/micropython/micropython-lib) and upload it to the ESP8266 with `webrepl_cli.py` manually. +
+ +```python +# Sample code to request the state of a Home Assistant entity. + +API_PASSWORD = 'YOUR_PASSWORD' +URL = 'http://192.168.0.5:8123/api/states/' +ENTITY = 'sensor.kitchen_temperature' +TIMEOUT = 30 +PIN = 5 + +def get_data(): + import urequests + url = '{}{}'.format(URL, ENTITY) + headers = {'x-ha-access': API_PASSWORD, + 'content-type': 'application/json'} + resp = urequests.get(URL, headers=headers) + return resp.json()['state'] + +def main(): + import machine + import time + + pin = machine.Pin(PIN, machine.Pin.OUT) + while True: + try: + if int(get_data()) >= 20: + pin.high() + else: + pin.low() + except TypeError: + pass + time.sleep(TIMEOUT) + +if __name__ == '__main__': + print('Get the state of {}'.format(ENTITY)) + main() +``` + +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. + +]]>+The public build of the firmware may be different than the firmware distributed to the backers of the campaign. Especially in regard of the [available modules](http://docs.micropython.org/en/latest/esp8266/py-modindex.html), turned on debug 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. + +```python +>>> import machine +>>> pin = machine.Pin(5, machine.Pin.OUT) +>>> pin.high() +``` + +You can toogle the LED by changing its state with `pin.high()` and `pin.low()`. + +Various ESP8266 development board are shipped with an onboard photocell or a light dependent resistors (LDR) connected to the analog pin of your ESP8266 check if you are able to obtain a value. + +```python +>>> import machine +>>> brightness = machine.ADC(0) +>>> brightness.read() +``` + +Make sure that you are familiar with REPL and WebREPL because this will be needed soon. Keep in mind the password for the WebREPL access. + +Read the [instructions](http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/network_basics.html) about how to setup your wireless connection. Basically you need to upload a `boot.py` file to the microcontroller and this file is taking care of the connection setup. Below you find a sample which is more or less the same as shown in the [documentation](http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/network_basics.html#configuration-of-the-wifi). + +```python +def do_connect(): + import network + + SSID = 'SSID' + PASSWORD = 'PASSWORD' + + sta_if = network.WLAN(network.STA_IF) + ap_if = network.WLAN(network.AP_IF) + if ap_if.active(): + ap_if.active(False) + if not sta_if.isconnected(): + print('connecting to network...') + sta_if.active(True) + sta_if.connect(SSID, PASSWORD) + while not sta_if.isconnected(): + pass + print('Network configuration:', sta_if.ifconfig()) +``` + +Upload this file with `webrepl_cli.py` or the WebREPL: + +```bash +$ python webrepl_cli.py boot.py 192.168.4.1:/boot.py +``` + +If you reboot, you should see your current IP address in the terminal. + +```bash +>>> Network configuration: ('192.168.0.10', '255.255.255.0', '192.168.0.1', '192.168.0.1') +``` + +First let's create a little consumer for Home Assistant sensor's state. The code to place in `main.py` is a mixture of code from above and the [RESTful API](/developers/rest_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 is it from [MicroPython Library overview](https://github.com/micropython/micropython-lib) and upload it to the ESP8266 with `webrepl_cli.py` manually. +
+ +```python +# Sample code to request the state of a Home Assistant entity. + +API_PASSWORD = 'YOUR_PASSWORD' +URL = 'http://192.168.0.5:8123/api/states/' +ENTITY = 'sensor.kitchen_temperature' +TIMEOUT = 30 +PIN = 5 + +def get_data(): + import urequests + url = '{}{}'.format(URL, ENTITY) + headers = {'x-ha-access': API_PASSWORD, + 'content-type': 'application/json'} + resp = urequests.get(URL, headers=headers) + return resp.json()['state'] + +def main(): + import machine + import time + + pin = machine.Pin(PIN, machine.Pin.OUT) + while True: + try: + if int(get_data()) >= 20: + pin.high() + else: + pin.low() + except TypeError: + pass + time.sleep(TIMEOUT) + +if __name__ == '__main__': + print('Get the state of {}'.format(ENTITY)) + main() +``` + +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. + +]]>