2026年9月14日月曜日

raspberry pi pico /w by micopython(microdot as httpd,aqm0802,dht11)

 http://jh7ubc.web.fc2.com/Raspberry_Pi/Raspberry_Pi_Pico/Pi_Pico_AQM0802A.htmlを参考に結線した やはりmpyは事例豊富、mmbasicとかkalumajsは英語サイトだけ

pico-sdkはむずい、かわりにluaをesp8266でリベンジ、言語勉強はvlangをリベンジ

まあarduinolang,micropython,tinygoで困らんけどね PIC&Raspberry Piは休眠。。。。


from machine import Pin, I2C

import utime

# I2C

i2c = I2C(

    0,

    freq=100000,

    scl=Pin(17),

    sda=Pin(16)

)


addr = 0x3e

buf = bytearray(2)


# コマンド送信

def write_cmd(cmd):

    buf[0] = 0x00

    buf[1] = cmd

    i2c.writeto(addr, buf)

# 文字送信

def write_char(char):

    buf[0] = 0x40

    buf[1] = char

    i2c.writeto(addr, buf)

# 文字列表示

def lcd_print(text):

    for c in text:

        write_char(ord(c))

# カーソル位置

def LCD_cursor(x, y):

    if y == 0:

        write_cmd(0x80 + x)

    if y == 1:

        write_cmd(0xc0 + x)


# LCDクリア

def LCD_clear():

    buf[0] = 0x00

    buf[1] = 0x01

    i2c.writeto(addr, buf)

    utime.sleep(0.002)


# LCDホーム

def LCD_home():

    buf[0] = 0x00

    buf[1] = 0x02

    i2c.writeto(addr, buf)

    utime.sleep(0.002)



# LCD初期化

def LCD_init():

    orders = [

        0x38,

        0x39,

        0x14,

        0x73,

        0x56,

        0x6c,

        0x38,

        0x0c,

        0x01

    ]


    utime.sleep(0.04)


    for order in orders:

        write_cmd(order)

        utime.sleep(0.002)


# =========================

# メイン

# =========================


LCD_init()


LCD_clear()

LCD_home()

lcd_print("JH7UBC")

num = 0

while True:

    LCD_cursor(0, 1)

    lcd_print(str(num))

    num = num + 1

    utime.sleep(1)

ーーーーーーーーーーーーーーーーーーーーーーーー

import machine

import time

import dht

# GPIO4番ピンにデータ線を接続した場合


sensor = dht.DHT11(machine.Pin(4))


while True:

    try:

        # センサーからデータを計測

        sensor.measure()


        # 温度(摂氏)と湿度(%)を取得

        temp = sensor.temperature()

        hum = sensor.humidity()


        print(f"温度: {temp} ℃, 湿度: {hum} %")


    except Exception as e:

        print("読み取りエラー:", e)


    # 2秒ごとに取得(DHT11は1秒に1回制限)

    time.sleep(2)



0 件のコメント:

コメントを投稿