Showing posts with label EFR32MG24. Show all posts
Showing posts with label EFR32MG24. Show all posts

Wednesday, October 4, 2023

DIY SwitchBot using Matter Over Thread running on Silicon Labs EFR32xG24 Explorer Kit in 5 Minutes.

The following steps guide you to DIY SwitchBot using Matter Over Thread running on Silicon Labs EFR32xG24 Explorer Kit xG24-EK2703A in 5 minutes.

1. Make sure you install Silicon Labs Simplicity Studio v5 and GSDK 4.3.1 with Matter extension 2.1.0-1.1.

2.Connect your xG24-EK2703A kit to your desktop and create "Matter - SoC OnOff Plug over Thread" project as base for your DIY SwitchBot using Matter Over Thread project.

3.Install and add a "sg90" PWM instance in "SOFTWARE COMPONENTS"

4.Configure "sg90" instance to use Timer0 PA00 as PWM output with PWM frequency 50 Hz.

 

5. Replace source code in "void AppTask::ActionCompleted(OnOffPlugManager::Action_t aAction)" with the following codes (red lines), which implements on/off rotating position for SG90 to turn on/off switch.

//YK for SG90 SwitchBot
#include "sl_pwm.h"
#include "sl_pwm_init_sg90_config.h"
#include "sl_sleeptimer.h"

extern sl_pwm_instance_t sl_pwm_sg90;

void AppTask::ActionCompleted(OnOffPlugManager::Action_t aAction)
{
    // action has been completed on the outlet
    if (aAction == OnOffPlugManager::ON_ACTION)
    {
        SILABS_LOG("Outlet ON")
        sl_pwm_set_duty_cycle(&sl_pwm_sg90, 2);
        sl_pwm_start(&sl_pwm_sg90);
        sl_sleeptimer_delay_millisecond(200);
        sl_pwm_set_duty_cycle(&sl_pwm_sg90, 5);
        sl_sleeptimer_delay_millisecond(200);
        sl_pwm_stop(&sl_pwm_sg90);
    }
    else if (aAction == OnOffPlugManager::OFF_ACTION)
    {
        SILABS_LOG("Outlet OFF")
        sl_pwm_set_duty_cycle(&sl_pwm_sg90, 9);
        sl_pwm_start(&sl_pwm_sg90);
        sl_sleeptimer_delay_millisecond(200);
        sl_pwm_set_duty_cycle(&sl_pwm_sg90, 5);
        sl_sleeptimer_delay_millisecond(200);
        sl_pwm_stop(&sl_pwm_sg90);
    }
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT
    if (sAppTask.mSyncClusterToButtonAction)
    {
        chip::DeviceLayer::PlatformMgr().ScheduleWork(UpdateClusterState, reinterpret_cast<intptr_t>(nullptr));
        sAppTask.mSyncClusterToButtonAction = false;
    }
#endif
}

6. Add the following two line to set initial SG90 position after "sl_pwm_init(&sl_pwm_sg90, &pwm_sg90_config);" in "void sl_pwm_init_instances(void)".

  //YK for SG90 SwitchBot
  sl_pwm_set_duty_cycle(&sl_pwm_sg90, 5);
  sl_pwm_start(&sl_pwm_sg90);

7. Build and download firmware into xG24-EK2703A kit(remember to dowload bootloader into the kit too)

8. Connect GND, PWR, Signal line of SG90 to GND, 3V3, and PWM pin on xG24-EK2703A kit.

9.Start RTT viewer to get QR code link for Matter Provision.


 

10. Since Apple Home supports Matter Over Thread now, we use Apple HomePod mini (iOS 17) and iPhone Home App to add our DIY SwitchBot into Apple Home.

11. Now, we can mount the DIY SwitchBot to wall switch and use Apple Home to control wall switch/light remotely.



Thursday, August 10, 2023

Using IO Stream and EXP pins on Silabs EFR32MG24+BRD4001A as second UART

The following steps show you how to create GSDK4.3.1/EmberZnet 7.3.1.0 Z3Light example with  BRD4186C+BRD4001A and use sl_iostream_write/sl_iostream_read to output/read data to/from USART1 to EXP header pin11(RX PA06)/p13(TX PA07).

1. Add exp instance from software component

圖片圖片2. Add the following code to main.c

  1. #include "sl_iostream.h"
  2. sl_iostream_t * exp_iostream = NULL;
  3. uint8_t rbuffer[128];
  4. size_t bread;
  5.  
  6. void app_init(void)
  7. {
  8. exp_iostream = sl_iostream_get_handle("exp");
  9. }
  10.  
  11. void app_process_action(void)
  12. {
  13. sl_iostream_read(exp_iostream,rbuffer,128,&bread);
  14. if(bread>0){
  15. sl_iostream_write(exp_iostream,rbuffer,bread);
  16. }
  17. }

 3. Build and run the example.

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.