« Turtorial Examples for ESP8266-MicroPython | トップページ | Turtorial Examples for RpiZero-MicroPython »

2020年3月 9日 (月)

Turtorial Examples for ESP32-MicroPython

2020/3/9

Turtorial Examples for ESP32-MicroPython

ESP32

http://docs.micropython.org/en/latest/esp32/quickref.html

from time import sleep from machine import Pin p0 = Pin(0, Pin.OUT, value=0) # external LED blink = 0 while True: p0.value(0 if blink == 0 else 1) blink = ~blink sleep(0.1)

Pin/GPIO

from machine import Pin p4 = Pin(4, Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor p4.value() # 1 if open p4.value() # 0 if GND

PWM

from time import sleep from machine import Pin, PWM pwm0 = PWM(Pin(0)) # create PWM object from a pin pwm0.freq(1000) # set frequency for p in range(100): pwm0.duty(10*p) sleep(0.05) for p in range(100): pwm0.duty(1000-10*p) sleep(0.05) pwm0.deinit() # turn off PWM on the pin

ADC

以下のピンがAnalogInとして使用できる:
0, 2, 4, 12, 13, 14, 15, 25, 26, 27, 32, 33, 34, 35, 36, 39
デフォルトのレンジは、0-4095(変更可能)

from machine import Pin, ADC from time import sleep p34 = ADC(Pin(34)) # create ADC object on ADC pin p34.read() # read value, 0-4095 across voltage range 0.0v - 1.0v p34.atten(ADC.ATTN_11DB) # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v) p34.width(ADC.WIDTH_9BIT) # set 9 bit return values (returned range 0-511) while True: print(p34.read()) sleep(0.1)

SPI

from machine import Pin, SDCard import os sd = SDCard(slot=3, width=1, sck=Pin(14), mosi=Pin(13), miso=Pin(12),cs=Pin(15)) # slot#3を使用する os.mount(sd, '/sd') os.chdir('/sd') os.listdir() 出力例: ['lib', 'ESP32_OSCrecvBundled_test.py', 'ESP32_demo_neopixel.py', 'ESP32_starwars.py', 'M5S_OSCrecvBundled_test.py', 'TEST.txt', 'sdmain.py', 'wlan.py'] # この時点でエラーがなく、SDのファイルが表示されたら動作確認としてはOKとなる import sys sys.path 出力例: ['', '/lib'] sys.path[1]='/sd/lib' # ライブラリのパスをSDに切り替える import ESP32_OSCrecvBundled_test # SDに置いてあるプログラム(ESP32_OSCrecvBundled_test.py)をロードして実行する

I2C

ESP8266_TMP102_test.py

# ESP8266/ESP32 import utime from machine import Pin, I2C from tmp102 import Tmp102 i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000) sensor = Tmp102(i2c, 0x48) while True: print('Temperature: {0:.1f}'.format(sensor.temperature)) utime.sleep(2)

RTC

from machine import RTC rtc = RTC() rtc.datetime((2020, 3, 9, 1, 8, 53, 0, 0)) # set a specific date and time # The 8-tuple has the following format: # (year, month, day, weekday, hours, minutes, seconds, subseconds) # weekday is 1-7 for Monday through Sunday. # subseconds counts down from 255 to 0 rtc.datetime() # get date and time

ESP32_8266_clock.py
以下は自分の環境に合わせる:
ssid = 'your_ssid'
password = 'your_passwd'

# NIC setup import network, socket WIFI_SSID = "your_ssid" WIFI_PASSWD = "your_passwd" def wlan_connect(ssid='SSID', password='PASSWD'): import network wlan = network.WLAN(network.STA_IF) if not wlan.active() or not wlan.isconnected(): wlan.active(True) print('connecting to:', ssid) wlan.connect(ssid, password) while not wlan.isconnected(): pass wlan_connect(ssid=WIFI_SSID, password=WIFI_PASSWD) #========================================== # ESP32_8266_clock.py from machine import Pin, RTC import network, urequests, utime import ujson rtc = RTC() url_jst = "http://worldtimeapi.org/api/timezone/Asia/Tokyo" # see http://worldtimeapi.org/timezones retry_delay = 5000 # interval time of retry after a failed Web query response = urequests.get(url_jst) # parse JSON parsed = response.json() #parsed=ujson.loads(res) # in case string datetime_str = str(parsed["datetime"]) year = int(datetime_str[0:4]) month = int(datetime_str[5:7]) day = int(datetime_str[8:10]) hour = int(datetime_str[11:13]) minute = int(datetime_str[14:16]) second = int(datetime_str[17:19]) subsecond = int(round(int(datetime_str[20:26]) / 10000)) # update internal RTC rtc.datetime((year, month, day, 0, hour, minute, second, subsecond)) # setup day of the week daysOfTheWeek = "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" tm = utime.localtime(utime.mktime(utime.localtime())) while True: # generate formated date/time strings from internal RTC date_str = "Date: {0:4d}/{1:02d}/{2:02d}".format(*rtc.datetime())+' ('+daysOfTheWeek[tm[6]]+')' time_str = "Time: {4:02d}:{5:02d}:{6:02d}".format(*rtc.datetime()) print(date_str) print(time_str) print('-----------') utime.sleep(1) #=============================================

NeoPixel

# neopixel simple test from machine import Pin from neopixel import NeoPixel pin = Pin(2, Pin.OUT) np = NeoPixel(pin, 8) np[0] = (255, 255, 255) np[1] = (255, 255, 0) np[2] = (255, 0, 0) np[3] = (0, 255, 255) np[4] = (0, 255, 0) np[5] = (0, 0, 255) np[6] = (255, 255,0) np[7] = (255, 0, 0) np.write()

DHT

ESP8266_demo_DHT11.py

# ESP8266/ESP32 from dht import DHT11 import machine from time import sleep d = DHT11(machine.Pin(14)) while True: d.measure() tempe=d.temperature() humi=d.humidity() print('tempe:'+str(tempe)+ ' humi:'+str(humi)) sleep(1)

Networking

以下は自分の環境に合わせる:
ssid = 'your_ssid'
password = 'your_passwd'

# NIC setup import network, socket WIFI_SSID = "your_ssid" WIFI_PASSWD = "your_passwd" def wlan_connect(ssid='SSID', password='PASSWD'): import network wlan = network.WLAN(network.STA_IF) if not wlan.active() or not wlan.isconnected(): wlan.active(True) print('connecting to:', ssid) wlan.connect(ssid, password) while not wlan.isconnected(): pass wlan_connect(ssid=WIFI_SSID, password=WIFI_PASSWD)

Performance Test

performanceTestESP32.py

# Peformace Test for ESP32 from machine import RTC rtc = RTC() rtc.datetime((2020, 2, 9, 1, 12, 48, 0, 0)) # (year, month, day, weekday, hours, minutes, seconds, subseconds) def performanceTest(): secs = rtc.datetime()[6] endTime = secs + 10 count = 0 while rtc.datetime()[6] < endTime: count += 1 print("Count: ", count) performanceTest()

以上

|

« Turtorial Examples for ESP8266-MicroPython | トップページ | Turtorial Examples for RpiZero-MicroPython »

linux」カテゴリの記事

MicroPython」カテゴリの記事

コメント

コメントを書く



(ウェブ上には掲載しません)




« Turtorial Examples for ESP8266-MicroPython | トップページ | Turtorial Examples for RpiZero-MicroPython »