---
# System prepended metadata

title: Lab06 改用 Adafruit 上傳與呈現數據
tags: [FM623A]

---

# 中華電信 IOT 平台不再提供註冊和使用
![messageImage_1704163082176](https://hackmd.io/_uploads/SktrP_MOp.jpg)

# 改成使用 Adafruit.io
1. 前往官網點擊右上角進行註冊
![FM623A_7](https://hackmd.io/_uploads/SJTOrFGd6.png)
如有帳號可以直接登入
![FM623A_8](https://hackmd.io/_uploads/BJo2rKM_p.png)
2. 登入後請點擊 Feeds, 再點擊 New Feed 新增專案
![FM623A_9](https://hackmd.io/_uploads/S1FGuYMO6.png)
3. 新增一個記錄溫度的專案, 依照下圖建立 Feed
![FM623A_10](https://hackmd.io/_uploads/SJfDuFMu6.png)
4. 完成後如下圖
![FM623A_11](https://hackmd.io/_uploads/H1gZtFMOa.png)
5. 接著請點擊右上角鑰匙圖示
![FM623A_12](https://hackmd.io/_uploads/H1iXFKf_6.png)
6. 取得用戶名和金鑰
![FM623A_13](https://hackmd.io/_uploads/B1jkqtz_p.png)
7. 以下為 Lab06 修改後的程式碼, 請將剛才取得的用戶名和金鑰填入到 "你的Adafruit用戶名" 和 "你的AIO密鑰"
```
from machine import Pin,ADC
import time
from keras_lite import Model  
import ulab as np
import network     # 匯入network模組
from umqtt.robust import MQTTClient

# 連線至無線網路
sta=network.WLAN(network.STA_IF)
sta.active(True)   
sta.connect('無線網路名稱','無線網路密碼') 

while not sta.isconnected() :
    pass

print('Wifi連線成功')

# Adafruit IO 設定
aio_username = "你的Adafruit用戶名"
aio_key = "你的AIO密鑰"
mqtt_client = MQTTClient(
    client_id=aio_username,
    server="io.adafruit.com",
    user=aio_username,
    password=aio_key,
    ssl=False)

mean = 170.98275862068965
std = 90.31162360353873
model = Model('temperature_model.json')

adc_pin = Pin(36)
adc = ADC(adc_pin)
adc.width(ADC.WIDTH_9BIT)
adc.atten(ADC.ATTN_11DB)

mqtt_client.connect()
print("MQTT 連線成功")

while True:
    data = 0
    for i in range(20):
        thermal = adc.read()
        data += thermal
        time.sleep(0.01)

    data = data / 20
    data = np.array([int(data)])
    data = data - mean
    data = data / std
    tem = model.predict(data)
    tem = round(tem[0] * 100, 1)
    print(tem, end='   ')

    # 將數據發布到 Adafruit IO
    topic = aio_username + "/feeds/temperature"
    mqtt_client.publish(topic, str(tem))
    print("數據已發布至 Adafruit IO")

    time.sleep(60)  # 暫停60秒

```
8. 執行後結果如下：
![FM623A_5](https://hackmd.io/_uploads/ryFzyKfOa.png)
9. 回去專案中會看到折線圖和上傳數據：
* 折線圖
![FM623A_3](https://hackmd.io/_uploads/ByhNJYMOa.png)
* 上傳數據
![FM623A_4](https://hackmd.io/_uploads/r1hNJKfd6.png)

