MicroPython
Marek Mansell
31.5.2022
PyBoard (STM32)
ESP8266
ESP32
MicroPython hardvér
BBC micro:bit
ESP32
- Frekvencia 240 MHz
- 32 bit architektúra
-
512 KB SRAM
-
802.11 b/g/n + BLE
-
3 x UART, 3 x SPI, 12 x ADC, 2 x DAC, 2 x I2C
-
5 μA v sleep móde
MicroPython firmvér
Komunikácia ESP32 s PC
"Ahoj Svet!"
>>> 1+1
2
>>> import math
>>> math.pi
3.141592
>>> from os import urandom
>>> urandom(1)[0]
61
>>> print('Ahoj Svet!')
Ahoj Svet!
>>> class TriedaHadov:
... def __init__(self):
... self.odpoved = "Ahoj Python!"
... def pozdrav(self):
... return self.odpoved
Program "Blink"
>>> from machine import Pin
>>> from time import sleep
>>> pin_diody = Pin(4, Pin.OUT)
>>> while True:
... pin_diody.value(0)
... sleep(1)
... pin_diody.value(1)
... sleep(1)
"Ahoj Svet!" Hardvérovo
Relé
>>> import dht
>>> import machine
>>> d = dht.DHT11(machine.Pin(4))
>>> d.measure()
>>> d.temperature()
>>> d.humidity()
DHT11/DHT22
NeoPixel
>>> from machine import Pin
>>> from neopixel import NeoPixel
>>> pin = Pin(0, Pin.OUT) # nastaví pin ako digitálny výstup
>>> np = NeoPixel(pin, 8) # Vytvorí NeoPixel driver pre 8 pixelov
>>> np[0] = (255, 0, 0) # nastaví prvý pixel na červenú farbu
>>> np.write() # odošle hodnoty, LEDky zmenia farby
>>> np[1] = (255, 255, 255) # nastaví druhý pixel na bielu farbu
>>> np[7] = (128, 0, 128) # nastaví ôsmy pixel na fialovú farbu
NeoPixel
WiFi
>>> import network
>>> wlan = network.WLAN(network.STA_IF) # vytvorí interface
>>> wlan.active(True) # aktivuje interface
>>> wlan.connect('nazov_siete', 'heslo') # pripojí sa k sieti
>>> wlan.ifconfig() # vráti IP adresu/masku/gateway/DNS
1. REST API
2. HTTP sockety
3. MQTT
REST API
import urequests as requests
r = requests.get("https://moj-cool-django-server.sk/api/hello")
print(r.content)
print(r.text)
print(r.json())
r.close()
Sockety - Server
>>> import socket
>>> from machine import Pin
>>> ledka = Pin(4, Pin.OUT)
>>> s.bind(("192.168.0.55", 5000))
>>> s.listen(1)
>>> while True:
... cl, addr = s.accept()
... data = cl.recv(500)
... cl.close()
... if data == "ON":
... ledka.value(1)
... elif data == "OFF":
... ledka.value(0)
>>> s = socket.socket()
Sockety - Klient
>>> import socket
>>> s = socket.socket()
>>> s.connect(("192.168.0.55", 5000))
>>> s.send("ON")
>>> s.send("OFF")
MQTT
MQTT
from umqttsimple import MQTTClient
def mqtt_msg_handler(topic, msg):
# handle
mqtt_client = MQTTClient("client_id", "mqtt_broker_ip")
mqtt_client.set_callback(mqtt_msg_handler)
mqtt_client.connect()
mqtt_client.subscribe(b"door1/relay1")
mqtt_client.publish(b"door1/btn_0", "1")
linkedin.com/in/marekmansell
www.marekmansell.sk
marek.mansell@gmail.com
Kontakt:
upip
x:int = 1
if x := 1:
...
PEP 526 -- Syntax for Variable Annotations
PEP 572 -- Assignment Expressions
Python Enhancement Proposals
PEP 498 -- Literal String Interpolation
f'The value is {value}.'
Bezpečnosť
Breadboard
Nepájivé kontaktné polia
REPL
MicroPython firmvér
ESP32
Odkazy
MicroPython (Májový Python meetup 2022)
By Marek Mansell