Rainmaker Lights Switch

This commit is contained in:
2025-06-28 06:39:58 -04:00
parent bf9d1a3886
commit 29eabe95bc
8 changed files with 258 additions and 253 deletions

View File

@@ -3,7 +3,7 @@
# project subdirectory.
#
PROJECT_NAME := led_light
PROJECT_NAME := switch
PROJECT_VER := 1.0
# Add RainMaker components and other common application components

View File

@@ -1,4 +1,4 @@
# LED Light Example
# Switch Example
## Build and Flash firmware
@@ -6,17 +6,15 @@ Follow the ESP RainMaker Documentation [Get Started](https://rainmaker.espressif
## What to expect in this example?
- This example uses the BOOT button and RGB LED on the ESP32-S2-Saola-1/ESP32-C3-DevKitC board to demonstrate a lightbulb.
- The LED acts as a lightbulb with hue, saturation and brightness.
- Pressing the BOOT button will toggle the power state of the lightbulb. This will also reflect on the phone app.
- This example uses the BOOT button and RGB LED on the ESP32-S2-Saola-1/ESP32-C3-DevKitC board to demonstrate a switch.
- The LED state (green color) indicates the state of the switch.
- Pressing the BOOT button will toggle the state of the switch and hence the LED. This will also reflect on the phone app.
- Toggling the button on the phone app should toggle the LED on your board, and also print messages like these on the ESP32-S2 monitor:
```
I (16073) app_main: Received value = true for Lightbulb - power
I (16073) app_main: Received value = true for Switch - power
```
- You may also try changing the hue, saturation and brightness from the phone app.
### LED not working?
The ESP32-S2-Saola-1 board has the RGB LED connected to GPIO 18. However, a few earlier boards may have it on GPIO 17. Please use `CONFIG_WS2812_LED_GPIO` to set the appropriate value.
@@ -24,34 +22,3 @@ The ESP32-S2-Saola-1 board has the RGB LED connected to GPIO 18. However, a few
### Reset to Factory
Press and hold the BOOT button for more than 3 seconds to reset the board to factory defaults. You will have to provision the board again to use it.
## Command Response
This example also shows a demo for ESP RainMaker's command - response framework.
### Registering Commands
- Enable the command response framework using `CONFIG_ESP_RMAKER_CMD_RESP_ENABLE`
- Register your command and a handler using `esp_rmaker_cmd_register(ESP_RMAKER_CMD_CUSTOM_START, ESP_RMAKER_USER_ROLE_PRIMARY_USER | ESP_RMAKER_USER_ROLE_SECONDARY_USER, led_light_cmd_handler, false, NULL);`
- This registers a command with id = `ESP_RMAKER_CMD_CUSTOM_START` (0x1000), which will be accessible to primary as well as secondary users (but not admin) and registers the function `led_light_cmd_handler` as the callback
- Whenever the node receives a command with id = `ESP_RMAKER_CMD_CUSTOM_START` (0x1000), it calls `led_light_cmd_handler`.
- The handler parses the data for brightness and on, calls `app_light_set_brightness()` or `app_light_set_power()` as per the data received and also sends the updated params via `esp_rmaker_param_update_and_report()`.
- **Note that the command - response framework is independent of RainMaker params. This example is updating the RainMaker params just so that the state is consistent. Going ahead, some special commands will be added for param updates so that this additional call won't be required.
### Sending Commands
The [RainMaker CLI](https://rainmaker.espressif.com/docs/cli-setup) can be used for sending commands to the node. Once the user node association (preferably from phone apps) is done and you are logged in using CLI, execute these commands:
```
$ ./rainmaker.py create_cmd_request --timeout 60 6055F97E2008 4096 '{"brightness":50}'
Request Id: BK00t2QNe7oT12dBdh9f8X
Status: success
$ ./rainmaker.py get_cmd_requests BK00t2QNe7oT12dBdh9f8X
Requests: [{'node_id': '6055F97E2008', 'request_id': 'BK00t2QNe7oT12dBdh9f8X', 'request_timestamp': 1685382006, 'status': 'in_progress', 'expiration_timestamp': 1685382066}]
Total: 1
$ ./rainmaker.py get_cmd_requests BK00t2QNe7oT12dBdh9f8X
Requests: [{'node_id': '6055F97E2008', 'request_id': 'BK00t2QNe7oT12dBdh9f8X', 'request_timestamp': 1685382006, 'response_timestamp': 1685382039, 'response_data': {'status': 'success'}, 'status': 'success', 'device_status': 0, 'expiration_timestamp': 1685382066}]
```

View File

@@ -9,4 +9,19 @@ menu "Example Configuration"
GPIO number on which the "Boot" button is connected. This is generally used
by the application for custom operations like toggling states, resetting to defaults, etc.
config EXAMPLE_ENABLE_TEST_NOTIFICATIONS
bool "Test Notifications"
default n
help
Enable this option to test mobile push notifications. When enabled, turning on the switch using
push button will trigger a parameter notification {"Switch":{"Power":true}} and turning off will
trigger an alert "Switch was turned off".
config EXAMPLE_OUTPUT_GPIO
int "Output GPIO"
default 19
help
This is an output GPIO that will be connected to a relay or other driver circuit in most cases.
If the power changes, this GPIO output level will also change.
endmenu

View File

@@ -1,5 +1,5 @@
/* LED Lightbulb demo implementation using RGB LED
/* Switch demo implementation using button and RGB LED
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
@@ -8,130 +8,80 @@
*/
#include <sdkconfig.h>
#include <esp_log.h>
#include <iot_button.h>
#include <esp_rmaker_core.h>
#include <esp_rmaker_standard_types.h>
#include <esp_rmaker_standard_params.h>
#include <esp_rmaker_standard_params.h>
#include <app_reset.h>
#if CONFIG_IDF_TARGET_ESP32C2
#include <ledc_driver.h>
#else
#include <ws2812_led.h>
#endif
#include "app_priv.h"
/* This is the button that is used for toggling the power */
#define BUTTON_GPIO CONFIG_EXAMPLE_BOARD_BUTTON_GPIO
#define BUTTON_ACTIVE_LEVEL 0
/* This is the GPIO on which the power will be set */
#define OUTPUT_GPIO CONFIG_EXAMPLE_OUTPUT_GPIO
static bool g_power_state = DEFAULT_POWER;
/* These values correspoind to H,S,V = 120,100,10 */
#define DEFAULT_RED 0
#define DEFAULT_GREEN 25
#define DEFAULT_BLUE 0
#define WIFI_RESET_BUTTON_TIMEOUT 3
#define FACTORY_RESET_BUTTON_TIMEOUT 10
static uint16_t g_hue = DEFAULT_HUE;
static uint16_t g_saturation = DEFAULT_SATURATION;
static uint16_t g_value = DEFAULT_BRIGHTNESS;
static bool g_power = DEFAULT_POWER;
#if CONFIG_IDF_TARGET_ESP32C2
esp_err_t app_light_set_led(uint32_t hue, uint32_t saturation, uint32_t brightness)
static void app_indicator_set(bool state)
{
/* Whenever this function is called, light power will be ON */
if (!g_power) {
g_power = true;
esp_rmaker_param_update_and_report(
esp_rmaker_device_get_param_by_type(light_device, ESP_RMAKER_PARAM_POWER),
esp_rmaker_bool(g_power));
}
ledc_set_hsv(hue, saturation, brightness);
return ESP_OK;
}
esp_err_t app_light_set_power(bool power)
{
g_power = power;
if (power) {
ledc_set_hsv(g_hue, g_saturation, g_value);
} else {
ledc_clear();
}
return ESP_OK;
}
esp_err_t app_light_init(void)
{
ledc_init();
return ESP_OK;
}
#else
esp_err_t app_light_set_led(uint32_t hue, uint32_t saturation, uint32_t brightness)
{
/* Whenever this function is called, light power will be ON */
if (!g_power) {
g_power = true;
esp_rmaker_param_update_and_report(
esp_rmaker_device_get_param_by_type(light_device, ESP_RMAKER_PARAM_POWER),
esp_rmaker_bool(g_power));
}
return ws2812_led_set_hsv(hue, saturation, brightness);
}
esp_err_t app_light_set_power(bool power)
{
g_power = power;
if (power) {
ws2812_led_set_hsv(g_hue, g_saturation, g_value);
if (state) {
ws2812_led_set_rgb(DEFAULT_RED, DEFAULT_GREEN, DEFAULT_BLUE);
} else {
ws2812_led_clear();
}
return ESP_OK;
}
esp_err_t app_light_init(void)
static void app_indicator_init(void)
{
esp_err_t err = ws2812_led_init();
if (err != ESP_OK) {
return err;
}
if (g_power) {
ws2812_led_set_hsv(g_hue, g_saturation, g_value);
} else {
ws2812_led_clear();
}
return ESP_OK;
ws2812_led_init();
app_indicator_set(g_power_state);
}
#endif
esp_err_t app_light_set_brightness(uint16_t brightness)
{
g_value = brightness;
return app_light_set_led(g_hue, g_saturation, g_value);
}
esp_err_t app_light_set_hue(uint16_t hue)
{
g_hue = hue;
return app_light_set_led(g_hue, g_saturation, g_value);
}
esp_err_t app_light_set_saturation(uint16_t saturation)
{
g_saturation = saturation;
return app_light_set_led(g_hue, g_saturation, g_value);
}
static void push_btn_cb(void *arg)
{
app_light_set_power(!g_power);
bool new_state = !g_power_state;
app_driver_set_state(new_state);
#ifdef CONFIG_EXAMPLE_ENABLE_TEST_NOTIFICATIONS
/* This snippet has been added just to demonstrate how the APIs esp_rmaker_param_update_and_notify()
* and esp_rmaker_raise_alert() can be used to trigger push notifications on the phone apps.
* Normally, there should not be a need to use these APIs for such simple operations. Please check
* API documentation for details.
*/
if (new_state) {
esp_rmaker_param_update_and_notify(
esp_rmaker_device_get_param_by_name(switch_device, ESP_RMAKER_DEF_POWER_NAME),
esp_rmaker_bool(new_state));
} else {
esp_rmaker_param_update_and_report(
esp_rmaker_device_get_param_by_name(switch_device, ESP_RMAKER_DEF_POWER_NAME),
esp_rmaker_bool(new_state));
esp_rmaker_raise_alert("Switch was turned off");
}
#else
esp_rmaker_param_update_and_report(
esp_rmaker_device_get_param_by_type(light_device, ESP_RMAKER_PARAM_POWER),
esp_rmaker_bool(g_power));
esp_rmaker_device_get_param_by_name(switch_device, ESP_RMAKER_DEF_POWER_NAME),
esp_rmaker_bool(new_state));
#endif
}
static void set_power_state(bool target)
{
gpio_set_level(OUTPUT_GPIO, target);
app_indicator_set(target);
}
void app_driver_init()
{
app_light_init();
button_handle_t btn_handle = iot_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL);
if (btn_handle) {
/* Register a callback for a button tap (short press) event */
@@ -139,4 +89,28 @@ void app_driver_init()
/* Register Wi-Fi reset and factory reset functionality on same button */
app_reset_button_register(btn_handle, WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT);
}
/* Configure power */
gpio_config_t io_conf = {
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = 1,
};
io_conf.pin_bit_mask = ((uint64_t)1 << OUTPUT_GPIO);
/* Configure the GPIO */
gpio_config(&io_conf);
app_indicator_init();
}
int IRAM_ATTR app_driver_set_state(bool state)
{
if(g_power_state != state) {
g_power_state = state;
set_power_state(g_power_state);
}
return ESP_OK;
}
bool app_driver_get_state(void)
{
return g_power_state;
}

View File

@@ -1,4 +1,4 @@
/* LED Light Example
/* Switch Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
@@ -8,18 +8,23 @@
*/
#include <string.h>
#include <inttypes.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_log.h>
#include <esp_event.h>
#include <nvs_flash.h>
#include <esp_rmaker_console.h>
#include <esp_rmaker_core.h>
#include <esp_rmaker_standard_types.h>
#include <esp_rmaker_standard_params.h>
#include <esp_rmaker_standard_devices.h>
#include <esp_rmaker_schedule.h>
#include <esp_rmaker_console.h>
#include <esp_rmaker_scenes.h>
#include <esp_rmaker_console.h>
#include <esp_rmaker_ota.h>
#include <esp_rmaker_common_events.h>
#include <app_network.h>
#include <app_insights.h>
@@ -27,98 +32,120 @@
#include "app_priv.h"
static const char *TAG = "app_main";
esp_rmaker_device_t *switch_device;
esp_rmaker_device_t *light_device;
#ifdef CONFIG_ESP_RMAKER_CMD_RESP_ENABLE
#include <json_parser.h>
#include <esp_rmaker_cmd_resp.h>
#include <esp_rmaker_standard_types.h>
static char resp_data[100];
/* Callback to handle commands received from the RainMaker cloud via the Command - Response Framework
*
* Sample payloads:
* - {"on":true}
* - {"brightness":30}
*/
esp_err_t led_light_cmd_handler(const void *in_data, size_t in_len, void **out_data, size_t *out_len, esp_rmaker_cmd_ctx_t *ctx, void *priv)
{
if (in_data == NULL ){
ESP_LOGE(TAG, "No data received");
return ESP_FAIL;
}
ESP_LOGI(TAG, "Got command: %.*s", in_len, (char *)in_data);
jparse_ctx_t jctx;
if (json_parse_start(&jctx, (char *)in_data, in_len) != 0) {
snprintf(resp_data, sizeof(resp_data), "{\"status\":\"fail\", \"description\":\"invalid json\"}");
} else {
int brightness;
bool on_state;
if (json_obj_get_int(&jctx, "brightness", &brightness) == 0) {
if (brightness < 0 || brightness > 100) {
snprintf(resp_data, sizeof(resp_data), "{\"status\":\"fail\", \"description\":\"out of bounds\"}");
} else {
app_light_set_brightness(brightness);
esp_rmaker_param_update_and_report(
esp_rmaker_device_get_param_by_type(light_device, ESP_RMAKER_PARAM_BRIGHTNESS),
esp_rmaker_int(brightness));
snprintf(resp_data, sizeof(resp_data), "{\"status\":\"success\"}");
}
} else if (json_obj_get_bool(&jctx, "on", &on_state) == 0) {
app_light_set_power(on_state);
esp_rmaker_param_update_and_report(
esp_rmaker_device_get_param_by_type(light_device, ESP_RMAKER_PARAM_POWER),
esp_rmaker_bool(on_state));
snprintf(resp_data, sizeof(resp_data), "{\"status\":\"success\"}");
} else {
snprintf(resp_data, sizeof(resp_data), "{\"status\":\"fail\", \"description\":\"invalid param\"}");
}
}
*out_data = resp_data;
*out_len = strlen(resp_data);
return ESP_OK;
}
#endif /* CONFIG_ESP_RMAKER_CMD_RESP_ENABLE */
/* Callback to handle param updates received from the RainMaker cloud */
static esp_err_t bulk_write_cb(const esp_rmaker_device_t *device, const esp_rmaker_param_write_req_t write_req[],
uint8_t count, void *priv_data, esp_rmaker_write_ctx_t *ctx)
/* Callback to handle commands received from the RainMaker cloud */
static esp_err_t write_cb(const esp_rmaker_device_t *device, const esp_rmaker_param_t *param,
const esp_rmaker_param_val_t val, void *priv_data, esp_rmaker_write_ctx_t *ctx)
{
if (ctx) {
ESP_LOGI(TAG, "Received write request via : %s", esp_rmaker_device_cb_src_to_str(ctx->src));
}
ESP_LOGI(TAG, "Light received %d params in write", count);
for (int i = 0; i < count; i++) {
const esp_rmaker_param_t *param = write_req[i].param;
esp_rmaker_param_val_t val = write_req[i].val;
const char *device_name = esp_rmaker_device_get_name(device);
const char *param_name = esp_rmaker_param_get_name(param);
if (strcmp(param_name, ESP_RMAKER_DEF_POWER_NAME) == 0) {
ESP_LOGI(TAG, "Received value = %s for %s - %s",
val.val.b? "true" : "false", device_name, param_name);
app_light_set_power(val.val.b);
} else if (strcmp(param_name, ESP_RMAKER_DEF_BRIGHTNESS_NAME) == 0) {
ESP_LOGI(TAG, "Received value = %d for %s - %s",
val.val.i, device_name, param_name);
app_light_set_brightness(val.val.i);
} else if (strcmp(param_name, ESP_RMAKER_DEF_HUE_NAME) == 0) {
ESP_LOGI(TAG, "Received value = %d for %s - %s",
val.val.i, device_name, param_name);
app_light_set_hue(val.val.i);
} else if (strcmp(param_name, ESP_RMAKER_DEF_SATURATION_NAME) == 0) {
ESP_LOGI(TAG, "Received value = %d for %s - %s",
val.val.i, device_name, param_name);
app_light_set_saturation(val.val.i);
} else {
ESP_LOGI(TAG, "Updating for %s", param_name);
}
if (strcmp(esp_rmaker_param_get_name(param), ESP_RMAKER_DEF_POWER_NAME) == 0) {
ESP_LOGI(TAG, "Received value = %s for %s - %s",
val.val.b? "true" : "false", esp_rmaker_device_get_name(device),
esp_rmaker_param_get_name(param));
app_driver_set_state(val.val.b);
esp_rmaker_param_update(param, val);
}
return ESP_OK;
}
/* Event handler for catching RainMaker events */
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == RMAKER_EVENT) {
switch (event_id) {
case RMAKER_EVENT_INIT_DONE:
ESP_LOGI(TAG, "RainMaker Initialised.");
break;
case RMAKER_EVENT_CLAIM_STARTED:
ESP_LOGI(TAG, "RainMaker Claim Started.");
break;
case RMAKER_EVENT_CLAIM_SUCCESSFUL:
ESP_LOGI(TAG, "RainMaker Claim Successful.");
break;
case RMAKER_EVENT_CLAIM_FAILED:
ESP_LOGI(TAG, "RainMaker Claim Failed.");
break;
case RMAKER_EVENT_LOCAL_CTRL_STARTED:
ESP_LOGI(TAG, "Local Control Started.");
break;
case RMAKER_EVENT_LOCAL_CTRL_STOPPED:
ESP_LOGI(TAG, "Local Control Stopped.");
break;
default:
ESP_LOGW(TAG, "Unhandled RainMaker Event: %"PRIi32, event_id);
}
} else if (event_base == RMAKER_COMMON_EVENT) {
switch (event_id) {
case RMAKER_EVENT_REBOOT:
ESP_LOGI(TAG, "Rebooting in %d seconds.", *((uint8_t *)event_data));
break;
case RMAKER_EVENT_WIFI_RESET:
ESP_LOGI(TAG, "Wi-Fi credentials reset.");
break;
case RMAKER_EVENT_FACTORY_RESET:
ESP_LOGI(TAG, "Node reset to factory defaults.");
break;
case RMAKER_MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT Connected.");
break;
case RMAKER_MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT Disconnected.");
break;
case RMAKER_MQTT_EVENT_PUBLISHED:
ESP_LOGI(TAG, "MQTT Published. Msg id: %d.", *((int *)event_data));
break;
default:
ESP_LOGW(TAG, "Unhandled RainMaker Common Event: %"PRIi32, event_id);
}
} else if (event_base == APP_NETWORK_EVENT) {
switch (event_id) {
case APP_NETWORK_EVENT_QR_DISPLAY:
ESP_LOGI(TAG, "Provisioning QR : %s", (char *)event_data);
break;
case APP_NETWORK_EVENT_PROV_TIMEOUT:
ESP_LOGI(TAG, "Provisioning Timed Out. Please reboot.");
break;
case APP_NETWORK_EVENT_PROV_RESTART:
ESP_LOGI(TAG, "Provisioning has restarted due to failures.");
break;
default:
ESP_LOGW(TAG, "Unhandled App Wi-Fi Event: %"PRIi32, event_id);
break;
}
} else if (event_base == RMAKER_OTA_EVENT) {
switch(event_id) {
case RMAKER_OTA_EVENT_STARTING:
ESP_LOGI(TAG, "Starting OTA.");
break;
case RMAKER_OTA_EVENT_IN_PROGRESS:
ESP_LOGI(TAG, "OTA is in progress.");
break;
case RMAKER_OTA_EVENT_SUCCESSFUL:
ESP_LOGI(TAG, "OTA successful.");
break;
case RMAKER_OTA_EVENT_FAILED:
ESP_LOGI(TAG, "OTA Failed.");
break;
case RMAKER_OTA_EVENT_REJECTED:
ESP_LOGI(TAG, "OTA Rejected.");
break;
case RMAKER_OTA_EVENT_DELAYED:
ESP_LOGI(TAG, "OTA Delayed.");
break;
case RMAKER_OTA_EVENT_REQ_FOR_REBOOT:
ESP_LOGI(TAG, "Firmware image downloaded. Please reboot your device to apply the upgrade.");
break;
default:
ESP_LOGW(TAG, "Unhandled OTA Event: %"PRIi32, event_id);
break;
}
} else {
ESP_LOGW(TAG, "Invalid event received!");
}
}
void app_main()
{
@@ -127,6 +154,7 @@ void app_main()
*/
esp_rmaker_console_init();
app_driver_init();
app_driver_set_state(DEFAULT_POWER);
/* Initialize NVS. */
esp_err_t err = nvs_flash_init();
@@ -136,32 +164,59 @@ void app_main()
}
ESP_ERROR_CHECK( err );
/* Initialize Wi-Fi/Thread. Note that, this should be called before esp_rmaker_node_init()
/* Initialize Wi-Fi. Note that, this should be called before esp_rmaker_node_init()
*/
app_network_init();
/* Register an event handler to catch RainMaker events */
ESP_ERROR_CHECK(esp_event_handler_register(RMAKER_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(RMAKER_COMMON_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(APP_NETWORK_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(RMAKER_OTA_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
/* Initialize the ESP RainMaker Agent.
* Note that this should be called after app_network_init() but before app_network_start()
* Note that this should be called after app_network_init() but before app_nenetworkk_start()
* */
esp_rmaker_config_t rainmaker_cfg = {
.enable_time_sync = false,
};
esp_rmaker_node_t *node = esp_rmaker_node_init(&rainmaker_cfg, "ESP RainMaker Device", "Lightbulb");
esp_rmaker_node_t *node = esp_rmaker_node_init(&rainmaker_cfg, "ESP RainMaker Device", "Switch");
if (!node) {
ESP_LOGE(TAG, "Could not initialise node. Aborting!!!");
vTaskDelay(5000/portTICK_PERIOD_MS);
abort();
}
/* Create a device and add the relevant parameters to it */
light_device = esp_rmaker_lightbulb_device_create("Light", NULL, DEFAULT_POWER);
esp_rmaker_device_add_bulk_cb(light_device, bulk_write_cb, NULL);
/* Create a Switch device.
* You can optionally use the helper API esp_rmaker_switch_device_create() to
* avoid writing code for adding the name and power parameters.
*/
switch_device = esp_rmaker_device_create("Switch", ESP_RMAKER_DEVICE_SWITCH, NULL);
esp_rmaker_device_add_param(light_device, esp_rmaker_brightness_param_create(ESP_RMAKER_DEF_BRIGHTNESS_NAME, DEFAULT_BRIGHTNESS));
esp_rmaker_device_add_param(light_device, esp_rmaker_hue_param_create(ESP_RMAKER_DEF_HUE_NAME, DEFAULT_HUE));
esp_rmaker_device_add_param(light_device, esp_rmaker_saturation_param_create(ESP_RMAKER_DEF_SATURATION_NAME, DEFAULT_SATURATION));
/* Add the write callback for the device. We aren't registering any read callback yet as
* it is for future use.
*/
esp_rmaker_device_add_cb(switch_device, write_cb, NULL);
esp_rmaker_node_add_device(node, light_device);
/* Add the standard name parameter (type: esp.param.name), which allows setting a persistent,
* user friendly custom name from the phone apps. All devices are recommended to have this
* parameter.
*/
esp_rmaker_device_add_param(switch_device, esp_rmaker_name_param_create(ESP_RMAKER_DEF_NAME_PARAM, "Switch"));
/* Add the standard power parameter (type: esp.param.power), which adds a boolean param
* with a toggle switch ui-type.
*/
esp_rmaker_param_t *power_param = esp_rmaker_power_param_create(ESP_RMAKER_DEF_POWER_NAME, DEFAULT_POWER);
esp_rmaker_device_add_param(switch_device, power_param);
/* Assign the power parameter as the primary, so that it can be controlled from the
* home screen of the phone apps.
*/
esp_rmaker_device_assign_primary_param(switch_device, power_param);
/* Add this switch device to the node */
esp_rmaker_node_add_device(node, switch_device);
/* Enable OTA */
esp_rmaker_ota_enable_default();
@@ -182,23 +237,18 @@ void app_main()
/* Enable Insights. Requires CONFIG_ESP_INSIGHTS_ENABLED=y */
app_insights_enable();
#ifdef CONFIG_ESP_RMAKER_CMD_RESP_ENABLE
/* Register a command for demonstration */
esp_rmaker_cmd_register(ESP_RMAKER_CMD_CUSTOM_START, ESP_RMAKER_USER_ROLE_PRIMARY_USER | ESP_RMAKER_USER_ROLE_SECONDARY_USER, led_light_cmd_handler, false, NULL);
#endif
/* Start the ESP RainMaker Agent */
esp_rmaker_start();
err = app_network_set_custom_mfg_data(MGF_DATA_DEVICE_TYPE_LIGHT, MFG_DATA_DEVICE_SUBTYPE_LIGHT);
/* Start the Wi-Fi/Thread.
err = app_network_set_custom_mfg_data(MGF_DATA_DEVICE_TYPE_SWITCH, MFG_DATA_DEVICE_SUBTYPE_SWITCH);
/* Start the Wi-Fi.
* If the node is provisioned, it will start connection attempts,
* else, it will start Wi-Fi provisioning. The function will return
* after a connection has been successfully established
*/
err = app_network_start(POP_TYPE_RANDOM);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Could not start network. Aborting!!!");
ESP_LOGE(TAG, "Could not start Wifi. Aborting!!!");
vTaskDelay(5000/portTICK_PERIOD_MS);
abort();
}

View File

@@ -9,16 +9,8 @@
#include <stdint.h>
#include <stdbool.h>
#define DEFAULT_POWER true
#define DEFAULT_HUE 180
#define DEFAULT_SATURATION 100
#define DEFAULT_BRIGHTNESS 25
extern esp_rmaker_device_t *light_device;
#define DEFAULT_POWER true
extern esp_rmaker_device_t *switch_device;
void app_driver_init(void);
esp_err_t app_light_set(uint32_t hue, uint32_t saturation, uint32_t brightness);
esp_err_t app_light_set_power(bool power);
esp_err_t app_light_set_brightness(uint16_t brightness);
esp_err_t app_light_set_hue(uint16_t hue);
esp_err_t app_light_set_saturation(uint16_t saturation);
int app_driver_set_state(bool state);
bool app_driver_get_state(void);

View File

@@ -2,4 +2,3 @@
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
COMPONENT_EMBED_TXTFILES := server.crt

View File

@@ -0,0 +1,8 @@
## IDF Component Manager Manifest File
dependencies:
## Required IDF version
idf:
version: ">=5.0.0"
espressif/esp_rainmaker:
version: ">=1.0"
override_path: '../../../components/esp_rainmaker/'