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.
2
2
#include " RMaker.h"
3
3
#include " WiFi.h"
4
4
#include " WiFiProv.h"
5
5
6
- #define DEFAULT_FAN_SPEED 4
7
6
#define DEFAULT_POWER_MODE true
8
- const char *service_name = " Prov_1234 " ;
7
+ const char *service_name = " PROV_1234 " ;
9
8
const char *pop = " abcd1234" ;
10
9
11
10
// GPIO for push button
12
11
static int gpio_0 = 0 ;
13
12
// GPIO for virtual device
14
13
static int gpio_switch = 16 ;
15
- static int gpio_fan = 17 ;
16
14
/* Variable for reading pin status*/
17
15
bool switch_state = true ;
18
- bool fan_state = true ;
19
16
20
17
// The framework provides some standard device types like switch, lightbulb, fan, temperaturesensor.
21
18
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);
24
19
25
20
void sysProvEvent (arduino_event_t *sys_event)
26
21
{
27
22
switch (sys_event->event_id ) {
28
23
case ARDUINO_EVENT_PROV_START:
29
24
#if CONFIG_IDF_TARGET_ESP32
25
+ Serial.printf (" \n Provisioning Started with name \" %s\" and PoP \" %s\" on BLE\n " , service_name, pop);
30
26
printQR (service_name, pop, " ble" );
31
27
#else
28
+ Serial.printf (" \n Provisioning Started with name \" %s\" and PoP \" %s\" on SoftAP\n " , service_name, pop);
32
29
printQR (service_name, pop, " softap" );
33
30
#endif
34
31
break ;
@@ -41,51 +38,36 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p
41
38
const char *param_name = param->getParamName ();
42
39
43
40
if (strcmp (param_name, " Power" ) == 0 ) {
44
- Serial.printf (" \n Received 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 (" \n Received 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);
56
45
}
57
- param->updateAndReport (val);
58
46
}
59
47
60
48
void setup ()
61
49
{
62
50
Serial.begin (115200 );
63
51
pinMode (gpio_0, INPUT);
64
52
pinMode (gpio_switch, OUTPUT);
65
- pinMode (gpio_fan, OUTPUT);
66
53
67
54
Node my_node;
68
55
my_node = RMaker.initNode (" ESP RainMaker Node" );
69
56
70
57
// Standard switch device
71
58
my_switch.addCb (write_callback);
72
59
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
81
61
my_node.addDevice (my_switch);
82
- my_node.addDevice (my_device);
83
62
84
63
// This is optional
85
64
RMaker.enableOTA (OTA_USING_PARAMS);
86
65
// If you want to enable scheduling, set time zone for your region using setTimeZone().
87
66
// 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
+
89
71
RMaker.enableSchedule ();
90
72
91
73
RMaker.start ();
@@ -96,18 +78,33 @@ void setup()
96
78
#else
97
79
WiFiProv.beginProvision (WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name);
98
80
#endif
99
-
100
81
}
101
82
102
83
void loop ()
103
84
{
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
+ }
111
108
}
112
109
delay (100 );
113
110
}
0 commit comments