Skip to content

Commit bf291ca

Browse files
committed
RainMaker library: Minor changes
- 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. - Enable push button based Factory reset and Wi-Fi reset. - Moved API doc to RainMaker library's top level README. - Other minor doc changes.
1 parent 82161be commit bf291ca

File tree

10 files changed

+535
-508
lines changed

10 files changed

+535
-508
lines changed

Diff for: libraries/RainMaker/README.md

+444
Large diffs are not rendered by default.

Diff for: libraries/RainMaker/examples/README.md

+7-431
Large diffs are not rendered by default.

Diff for: libraries/RainMaker/examples/RMakerDevice/README.md

-34
This file was deleted.

Diff for: libraries/RainMaker/examples/RMakerSwitch/README.md

+27
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,36 @@ 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);
6653

6754
Node my_node;
6855
my_node = RMaker.initNode("ESP RainMaker Node");
6956

7057
//Standard switch device
7158
my_switch.addCb(write_callback);
7259

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
60+
//Add switch device to the node
8161
my_node.addDevice(my_switch);
82-
my_node.addDevice(my_device);
8362

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

9173
RMaker.start();
@@ -96,18 +78,33 @@ void setup()
9678
#else
9779
WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name);
9880
#endif
99-
10081
}
10182

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

Diff for: libraries/RainMaker/src/RMaker.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Arduino.h"
55
#include "RMakerNode.h"
66
#include "RMakerQR.h"
7+
#include "RMakerUtils.h"
78
#include <esp_rmaker_standard_types.h>
89

910
class RMakerClass
@@ -25,4 +26,4 @@ class RMakerClass
2526

2627
extern RMakerClass RMaker;
2728

28-
#endif
29+
#endif

Diff for: libraries/RainMaker/src/RMakerQR.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ static void printQR(const char *name, const char *pop, const char *transport)
1616
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
1717
",\"pop\":\"%s\",\"transport\":\"%s\"}",
1818
PROV_QR_VERSION, name, pop, transport);
19-
log_i("Scan this QR code from the phone app for Provisioning.");
19+
Serial.printf("Scan this QR code from the ESP RainMaker phone app.\n");
2020
qrcode_display(payload);
21-
log_i("If QR code is not visible, copy paste the below URL in a browser.\n%s?data=%s", QRCODE_BASE_URL, payload);
21+
Serial.printf("If QR code is not visible, copy paste the below URL in a browser.\n%s?data=%s\n", QRCODE_BASE_URL, payload);
2222
}
2323

24-
#endif
24+
#endif

Diff for: libraries/RainMaker/src/RMakerUtils.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "esp_system.h"
2+
#if ESP_IDF_VERSION_MAJOR >= 4 && CONFIG_IDF_TARGET_ESP32
3+
4+
#include <esp_rmaker_utils.h>
5+
6+
static void RMakerFactoryReset(int seconds)
7+
{
8+
esp_rmaker_factory_reset(seconds);
9+
}
10+
11+
static void RMakerWiFiReset(int seconds)
12+
{
13+
esp_rmaker_wifi_reset(seconds);
14+
}
15+
16+
#endif

0 commit comments

Comments
 (0)