RS9116 Radio Module
This page will show how to use the RS9116 radio module in both MicroPython and FreeRTOS. The RS9116 module currently supports both WiFi and Bluetooth 5.
Background
Hardware Required
- Desktop PC (Windows/Linux/Mac will work)
- CutiPy R1 or MitiPy R1 board with RS9116 populated
- Micro USB cable
Software Required
- A shell program such as rshell (MicroPython only)
- The latest firmware for the board (MicroPython or FreeRTOS) See here for details
General Information
Using the RS9116
MicroPython
EMAC provides a MicroPython driver for the RS9116 that supports both WiFi and Bluetooth. Both WiFi and Bluetooth can be used concurrently, in the right application.
WiFi
The WiFi setup is relatively straightforward. The RS9116
module is part of the standard MicroPython network
module, and is able to use the MicroPython socket
module.
Initialization
Use the following code to initialize the module for WiFi usage.
import network
nic = network.RS9116()
nic.init()
Connecting
Once the RS9116 module is initialized, it can be connected to a network. In the following example, the network is SSID and the network key is PASSWORD.
nic.connect('SSID', 'PASSWORD')
If the network name is unknown, it can be scanned for by using the scan function. The function returns a list of 5-tuples with (SSID, RSSI value, security mode, RF channel, BSSID)
nic.scan()
Once connected, the IP address of the board can be retrieved with the following function. The function returns a 4-tuple with (ip address, subnet mask, gateway, MAC address).
nic.ifconfig()
Sockets
More detailed examples can be found here, but a simple socket example is shown below. The demo can be stopped by entering a Ctrl-C keystroke.
import socket
addr_info = socket.getaddrinfo("towel.blinkenlights.nl", 23)
addr = addr_info[0][-1]
s = socket.socket()
s.connect(addr)
while True:
data = s.recv(500)
print(str(data, 'utf8'), end='')
This demo will display an ASCII animation of the original Star Wars film. The data is retrieved over the internet, so it shows how well the RS9116 handles sending and receiving a lot of packets.
Bluetooth
Initialization
Initialization for Bluetooth/BLE is exactly the same as for WiFi.
import network
nic = network.RS9116()
nic.init()
Setup
Unlike WiFi, Bluetooth requires more setup to get going. A service handler must be added, attributes must be added, and advertise must be enabled. The following code sets the module up to advertise a write property and two read properties, with one having notify enabled.
handle = nic.add_service(0xaabb)
write_handle = nic.add_attribute(handle, 0x1aa2, nic.ATT_PROPERTY_WRITE)
read_sw_handle = nic.add_attribute(handle, 0x2bb1, nic.ATT_PROPERTY_NOTIFY | nic.ATT_PROPERTY_READ)
read_rand_handle = nic.add_attribute(handle, 0x3cc4, nic.ATT_PROPERTY_READ)
nic.set_antenna(nic.INTERNAL_ANTENNA)
nic.set_adv_data(name)
nic.set_scan_resp_data(name)
nic.set_local_name('{} Peripheral'.format("ADVERT_NAME"))
### Optional
local_mac = nic.get_local_addr()
local_name = nic.get_local_name()
print('Local Device MAC Address = {}'.format(local_mac))
print('Local Device Name = {}'.format(local_name))
###
nic.advertise(True)
Events
Once the module is setup, the board can be put into a loop to check events as they come in. The following code is very simplified. If more information is needed, please see the Bluetooth LE Demo page (and the source code linked there),
while True:
event = nic.get_event()
if event == nic.NO_EVENT:
nic.clear_event(event)
continue
if event == nic.CONNECT_EVENT:
print('Connected')
addr = nic.get_remote_addr()
print('Remote Device MAC Address = {}'.format(addr))
print('Remote Device RSSI = {}'.format(nic.get_rssi(addr)))
print('Remote Device State = {}'.format(nic.get_device_status()))
elif event == nic.DISCONNECT_EVENT:
print('Disconnected')
elif event == nic.WRITE_EVENT:
event_rsp = nic.get_event_data(event)
# Do something with event_rsp.
# event_rsp[0] contains the text or bytes sent from the client
else:
print('Unexpected Event {}'.format(event))
nic.clear_event(event)
FreeRTOS
Conclusion
Pages with Related Content
CutiPy Micropython Documentation
Cutipy-MicroPython Bluetooth LE demo