Skip to content

Commit 196bcb2

Browse files
committed
examples: Use the new wifi-reset and factory-reset APIs
1 parent 7be8b6e commit 196bcb2

File tree

11 files changed

+165
-77
lines changed

11 files changed

+165
-77
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
idf_component_register(SRCS "app_reset.c"
2+
INCLUDE_DIRS "."
3+
REQUIRES button esp_rainmaker)

examples/common/app_reset/app_reset.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
This example code is in the Public Domain (or CC0 licensed, at your option.)
3+
4+
Unless required by applicable law or agreed to in writing, this
5+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6+
CONDITIONS OF ANY KIND, either express or implied.
7+
*/
8+
9+
/* It is recommended to copy this code in your example so that you can modify as
10+
* per your application's needs, especially for the indicator calbacks,
11+
* wifi_reset_indicate() and factory_reset_indicate().
12+
*/
13+
#include <esp_log.h>
14+
#include <esp_err.h>
15+
#include <iot_button.h>
16+
#include <esp_rmaker_utils.h>
17+
18+
static const char *TAG = "app_reset";
19+
20+
#define REBOOT_DELAY 2
21+
22+
static void wifi_reset_trigger(void *arg)
23+
{
24+
esp_rmaker_wifi_reset(REBOOT_DELAY);
25+
}
26+
27+
static void wifi_reset_indicate(void *arg)
28+
{
29+
ESP_LOGI(TAG, "Release button now for Wi-Fi reset. Keep pressed for factory reset.");
30+
}
31+
32+
static void factory_reset_trigger(void *arg)
33+
{
34+
esp_rmaker_factory_reset(REBOOT_DELAY);
35+
}
36+
37+
static void factory_reset_indicate(void *arg)
38+
{
39+
ESP_LOGI(TAG, "Release button to trigger factory reset.");
40+
}
41+
42+
esp_err_t app_reset_button_register(button_handle_t btn_handle, uint8_t wifi_reset_timeout,
43+
uint8_t factory_reset_timeout)
44+
{
45+
if (!btn_handle) {
46+
return ESP_ERR_INVALID_ARG;
47+
}
48+
if (wifi_reset_timeout) {
49+
iot_button_add_on_release_cb(btn_handle, wifi_reset_timeout, wifi_reset_trigger, NULL);
50+
iot_button_add_on_press_cb(btn_handle, wifi_reset_timeout, wifi_reset_indicate, NULL);
51+
}
52+
if (factory_reset_timeout) {
53+
if (factory_reset_timeout <= wifi_reset_timeout) {
54+
ESP_LOGW(TAG, "It is recommended to have factory_reset_timeout > wifi_reset_timeout");
55+
}
56+
iot_button_add_on_release_cb(btn_handle, factory_reset_timeout, factory_reset_trigger, NULL);
57+
iot_button_add_on_press_cb(btn_handle, factory_reset_timeout, factory_reset_indicate, NULL);
58+
}
59+
return ESP_OK;
60+
}
61+
62+
button_handle_t app_reset_button_create(gpio_num_t gpio_num, button_active_t active_level)
63+
{
64+
return iot_button_create(gpio_num, active_level);
65+
}

examples/common/app_reset/app_reset.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
This example code is in the Public Domain (or CC0 licensed, at your option.)
3+
4+
Unless required by applicable law or agreed to in writing, this
5+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6+
CONDITIONS OF ANY KIND, either express or implied.
7+
*/
8+
#pragma once
9+
#include <stdint.h>
10+
#include <esp_err.h>
11+
#include <iot_button.h>
12+
13+
/** Create a button handle
14+
*
15+
* This is just a wrapper over iot_button_create(). This can be used to register
16+
* Wi-Fi/Factory reset functionality for a button.
17+
*
18+
* @param[in] gpio_num GPIO index of the pin that the button uses.
19+
* @param[in] active_level button hardware active level.
20+
* "BUTTON_ACTIVE_LOW" means that when the button is pressed, the GPIO will read low level.
21+
* For "BUTTON_ACTIVE_HIGH", it will be reverse.
22+
*
23+
* @return A button_handle_t handle to the created button object, or NULL in case of error.
24+
*/
25+
button_handle_t app_reset_button_create(gpio_num_t gpio_num, button_active_t active_level);
26+
27+
/** Register callbacks for Wi-Fi/Factory reset
28+
*
29+
* Register Wi-Fi reset or factory reset functionality on a button.
30+
* If you want to use different buttons for these two, call this API twice, with appropriate
31+
* button handles.
32+
*
33+
* @param[in] btn_handle Button handle returned by iot_button_create() or app_button_create()
34+
* @param[in] wifi_reset_timeout Timeout after which the Wi-Fi reset should be triggered. Set to 0,
35+
* if you do not want Wi-Fi reset.
36+
* @param[in] factory_reset_timeout Timeout after which the factory reset should be triggered. Set to 0,
37+
* if you do not want factory reset.
38+
*
39+
* @return ESP_OK on success.
40+
* @return error in case of failure.
41+
*/
42+
esp_err_t app_reset_button_register(button_handle_t btn_handle, uint8_t wifi_reset_timeout, uint8_t factory_reset_timeout);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
COMPONENT_ADD_INCLUDEDIRS := .
2+
COMPONENT_SRCDIRS := .

