Broadcast UDP Packet

Gecko OS devices broadcast module properties in JSON format. The properties can be sent either as UDP packets to a UDP host or by a post request to an HTTP host. Properties include by default the IP address and the MAC address.

See the broadcast variables.

This application note demonstrates how to send and receive a broadcast UDP packet, and how to extract properties from the JSON format data.

This application uses a Gecko OS device and a Python Script (Python v2.7).

The Gecko OS device is programmed with the factory default configuration.

The Python script listens for UDP packets and validates any packet received as JSON data. It then extracts the IP address from the JSON data.

Setup

The following assumes that the module starts in its default state. To return it to the default state, perform a factory reset .

The python script runs on a test computer on the same Wi-Fi network as the Gecko OS device.

Copy the following python script to the test computer: broadcast_udp_packet.py

The Python script procedure is:

  1. While no IP address has been successfully parsed:
    • Wait until the server receives a broadcast UDP packet from the module
    • When a packet is received, parse the packet for the module's IP address
    • If the packet is parsed as valid JSON and the module IP address is received, return.
  2. Display the JSON packet and the IP address
  3. Repeat from step 1

To break out of the Python script loop, press Ctrl-C (Command-C on a Mac.)

Method

Open a Gecko OS Terminal to the Gecko OS device

To open a Gecko OS terminal connection to the Gecko OS device, see Getting Started .

Associate Module to Wi-Fi Network

To associate the module to a Wi-Fi network, set the values of wlan.ssid and wlan.passkey and run the network_up command. See Getting Started .

Issue the following commands to join the network automatically and reduce the broadcast interval to 2 seconds:

Module Gecko OS commands Comments

set wlan.auto_join.enabled 1
set broadcast.interval 2
save
reboot

Join network automatically on reboot
Set broadcast interval to 2 seconds
Save variables
Reboot to restart the periodic UDP broadcast service

The module automatically broadcasts UDP packets every 2 seconds after association to an AP has completed.

Run Python Script

Execute the broadcast_udp_packet.py python script. The script displays the following text:

Listening for Gecko OS device broadcast UDP packets...

Within the broadcast interval of 2 seconds, the python script prints a result similar to the following:

Broadcast UDP Packet (json format):
{
    "mac"     : "4C:55:CC:10:06:12",
    "bssid"   : "30:85:A9:E7:9C:B0",
    "channel" : 11,
    "ip"      : "192.168.1.173",
    "ssid"    : "My Network",
    "rssi"    : -28,
    "time"    : 1416801734637,
    "version" : "SILABS-WGM160P-4.0.0,
                 Gecko_OS-STANDARD-4.0.12-1198,
                 WGM160P",
    "remote_terminal_port" : 2000
}

IP found in broadcast UDP packet: 192.168.1.173

Change Properties in Broadcast UDP Packet

At the Gecko OS prompt, enter the following command to change the properties in the broadcast UDP packet:

Module Gecko OS Commands Comments

set broadcast.data ip,mac
save
reboot

Set broadcast properties to IP and MAC address
Save variables
Reboot to restart the periodic broadcast service

The python script now prints text similar to:

Listening for Gecko OS device broadcast UDP packets...
Broadcast UDP Packet (json format):
{
    "mac" : "4C:55:CC:10:06:12",
    "ip"  : "192.168.1.173"
}

IP found in broadcast UDP packet: 192.168.1.173

Note that only the mac (module MAC address) and ip (module IP address) properties are broadcast.

Set the IP Address to the Test Computer

The module can be configured to send the broadcast UDP packet to a fixed IP address other than the broadcast address of 255.255.255.255 . At the Gecko OS prompt, enter the following command to set the target IP for the broadcast UDP packet.

Module Gecko OS Commands Comments

set broadcast.udp.ip <IP address of test computer>
save
reboot

Restrict broadcast to test computer
Save variables
Reboot to use updated IP address

The python script continues to print the same result as in the previous step.

Set IP address to Target Other than Test Computer

Set the broadcast.udp.ip variable to some address on your network other than your test computer.

Module Gecko OS commands Comments

set broadcast.udp.ip <not ip of test computer>
save
reboot

Restrict broadcast to other host
Save
Reboot

The python script stops printing results, because it never sees the broadcast UDP packet.

Now, while the script is running, follow the steps in Set the IP Address to Test Computer . Within two seconds of the Gecko OS device network coming up, the script begins to display the extracted IP address again.

Send ADC Values and GPIO Values in the Broadcast UDP Packet

Specify ADCs by ADC number, not by GPIO.

GPIO0 corresponds to Button 1 on Gecko OS evaluation boards.

Module Gecko OS commands Comments

set broadcast.data ip,mac,adc0,adc9,gpio0
save
reboot

Add ADC0, ADC9, GPIO0 to broadcast properties
Save
Reboot

The python script displays a result similar to the following:

Listening for Gecko OS device broadcast UDP packets...
Broadcast UDP Packet (json format):
{
    "mac"   : "4C:55:CC:10:06:12",
    "ip"    : "192.168.1.173",
    "gpio0" : 0,
    "adc0"  : 1845,
    "adc9"  : 1513
}

IP found in broadcast UDP packet: 192.168.1.173

ADC values in the packet are in decimal. To convert to mV, see Peripherals, ADCs . The python script displays a result similar to the following:

Listening for Gecko OS device broadcast UDP packets...
Broadcast UDP Packet (json format):
{
    "mac"   : "4C:55:CC:10:06:12",
    "ip"    : "192.168.1.173",
    "gpio0" : 1,
    "adc0"  : 1845,
    "adc9"  : 1513
}

Release Button 1 and the value for gpio0 returns to 0.


Supporting Gecko OS Versions

Change Log

Modified Changes
2019-01-01 Created