## Robíme IoT s ESP32 v jazyku MicroPython
#### Time to Say Goodbye to Arduino
miroslav.binas@tuke.sk / [**OSS Conf 2019**](http://ossconf.soit.sk/)
![Arduino](images/arduino.png)
![micro:bit](images/microbit.png)
Note:
* https://www.womenofwearables.com/blogwrite/announcing-partnership-with-bbc-microbit-educational-foundation
![Mu](images/mu.png)
## [Mu](https://codewith.mu/): A Python Code Editor
![ESP32](images/esp32.png)
Notes:
* https://www.mouser.co.uk/new/espressif/espressif-esp32-devkitc-boards/
![ESP32 Block Diagram](images/esp32-block-diagram.png)
Notes:
* https://cdn.sparkfun.com/assets/home_page_posts/2/0/1/7/
[![Micropython](images/logo-micropython.jpg)](https://micropython.org/)
## Blink
```python
from machine import Pin
from time import sleep
led = Pin(23, Pin.OUT)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
```
## DHT22
```python
from dht import DHT22
from machine import Pin
from time import sleep
sensor = DHT22(Pin(22))
while True:
sensor.measure()
print(sensor.temperature(), sensor.humidity())
sleep(5)
```
## WiFi
```python
import network
iface = network.WLAN(network.STA_IF)
if not iface.isconnected():
print('connecting to network...')
iface.active(True)
iface.connect('ssid', 'password')
while not iface.isconnected():
pass
print('network config:', iface.ifconfig())
```
## MQTT
```python
from umqtt.robust import MQTTClient
# connecting to broker
client = MQTTClient('client-id', 'broker-ip')
client.connect()
# publishing message
client.publish('messages', 'hello world')
```
## Get Ready for Watson
* no direct support for _Watson_
* install appropriate package with `upip`
* _MicroPython_ version of `pip`
* packages for _MicroPython_ have prefix `micropython-`
* install `micropython-watson-iot` package:
```python
import upip
upip.install('micropython-watson-iot')
```
## ESP32 with Watson
```python
from watson_iot import Device
device = Device(device_id='esp32-dht22',
device_type='my-device-type',
token='my-device-token')
device.connect()
device.publishEvent('temperature', {'degrees': 30.7, 'unit': 'C'})
```
## The Final Code
```python
# imports ...
sensor = DHT22(Pin(23))
device = Device(device_id='esp32-with-dht22',
device_type='type', token='token')
device.connect()
while True:
sensor.measure()
temp = sensor.temperature()
device.publishEvent('temperature', {'degrees': temp,
'unit': 'C'})
sleep(10)
```
### This Talk is Dedicated to...
![Fedora Call to Play with MicroPython at Pycon 2019](images/fedora.call.png)
#### ...unknown guy from Fedora. Thank you, you have changed my life :-*
## Links
* [Mu Editor](https://codewith.mu/)
* [MicroPython](https://micropython.org/) - Python for microcontrollers
* [MicroPython documentation](https://docs.micropython.org/en/latest/index.html)
* [esptool](https://github.com/espressif/esptool), [ampy](https://github.com/pycampers/ampy), [rshell](https://github.com/dhylands/rshell) - tools for serial interaction with _ESP32_
* [Random Nerd Tutorials](https://randomnerdtutorials.com/) - _MicroPython_ projects with _ESP32_
![qr code](https://api.qrserver.com/v1/create-qr-code/?data=http://bit.ly/2NrMSWq&size=300x300)
(**http://bit.ly/2NrMSWq**)