Unverified Commit c0737f53 authored by Sanket Wadekar's avatar Sanket Wadekar Committed by GitHub

Fixed memory leaks in rainmaker examples (#7965)

* task: Added Rainmaker partition to esp32 wrover and
s3 box

* fix: memory leaks in rainmaker examples

* Rainmaker: Improved error log messages

* task: format Rainmaker examples
parent 8ebf7581
...@@ -1012,6 +1012,9 @@ esp32wrover.menu.PartitionScheme.min_spiffs.build.partitions=min_spiffs ...@@ -1012,6 +1012,9 @@ esp32wrover.menu.PartitionScheme.min_spiffs.build.partitions=min_spiffs
esp32wrover.menu.PartitionScheme.min_spiffs.upload.maximum_size=1966080 esp32wrover.menu.PartitionScheme.min_spiffs.upload.maximum_size=1966080
esp32wrover.menu.PartitionScheme.fatflash=16M Flash (2MB APP/12.5MB FATFS) esp32wrover.menu.PartitionScheme.fatflash=16M Flash (2MB APP/12.5MB FATFS)
esp32wrover.menu.PartitionScheme.fatflash.build.partitions=ffat esp32wrover.menu.PartitionScheme.fatflash.build.partitions=ffat
esp32wrover.menu.PartitionScheme.rainmaker=RainMaker
esp32wrover.menu.PartitionScheme.rainmaker.build.partitions=rainmaker
esp32wrover.menu.PartitionScheme.rainmaker.upload.maximum_size=3145728
esp32wrover.menu.FlashMode.qio=QIO esp32wrover.menu.FlashMode.qio=QIO
esp32wrover.menu.FlashMode.qio.build.flash_mode=dio esp32wrover.menu.FlashMode.qio.build.flash_mode=dio
...@@ -1238,6 +1241,9 @@ esp32s3box.menu.PartitionScheme.fatflash.upload.maximum_size=2097152 ...@@ -1238,6 +1241,9 @@ esp32s3box.menu.PartitionScheme.fatflash.upload.maximum_size=2097152
esp32s3box.menu.PartitionScheme.app3M_fat9M_16MB=16M Flash (3MB APP/9.9MB FATFS) esp32s3box.menu.PartitionScheme.app3M_fat9M_16MB=16M Flash (3MB APP/9.9MB FATFS)
esp32s3box.menu.PartitionScheme.app3M_fat9M_16MB.build.partitions=app3M_fat9M_16MB esp32s3box.menu.PartitionScheme.app3M_fat9M_16MB.build.partitions=app3M_fat9M_16MB
esp32s3box.menu.PartitionScheme.app3M_fat9M_16MB.upload.maximum_size=3145728 esp32s3box.menu.PartitionScheme.app3M_fat9M_16MB.upload.maximum_size=3145728
esp32s3box.menu.PartitionScheme.rainmaker=RainMaker
esp32s3box.menu.PartitionScheme.rainmaker.build.partitions=rainmaker
esp32s3box.menu.PartitionScheme.rainmaker.upload.maximum_size=3145728
esp32s3box.menu.DebugLevel.none=None esp32s3box.menu.DebugLevel.none=None
esp32s3box.menu.DebugLevel.none.build.code_debug=0 esp32s3box.menu.DebugLevel.none.build.code_debug=0
...@@ -1686,6 +1692,9 @@ esp32wroverkit.menu.PartitionScheme.min_spiffs.build.partitions=min_spiffs ...@@ -1686,6 +1692,9 @@ esp32wroverkit.menu.PartitionScheme.min_spiffs.build.partitions=min_spiffs
esp32wroverkit.menu.PartitionScheme.min_spiffs.upload.maximum_size=1966080 esp32wroverkit.menu.PartitionScheme.min_spiffs.upload.maximum_size=1966080
esp32wroverkit.menu.PartitionScheme.fatflash=16M Flash (2MB APP/12.5MB FATFS) esp32wroverkit.menu.PartitionScheme.fatflash=16M Flash (2MB APP/12.5MB FATFS)
esp32wroverkit.menu.PartitionScheme.fatflash.build.partitions=ffat esp32wroverkit.menu.PartitionScheme.fatflash.build.partitions=ffat
esp32wroverkit.menu.PartitionScheme.rainmaker=RainMaker
esp32wroverkit.menu.PartitionScheme.rainmaker.build.partitions=rainmaker
esp32wroverkit.menu.PartitionScheme.rainmaker.upload.maximum_size=3145728
esp32wroverkit.menu.FlashMode.qio=QIO esp32wroverkit.menu.FlashMode.qio=QIO
esp32wroverkit.menu.FlashMode.qio.build.flash_mode=dio esp32wroverkit.menu.FlashMode.qio.build.flash_mode=dio
esp32wroverkit.menu.FlashMode.qio.build.boot=qio esp32wroverkit.menu.FlashMode.qio.build.boot=qio
......
...@@ -22,27 +22,27 @@ bool dimmer_state = true; ...@@ -22,27 +22,27 @@ bool dimmer_state = true;
// The framework provides some standard device types like switch, lightbulb, fan, temperature sensor. // The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
// But, you can also define custom devices using the 'Device' base class object, as shown here // But, you can also define custom devices using the 'Device' base class object, as shown here
static Device my_device("Dimmer", "custom.device.dimmer", &gpio_dimmer); static Device *my_device = NULL;
void sysProvEvent(arduino_event_t *sys_event) void sysProvEvent(arduino_event_t *sys_event)
{ {
switch (sys_event->event_id) { switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START: case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32S2 #if CONFIG_IDF_TARGET_ESP32S2
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap"); printQR(service_name, pop, "softap");
#else #else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble"); printQR(service_name, pop, "ble");
#endif #endif
break; break;
case ARDUINO_EVENT_PROV_INIT: case ARDUINO_EVENT_PROV_INIT:
wifi_prov_mgr_disable_auto_stop(10000); wifi_prov_mgr_disable_auto_stop(10000);
break; break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: case ARDUINO_EVENT_PROV_CRED_SUCCESS:
wifi_prov_mgr_stop_provisioning(); wifi_prov_mgr_stop_provisioning();
break; break;
default:; default:;
} }
} }
...@@ -51,8 +51,8 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p ...@@ -51,8 +51,8 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p
const char *device_name = device->getDeviceName(); const char *device_name = device->getDeviceName();
const char *param_name = param->getParamName(); const char *param_name = param->getParamName();
if(strcmp(param_name, "Power") == 0) { if (strcmp(param_name, "Power") == 0) {
Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name); Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name);
dimmer_state = val.val.b; dimmer_state = val.val.b;
(dimmer_state == false) ? digitalWrite(gpio_dimmer, LOW) : digitalWrite(gpio_dimmer, HIGH); (dimmer_state == false) ? digitalWrite(gpio_dimmer, LOW) : digitalWrite(gpio_dimmer, HIGH);
param->updateAndReport(val); param->updateAndReport(val);
...@@ -71,22 +71,25 @@ void setup() ...@@ -71,22 +71,25 @@ void setup()
Node my_node; Node my_node;
my_node = RMaker.initNode("ESP RainMaker Node"); my_node = RMaker.initNode("ESP RainMaker Node");
my_device = new Device("Dimmer", "custom.device.dimmer", &gpio_dimmer);
if (!my_device) {
return;
}
//Create custom dimmer device //Create custom dimmer device
my_device.addNameParam(); my_device->addNameParam();
my_device.addPowerParam(DEFAULT_POWER_MODE); my_device->addPowerParam(DEFAULT_POWER_MODE);
my_device.assignPrimaryParam(my_device.getParamByName(ESP_RMAKER_DEF_POWER_NAME)); my_device->assignPrimaryParam(my_device->getParamByName(ESP_RMAKER_DEF_POWER_NAME));
//Create and add a custom level parameter //Create and add a custom level parameter
Param level_param("Level", "custom.param.level", value(DEFAULT_DIMMER_LEVEL), PROP_FLAG_READ | PROP_FLAG_WRITE); Param level_param("Level", "custom.param.level", value(DEFAULT_DIMMER_LEVEL), PROP_FLAG_READ | PROP_FLAG_WRITE);
level_param.addBounds(value(0), value(100), value(1)); level_param.addBounds(value(0), value(100), value(1));
level_param.addUIType(ESP_RMAKER_UI_SLIDER); level_param.addUIType(ESP_RMAKER_UI_SLIDER);
my_device.addParam(level_param); my_device->addParam(level_param);
my_device.addCb(write_callback); my_device->addCb(write_callback);
//Add custom dimmer device to the node //Add custom dimmer device to the node
my_node.addDevice(my_device); my_node.addDevice(*my_device);
//This is optional //This is optional
RMaker.enableOTA(OTA_USING_TOPICS); RMaker.enableOTA(OTA_USING_TOPICS);
...@@ -112,29 +115,33 @@ void setup() ...@@ -112,29 +115,33 @@ void setup()
void loop() void loop()
{ {
if(digitalRead(gpio_0) == LOW) { //Push button pressed if (digitalRead(gpio_0) == LOW) { //Push button pressed
// Key debounce handling // Key debounce handling
delay(100); delay(100);
int startTime = millis(); int startTime = millis();
while(digitalRead(gpio_0) == LOW) delay(50); while (digitalRead(gpio_0) == LOW) {
delay(50);
}
int endTime = millis(); int endTime = millis();
if ((endTime - startTime) > 10000) { if ((endTime - startTime) > 10000) {
// If key pressed for more than 10secs, reset all // If key pressed for more than 10secs, reset all
Serial.printf("Reset to factory.\n"); Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2); RMakerFactoryReset(2);
} else if ((endTime - startTime) > 3000) { } else if ((endTime - startTime) > 3000) {
Serial.printf("Reset Wi-Fi.\n"); Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi // If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2); RMakerWiFiReset(2);
} else { } else {
// Toggle device state // Toggle device state
dimmer_state = !dimmer_state; dimmer_state = !dimmer_state;
Serial.printf("Toggle State to %s.\n", dimmer_state ? "true" : "false"); Serial.printf("Toggle State to %s.\n", dimmer_state ? "true" : "false");
my_device.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, dimmer_state); if (my_device) {
(dimmer_state == false) ? digitalWrite(gpio_dimmer, LOW) : digitalWrite(gpio_dimmer, HIGH); my_device->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, dimmer_state);
} }
(dimmer_state == false) ? digitalWrite(gpio_dimmer, LOW) : digitalWrite(gpio_dimmer, HIGH);
}
} }
delay(100); delay(100);
} }
...@@ -39,27 +39,27 @@ bool power_state = true; ...@@ -39,27 +39,27 @@ bool power_state = true;
// The framework provides some standard device types like switch, lightbulb, fan, temperature sensor. // The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
// But, you can also define custom devices using the 'Device' base class object, as shown here // But, you can also define custom devices using the 'Device' base class object, as shown here
static Device my_device("Air Cooler", "my.device.air-cooler", NULL); static Device *my_device = NULL;
void sysProvEvent(arduino_event_t *sys_event) void sysProvEvent(arduino_event_t *sys_event)
{ {
switch (sys_event->event_id) { switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START: case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32S2 #if CONFIG_IDF_TARGET_ESP32S2
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap"); printQR(service_name, pop, "softap");
#else #else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble"); printQR(service_name, pop, "ble");
#endif #endif
break; break;
case ARDUINO_EVENT_PROV_INIT: case ARDUINO_EVENT_PROV_INIT:
wifi_prov_mgr_disable_auto_stop(10000); wifi_prov_mgr_disable_auto_stop(10000);
break; break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: case ARDUINO_EVENT_PROV_CRED_SUCCESS:
wifi_prov_mgr_stop_provisioning(); wifi_prov_mgr_stop_provisioning();
break; break;
default:; default:;
} }
} }
...@@ -68,13 +68,13 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p ...@@ -68,13 +68,13 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p
const char *device_name = device->getDeviceName(); const char *device_name = device->getDeviceName();
const char *param_name = param->getParamName(); const char *param_name = param->getParamName();
if(strcmp(param_name, "Power") == 0) { if (strcmp(param_name, "Power") == 0) {
Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name); Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name);
power_state = val.val.b; power_state = val.val.b;
(power_state == false) ? digitalWrite(gpio_power, LOW) : digitalWrite(gpio_power, HIGH); (power_state == false) ? digitalWrite(gpio_power, LOW) : digitalWrite(gpio_power, HIGH);
param->updateAndReport(val); param->updateAndReport(val);
} else if (strcmp(param_name, "Swing") == 0) { } else if (strcmp(param_name, "Swing") == 0) {
Serial.printf("\nReceived value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name); Serial.printf("\nReceived value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name);
bool swing = val.val.b; bool swing = val.val.b;
(swing == false) ? digitalWrite(gpio_swing, LOW) : digitalWrite(gpio_swing, HIGH); (swing == false) ? digitalWrite(gpio_swing, LOW) : digitalWrite(gpio_swing, HIGH);
param->updateAndReport(val); param->updateAndReport(val);
...@@ -84,7 +84,7 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p ...@@ -84,7 +84,7 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p
analogWrite(gpio_speed, speed); analogWrite(gpio_speed, speed);
param->updateAndReport(val); param->updateAndReport(val);
} else if (strcmp(param_name, "Mode") == 0) { } else if (strcmp(param_name, "Mode") == 0) {
const char* mode = val.val.s; const char *mode = val.val.s;
if (strcmp(mode, "Auto") == 0) { if (strcmp(mode, "Auto") == 0) {
digitalWrite(gpio_mode_auto, HIGH); digitalWrite(gpio_mode_auto, HIGH);
digitalWrite(gpio_mode_heat, LOW); digitalWrite(gpio_mode_heat, LOW);
...@@ -112,41 +112,50 @@ void setup() ...@@ -112,41 +112,50 @@ void setup()
pinMode(gpio_swing, OUTPUT); pinMode(gpio_swing, OUTPUT);
digitalWrite(gpio_swing, DEFAULT_SWING); digitalWrite(gpio_swing, DEFAULT_SWING);
pinMode(gpio_mode_auto, OUTPUT); pinMode(gpio_mode_auto, OUTPUT);
if (strcmp(DEFAULT_MODE, "Auto") == 0) digitalWrite(gpio_mode_auto, HIGH); if (strcmp(DEFAULT_MODE, "Auto") == 0) {
digitalWrite(gpio_mode_auto, HIGH);
}
pinMode(gpio_mode_cool, OUTPUT); pinMode(gpio_mode_cool, OUTPUT);
if (strcmp(DEFAULT_MODE, "Cool") == 0) digitalWrite(gpio_mode_auto, HIGH); if (strcmp(DEFAULT_MODE, "Cool") == 0) {
digitalWrite(gpio_mode_auto, HIGH);
}
pinMode(gpio_mode_heat, OUTPUT); pinMode(gpio_mode_heat, OUTPUT);
if (strcmp(DEFAULT_MODE, "Heat") == 0) digitalWrite(gpio_mode_auto, HIGH); if (strcmp(DEFAULT_MODE, "Heat") == 0) {
digitalWrite(gpio_mode_auto, HIGH);
}
pinMode(gpio_speed, OUTPUT); pinMode(gpio_speed, OUTPUT);
analogWrite(gpio_speed, DEFAULT_SPEED); analogWrite(gpio_speed, DEFAULT_SPEED);
Node my_node; Node my_node;
my_node = RMaker.initNode("ESP RainMaker Node"); my_node = RMaker.initNode("ESP RainMaker Node");
my_device = new Device("Air Cooler", "my.device.air-cooler", NULL);
if (!my_device) {
return;
}
//Create custom air cooler device //Create custom air cooler device
my_device.addNameParam(); my_device->addNameParam();
my_device.addPowerParam(DEFAULT_POWER_MODE); my_device->addPowerParam(DEFAULT_POWER_MODE);
my_device.assignPrimaryParam(my_device.getParamByName(ESP_RMAKER_DEF_POWER_NAME)); my_device->assignPrimaryParam(my_device->getParamByName(ESP_RMAKER_DEF_POWER_NAME));
Param swing("Swing", ESP_RMAKER_PARAM_TOGGLE, value(DEFAULT_SWING), PROP_FLAG_READ | PROP_FLAG_WRITE); Param swing("Swing", ESP_RMAKER_PARAM_TOGGLE, value(DEFAULT_SWING), PROP_FLAG_READ | PROP_FLAG_WRITE);
swing.addUIType(ESP_RMAKER_UI_TOGGLE); swing.addUIType(ESP_RMAKER_UI_TOGGLE);
my_device.addParam(swing); my_device->addParam(swing);
Param speed("Speed", ESP_RMAKER_PARAM_RANGE, value(DEFAULT_SPEED), PROP_FLAG_READ | PROP_FLAG_WRITE); Param speed("Speed", ESP_RMAKER_PARAM_RANGE, value(DEFAULT_SPEED), PROP_FLAG_READ | PROP_FLAG_WRITE);
speed.addUIType(ESP_RMAKER_UI_SLIDER); speed.addUIType(ESP_RMAKER_UI_SLIDER);
speed.addBounds(value(0), value(255), value(1)); speed.addBounds(value(0), value(255), value(1));
my_device.addParam(speed); my_device->addParam(speed);
static const char* modes[] = { "Auto", "Cool", "Heat" }; static const char *modes[] = { "Auto", "Cool", "Heat" };
Param mode_param("Mode", ESP_RMAKER_PARAM_MODE, value("Auto"), PROP_FLAG_READ | PROP_FLAG_WRITE); Param mode_param("Mode", ESP_RMAKER_PARAM_MODE, value("Auto"), PROP_FLAG_READ | PROP_FLAG_WRITE);
mode_param.addValidStrList(modes, 3); mode_param.addValidStrList(modes, 3);
mode_param.addUIType(ESP_RMAKER_UI_DROPDOWN); mode_param.addUIType(ESP_RMAKER_UI_DROPDOWN);
my_device.addParam(mode_param); my_device->addParam(mode_param);
my_device.addCb(write_callback); my_device->addCb(write_callback);
//Add custom Air Cooler device to the node //Add custom Air Cooler device to the node
my_node.addDevice(my_device); my_node.addDevice(*my_device);
//This is optional //This is optional
// RMaker.enableOTA(OTA_USING_TOPICS); // RMaker.enableOTA(OTA_USING_TOPICS);
...@@ -172,29 +181,33 @@ void setup() ...@@ -172,29 +181,33 @@ void setup()
void loop() void loop()
{ {
if(digitalRead(gpio_reset) == LOW) { //Push button pressed if (digitalRead(gpio_reset) == LOW) { //Push button pressed
// Key debounce handling // Key debounce handling
delay(100); delay(100);
int startTime = millis(); int startTime = millis();
while(digitalRead(gpio_reset) == LOW) delay(50); while (digitalRead(gpio_reset) == LOW) {
delay(50);
}
int press_duration = millis() - startTime; int press_duration = millis() - startTime;
if (press_duration > 10000) { if (press_duration > 10000) {
// If key pressed for more than 10secs, reset all // If key pressed for more than 10secs, reset all
Serial.printf("Reset to factory.\n"); Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2); RMakerFactoryReset(2);
} else if (press_duration > 3000) { } else if (press_duration > 3000) {
Serial.printf("Reset Wi-Fi.\n"); Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi // If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2); RMakerWiFiReset(2);
} else { } else {
// Toggle device state // Toggle device state
power_state = !power_state; power_state = !power_state;
Serial.printf("Toggle power state to %s.\n", power_state ? "true" : "false"); Serial.printf("Toggle power state to %s.\n", power_state ? "true" : "false");
my_device.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, power_state); if (my_device) {
(power_state == false) ? digitalWrite(gpio_power, LOW) : digitalWrite(gpio_power, HIGH); my_device->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, power_state);
} }
(power_state == false) ? digitalWrite(gpio_power, LOW) : digitalWrite(gpio_power, HIGH);
}
} }
delay(100); delay(100);
} }
...@@ -31,32 +31,32 @@ LightSwitch switch_ch1 = {gpio_switch1, false}; ...@@ -31,32 +31,32 @@ LightSwitch switch_ch1 = {gpio_switch1, false};
LightSwitch switch_ch2 = {gpio_switch2, false}; LightSwitch switch_ch2 = {gpio_switch2, false};
//The framework provides some standard device types like switch, lightbulb, fan, temperature sensor. //The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
static Switch my_switch1; static Switch *my_switch1 = NULL;
static Switch my_switch2; static Switch *my_switch2 = NULL;
void sysProvEvent(arduino_event_t *sys_event) void sysProvEvent(arduino_event_t *sys_event)
{ {
switch (sys_event->event_id) { switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START: case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32 #if CONFIG_IDF_TARGET_ESP32
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble"); printQR(service_name, pop, "ble");
#else #else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap"); printQR(service_name, pop, "softap");
#endif #endif
break; break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED: case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.printf("\nConnected to Wi-Fi!\n"); Serial.printf("\nConnected to Wi-Fi!\n");
digitalWrite(gpio_led, true); digitalWrite(gpio_led, true);
break; break;
case ARDUINO_EVENT_PROV_INIT: case ARDUINO_EVENT_PROV_INIT:
wifi_prov_mgr_disable_auto_stop(10000); wifi_prov_mgr_disable_auto_stop(10000);
break; break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: case ARDUINO_EVENT_PROV_CRED_SUCCESS:
wifi_prov_mgr_stop_provisioning(); wifi_prov_mgr_stop_provisioning();
break; break;
default:; default:;
} }
} }
...@@ -67,34 +67,35 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p ...@@ -67,34 +67,35 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p
const char *device_name = device->getDeviceName(); const char *device_name = device->getDeviceName();
const char *param_name = param->getParamName(); const char *param_name = param->getParamName();
if(strcmp(device_name, "Switch_ch1") == 0) { if (strcmp(device_name, "Switch_ch1") == 0) {
Serial.printf("Lightbulb = %s\n", val.val.b? "true" : "false"); Serial.printf("Lightbulb = %s\n", val.val.b ? "true" : "false");
if(strcmp(param_name, "Power") == 0) { if (strcmp(param_name, "Power") == 0) {
Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name); Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name);
switch_state_ch1 = val.val.b; switch_state_ch1 = val.val.b;
(switch_state_ch1 == false) ? digitalWrite(gpio_relay1, LOW) : digitalWrite(gpio_relay1, HIGH); (switch_state_ch1 == false) ? digitalWrite(gpio_relay1, LOW) : digitalWrite(gpio_relay1, HIGH);
param->updateAndReport(val); param->updateAndReport(val);
} }
} else if(strcmp(device_name, "Switch_ch2") == 0) { } else if (strcmp(device_name, "Switch_ch2") == 0) {
Serial.printf("Switch value = %s\n", val.val.b? "true" : "false"); Serial.printf("Switch value = %s\n", val.val.b ? "true" : "false");
if(strcmp(param_name, "Power") == 0) { if (strcmp(param_name, "Power") == 0) {
Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name); Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name);
switch_state_ch2 = val.val.b; switch_state_ch2 = val.val.b;
(switch_state_ch2 == false) ? digitalWrite(gpio_relay2, LOW) : digitalWrite(gpio_relay2, HIGH); (switch_state_ch2 == false) ? digitalWrite(gpio_relay2, LOW) : digitalWrite(gpio_relay2, HIGH);
param->updateAndReport(val); param->updateAndReport(val);
} }
} }
} }
void ARDUINO_ISR_ATTR isr(void* arg) { void ARDUINO_ISR_ATTR isr(void *arg)
LightSwitch* s = static_cast<LightSwitch*>(arg); {
LightSwitch *s = static_cast<LightSwitch *>(arg);
s->pressed = true; s->pressed = true;
} }
...@@ -125,16 +126,19 @@ void setup() ...@@ -125,16 +126,19 @@ void setup()
my_node = RMaker.initNode("Sonoff Dual R3"); my_node = RMaker.initNode("Sonoff Dual R3");
//Initialize switch device //Initialize switch device
my_switch1 = Switch("Switch_ch1", &gpio_relay1); my_switch1 = new Switch("Switch_ch1", &gpio_relay1);
my_switch2 = Switch("Switch_ch2", &gpio_relay2); my_switch2 = new Switch("Switch_ch2", &gpio_relay2);
if (!my_switch1 || !my_switch2) {
return;
}
//Standard switch device //Standard switch device
my_switch1.addCb(write_callback); my_switch1->addCb(write_callback);
my_switch2.addCb(write_callback); my_switch2->addCb(write_callback);
//Add switch device to the node //Add switch device to the node
my_node.addDevice(my_switch1); my_node.addDevice(*my_switch1);
my_node.addDevice(my_switch2); my_node.addDevice(*my_switch2);
//This is optional //This is optional
RMaker.enableOTA(OTA_USING_TOPICS); RMaker.enableOTA(OTA_USING_TOPICS);
...@@ -147,8 +151,8 @@ void setup() ...@@ -147,8 +151,8 @@ void setup()
RMaker.enableScenes(); RMaker.enableScenes();
//Service Name //Service Name
for(int i=0; i<17; i=i+8) { for (int i = 0; i < 17; i = i + 8) {
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i; chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
} }
Serial.printf("\nChip ID: %d Service Name: %s\n", chipId, service_name); Serial.printf("\nChip ID: %d Service Name: %s\n", chipId, service_name);
...@@ -173,7 +177,9 @@ void loop() ...@@ -173,7 +177,9 @@ void loop()
// Toggle switch 1 device state // Toggle switch 1 device state
switch_state_ch1 = !switch_state_ch1; switch_state_ch1 = !switch_state_ch1;
Serial.printf("Toggle State to %s.\n", switch_state_ch1 ? "true" : "false"); Serial.printf("Toggle State to %s.\n", switch_state_ch1 ? "true" : "false");
my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch1); if (my_switch1) {
my_switch1->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch1);
}
(switch_state_ch1 == false) ? digitalWrite(gpio_relay1, LOW) : digitalWrite(gpio_relay1, HIGH); (switch_state_ch1 == false) ? digitalWrite(gpio_relay1, LOW) : digitalWrite(gpio_relay1, HIGH);
} else if (switch_ch2.pressed) { } else if (switch_ch2.pressed) {
Serial.printf("Switch 2 has been changed\n"); Serial.printf("Switch 2 has been changed\n");
...@@ -181,27 +187,31 @@ void loop() ...@@ -181,27 +187,31 @@ void loop()
// Toggle switch 2 device state // Toggle switch 2 device state
switch_state_ch2 = !switch_state_ch2; switch_state_ch2 = !switch_state_ch2;
Serial.printf("Toggle State to %s.\n", switch_state_ch2 ? "true" : "false"); Serial.printf("Toggle State to %s.\n", switch_state_ch2 ? "true" : "false");
my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch2); if (my_switch2) {
my_switch2->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch2);
}
(switch_state_ch2 == false) ? digitalWrite(gpio_relay2, LOW) : digitalWrite(gpio_relay2, HIGH); (switch_state_ch2 == false) ? digitalWrite(gpio_relay2, LOW) : digitalWrite(gpio_relay2, HIGH);
} }
// Read GPIO0 (external button to reset device // Read GPIO0 (external button to reset device
if(digitalRead(gpio_reset) == LOW) { //Push button pressed if (digitalRead(gpio_reset) == LOW) { //Push button pressed
Serial.printf("Reset Button Pressed!\n"); Serial.printf("Reset Button Pressed!\n");
// Key debounce handling // Key debounce handling
delay(100); delay(100);
int startTime = millis(); int startTime = millis();
while(digitalRead(gpio_reset) == LOW) delay(50); while (digitalRead(gpio_reset) == LOW) {
delay(50);
}
int endTime = millis(); int endTime = millis();
if ((endTime - startTime) > 10000) { if ((endTime - startTime) > 10000) {
// If key pressed for more than 10secs, reset all // If key pressed for more than 10secs, reset all
Serial.printf("Reset to factory.\n"); Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2); RMakerFactoryReset(2);
} else if ((endTime - startTime) > 3000) { } else if ((endTime - startTime) > 3000) {
Serial.printf("Reset Wi-Fi.\n"); Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi // If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2); RMakerWiFiReset(2);
} }
} }
delay(100); delay(100);
......
//This example demonstrates the ESP RainMaker with a standard Switch device. // This example demonstrates the ESP RainMaker with a standard Switch device.
#include "RMaker.h" #include "RMaker.h"
#include "WiFi.h" #include "WiFi.h"
#include "WiFiProv.h" #include "WiFiProv.h"
...@@ -7,12 +7,12 @@ ...@@ -7,12 +7,12 @@
const char *service_name = "PROV_1234"; const char *service_name = "PROV_1234";
const char *pop = "abcd1234"; const char *pop = "abcd1234";
//GPIO for push button // GPIO for push button
#if CONFIG_IDF_TARGET_ESP32C3 #if CONFIG_IDF_TARGET_ESP32C3
static int gpio_0 = 9; static int gpio_0 = 9;
static int gpio_switch = 7; static int gpio_switch = 7;
#else #else
//GPIO for virtual device // GPIO for virtual device
static int gpio_0 = 0; static int gpio_0 = 0;
static int gpio_switch = 16; static int gpio_switch = 16;
#endif #endif
...@@ -20,40 +20,46 @@ static int gpio_switch = 16; ...@@ -20,40 +20,46 @@ static int gpio_switch = 16;
/* Variable for reading pin status*/ /* Variable for reading pin status*/
bool switch_state = true; bool switch_state = true;
//The framework provides some standard device types like switch, lightbulb, fan, temperaturesensor. // The framework provides some standard device types like switch, lightbulb,
static Switch my_switch; // fan, temperaturesensor.
static Switch *my_switch = NULL;
void sysProvEvent(arduino_event_t *sys_event) void sysProvEvent(arduino_event_t *sys_event)
{ {
switch (sys_event->event_id) { switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START: case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32S2 #if CONFIG_IDF_TARGET_ESP32S2
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n",
printQR(service_name, pop, "softap"); service_name, pop);
printQR(service_name, pop, "softap");
#else #else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n",
printQR(service_name, pop, "ble"); service_name, pop);
printQR(service_name, pop, "ble");
#endif #endif
break; break;
case ARDUINO_EVENT_PROV_INIT: case ARDUINO_EVENT_PROV_INIT:
wifi_prov_mgr_disable_auto_stop(10000); wifi_prov_mgr_disable_auto_stop(10000);
break; break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: case ARDUINO_EVENT_PROV_CRED_SUCCESS:
wifi_prov_mgr_stop_provisioning(); wifi_prov_mgr_stop_provisioning();
break; break;
default:; default:;
} }
} }
void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx) void write_callback(Device *device, Param *param, const param_val_t val,
void *priv_data, write_ctx_t *ctx)
{ {
const char *device_name = device->getDeviceName(); const char *device_name = device->getDeviceName();
const char *param_name = param->getParamName(); const char *param_name = param->getParamName();
if(strcmp(param_name, "Power") == 0) { if (strcmp(param_name, "Power") == 0) {
Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name); Serial.printf("Received value = %s for %s - %s\n",
val.val.b ? "true" : "false", device_name, param_name);
switch_state = val.val.b; switch_state = val.val.b;
(switch_state == false) ? digitalWrite(gpio_switch, LOW) : digitalWrite(gpio_switch, HIGH); (switch_state == false) ? digitalWrite(gpio_switch, LOW)
: digitalWrite(gpio_switch, HIGH);
param->updateAndReport(val); param->updateAndReport(val);
} }
} }
...@@ -68,21 +74,25 @@ void setup() ...@@ -68,21 +74,25 @@ void setup()
Node my_node; Node my_node;
my_node = RMaker.initNode("ESP RainMaker Node"); my_node = RMaker.initNode("ESP RainMaker Node");
//Initialize switch device // Initialize switch device
my_switch = Switch("Switch", &gpio_switch); my_switch = new Switch("Switch", &gpio_switch);
if (!my_switch) {
//Standard switch device return;
my_switch.addCb(write_callback); }
// Standard switch device
my_switch->addCb(write_callback);
//Add switch device to the node // Add switch device to the node
my_node.addDevice(my_switch); my_node.addDevice(*my_switch);
//This is optional // This is optional
RMaker.enableOTA(OTA_USING_TOPICS); RMaker.enableOTA(OTA_USING_TOPICS);
//If you want to enable scheduling, set time zone for your region using setTimeZone(). // If you want to enable scheduling, set time zone for your region using
//The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html // setTimeZone(). The list of available values are provided here
// RMaker.setTimeZone("Asia/Shanghai"); // https://rainmaker.espressif.com/docs/time-service.html
// Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone // RMaker.setTimeZone("Asia/Shanghai");
// Alternatively, enable the Timezone service and let the phone apps set the
// appropriate timezone
RMaker.enableTZService(); RMaker.enableTZService();
RMaker.enableSchedule(); RMaker.enableSchedule();
...@@ -93,37 +103,45 @@ void setup() ...@@ -93,37 +103,45 @@ void setup()
WiFi.onEvent(sysProvEvent); WiFi.onEvent(sysProvEvent);
#if CONFIG_IDF_TARGET_ESP32S2 #if CONFIG_IDF_TARGET_ESP32S2
WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name); WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE,
WIFI_PROV_SECURITY_1, pop, service_name);
#else #else
WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name); WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM,
WIFI_PROV_SECURITY_1, pop, service_name);
#endif #endif
} }
void loop() void loop()
{ {
if(digitalRead(gpio_0) == LOW) { //Push button pressed if (digitalRead(gpio_0) == LOW) { // Push button pressed
// Key debounce handling // Key debounce handling
delay(100); delay(100);
int startTime = millis(); int startTime = millis();
while(digitalRead(gpio_0) == LOW) delay(50); while (digitalRead(gpio_0) == LOW) {
delay(50);
}
int endTime = millis(); int endTime = millis();
if ((endTime - startTime) > 10000) { if ((endTime - startTime) > 10000) {
// If key pressed for more than 10secs, reset all // If key pressed for more than 10secs, reset all
Serial.printf("Reset to factory.\n"); Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2); RMakerFactoryReset(2);
} else if ((endTime - startTime) > 3000) { } else if ((endTime - startTime) > 3000) {
Serial.printf("Reset Wi-Fi.\n"); Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi // If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2); RMakerWiFiReset(2);
} else { } else {
// Toggle device state // Toggle device state
switch_state = !switch_state; switch_state = !switch_state;
Serial.printf("Toggle State to %s.\n", switch_state ? "true" : "false"); Serial.printf("Toggle State to %s.\n", switch_state ? "true" : "false");
my_switch.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state); if (my_switch) {
(switch_state == false) ? digitalWrite(gpio_switch, LOW) : digitalWrite(gpio_switch, HIGH); my_switch->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME,
} switch_state);
}
(switch_state == false) ? digitalWrite(gpio_switch, LOW)
: digitalWrite(gpio_switch, HIGH);
}
} }
delay(100); delay(100);
} }
...@@ -33,7 +33,7 @@ esp_err_t Device::deleteDevice() ...@@ -33,7 +33,7 @@ esp_err_t Device::deleteDevice()
{ {
err = esp_rmaker_device_delete(getDeviceHandle()); err = esp_rmaker_device_delete(getDeviceHandle());
if(err != ESP_OK) { if(err != ESP_OK) {
log_e("Device deletion error"); log_e("Failed to delete device");
return err; return err;
} }
return ESP_OK; return ESP_OK;
...@@ -45,7 +45,7 @@ void Device::addCb(deviceWriteCb writeCb, deviceReadCb readCb) ...@@ -45,7 +45,7 @@ void Device::addCb(deviceWriteCb writeCb, deviceReadCb readCb)
read_cb = readCb; read_cb = readCb;
err = esp_rmaker_device_add_cb(getDeviceHandle(), write_callback, read_callback); err = esp_rmaker_device_add_cb(getDeviceHandle(), write_callback, read_callback);
if(err != ESP_OK) { if(err != ESP_OK) {
log_e("Callback register error"); log_e("Failed to register callback");
} }
} }
...@@ -64,7 +64,7 @@ esp_err_t Device::addParam(Param parameter) ...@@ -64,7 +64,7 @@ esp_err_t Device::addParam(Param parameter)
{ {
err = esp_rmaker_device_add_param(getDeviceHandle(), parameter.getParamHandle()); err = esp_rmaker_device_add_param(getDeviceHandle(), parameter.getParamHandle());
if(err != ESP_OK) { if(err != ESP_OK) {
log_e("Adding custom parameter error"); log_e("Failed to add custom parameter");
return err; return err;
} }
return ESP_OK; return ESP_OK;
...@@ -140,7 +140,7 @@ esp_err_t Device::assignPrimaryParam(param_handle_t *param) ...@@ -140,7 +140,7 @@ esp_err_t Device::assignPrimaryParam(param_handle_t *param)
{ {
err = esp_rmaker_device_assign_primary_param(getDeviceHandle(), param); err = esp_rmaker_device_assign_primary_param(getDeviceHandle(), param);
if(err != ESP_OK){ if(err != ESP_OK){
log_e("Assigning primary param error"); log_e("Failed to assign primary parameter");
} }
return err; return err;
} }
...@@ -157,7 +157,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, bool my_val) ...@@ -157,7 +157,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, bool my_val)
param_val_t val = esp_rmaker_bool(my_val); param_val_t val = esp_rmaker_bool(my_val);
err = esp_rmaker_param_update_and_report(param, val); err = esp_rmaker_param_update_and_report(param, val);
if(err != ESP_OK) { if(err != ESP_OK) {
log_e("Update paramter failed"); log_e("Update parameter failed");
return err; return err;
}else { }else {
log_i("Device : %s, Param Name : %s, Val : %s", getDeviceName(), param_name, my_val ? "true" : "false"); log_i("Device : %s, Param Name : %s, Val : %s", getDeviceName(), param_name, my_val ? "true" : "false");
...@@ -171,7 +171,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, int my_val) ...@@ -171,7 +171,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, int my_val)
param_val_t val = esp_rmaker_int(my_val); param_val_t val = esp_rmaker_int(my_val);
esp_err_t err = esp_rmaker_param_update_and_report(param, val); esp_err_t err = esp_rmaker_param_update_and_report(param, val);
if(err != ESP_OK) { if(err != ESP_OK) {
log_e("Update paramter failed"); log_e("Update parameter failed");
return err; return err;
}else { }else {
log_i("Device : %s, Param Name : %s, Val : %d", getDeviceName(), param_name, my_val); log_i("Device : %s, Param Name : %s, Val : %d", getDeviceName(), param_name, my_val);
...@@ -185,7 +185,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, float my_val) ...@@ -185,7 +185,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, float my_val)
param_val_t val = esp_rmaker_float(my_val); param_val_t val = esp_rmaker_float(my_val);
esp_err_t err = esp_rmaker_param_update_and_report(param, val); esp_err_t err = esp_rmaker_param_update_and_report(param, val);
if(err != ESP_OK) { if(err != ESP_OK) {
log_e("Update paramter failed"); log_e("Update parameter failed");
return err; return err;
}else { }else {
log_i("Device : %s, Param Name : %s, Val : %f", getDeviceName(), param_name, my_val); log_i("Device : %s, Param Name : %s, Val : %f", getDeviceName(), param_name, my_val);
...@@ -199,7 +199,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, const char *my_va ...@@ -199,7 +199,7 @@ esp_err_t Device::updateAndReportParam(const char *param_name, const char *my_va
param_val_t val = esp_rmaker_str(my_val); param_val_t val = esp_rmaker_str(my_val);
esp_err_t err = esp_rmaker_param_update_and_report(param, val); esp_err_t err = esp_rmaker_param_update_and_report(param, val);
if(err != ESP_OK) { if(err != ESP_OK) {
log_e("Update paramter failed"); log_e("Update parameter failed");
return err; return err;
}else { }else {
log_i("Device : %s, Param Name : %s, Val : %s", getDeviceName(), param_name, my_val); log_i("Device : %s, Param Name : %s, Val : %s", getDeviceName(), param_name, my_val);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment