### ex3-1 ### # Adafruit IO 網站 https://io.adafruit.com ### ex3-2 ### https://teachablemachine.withgoogle.com/train/image ### ex3-3 ### # Lab04 心情日記的測試網頁 https://flagtech.github.io/FM633A/lab04.html ### ex3-4 ### import network sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect('無線網路名稱', '無線網路密碼') >>> ip = sta_if.ifconfig() >>> type(ip) # type() 可查物件的型別 >>> print(ip[0]) ### ex3-5 (Python 的資料結構 (容器)) ### >>> string = "52python" # 字串容器:由字元組成 >>> string[1] # 索引字串容器,傳回字串 "2" >>> tuple = (1, '2', 3) # tuple 容器:由資料物件組成 >>> tuple[2] # 索引 tuple 容器,傳回整數 3 >>> list = [1, (1, 2, 3), 3] # 串列容器:由資料物件組成 >>> list[1] # 索引串列容器,傳回 tuple (1, 2, 3) >>> set = {1, '2', 3} # 集合容器:由資料物件組成 >>> set[0] # 索引集合容器,傳回整數 1 >>> dick = {'A':1, 'B':'2', 'C':3} # 字典容器:由資料物件組成,以鍵:值表示 >>> dick['B'] # 已鍵索引字典容器,傳回字串 "2" ### ex3-6 #### # for 迴圈走訪容器 tuple = (1, (2, "3"), 4) j = 0 for i in tuple: print(i) j += 1 print(j) ### ex3-7 ### import network sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect('無線網路名稱', '無線網路密碼') while not sta_if.isconnected(): pass print(sta_if.ifconfig()[0]) ### ex3-8 (lab05.py) ### import network import ledstrip from umqtt.robust import MQTTClient led_strip_effect = b'0' ledstrip.setup(4, 15) sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect("無線網路基地台", "無線網路密碼") while not sta_if.isconnected(): pass print("控制板已連線") # 建立 MQTT 客戶端物件 client = MQTTClient( client_id = "D1mini", server = "io.adafruit.com", user = "AIO 帳號", password = "AIO 金鑰", ssl = False) # 註冊收到訂閱資料時的處理函式 def get_cmd(topic, msg): global led_strip_effect # 宣告使用全域變數 led_strip_effect print(topic, msg) led_strip_effect = msg # 設定條燈特效項目 client.connect() client.set_callback(get_cmd) client.subscribe(client.user.encode() + b"/feeds/mood") while True: client.check_msg() # 查看是否有資料傳入,有的就執行 client.set_callback(get_cmd) if led_strip_effect == b'0': ledstrip.clear() elif led_strip_effect == b'1': ledstrip.rainbow_cycle(5) # rainbow_cycle(間隔毫秒時間) elif led_strip_effect == b'2': ledstrip.cycle(123, 0, 20, 100) # cycle(r, g, b, 間隔毫秒時間) elif led_strip_effect == b'3': ledstrip.bounce(23, 20, 128, 50) # bounce(r, g, b, 間隔毫秒時間) ### ex3-9 ### # Lab05 心情分享氣氛燈的測試網頁 https://flagtech.github.io/FM633A/lab05.html