# Programming with MicroPython: Wireless Radio

### Import FutureBoard Library

Import the Library to make use of its functions.

```
from future import *
```

### 10: Wireless Radio

### Import Radio Library

```
from radio import *
```

#### 1. Initiate a Radio

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

Define an event trigger function and call it in the trigger parameter to automatically call the function on receiving message.

#### 2. Set the radio channel

```
r.channel=1
```

#### 3. Read the channel feed

```
r.read()
```

Returns the message.

#### 4. Publish a message to channel

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

#### Sample Program 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)
```

#### Sample Program 2: Event Triggers

```
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")
```

#### Advanced Program: MESH with ESPNOW

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

# 2. MAC Address
mac = w0.config('mac')
>>> mac
b'\xc4O3"\xdb\x89'

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

# 4. Add peer MAC Address
# Note: Add the addess of the peer device
e.add_peer(b'\x8c\xaa\xb5\xb9\xf8\xf0')

# 5. Event Trigger
def rxcb(mac, msg):
    print("Recv:", mac, msg)
e.on_recv(rxcb)

# 6. Publish
e.send(b'\x8c\xaa\xb5\xb9\xf8\xf0', 'hello world')

# Seeable on receving end
# Recv: b'\xc4O3"\xdb\x89' b'hello world
```


---

# Agent Instructions: 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:

```
GET https://sharinghub-eng.kittenbot.hk/main-controllers/futureboard/futureboard-micropython-programming-tutorial/programming-with-micropython-wireless-radio.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
