Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e62ff6d

Browse filesBrowse files
authored
RainMaker library: Minor changes (espressif#5092)
- Use Serial.print instead of log_i for QR code helper information, so that it is always printed by default. - Expose the RainMaker factory reset and wifi reset APIs. - Simplify example to have only a Switch device. Create another example for custom device. - Enable push button based Factory reset and Wi-Fi reset. - Added support for the TimeZone service. - Moved API doc to RainMaker library's top level README. - Other minor doc changes.
1 parent 1824990 commit e62ff6d
Copy full SHA for e62ff6d

File tree

Expand file treeCollapse file tree

15 files changed

+700
-509
lines changed
Filter options
Expand file treeCollapse file tree

15 files changed

+700
-509
lines changed

‎libraries/RainMaker/README.md

Copy file name to clipboardExpand all lines: libraries/RainMaker/README.md
+444Lines changed: 444 additions & 0 deletions
Large diffs are not rendered by default.

‎libraries/RainMaker/examples/README.md

Copy file name to clipboardExpand all lines: libraries/RainMaker/examples/README.md
+7-431Lines changed: 7 additions & 431 deletions
Large diffs are not rendered by default.
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ESP RainMaker Switch
2+
3+
This example demonstrates how to build a switch device to be used with ESP RainMaker.
4+
5+
## What to expect in this example?
6+
7+
- This example sketch uses the on board Boot button and GPIO16 to demonstrate an ESP RainMaker switch device.
8+
- After compiling and flashing the example, add your device using the [ESP RainMaker phone apps](https://rainmaker.espressif.com/docs/quick-links.html#phone-apps) by scanning the QR code.
9+
- Toggling the state from the phone app will toggle the switch state (GPIO16).
10+
- Pressing the Boot button will toggle the switch state (GPIO16) and the same will reflect on the phone app.
11+
12+
### Output
13+
14+
```
15+
[ 63][I][RMaker.cpp:13] event_handler(): RainMaker Initialised.
16+
[ 69][I][WiFiProv.cpp:158] beginProvision(): Already Provisioned
17+
[ 69][I][WiFiProv.cpp:162] beginProvision(): Attempting connect to AP: Viking007_2GEXT
18+
19+
Toggle State to false.
20+
[ 8182][I][RMakerDevice.cpp:162] updateAndReportParam(): Device : Switch, Param Name : Power, Val : false
21+
Toggle State to true.
22+
[ 9835][I][RMakerDevice.cpp:162] updateAndReportParam(): Device : Switch, Param Name : Power, Val : true
23+
Received value = false for Switch - Power
24+
Received value = true for Switch - Power
25+
Toggle State to false.
26+
[ 29937][I][RMakerDevice.cpp:162] updateAndReportParam(): Device : Switch, Param Name : Power, Val : false
27+
```
+126Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
//This example demonstrates the ESP RainMaker with a custom device
2+
#include "RMaker.h"
3+
#include "WiFi.h"
4+
#include "WiFiProv.h"
5+
6+
#define DEFAULT_POWER_MODE true
7+
#define DEFAULT_DIMMER_LEVEL 50
8+
const char *service_name = "PROV_1234";
9+
const char *pop = "abcd1234";
10+
11+
//GPIO for push button
12+
static int gpio_0 = 0;
13+
//GPIO for virtual device
14+
static int gpio_dimmer = 16;
15+
/* Variable for reading pin status*/
16+
bool dimmer_state = true;
17+
18+
// The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
19+
// But, you can also define custom devices using the 'Device' base class object, as shown here
20+
static Device my_device("Dimmer", "custom.device.dimmer", &gpio_dimmer);
21+
22+
void sysProvEvent(arduino_event_t *sys_event)
23+
{
24+
switch (sys_event->event_id) {
25+
case ARDUINO_EVENT_PROV_START:
26+
#if CONFIG_IDF_TARGET_ESP32
27+
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
28+
printQR(service_name, pop, "ble");
29+
#else
30+
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
31+
printQR(service_name, pop, "softap");
32+
#endif
33+
break;
34+
}
35+
}
36+
37+
void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx)
38+
{
39+
const char *device_name = device->getDeviceName();
40+
const char *param_name = param->getParamName();
41+
42+
if(strcmp(param_name, "Power") == 0) {
43+
Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
44+
dimmer_state = val.val.b;
45+
(dimmer_state == false) ? digitalWrite(gpio_dimmer, LOW) : digitalWrite(gpio_dimmer, HIGH);
46+
param->updateAndReport(val);
47+
} else if (strcmp(param_name, "Level") == 0) {
48+
Serial.printf("\nReceived value = %d for %s - %s\n", val.val.i, device_name, param_name);
49+
param->updateAndReport(val);
50+
}
51+
}
52+
53+
void setup()
54+
{
55+
Serial.begin(115200);
56+
pinMode(gpio_0, INPUT);
57+
pinMode(gpio_dimmer, OUTPUT);
58+
digitalWrite(gpio_dimmer, DEFAULT_POWER_MODE);
59+
60+
Node my_node;
61+
my_node = RMaker.initNode("ESP RainMaker Node");
62+
63+
//Create custom dimmer device
64+
my_device.addNameParam();
65+
my_device.addPowerParam(DEFAULT_POWER_MODE);
66+
my_device.assignPrimaryParam(my_device.getParamByName(ESP_RMAKER_DEF_POWER_NAME));
67+
68+
//Create and add a custom level parameter
69+
Param level_param("Level", "custom.param.level", value(DEFAULT_DIMMER_LEVEL), PROP_FLAG_READ | PROP_FLAG_WRITE);
70+
level_param.addBounds(value(0), value(100), value(1));
71+
level_param.addUIType(ESP_RMAKER_UI_SLIDER);
72+
my_device.addParam(level_param);
73+
74+
my_device.addCb(write_callback);
75+
76+
//Add custom dimmer device to the node
77+
my_node.addDevice(my_device);
78+
79+
//This is optional
80+
RMaker.enableOTA(OTA_USING_PARAMS);
81+
//If you want to enable scheduling, set time zone for your region using setTimeZone().
82+
//The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html
83+
// RMaker.setTimeZone("Asia/Shanghai");
84+
// Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone
85+
RMaker.enableTZService();
86+
87+
RMaker.enableSchedule();
88+
89+
RMaker.start();
90+
91+
WiFi.onEvent(sysProvEvent);
92+
#if CONFIG_IDF_TARGET_ESP32
93+
WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name);
94+
#else
95+
WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name);
96+
#endif
97+
}
98+
99+
void loop()
100+
{
101+
if(digitalRead(gpio_0) == LOW) { //Push button pressed
102+
103+
// Key debounce handling
104+
delay(100);
105+
int startTime = millis();
106+
while(digitalRead(gpio_0) == LOW) delay(50);
107+
int endTime = millis();
108+
109+
if ((endTime - startTime) > 10000) {
110+
// If key pressed for more than 10secs, reset all
111+
Serial.printf("Reset to factory.\n");
112+
RMakerFactoryReset(2);
113+
} else if ((endTime - startTime) > 3000) {
114+
Serial.printf("Reset Wi-Fi.\n");
115+
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi
116+
RMakerWiFiReset(2);
117+
} else {
118+
// Toggle device state
119+
dimmer_state = !dimmer_state;
120+
Serial.printf("Toggle State to %s.\n", dimmer_state ? "true" : "false");
121+
my_device.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, dimmer_state);
122+
(dimmer_state == false) ? digitalWrite(gpio_dimmer, LOW) : digitalWrite(gpio_dimmer, HIGH);
123+
}
124+
}
125+
delay(100);
126+
}

