This topic describes how to develop firmware for a Bluetooth Mesh smart light device based on the TG7100B chip using the light_ctl application example from the Bluetooth Mesh SDK.
Background information
The light_ctl application example provides the following features.
- Complies with the Bluetooth Mesh Module Software Specifications and the Bluetooth Mesh Device Extension Protocol. It supports network provisioning and control through the Tmall Genie speaker and Tmall Genie app for Tmall Genie ecosystem products. It also supports network provisioning and control through the Cloud Intelligence app for private label products.
- Supports control of the light's on/off state, brightness, color temperature, and scene modes.
- Supports device over-the-air (OTA) updates through IoT Platform and the Tmall Genie app. This feature is currently available only for Tmall Genie ecosystem projects.
TG7100B overview
The TG7100B is a cost-effective Bluetooth chip customized by Tmall Genie for Bluetooth Mesh access. It features a simple circuit design, excellent radio frequency (RF) performance, low power consumption, and an automotive-grade temperature range of -40 °C to 125 °C. For documents and software tools related to the TG7100B chip, see TG7100B.
For issues related to the TG7100B chip, such as drivers, production testing, hardware design, and RF, contact technical support through your business representative. You can also obtain support for application development using the IoT Platform Bluetooth Mesh SDK, including product configuration, network provisioning, cloud connectivity, and OTA updates.
Compile and flash the firmware
- Download the SDK. For the download link, see Get the SDK. Make sure that you download TG7100B SDK V1.3.4.
- Configure the development environment. For more information, see Prepare the development environment.
- Compile the firmware. For more information, see Develop device firmware.Note
- Generated flashing file: out/bluetooth.light_ctl@tg7100b/binary/total_image.hexf
- Generated OTA file: out/bluetooth.light_ctl@tg7100b/binary/fota.bin
- Flash the firmware. For more information, see Flash the firmware.
- Flash the device certificate.
- Disconnect the TG71XX Programmer.exe flashing tool, set the DIP switch on the development board to GND, and press the reset button on the board.
- Open the SecureCRT serial debugging tool, select File > Quick Connect, and configure the serial port parameters for the development board as shown in the following figure. The default baud rate is 512000.Note The port number must be the same as the one that is automatically detected by the TG71XX Programmer.exe tool. You can also find the port number by right-clicking This PC and selecting Manage > System Tools > Device Manager > Ports (COM & LPT). The path to Device Manager may vary based on your operating system.

