Difference between revisions of "RS9116 Radio Module"

From wiki.emacinc.com
Jump to: navigation, search
(Add wifi and bluetooth info)
Line 36: Line 36:
 
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.
 
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 ====
 
==== WiFi ====
 +
The WiFi setup is relatively straightforward. The <code>RS9116</code> module is part of the standard MicroPython <code>network</code> module, and is able to use the MicroPython <code>socket</code> module.
 +
===== Initialization =====
 +
Use the following code to initialize the module for WiFi usage.
 +
<syntaxhighlight lang=python>
 +
import network
 +
nic = network.RS9116()
 +
nic.init()
 +
</syntaxhighlight>
 +
===== 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''.
 +
<syntaxhighlight lang=python>
 +
nic.connect('SSID', 'PASSWORD')
 +
</syntaxhighlight>
 +
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)
 +
<syntaxhighlight lang=python>
 +
nic.scan()
 +
</syntaxhighlight>
 +
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).
 +
<syntaxhighlight lang=python>
 +
nic.ifconfig()
 +
</syntaxhighlight>
 +
===== Sockets =====
 +
More detailed examples can be found [https://docs.micropython.org/en/latest/esp8266/tutorial/network_tcp.html here], but a simple socket example is shown below. The demo can be stopped by entering a Ctrl-C keystroke.
 +
<syntaxhighlight lang=python>
 +
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='')
 +
</syntaxhighlight>
 +
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 ====
 
==== Bluetooth ====
 +
===== Initialization =====
 +
Initialization for Bluetooth/BLE is exactly the same as for WiFi.
 +
<syntaxhighlight lang=python>
 +
import network
 +
nic = network.RS9116()
 +
nic.init()
 +
</syntaxhighlight>
 +
===== 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.
 +
<syntaxhighlight lang=python>
 +
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)
 +
</syntaxhighlight>
 +
===== 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 [[Cutipy-MicroPython_Bluetooth_LE_demo|Bluetooth LE Demo]] page (and the source code linked there),
 +
<syntaxhighlight lang=python>
 +
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)
  
 +
</syntaxhighlight>
 
=== FreeRTOS ===
 
=== FreeRTOS ===
  

Revision as of 11:10, 13 April 2020

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

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 Test Software

Cutipy-MicroPython Bluetooth LE demo

Micropython

CutiPy-Installing Micropython Firmware

RS9116 resources