examples/fan/main/app_driver.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
*/
99

1010
#include <sdkconfig.h>
11-
#include <freertos/FreeRTOS.h>
12-
#include <esp_system.h>
1311
#include <esp_log.h>
14-
#include <nvs_flash.h>
1512
#include <driver/rmt.h>
1613

1714
#include <iot_button.h>
@@ -20,6 +17,7 @@
2017
#include <esp_rmaker_standard_types.h>
2118
#include <esp_rmaker_standard_params.h>
2219

20+
#include <app_reset.h>
2321
#include "app_priv.h"
2422

2523
#define RMT_TX_CHANNEL RMT_CHANNEL_0
@@ -33,6 +31,9 @@
3331
#define DEFAULT_SATURATION 100
3432
#define DEFAULT_BRIGHTNESS ( 20 * DEFAULT_SPEED)
3533

34+
#define WIFI_RESET_BUTTON_TIMEOUT 3
35+
#define FACTORY_RESET_BUTTON_TIMEOUT 10
36+
3637
static uint8_t g_speed = DEFAULT_SPEED;
3738
static uint16_t g_hue = DEFAULT_HUE;
3839
static uint16_t g_saturation = DEFAULT_SATURATION;
@@ -173,19 +174,14 @@ static void push_btn_cb(void *arg)
173174
}
174175
}
175176

176-
static void button_press_3sec_cb(void *arg)
177-
{
178-
nvs_flash_deinit();
179-
nvs_flash_erase();
180-
esp_restart();
181-
}
182-
183177
void app_driver_init()
184178
{
185179
app_fan_init();
186180
button_handle_t btn_handle = iot_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL);
187181
if (btn_handle) {
188-
iot_button_set_evt_cb(btn_handle, BUTTON_CB_RELEASE, push_btn_cb, "RELEASE");
189-
iot_button_add_on_press_cb(btn_handle, 3, button_press_3sec_cb, NULL);
182+
/* Register a callback for a button tap (short press) event */
183+
iot_button_set_evt_cb(btn_handle, BUTTON_CB_TAP, push_btn_cb, NULL);
184+
/* Register Wi-Fi reset and factory reset functionality on same button */
185+
app_reset_button_register(btn_handle, WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT);
190186
}
191187
}

examples/gpio/main/app_driver.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99

1010
#include <sdkconfig.h>
1111
#include <string.h>
12-
#include <freertos/FreeRTOS.h>
13-
#include <esp_system.h>
14-
#include <nvs_flash.h>
1512
#include <esp_log.h>
1613

17-
#include <iot_button.h>
18-
14+
#include <app_reset.h>
1915
#include "app_priv.h"
2016

2117
#define RMT_TX_CHANNEL RMT_CHANNEL_0
@@ -28,6 +24,9 @@
2824
#define OUTPUT_GPIO_GREEN 14ULL
2925
#define OUTPUT_GPIO_BLUE 15ULL
3026

