Saturday, April 15, 2023

Amazon Sidewalk Test over BLE using Silcon Labs EFR32MG24 BRD4186C

Amazon Sidewalk is broadly announced in US market. It leverage exising Amazon devices such as Echo, Ring Alarm Panel, or DoorPhone. The following steps show you how to setup Silicon Labs EFR32MG24 radio board BRD4186C to test sending and receiving packets by Amazon Sidewalk. 

1. Setup my Amazon Echo 4th generation. Most important thing is that Amazon Sidewalk is only available in US region so your IP address needs to be in US region. If not, you have to setup VPN for this.

2. Connect BRD4186C to my desktop and start Simplicity Studio v5 to create "Amazon Sidewalk - SoC Bluetooth Hello Neighbor" project, which is available in Silicon Labs GSDK 4.2.2.

3. Add and revise the following code in on_sidewalk_msg_received to receive on/off command (1 as on and 0 as off) later from AWS IoT MQTT client to toggle LED0 on BRD4001A or BRD4002A mainboard.

#include <stdlib.h>
#include "sl_simple_led.h"

extern const sl_led_t sl_led_led0;

static void on_sidewalk_msg_received(const struct sid_msg_desc *msg_desc,
                                     const struct sid_msg *msg,
                                     void *context)
{
  char cmd;
  UNUSED(context);
  app_log_info("App - received message (type: %d, id: %u, size %u)", (int)msg_desc->type, msg_desc->id, msg->size);
  app_log_info("App - %s", (char *) msg->data);
  memcpy(&cmd, msg->data,1);
  if(cmd=='1'){
    sl_led_turn_on(&sl_led_led0);
    app_log_info("from YK App - on");
  }
  else if(cmd=='0'){
      sl_led_turn_off(&sl_led_led0);
      app_log_info("from YK App - off");
    }
}

4. Revise the following code in send_counter_update to send "YK Amazon Sidewalk test %d" when press button 1 on BRD4001A or BRD4002A mainboard.

char buffer[64];

static void send_counter_update(app_context_t *app_context)
{
  if (app_context->state == STATE_SIDEWALK_READY
      || app_context->state == STATE_SIDEWALK_SECURE_CONNECTION) {
    app_log_info("App - sending counter update: %d", app_context->counter);

    sprintf(buffer,"YK Amazon Sidewalk test %d\0", app_context->counter);
    struct sid_msg msg = {
      .data = (uint8_t *)buffer,//&app_context->counter,
      .size = strlen(buffer)//sizeof(uint8_t)
    };
    struct sid_msg_desc desc = {
      .type = SID_MSG_TYPE_NOTIFY,
      .link_type = SID_LINK_TYPE_ANY,
    };

    sid_error_t ret = sid_put_msg(app_context->sidewalk_handle, &msg, &desc);
    if (ret != SID_ERROR_NONE) {
      app_log_error("App - failed queueing data: %d", (int)ret);
    } else {
      app_log_info("App - queued data message id: %u", desc.id);
    }

    app_context->counter++;
  } else {
    app_log_error("App - sidewalk is not ready yet");
  }
}

5. Build and download "Amazon Sidewalk - SoC Bluetooth Hello Neighbor" firmware into BRD4186C. Also need to download bootloader into BRD4186C to make application work.

6. Login your Amazon "Identity and Access Management (IAM)" and go to "Security credentials" to create "access key ID" and "secret access key" for provision your Amazon Sidewalk device.

7. Refer to AWS Command Line Interface (CLI) to Install AWS CLI version 1.

8. Start a Dos console to run "aws configure" to input your "Security credentials" to create "access key ID" that are created in step 6.

9. Follow steps in Provision your Amazon Sidewalk Device.

10. Disconnect and reconnect USB cable from BRD4001A/BRD4002A to restart BRD4186C, and start RTT viewer. You should able to see your "Amazon Sidewalk - SoC Bluetooth Hello Neighbor" running on BRD4186C.

10. You can press button 0 on BRD4001A or BRD4002A mainboard to get connect and button 1 to send messages. You can refer to "Send Data"section in Interacting with the Cloud to use AWS MQTT client test to check message received.


11. For sending on/off message to control LED0 on BRD4001A or BRD4002A mainboard, you can refer to "Receive Data"section in Interacting with the Cloud to start a Dos console to use the following command to send on (or off) command.

aws iotwireless send-data-to-wireless-device --id=xxxx --transmit-mode 0 --payload-data="MQ==" --wireless-metadata "Sidewalk={Seq=1}"

If the device receives command from Amazon Sidewalk, it would pops the following message on RTT viewer and LED0 would be turned on (or off).

p.s. payload-data is Base64 encoded data. "MQ==" is equal to "1" which is used for on command in my code.

12. For power consumption, it's about 132uA when device is idle and about 810 uA when device is receiving message or sending message according to Energy Profiler. These power consumption numbers are quite satisfying for battery power device.



No comments:

Post a Comment