Skip to content

Fixed memory leaks in rainmaker examples #7965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.fatflash=16M Flash (2MB APP/12.5MB FATFS)
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.build.flash_mode=dio
Expand Down Expand Up @@ -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.build.partitions=app3M_fat9M_16MB
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.build.code_debug=0
Expand Down Expand Up @@ -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.fatflash=16M Flash (2MB APP/12.5MB FATFS)
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.build.flash_mode=dio
esp32wroverkit.menu.FlashMode.qio.build.boot=qio
Expand Down
81 changes: 44 additions & 37 deletions libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ bool dimmer_state = true;

// 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
static Device my_device("Dimmer", "custom.device.dimmer", &gpio_dimmer);
static Device *my_device = NULL;

void sysProvEvent(arduino_event_t *sys_event)
{
switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START:
case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32S2
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap");
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap");
#else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
#endif
break;
case ARDUINO_EVENT_PROV_INIT:
wifi_prov_mgr_disable_auto_stop(10000);
break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS:
wifi_prov_mgr_stop_provisioning();
break;
default:;
break;
case ARDUINO_EVENT_PROV_INIT:
wifi_prov_mgr_disable_auto_stop(10000);
break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS:
wifi_prov_mgr_stop_provisioning();
break;
default:;
}
}

Expand All @@ -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 *param_name = param->getParamName();

if(strcmp(param_name, "Power") == 0) {
Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
if (strcmp(param_name, "Power") == 0) {
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 == false) ? digitalWrite(gpio_dimmer, LOW) : digitalWrite(gpio_dimmer, HIGH);
param->updateAndReport(val);
Expand All @@ -71,22 +71,25 @@ void setup()

Node my_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
my_device.addNameParam();
my_device.addPowerParam(DEFAULT_POWER_MODE);
my_device.assignPrimaryParam(my_device.getParamByName(ESP_RMAKER_DEF_POWER_NAME));
my_device->addNameParam();
my_device->addPowerParam(DEFAULT_POWER_MODE);
my_device->assignPrimaryParam(my_device->getParamByName(ESP_RMAKER_DEF_POWER_NAME));

//Create and add a custom level parameter
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.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
my_node.addDevice(my_device);
my_node.addDevice(*my_device);

//This is optional
RMaker.enableOTA(OTA_USING_TOPICS);
Expand All @@ -112,29 +115,33 @@ void setup()

void loop()
{
if(digitalRead(gpio_0) == LOW) { //Push button pressed
if (digitalRead(gpio_0) == LOW) { //Push button pressed

// Key debounce handling
delay(100);
int startTime = millis();
while(digitalRead(gpio_0) == LOW) delay(50);
while (digitalRead(gpio_0) == LOW) {
delay(50);
}
int endTime = millis();

if ((endTime - startTime) > 10000) {
// If key pressed for more than 10secs, reset all
Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2);
// If key pressed for more than 10secs, reset all
Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2);
} else if ((endTime - startTime) > 3000) {
Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2);
Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2);
} else {
// Toggle device state
dimmer_state = !dimmer_state;
Serial.printf("Toggle State to %s.\n", dimmer_state ? "true" : "false");
my_device.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, dimmer_state);
(dimmer_state == false) ? digitalWrite(gpio_dimmer, LOW) : digitalWrite(gpio_dimmer, HIGH);
}
// Toggle device state
dimmer_state = !dimmer_state;
Serial.printf("Toggle State to %s.\n", dimmer_state ? "true" : "false");
if (my_device) {
my_device->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, dimmer_state);
}
(dimmer_state == false) ? digitalWrite(gpio_dimmer, LOW) : digitalWrite(gpio_dimmer, HIGH);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow same styling across the file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are losing time with style issues when an automatic script with clang formatter could keep all PR at same pattern.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a script in IDF which uses astyle programme to achieve this: here. However this has to be run manually for this repo.

If you have a change for using automated clang formatter, would you please raise a PR for the same?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not an expert in this subject otherwise I could make a PR. Actually I'm using uncrustify in VSCode only.

