iot:micropython
Inhaltsverzeichnis
MicroPython
MicroPython für ESP8266 / ESP32
Develop for / with Micropython
Porting Micropython
Blink LED:
from machine import Pin from time import sleep led = Pin(2, Pin.OUT) # sets PIN 2 as Output (most boards have an LED connected) while True: led.value(not led.value()) # toggle LED status sleep(0.25)
LED Helligkeit per PWM regeln:
from machine import Pin, PWM from time import sleep frequency = 5000 # Hz: can be a value between 0 and 78125 led = PWM(Pin(2), frequency) while True: for duty_cycle in range(0, 1024): #can be a value between 0 and 1023 (100%) led.duty(duty_cycle) sleep(0.005)
Quelle: https://randomnerdtutorials.com/esp32-esp8266-pwm-micropython/
MQTT auf dem ESP32:
import network from umqtt.robust import MQTTClient sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect('<YOUR SSID>', '<YOUR PASSWORD>') client = MQTTClient('<CLIENT-ID>', '<BROKER>') client.connect() client.publish('<TOPIC>', '<BYTE_STRING>')
I2S Audio auf dem ESP32:
- https://www.youtube.com/watch?v=UXt27kOokh0 - MicroPython I2S Audio with the ESP32
BME280
DHT11/22 (ESP32)
from machine import Pin from time import sleep import dht PIN_NR = # define your PIN # here sensor = dht.DHT22(Pin(PIN_NR)) while True: try: sensor.measure() temp = sensor.temperature() hum = sensor.humidity() if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)): msg = (b'{0:3.1f},{1:3.1f}'.format(temp, hum)) print(msg) except OSError as e: print('Failed to read sensor.') sleep(2)
MicroPython to AWS-IOT
Enthaltene Module (ESP32)
help('modules') __main__ framebuf socket upip _boot gc ssl upip_utarfile _onewire hashlib struct upysh _thread heapq sys urandom _webrepl inisetup time ure apa106 io ubinascii urequests array json ucollections uselect binascii machine ucryptolib usocket btree math uctypes ussl builtins micropython uerrno ustruct cmath neopixel uhashlib utime collections network uhashlib utimeq dht ntptime uheapq uzlib ds18x20 onewire uio webrepl errno os ujson webrepl_setup esp random umqtt/robust websocket esp32 re umqtt/simple websocket_helper flashbdev select uos zlib
Freien Speicher ermitteln
>>> import gc >>> gc.collect() >>> gc.mem_free() 107488 >>> import micropython >>> micropython.mem_info()
iot/micropython.txt · Zuletzt geändert: 2024/02/04 11:35 von admin