27+
#define WIFI_RESET_BUTTON_TIMEOUT 3
28+
#define FACTORY_RESET_BUTTON_TIMEOUT 10
29+
3130
esp_err_t app_driver_set_gpio(const char *name, bool state)
3231
{
3332
if (strcmp(name, "Red") == 0) {
@@ -41,19 +40,11 @@ esp_err_t app_driver_set_gpio(const char *name, bool state)
4140
}
4241
return ESP_OK;
4342
}
44-
static void button_press_3sec_cb(void *arg)
45-
{
46-
nvs_flash_deinit();
47-
nvs_flash_erase();
48-
esp_restart();
49-
}
5043

5144
void app_driver_init()
5245
{
53-
button_handle_t btn_handle = iot_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL);
54-
if (btn_handle) {
55-
iot_button_add_on_press_cb(btn_handle, 3, button_press_3sec_cb, NULL);
56-
}
46+
app_reset_button_register(app_reset_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL),
47+
WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT);
5748

5849
/* Configure power */
5950
gpio_config_t io_conf = {

examples/led_light/main/app_driver.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
*/
99

1010
#include <sdkconfig.h>
11-
#include <freertos/FreeRTOS.h>
12-
#include <esp_system.h>
1311
#include <esp_log.h>
14-
#include <nvs_flash.h>
12+
#include <esp_wifi.h>
1513
#include <driver/rmt.h>
1614

1715
#include <iot_button.h>
@@ -20,6 +18,7 @@
2018
#include <esp_rmaker_standard_types.h>
2119
#include <esp_rmaker_standard_params.h>
2220

21+
#include <app_reset.h>
2322
#include "app_priv.h"
2423

2524
#define RMT_TX_CHANNEL RMT_CHANNEL_0
@@ -29,6 +28,8 @@
2928
/* This is the GPIO on which the power will be set */
3029
#define OUTPUT_GPIO 19
3130

31+
#define WIFI_RESET_BUTTON_TIMEOUT 3
32+
#define FACTORY_RESET_BUTTON_TIMEOUT 10
3233

3334
static uint16_t g_hue = DEFAULT_HUE;
3435
static uint16_t g_saturation = DEFAULT_SATURATION;
@@ -173,19 +174,14 @@ static void push_btn_cb(void *arg)
173174
esp_rmaker_bool(g_power));
174175
}
175176

176-
static void button_press_3sec_cb(void *arg)
177-
{
178-
nvs_flash_deinit();
179-
nvs_flash_erase();
180-
esp_restart();
181-
}
182-
183177
void app_driver_init()
184178
{
185179
app_light_init();
186180
button_handle_t btn_handle = iot_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL);
187181
if (btn_handle) {
188-
iot_button_set_evt_cb(btn_handle, BUTTON_CB_RELEASE, push_btn_cb, "RELEASE");
189-
iot_button_add_on_press_cb(btn_handle, 3, button_press_3sec_cb, NULL);
182+
/* Register a callback for a button tap (short press) event */
183+
iot_button_set_evt_cb(btn_handle, BUTTON_CB_TAP, push_btn_cb, NULL);
184+
/* Register Wi-Fi reset and factory reset functionality on same button */
185+
app_reset_button_register(btn_handle, WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT);
190186
}
191187
}

examples/multi_device/main/app_driver.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
*/
88

99
#include <sdkconfig.h>
10-
#include <freertos/FreeRTOS.h>
11-
#include <esp_system.h>
1210
#include <esp_log.h>
13-
#include <nvs_flash.h>
1411
#include <driver/rmt.h>
1512

1613
#include <iot_button.h>
@@ -19,6 +16,7 @@
1916
#include <esp_rmaker_standard_types.h>
2017
#include <esp_rmaker_standard_params.h>
2118

19+
#include <app_reset.h>
2220
#include "app_priv.h"
2321

2422
#define RMT_TX_CHANNEL RMT_CHANNEL_0
@@ -35,6 +33,9 @@ static const char *TAG = "app_driver";
3533
#define DEFAULT_GREEN 25
3634
#define DEFAULT_BLUE 0
3735

36+
#define WIFI_RESET_BUTTON_TIMEOUT 3
37+
#define FACTORY_RESET_BUTTON_TIMEOUT 10
38+
3839
static bool g_power_state = DEFAULT_SWITCH_POWER;
3940
static float g_temperature = DEFAULT_TEMPERATURE;
4041
static esp_timer_handle_t sensor_timer;
@@ -114,13 +115,6 @@ static void push_btn_cb(void *arg)
114115
esp_rmaker_bool(new_state));
115116
}
116117