Anyway there's an action with example for artistic style here:
https://github.com/marketplace/actions/artistic-style

}
delay(100);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@ bool power_state = true;

// 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
static Device my_device("Air Cooler", "my.device.air-cooler", NULL);
static Device *my_device = NULL;

void sysProvEvent(arduino_event_t *sys_event)
{
switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START:
case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32S2
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap");
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap");
#else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
#endif
break;
case ARDUINO_EVENT_PROV_INIT:
wifi_prov_mgr_disable_auto_stop(10000);
break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS:
wifi_prov_mgr_stop_provisioning();
break;
default:;
break;
case ARDUINO_EVENT_PROV_INIT:
wifi_prov_mgr_disable_auto_stop(10000);
break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS:
wifi_prov_mgr_stop_provisioning();
break;
default:;
}
}

Expand All @@ -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 *param_name = param->getParamName();

if(strcmp(param_name, "Power") == 0) {
Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
if (strcmp(param_name, "Power") == 0) {
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 == false) ? digitalWrite(gpio_power, LOW) : digitalWrite(gpio_power, HIGH);
param->updateAndReport(val);
} 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;
(swing == false) ? digitalWrite(gpio_swing, LOW) : digitalWrite(gpio_swing, HIGH);
param->updateAndReport(val);
Expand All @@ -84,7 +84,7 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p
analogWrite(gpio_speed, speed);
param->updateAndReport(val);
} else if (strcmp(param_name, "Mode") == 0) {
const char* mode = val.val.s;
const char *mode = val.val.s;
if (strcmp(mode, "Auto") == 0) {
digitalWrite(gpio_mode_auto, HIGH);
digitalWrite(gpio_mode_heat, LOW);
Expand Down Expand Up @@ -112,41 +112,50 @@ void setup()
pinMode(gpio_swing, OUTPUT);
digitalWrite(gpio_swing, DEFAULT_SWING);
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);
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);
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);
analogWrite(gpio_speed, DEFAULT_SPEED);

Node my_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
my_device.addNameParam();
my_device.addPowerParam(DEFAULT_POWER_MODE);
my_device.assignPrimaryParam(my_device.getParamByName(ESP_RMAKER_DEF_POWER_NAME));
my_device->addNameParam();
my_device->addPowerParam(DEFAULT_POWER_MODE);
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);
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);
speed.addUIType(ESP_RMAKER_UI_SLIDER);
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);
mode_param.addValidStrList(modes, 3);
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
my_node.addDevice(my_device);
my_node.addDevice(*my_device);

//This is optional
// RMaker.enableOTA(OTA_USING_TOPICS);
Expand All @@ -172,29 +181,33 @@ void setup()

void loop()
{
if(digitalRead(gpio_reset) == LOW) { //Push button pressed
if (digitalRead(gpio_reset) == LOW) { //Push button pressed

// Key debounce handling
delay(100);
int startTime = millis();
while(digitalRead(gpio_reset) == LOW) delay(50);
while (digitalRead(gpio_reset) == LOW) {
delay(50);
}
int press_duration = millis() - startTime;

if (press_duration > 10000) {
// If key pressed for more than 10secs, reset all
Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2);
// If key pressed for more than 10secs, reset all
Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2);
} else if (press_duration > 3000) {
Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2);
Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2);
} else {
// Toggle device state
power_state = !power_state;
Serial.printf("Toggle power state to %s.\n", power_state ? "true" : "false");
my_device.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, power_state);
(power_state == false) ? digitalWrite(gpio_power, LOW) : digitalWrite(gpio_power, HIGH);
}
// Toggle device state
power_state = !power_state;
Serial.printf("Toggle power state to %s.\n", power_state ? "true" : "false");
if (my_device) {
my_device->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, power_state);
}
(power_state == false) ? digitalWrite(gpio_power, LOW) : digitalWrite(gpio_power, HIGH);
}
}
delay(100);
}
Loading