-------------------mqtt------------
https://mqttx.app/web-client はウエブソケット版に変更されてもはや1883は使えん!
https://github.com/micropython/\
micropython-lib/blob/master/micropython/umqtt.simple/umqtt/simple.py
をumqtt.pyでインストした
ーーーーーーーサブスクライブ例ーーーーーーーーーーーーーーーーーーー
subscribe codeでLEDオンオフ成功! https://mqttx.app/web_clientを開く
user名は適当でいいのでログインしてCONNECT
最初に{"message":"on"}などが入っていたらplaintext設定確認後に
"on"と書き直してpublishでled on,"off"lをpublishでled offに成功した
from machine import Pin
import time
from umqtt import MQTTClient
import ubinascii
import machine
import micropython
import network
import gc
#led alive check
led_pin = Pin(15, Pin.OUT)
led_pin.value(1)
time.sleep(5)
led_pin.value(0)
gc.collect()
ssid = 'HUAWEI@nova@lite@3+'
password = '99991111'
mqtt_server = 'broker.emqx.io' #Replace with your MQTT Broker IP
client_id = ubinascii.hexlify(machine.unique_id())
#ユニークな名でログインのため
topic_sub = b'rpi_pico_w/test_sub'
# なかったら作る(publish欄で?再調査!)
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
print(station.ifconfig())
def sub_cb(topic, msg): #このcb(callback)でやりたい仕事を仕分ける
print ('Received Message %s from topic %s' %(msg, topic))
if msg==b'"on"':
led_pin.value(1)
elif msg==b'"off"':
led_pin.value(0)
def connect_and_subscribe():
global client_id, mqtt_server, topic_sub
client = MQTTClient(client_id, mqtt_server)
client.set_callback(sub_cb)
client.connect()
client.subscribe(topic_sub)
print('Connected to %s MQTT broker, subscribed to %s topic' % \
(mqtt_server, topic_sub))
return client
def restart_and_reconnect():
print('Failed to connect to MQTT broker. Reconnecting...')
time.sleep(10)
machine.reset()
try:
client = connect_and_subscribe()
except OSError as e: # もしこれ起こったら
restart_and_reconnect() # 上記でmachine.resetがかかるのでOK
while True:
try:
new_msg = client.check_msg() #ここは再調査
time.sleep(10)
except OSError as e: # もしこれ起こったら
restart_and_reconnect() # 上記でmachine.resetがかかるのでOK
ーーーーー以下はパブリッシュ例ーーーーーーーーーーーーーーーーーーーーーー
https://mqttx.app/web-client#/recent_connections/32a3a13e-7aef-4224-8ddc-6226007aa6caでtest/topic1をつくる
https://mqttx.app/web-clienthttps://tech-and-investment.com/raspberrypi-picow-6-mqtt/ にある以下のコードに成功
import time
import network
from umqtt
import MQTTClient
import machine
from picozero import pico_temp_sensor,
import sys
#
# Wi-Fi ルーターのSSIDとパスワードです。
# お使いの設定に書き換えてください。
#
ssid = 'NAME OF YOUR WIFI NETWORK'
password = 'YOUR SECRET PASSWORD'
#
# Wi-Fiに接続する関数です
#
def connect():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print('Waiting for connection...')
time.sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip
#
# 受信(Subscribe)したメッセージを表示する関数です
#
def printMessage(topic, message):
# 受信データはbytes型なのでUTF-8の文字列に変換してから表示します
print("topic:" + topic.decode("utf-8") )
print("message:" + message.decode("utf-8") )
#
# メインの処理部です
#
# ブローカーのIPやトピック名を定義します。
mqttBroker = '192.168.10.9'
myId = 'esu'
topic= b'test/topic1'
# MQTTのオブジェクト(変数)を定義します
client = MQTTClient(myId, mqttBroker, keepalive=3600)
# 受信(Subscribe)した時に呼ぶ関数を設定します
client.set_callback(printMessage)
# Wi-Fiに接続します
connect()
try:
# ブローカーに接続します
client.connect()
# Subscribeするトピックを登録します。(注:接続前はエラー)
client.subscribe(topic)
except:
# ブローカーへの接続に失敗した場合は、プログラムを終了します
print("Could not connect to mqtt server.")
sys.exit()
print("mqqtt connect done ")
#
# 温度センサの情報を3秒ごとにPublishします
#
while True:
# Pico W 本体の温度情報を取得します
temp = pico_temp_sensor.temp
# 送信(Publish)用のメッセージに温度を代入します
msg = " \"temp\":" + str(temp)
# 送信(Publish)します。
client.publish(topic, msg)
# ブローカーからのPublishをチェックします。
client.check_msg()
# test/topic1に"on"を書くとmessage:onとでる!
time.sleep(3)
------------------------ pwm --------------------------------------------------
# https://hellobreak.net/raspberry-pi-pico-dc-motor/
from machine import Pin,PWM # 入出力とアナログ入出力、PWM制御用モジュールを準備
import time # タイマーモジュールを準備
led = Pin("LED", Pin.OUT) # 本体LEDピンをledとして出力に設定
from machine import PWM, Pin
import utime
IN1 = PWM(Pin(0))
IN2 = PWM(Pin(1))
IN1.freq(100)
IN2.freq(100)
max_duty = 32512
# 65025 too big for 4.5v supply to drv8833
while True:
print("for")
# 正転
IN2.duty_u16(0)
for i in range(50, 100):
IN1.duty_u16(int(max_duty*i*0.01))
utime.sleep(0.1)
print("stop")
# ブレーキ
IN1.duty_u16(max_duty)
IN2.duty_u16(max_duty)
utime.sleep(2)
print("rev")
# 逆転
IN1.duty_u16(0)
for i in range(50, 100):
IN2.duty_u16(int(max_duty*i*0.01))
utime.sleep(0.1)
print("stop2")
# 停止
IN1.duty_u16(0)
IN2.duty_u16(0)
utime.sleep(2)
0 件のコメント:
コメントを投稿