Skip to content

Commit 04adf40

Browse files
committed
Merge branch 'feature/wifi_reset' into 'master'
examples: Separate wifi-reset and factory-reset See merge request app-frameworks/esp-rainmaker!142
2 parents 4628715 + e0385e9 commit 04adf40

File tree

15 files changed

+239
-85
lines changed

15 files changed

+239
-85
lines changed

components/button/button/include/iot_button.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern "C" {
3131
#endif
3232

3333
#include <driver/gpio.h>
34+
#include <freertos/FreeRTOS.h>
3435
#include <freertos/portmacro.h>
3536
typedef void (* button_cb)(void*);
3637
typedef void* button_handle_t;

components/esp_rainmaker/include/esp_rmaker_core.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ typedef enum {
3939
RMAKER_EVENT_CLAIM_SUCCESSFUL,
4040
/** Self Claiming Failed */
4141
RMAKER_EVENT_CLAIM_FAILED,
42+
/** Node reboot has been triggered. The associated event data is the time in seconds
43+
* (type: uint8_t) after which the node will reboot. Note that this time may not be
44+
* accurate as the events are received asynchronously.*/
45+
RMAKER_EVENT_REBOOT,
46+
/** Wi-Fi credentials reset. Triggered after calling esp_rmaker_wifi_reset() */
47+
RMAKER_EVENT_WIFI_RESET,
48+
/** Node reset to factory defaults. Triggered after calling esp_rmaker_factory_reset() */
49+
RMAKER_EVENT_FACTORY_RESET
4250
} esp_rmaker_event_t;
4351

4452
/** ESP RainMaker Node information */

components/esp_rainmaker/include/esp_rmaker_utils.h

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,51 @@ typedef struct esp_rmaker_time_config {
2727

2828
/** Reboot the chip after a delay
2929
*
30-
* This API just starts an esp_timer and executes a reboot from that.
31-
* Useful if you want to reboot after a delay, to allow other tasks to finish
32-
* their operations (Eg. MQTT publish to indicate OTA success)
30+
* This API just starts a reboot timer and returns immediately.
31+
* The actual reboot is trigerred asynchronously in the timer callback.
32+
* This is useful if you want to reboot after a delay, to allow other tasks to finish
33+
* their operations (Eg. MQTT publish to indicate OTA success). The \ref RMAKER_EVENT_REBOOT
34+
* event is triggered when the reboot timer is started.
3335
*
34-
* @param[in] seconds Time in seconds after which the chip should reboot
36+
* @param[in] seconds Time in seconds after which the chip should reboot.
3537
*
36-
* @return ESP_OK on success
37-
* @return error on failure
38+
* @return ESP_OK on success.
39+
* @return error on failure.
3840
*/
3941
esp_err_t esp_rmaker_reboot(uint8_t seconds);
4042

43+
/** Reset Wi-Fi credentials and reboot
44+
*
45+
* This will reset just the Wi-Fi credentials and trigger a reboot.
46+
* This is useful when you want to keep all the entries in NVS memory
47+
* intact, but just change the Wi-Fi credentials. The \ref RMAKER_EVENT_WIFI_RESET
48+
* event is triggered after the reset.
49+
*
50+
* @note This function internally calls esp_rmaker_reboot() and returns
51+
* immediately. The reboot happens asynchronously.
52+
*
53+
* @param[in] seconds Time in seconds after which the chip should reboot.
54+
*
55+
* @return ESP_OK on success.
56+
* @return error on failure.
57+
*/
58+
esp_err_t esp_rmaker_wifi_reset(uint8_t seconds);
59+
60+
/** Reset to factory defaults and reboot
61+
*
62+
* This will clear entire NVS partition and trigger a reboot.
63+
* The \ref RMAKER_EVENT_FACTORY_RESET event is triggered after the reset.
64+
*
65+
* @note This function internally calls esp_rmaker_reboot() and returns.
66+
* The reboot happens asynchronously.
67+
*
68+
* @param[in] seconds Time in seconds after which the chip should reboot.
69+
*
70+
* @return ESP_OK on success.
71+
* @return error on failure.
72+
*/
73+
esp_err_t esp_rmaker_factory_reset(uint8_t seconds);
74+
4175
/** Initialize time synchronization
4276
*
4377
* This API initializes SNTP for time synchronization.

components/esp_rainmaker/src/core/esp_rmaker_utils.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
#include <stdint.h>
1515
#include <esp_timer.h>
1616
#include <esp_system.h>
17+
#include <esp_wifi.h>
18+
#include <nvs_flash.h>
19+
#include <esp_rmaker_core.h>
1720

21+
#include <esp_rmaker_internal.h>
1822

1923
static esp_timer_handle_t reboot_timer;
2024

@@ -33,8 +37,27 @@ esp_err_t esp_rmaker_reboot(uint8_t seconds)
3337
.dispatch_method = ESP_TIMER_TASK,
3438
.name = "rmaker_reboot_tm"
3539
};
40+
esp_err_t err = ESP_FAIL;
3641
if (esp_timer_create(&reboot_timer_conf, &reboot_timer) == ESP_OK) {
37-
return esp_timer_start_once(reboot_timer, seconds * 1000000U);
42+
err = esp_timer_start_once(reboot_timer, seconds * 1000000U);
3843
}
39-
return ESP_FAIL;
44+
if (err == ESP_OK) {
45+
esp_rmaker_post_event(RMAKER_EVENT_REBOOT, &seconds, sizeof(seconds));
46+
}
47+
return err;
48+
}
49+
50+
esp_err_t esp_rmaker_wifi_reset(uint8_t seconds)
51+
{
52+
esp_wifi_restore();
53+
esp_rmaker_post_event(RMAKER_EVENT_WIFI_RESET, NULL, 0);
54+
return esp_rmaker_reboot(seconds);
55+
}
56+
57+
esp_err_t esp_rmaker_factory_reset(uint8_t seconds)
58+
{
59+
nvs_flash_deinit();
60+
nvs_flash_erase();
61+
esp_rmaker_post_event(RMAKER_EVENT_FACTORY_RESET, NULL, 0);
62+
return esp_rmaker_reboot(seconds);
4063
}
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
}

0 commit comments

Comments
 (0)