117-
static void button_press_3sec_cb(void *arg)
118-
{
119-
nvs_flash_deinit();
120-
nvs_flash_erase();
121-
esp_restart();
122-
}
123-
124118
static void set_power_state(bool target)
125119
{
126120
gpio_set_level(OUTPUT_GPIO, target);
@@ -131,8 +125,10 @@ void app_driver_init()
131125
{
132126
button_handle_t btn_handle = iot_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL);
133127
if (btn_handle) {
134-
iot_button_set_evt_cb(btn_handle, BUTTON_CB_RELEASE, push_btn_cb, "RELEASE");
135-
iot_button_add_on_press_cb(btn_handle, 3, button_press_3sec_cb, NULL);
128+
/* Register a callback for a button tap (short press) event */
129+
iot_button_set_evt_cb(btn_handle, BUTTON_CB_TAP, push_btn_cb, NULL);
130+
/* Register Wi-Fi reset and factory reset functionality on same button */
131+
app_reset_button_register(btn_handle, WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT);
136132
}
137133

138134
/* Configure power */

examples/switch/main/app_driver.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
*/
99

1010
#include <sdkconfig.h>
11-
#include <freertos/FreeRTOS.h>
12-
#include <esp_system.h>
1311
#include <esp_log.h>
14-
#include <nvs_flash.h>
1512
#include <driver/rmt.h>
1613

1714
#include <iot_button.h>
1815
#include <led_strip.h>
1916
#include <esp_rmaker_core.h>
2017
#include <esp_rmaker_standard_params.h>
2118

19+
#include <app_reset.h>
2220
#include "app_priv.h"
2321

2422
#define RMT_TX_CHANNEL RMT_CHANNEL_0
@@ -35,6 +33,9 @@ static const char *TAG = "app_driver";
3533
#define DEFAULT_GREEN 25
3634
#define DEFAULT_BLUE 0
3735

36+
#define WIFI_RESET_BUTTON_TIMEOUT 3
37+
#define FACTORY_RESET_BUTTON_TIMEOUT 10
38+
3839
static void app_indicator_set(bool state)
3940
{
4041
if (!strip) {
@@ -76,12 +77,6 @@ static void push_btn_cb(void *arg)
7677
esp_rmaker_bool(new_state));
7778
}
7879

79-
static void button_press_3sec_cb(void *arg)
80-
{
81-
nvs_flash_deinit();
82-
nvs_flash_erase();
83-
esp_restart();
84-
}
8580
static void set_power_state(bool target)
8681
{
8782
gpio_set_level(OUTPUT_GPIO, target);
@@ -92,8 +87,10 @@ void app_driver_init()
9287
{
9388
button_handle_t btn_handle = iot_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL);
9489
if (btn_handle) {
95-
iot_button_set_evt_cb(btn_handle, BUTTON_CB_RELEASE, push_btn_cb, "RELEASE");
96-
iot_button_add_on_press_cb(btn_handle, 3, button_press_3sec_cb, NULL);
90+
/* Register a callback for a button tap (short press) event */
91+
iot_button_set_evt_cb(btn_handle, BUTTON_CB_TAP, push_btn_cb, NULL);
92+
/* Register Wi-Fi reset and factory reset functionality on same button */
93+
app_reset_button_register(btn_handle, WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT);
9794
}
9895

9996
/* Configure power */

examples/switch/main/app_main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ static void event_handler(void* arg, esp_event_base_t event_base,
5959
case RMAKER_EVENT_CLAIM_FAILED:
6060
ESP_LOGI(TAG, "RainMaker Claim Failed.");
6161
break;
62+
case RMAKER_EVENT_REBOOT:
63+
ESP_LOGI(TAG, "Rebooting in %d seconds.", *((uint8_t *)event_data));
64+
break;
65+
case RMAKER_EVENT_WIFI_RESET:
66+
ESP_LOGI(TAG, "Wi-Fi credentials reset.");
67+
break;
68+
case RMAKER_EVENT_FACTORY_RESET:
69+
ESP_LOGI(TAG, "Node reset to factory defaults.");
70+
break;
6271
default:
6372
ESP_LOGW(TAG, "Unhandled RainMaker Event: %d", event_id);
6473
}

0 commit comments

Comments
 (0)