> For the complete documentation index, see [llms.txt](https://sharinghub.kittenbot.hk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sharinghub.kittenbot.hk/mcu/futureboard/micropython/api10.md).

# 未來板MicroPython編程10：無綫通訊

### 導入未來板庫

需要先導入未來板的庫才可以使用未來板的硬件。

```
from future import *
```

### 10: 無綫通訊

### 導入無綫通訊庫

```
from radio import *
```

#### 1. 初始化無綫通訊

```
r=Radio(trigger)
```

可以在括號裏呼叫函數，每當收到訊息時會自動觸發。

#### 2. 設定通訊頻道

```
r.channel=1
```

需要雙方都在同一個頻道上才能通訊。

#### 3. 讀取訊息

```
r.read()
```

#### 4. 發佈信息

```
r.send(msg)
```

#### 範例程式1：直接讀取

```
import time
from radio import *

r = Radio()
print('channel', r.channel)
r.channel = 12 

while True:
    time.sleep(1)
    a = r.read()
    if a:
        print('get', a)
```

#### 範例程式2：自動觸發

```
from radio import *

def onmsg(msg, mac):
    print('get', msg, 'from', mac)

r = Radio(onmsg)
print('channel', r.channel)
r.channel = 12
r.send("hello world")
```

#### 進階範例程式：MESH無綫通訊(ESPNOW)

```
# 1. 啟動WiFi
import network
w0 = network.WLAN(network.STA_IF)
w0.active(True)

# 2. 取得自機的MAC地址
mac = w0.config('mac')
>>> mac
b'\xc4O3"\xdb\x89'

# 3. 導入espnow
from esp import espnow
e = espnow.ESPNow()
e.init()

# 4. 加入對方的MAC地址
# 注意： 不是自己的MAC地址
e.add_peer(b'\x8c\xaa\xb5\xb9\xf8\xf0')

# 5. 信息觸發
def rxcb(mac, msg):
    print("Recv:", mac, msg)
e.on_recv(rxcb)

# 6. 發佈信息
e.send(b'\x8c\xaa\xb5\xb9\xf8\xf0', 'hello world')

# 在接收端可以看到
# Recv: b'\xc4O3"\xdb\x89' b'hello world'
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://sharinghub.kittenbot.hk/mcu/futureboard/micropython/api10.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
