030_esp32_mpython - kotaproj/study_zenpan GitHub Wiki
mqttを使う
from umqtt.simple import MQTTClient
import time
# Test reception e.g. with:
# mosquitto_sub -t foo_topic
SSID = "XXXXXXXXXXXXXXXXXXX"
PASS = "XXXXXXXXXXXXXXXXXXX"
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
# wlan.connect('essid', 'password')
wlan.connect(SSID, PASS)
while not wlan.isconnected():
pass
# time.sleep(1)
time.sleep(3)
print(".")
print('network config:', wlan.ifconfig())
return
TOPIC = b"topic_1"
MESSAGE = b"from_esp32"
def main(server="localhost"):
do_connect()
# c = MQTTClient("umqtt_client", server, ssl=True)
c = MQTTClient("umqtt_client", server)
c.connect()
c.publish(TOPIC, MESSAGE)
c.disconnect()
if __name__ == "__main__":
main()
i2cを使う
https://note.com/upyc101/n/n07b92cb2e8e8
初期化
from machine import Pin, I2C
PIN_SCL = 22
PIN_SDA = 21
bus = machine.I2C(scl=machine.Pin(PIN_SCL), sda=machine.Pin(PIN_SDA))
devadr=42, regadr=2に0x10をライト
i2c.writeto_mem(42, 2, b'\x10')
devadr=0x3A, regadr=8に3byteリード
i2c.readfrom_mem(0x3a, 8, 3)
deepsleep関連
- https://qiita.com/taka-murakami/items/0d2b4734b08516135d74
- https://qiita.com/azusa9/items/65a5c3772c41631b5ca1
- https://www.programcreek.com/python/example/101397/machine.deepsleep
wifi
https://note.com/upyc101/n/n0d052c0bf51c
ntp
https://note.com/upyc101/n/n5fc3352d349f
micropython組み込み関数, 例外
https://micropython-docs-ja.readthedocs.io/ja/latest/library/builtins.html