‎libraries/RainMaker/examples/RMakerDevice/README.md

Copy file name to clipboardExpand all lines: libraries/RainMaker/examples/RMakerDevice/README.md
-34Lines changed: 0 additions & 34 deletions
This file was deleted.

‎libraries/RainMaker/examples/RMakerSwitch/.skip.esp32c3

Copy file name to clipboardExpand all lines: libraries/RainMaker/examples/RMakerSwitch/.skip.esp32c3
Whitespace-only changes.

‎libraries/RainMaker/examples/RMakerSwitch/.skip.esp32s2

Copy file name to clipboardExpand all lines: libraries/RainMaker/examples/RMakerSwitch/.skip.esp32s2
Whitespace-only changes.
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ESP RainMaker Switch
2+
3+
This example demonstrates how to build a switch device to be used with ESP RainMaker.
4+
5+
## What to expect in this example?
6+
7+
- This example sketch uses the on board Boot button and GPIO16 to demonstrate an ESP RainMaker switch device.
8+
- After compiling and flashing the example, add your device using the [ESP RainMaker phone apps](https://rainmaker.espressif.com/docs/quick-links.html#phone-apps) by scanning the QR code.
9+
- Toggling the state from the phone app will toggle the switch state (GPIO16).
10+
- Pressing the Boot button will toggle the switch state (GPIO16) and the same will reflect on the phone app.
11+
12+
### Output
13+
14+
```
15+
[ 63][I][RMaker.cpp:13] event_handler(): RainMaker Initialised.
16+
[ 69][I][WiFiProv.cpp:158] beginProvision(): Already Provisioned
17+
[ 69][I][WiFiProv.cpp:162] beginProvision(): Attempting connect to AP: Viking007_2GEXT
18+
19+
Toggle State to false.
20+
[ 8182][I][RMakerDevice.cpp:162] updateAndReportParam(): Device : Switch, Param Name : Power, Val : false
21+
Toggle State to true.
22+
[ 9835][I][RMakerDevice.cpp:162] updateAndReportParam(): Device : Switch, Param Name : Power, Val : true
23+
Received value = false for Switch - Power
24+
Received value = true for Switch - Power
25+
Toggle State to false.
26+
[ 29937][I][RMakerDevice.cpp:162] updateAndReportParam(): Device : Switch, Param Name : Power, Val : false
27+
```
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
//This example demonstrates the ESP RainMaker with the custom device and standard Switch device.
1+
//This example demonstrates the ESP RainMaker with a standard Switch device.
22
#include "RMaker.h"
33
#include "WiFi.h"
44
#include "WiFiProv.h"
55

6-
#define DEFAULT_FAN_SPEED 4
76
#define DEFAULT_POWER_MODE true
8-
const char *service_name = "Prov_1234";
7+
const char *service_name = "PROV_1234";
98
const char *pop = "abcd1234";
109

1110
//GPIO for push button
1211
static int gpio_0 = 0;
1312
//GPIO for virtual device
1413
static int gpio_switch = 16;
15-
static int gpio_fan = 17;
1614
/* Variable for reading pin status*/
1715
bool switch_state = true;
18-
bool fan_state = true;
1916

2017
//The framework provides some standard device types like switch, lightbulb, fan, temperaturesensor.
2118
static Switch my_switch("Switch", &gpio_switch);
22-
//You can also define custom devices using the 'Device' base class object, as shown here
23-
static Device my_device("Fan", ESP_RMAKER_DEVICE_FAN, &gpio_fan);
2419

2520
void sysProvEvent(arduino_event_t *sys_event)
2621
{
2722
switch (sys_event->event_id) {
2823
case ARDUINO_EVENT_PROV_START:
2924
#if CONFIG_IDF_TARGET_ESP32
25+
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
3026
printQR(service_name, pop, "ble");
3127
#else
28+
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
3229
printQR(service_name, pop, "softap");
3330
#endif
3431
break;
@@ -41,51 +38,37 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p
4138
const char *param_name = param->getParamName();
4239

4340
if(strcmp(param_name, "Power") == 0) {
44-
Serial.printf("\nReceived value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
45-
if(strcmp(device_name, "Switch") == 0) {
46-
switch_state = val.val.b;
47-
(switch_state == false) ? digitalWrite(gpio_switch, LOW) : digitalWrite(gpio_switch, HIGH);
48-
}
49-
if(strcmp(device_name, "Fan") == 0) {
50-
fan_state = val.val.b;
51-
(fan_state == false) ? digitalWrite(gpio_fan, LOW) : digitalWrite(gpio_fan, HIGH);
52-
}
53-
}
54-
else if(strcmp(param_name, "Speed") == 0) {
55-
Serial.printf("\nReceived value = %d for %s - %s\n", val.val.i, device_name, param_name);
41+
Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
42+
switch_state = val.val.b;
43+
(switch_state == false) ? digitalWrite(gpio_switch, LOW) : digitalWrite(gpio_switch, HIGH);
44+
param->updateAndReport(val);
5645
}
57-
param->updateAndReport(val);
5846
}
5947

6048
void setup()
6149
{
6250
Serial.begin(115200);
6351
pinMode(gpio_0, INPUT);
6452
pinMode(gpio_switch, OUTPUT);
65-
pinMode(gpio_fan, OUTPUT);
53+
digitalWrite(gpio_switch, DEFAULT_POWER_MODE);
6654

6755
Node my_node;
6856
my_node = RMaker.initNode("ESP RainMaker Node");
6957

7058
//Standard switch device
7159
my_switch.addCb(write_callback);
7260

73-
//Creating custom fan device
74-
my_device.addNameParam();
75-
my_device.addPowerParam(DEFAULT_POWER_MODE);
76-
my_device.addSpeedParam(DEFAULT_FAN_SPEED);
77-
my_device.assignPrimaryParam(my_device.getParamByName(ESP_RMAKER_DEF_POWER_NAME));
78-
my_device.addCb(write_callback);
79-
80-
//Add switch and fan device to the node
61+
//Add switch device to the node
8162
my_node.addDevice(my_switch);
82-
my_node.addDevice(my_device);
8363

8464
//This is optional
8565
RMaker.enableOTA(OTA_USING_PARAMS);
8666
//If you want to enable scheduling, set time zone for your region using setTimeZone().
8767
//The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html
88-
RMaker.setTimeZone("Asia/Shanghai");
68+
// RMaker.setTimeZone("Asia/Shanghai");
69+
// Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone
70+
RMaker.enableTZService();
71+
8972
RMaker.enableSchedule();
9073

9174
RMaker.start();
@@ -96,18 +79,33 @@ void setup()
9679
#else
9780
WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name);
9881
#endif
99-
10082
}
10183

10284
void loop()
10385
{
104-
if(digitalRead(gpio_0) == LOW) { //Push button
105-
switch_state = !switch_state;
106-
fan_state = !fan_state;
107-
my_switch.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state);
108-
my_device.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, fan_state);
109-
(switch_state == false) ? digitalWrite(gpio_switch, LOW) : digitalWrite(gpio_switch, HIGH);
110-
(fan_state == false) ? digitalWrite(gpio_fan, LOW) : digitalWrite(gpio_fan, HIGH);
86+
if(digitalRead(gpio_0) == LOW) { //Push button pressed
87+
88+
// Key debounce handling
89+
delay(100);
90+
int startTime = millis();
91+
while(digitalRead(gpio_0) == LOW) delay(50);
92+
int endTime = millis();
93+
94+
if ((endTime - startTime) > 10000) {
95+
// If key pressed for more than 10secs, reset all
96+
Serial.printf("Reset to factory.\n");
97+
RMakerFactoryReset(2);
98+
} else if ((endTime - startTime) > 3000) {
99+
Serial.printf("Reset Wi-Fi.\n");
100+
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi
101+
RMakerWiFiReset(2);
102+
} else {
103+
// Toggle device state
104+
switch_state = !switch_state;
105+
Serial.printf("Toggle State to %s.\n", switch_state ? "true" : "false");
106+
my_switch.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state);
107+
(switch_state == false) ? digitalWrite(gpio_switch, LOW) : digitalWrite(gpio_switch, HIGH);
108+
}
111109
}
112110
delay(100);
113111
}

‎libraries/RainMaker/src/RMaker.cpp

Copy file name to clipboardExpand all lines: libraries/RainMaker/src/RMaker.cpp
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ esp_err_t RMakerClass::enableSchedule()
9292
return err;
9393
}
9494

95+
esp_err_t RMakerClass::enableTZService()
96+
{
97+
err = esp_rmaker_timezone_service_enable();
98+
if(err != ESP_OK) {
99+
log_e("Timezone service enable failed");
100+
}
101+
return err;
102+
}
103+
95104
esp_err_t RMakerClass::enableOTA(ota_type_t type, const char *cert)
96105
{
97106
esp_rmaker_ota_config_t ota_config;
@@ -104,4 +113,4 @@ esp_err_t RMakerClass::enableOTA(ota_type_t type, const char *cert)
104113
}
105114

106115
RMakerClass RMaker;
107-
#endif
116+
#endif

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.