- Click Connect.
- Enter the following command to flash the device certificate.
set_tt <ProductID> <Device Secret> <Device Name>Note The Product id, Device Secret, and Device Name (MAC address) in this command are the device certificate credentials that are generated when you add a device.
Application model configuration
For more information about Mesh Models, see Mesh Model guide for Bluetooth Mesh light applications. The light_ctl application example in the Bluetooth Mesh SDK implements control only for the on/off state, brightness, color temperature, and scene modes. The following table lists the Thing Specification Language (TSL) models and Attribute Types that are supported by default in the firmware.
| Element | Name | Model | Attribute Type | Attribute Parameter | Notes |
| Light (Primary element) | On/Off | Generic OnOff Server 0x1000 | N/A | N/A | Required |
| Lightness | Lightness Server 0x1300 | Optional | |||
| Color Temperature | Light CTL Server 0x1303 | Optional | |||
| Mode | Scene Server 0x1203 | Optional | |||
| Error Code | Vendor Model Server 0x01A80000 | 0x0000 | 1 byte | Optional | |
| On/Off | 0x0100 | 1 byte: 0 for Off, 1 for On | Required. The status is consistent with the on/off status of the Generic OnOff Server Model. It is used for the device to proactively report its on/off status. | ||
| Lightness | 0x0121 | 2 bytes: 0 to 65535 | Optional. The status is consistent with the lightness status of the Lightness Server Model. It is used for the device to proactively report the lightness property. | ||
| Color Temperature | 0x0122 | 2 bytes: 800 to 20000 | Optional. The status is consistent with the color temperature status of the Light CTL Server. It is used for the device to proactively report the color temperature property. | ||
| Mode | 0xF004 | 2-byte enumeration | Optional. The status is consistent with the Scene Server. It is used for the device to proactively report the mode property. | ||
| Event | 0xF009 | 1 byte |
| ||
| Timed On/Off | 0xF010 | Variable | Optional | ||
| Time Zone | 0xF01E | 1 byte: -12 to 12 | Optional | ||
| Time | 0xF01F | 4 bytes: standard UNIX time | Optional |
The code that corresponds to the model configuration is in the app/example/bluetooth/light_ctl/light_ctl.c file, as shown below.
/* SIG general-purpose model definitions in the light product */
static struct bt_mesh_model primary_element[] = {
BT_MESH_MODEL_CFG_SRV(), /* Configuration Server */
BT_MESH_MODEL_HEALTH_SRV(), /* Health Server */
MESH_MODEL_GEN_ONOFF_SRV(&light_elem_stat[0]), /* On/Off Generic OnOff Server */
MESH_MODEL_LIGHTNESS_SRV(&light_elem_stat[0]), /* Lightness Server */
MESH_MODEL_CTL_SRV(&light_elem_stat[0]), /* Color Temperature Light CTL Server */
MESH_MODEL_SCENE_SRV(&light_elem_stat[0]), /* Scene Mode Scene Server */
};
/* Vendor-defined model definitions */
static struct bt_mesh_model primary_vendor_element[] = {
MESH_MODEL_VENDOR_SRV(&light_elem_stat[0]),
};
/* Register the SIG general-purpose models and vendor-defined models for the main element of the light. GENIE_ADDR_LIGHT is defined as the multicast address 0xC000 for the light category. */
struct bt_mesh_elem light_elements[] = {
BT_MESH_ELEM(0, primary_element, primary_vendor_element, GENIE_ADDR_LIGHT),
};Application layer event handling
The code for application layer event handling is located in the app/example/bluetooth/light_ctl/light_ctl.c file, as shown below.
static void light_ctl_event_handler(genie_event_e event, void *p_arg)
{
switch (event)
{
case GENIE_EVT_SW_RESET: /* Software reset */
{
light_param_reset();
light_led_blink(3, 1);
aos_msleep(3000);
}
break;
case GENIE_EVT_MESH_READY: /* The Mesh protocol stack is ready. Data can be sent and received. */
{
//User can report data to cloud at here
GENIE_LOG_INFO("User report data");
light_report_poweron_state(&light_elem_stat[0]);
}
break;
case GENIE_EVT_SDK_MESH_PROV_SUCCESS: /* Network provisioning successful */
{
light_led_blink(3, 0); /* The light blinks to indicate successful network provisioning. */
}
break;
#ifdef CONFIG_MESH_MODEL_TRANS
case GENIE_EVT_USER_TRANS_CYCLE:
#endif
case GENIE_EVT_USER_ACTION_DONE: /* Light effect transition finished */
{
sig_model_element_state_t *p_elem = (sig_model_element_state_t *)p_arg;
light_update(p_elem);
if (event == GENIE_EVT_USER_ACTION_DONE)
{
light_save_state(p_elem);
}
}
break;
case GENIE_EVT_SIG_MODEL_MSG: /* Received a downstream SIG Model message */
{
sig_model_msg *p_msg = (sig_model_msg *)p_arg;
if (p_msg)
{
GENIE_LOG_INFO("SIG mesg ElemID(%d)", p_msg->element_id);
}
}
break;
case GENIE_EVT_VENDOR_MODEL_MSG: /* Received a downstream Vendor Model message */
{
genie_transport_model_param_t *p_msg = (genie_transport_model_param_t *)p_arg;
if (p_msg && p_msg->p_model && p_msg->p_model->user_data)
{
sig_model_element_state_t *p_elem_state = (sig_model_element_state_t *)p_msg->p_model->user_data;
GENIE_LOG_INFO("ElemID(%d) TID(%d)", p_elem_state->element_id, p_msg->tid);
}
}
break;
#ifdef CONFIG_GENIE_MESH_USER_CMD /* Used to implement a custom serial protocol */
case GENIE_EVT_DOWN_MSG:
{
genie_down_msg_t *p_msg = (genie_down_msg_t *)p_arg;
//User handle this msg,such as send to MCU /* Here, you can forward data to the MCU based on a custom serial protocol. */
if (p_msg)
{
}
}
break;
#endif
#ifdef MESH_MODEL_VENDOR_TIMER
case GENIE_EVT_TIMEOUT: /* Entry point for handling local TimerOnOff timeouts */
{
vendor_attr_data_t *pdata = (vendor_attr_data_t *)p_arg;
//User handle vendor timeout event at here
if (pdata)
{
light_ctl_handle_order_msg(pdata); /* The light On/Off operation is performed here. */
}
}
break;
#endif
default:
break;
Serial command description
The following serial commands can be used for development and debugging.
| Command Name | Description | Example |
| set_tt | Bluetooth Mesh device certificate | set_tt 5297793 0c51b11c6ec78b52b803b3bbaae64fba 486e704a5bf6 |
| get_tt | View the Bluetooth Mesh device certificate. | No parameters |
| get_info | View information such as the version and MAC address. | No parameters |
| reboot | Restart the system. | No parameters |
| reset | Reset the device. | No parameters |
| mesg | Send Mesh data. | mesg D4 1 F000 000101 |
- The first parameter, D4, is the abbreviation for the first byte of the Opcode for the Vendor Message Attribute Indication message. Other values include D3, CE, and CF.
- The second parameter specifies the sending mode and number of retries:
- 0: No retries.
- 1-252: The number of retries.
- 253: Uses the first byte of the payload as the time interval parameter, in units of 100 ms. For example, `mesg D4 253 F000 030201` sends 0201 every 300 ms, and `mesg D4 253 F000 1e0201` sends 0201 every 3 seconds.
- 254: Resends the message when a reply is received or a timeout occurs.
- 255: Sends the message automatically once per second.
- The third parameter is the destination address. It must be four characters long. If this parameter is set to 0000, the Mesh gateway multicast address F000 is used by default.
- The fourth parameter is the content to send. For example, 000101 sends 0x00, 0x01, and 0x01. The content must be an even number of hexadecimal characters (0-9, a-f).
Firmware macro definitions
| Macro name | Description |
| CONFIG_BT_MESH_GATT_PROXY | Enables the Proxy feature. |
| CONFIG_BT_MESH_PB_GATT | Enables network provisioning using a mobile phone. |
| CONFIG_BT_MESH_RELAY | Enables the relay feature. |
| CONFIG_GENIE_OTA | Enables the OTA feature for mobile phones. |
| CONFIG_GENIE_RESET_BY_REPEAT | Enables the feature to enter network provisioning mode by powering the device on and off five consecutive times. |
| PROJECT_SW_VERSION | Configures the version number, which is used for the OTA feature. The data type is int32. |
| CONFIG_PM_SLEEP | Enables the low-power feature. |
| CONFIG_GENIE_MESH_GLP | Enables the low-power feature in GLP mode. |
| CONFIG_DEBUG | Enables BT_xxx log output. |
| CONFIG_BT_DEBUG_LOG | Enables BT_DBG log output. |
| MESH_DEBUG_PROV | Enables log output for network provisioning. |
| MESH_DEBUG_TX | Enables log output for sending Mesh data at the Access layer. |
| MESH_DEBUG_RX | Enables log output for receiving Mesh data at the Access layer. |