====== MicroPython ======
[[iot:expressiv|Micropython Installation]]
==== MicroPython für ESP8266 / ESP32 ====
* https://docs.micropython.org/en/latest/esp8266/general.html
* https://docs.micropython.org/en/latest/esp32/quickref.html
* https://docs.zerynth.com/latest/official/board.zerynth.nodemcu_esp32/docs/index.html
* [[https://github.com/lvidarte/esp8266/wiki/MicroPython%3A-REPL|Getting a MicroPython REPL prompt]]
==== Develop for / with Micropython ====
* https://learn.adafruit.com/micropython-basics-loading-modules/frozen-modules
* https://github.com/micropython/micropython-lib
* https://github.com/adafruit/circuitpython
==== Porting Micropython ====
* https://docs.micropython.org/en/latest/develop/porting.html
* https://www.snaums.de/informatik/porting-micropython-to-bare-metal-raspberry-pi.html
* https://www.snaums.de/informatik/porting-micropython-to-the-raspberry-pi-part-2.html
* https://forum.micropython.org/viewtopic.php?t=5779
==== 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: ====
* https://boneskull.com/micropython-on-esp32-part-2/
* https://randomnerdtutorials.com/micropython-mqtt-esp32-esp8266/
import network
from umqtt.robust import MQTTClient
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('', '')
client = MQTTClient('', '')
client.connect()
client.publish('', '')
==== I2S Audio auf dem ESP32: ====
* https://www.youtube.com/watch?v=UXt27kOokh0 - MicroPython I2S Audio with the ESP32
* https://github.com/miketeachman/micropython-i2s-examples
==== BME280====
* https://randomnerdtutorials.com/micropython-bme280-esp32-esp8266/
==== DHT11/22 (ESP32)====
* https://randomnerdtutorials.com/micropython-esp32-esp8266-dht11-dht22-web-server/
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 ====
* https://www.hackster.io/user3282664/micropython-to-aws-iot-cc1c20
==== 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()