You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Amended esp-rainmaker\examples\switch example
gpio_switch = PIN_21
Version
v2.0.9
IDE Name
Arduino IDE
Operating System
Windows 11
Flash frequency
No idea
PSRAM enabled
yes
Upload speed
115200
Description
I elaborate the esp-rainmaker\examples\switch example. Originally it works fine, but after adding a new device with his callback the switch callback become the same as new device callback.
Sketch
// This example demonstrates the ESP RainMaker with a standard Switch device.
#include"RMaker.h"
#include"WiFi.h"
#include"WiFiProv.h"
#include"AppInsights.h"
#defineDEFAULT_POWER_MODEtrueconstchar *service_name = "PROV_1234";
constchar *pop = "abcd1234";
// GPIO for push buttonstaticint gpio_0 = 0;
staticint gpio_switch = PIN_21;
/* Variable for reading pin status*/bool switch_state = true;
// The framework provides some standard device types like switch, lightbulb,// fan, temperaturesensor.static Switch *my_switch = NULL;
static Device *my_menu = NULL;
voidsysProvEvent(arduino_event_t *sys_event)
{
switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START:
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n",
service_name, pop);
printQR(service_name, pop, "softap");
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:;
}
}
voidwrite_callback(Device *device, Param *param, constparam_val_t val,
void *priv_data, write_ctx_t *ctx)
{
constchar *device_name = device->getDeviceName();
constchar *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);
switch_state = val.val.b;
(switch_state == false) ? digitalWrite(gpio_switch, LOW)
: digitalWrite(gpio_switch, HIGH);
param->updateAndReport(val);
}
}
/******* Added *******/voidwrite_debug_callback(Device *device, Param *param, constparam_val_t val,
void *priv_data, write_ctx_t *ctx) {
constchar *device_name = device->getDeviceName();
constchar *param_name = param->getParamName();
Serial.printf("[debug] %s/%s: address %08X\n", device_name, param_name, &val);
}
/**********************/voidsetup()
{
Serial.begin(115200);
pinMode(gpio_0, INPUT);
pinMode(gpio_switch, OUTPUT);
digitalWrite(gpio_switch, DEFAULT_POWER_MODE);
Node my_node;
my_node = RMaker.initNode("ESP RainMaker Node");
// Initialize switch device
my_switch = newSwitch("Switch", &gpio_switch);
if (!my_switch) {
return;
}
// Standard switch device
my_switch->addCb(write_callback);
// Add switch device to the node
my_node.addDevice(*my_switch);
/******* Added *******/
my_menu = newDevice("Main menu", NULL, NULL);
if (!my_menu) {
return;
}
my_menu->addNameParam();
my_menu->addPowerParam(true);
my_menu->assignPrimaryParam(my_menu->getParamByName(ESP_RMAKER_DEF_POWER_NAME));
Param mode_param("Mode", "custom.param.mode", esp_rmaker_str("Main"), PROP_FLAG_READ | PROP_FLAG_WRITE);
staticconstchar *mode_strs[] = {"Main", "Temp", "Internet", "About"};
mode_param.addValidStrList(mode_strs, 4);
mode_param.addUIType(ESP_RMAKER_UI_DROPDOWN);
my_menu->addParam(mode_param);
// !!!
my_menu->addCb(write_debug_callback); // <- after this both my_switch and my_menu call the same callback-function (write_debug_callback)
my_node.addDevice(*my_menu);
// !!! However all variables differ
Serial.printf("[write_callback] Value: %08X\n", write_callback);
Serial.printf("[my_switch] Value: %08X\n", my_switch);
Serial.printf("[getDeviceHandle] Value: %08X\n", my_switch->getDeviceHandle());
Serial.println("=======");
Serial.printf("[write_debug_callback] Value: %08X\n", write_debug_callback);
Serial.printf("[my_menu] Value: %08X\n", my_menu);
Serial.printf("[getDeviceHandle] Value: %08X\n", my_menu->getDeviceHandle());
/**********************/// This is optional
RMaker.enableOTA(OTA_USING_TOPICS);
// If you want to enable scheduling, set time zone for your region using// setTimeZone(). The list of available values are provided here// https://rainmaker.espressif.com/docs/time-service.html// RMaker.setTimeZone("Asia/Shanghai");// Alternatively, enable the Timezone service and let the phone apps set the// appropriate timezone
RMaker.enableTZService();
RMaker.enableSchedule();
RMaker.enableScenes();
// Enable ESP Insights. Insteads of using the default http transport, this function will// reuse the existing MQTT connection of Rainmaker, thereby saving memory space.initAppInsights();
RMaker.enableSystemService(SYSTEM_SERV_FLAGS_ALL, 2, 2, 2);
RMaker.start();
WiFi.onEvent(sysProvEvent);
WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE,
WIFI_PROV_SECURITY_1, pop, service_name);
}
voidloop()
{
if (digitalRead(gpio_0) == LOW) { // Push button pressed// Key debounce handlingdelay(100);
int startTime = millis();
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);
} elseif ((endTime - startTime) > 3000) {
Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-FiRMakerWiFiReset(2);
} else {
// Toggle device state
switch_state = !switch_state;
Serial.printf("Toggle State to %s.\n", switch_state ? "true" : "false");
if (my_switch) {
my_switch->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME,
switch_state);
}
(switch_state == false) ? digitalWrite(gpio_switch, LOW)
: digitalWrite(gpio_switch, HIGH);
}
}
delay(100);
}
This PR fixes an issue of multiple device callbacks (one callback per device) were not getting registered and invoked.
Consider an example where I create two devices, a switch and a fan; each of them having their own write callbacks. On controlling either switch/fan through Rainmaker app, the callback that got registered at last gets invoked. This is also seen in the issue reported in #8231
Board
LilyGo T-Display-S3
Device Description
LilyGo T-Display-S3
Hardware Configuration
Amended esp-rainmaker\examples\switch example
gpio_switch = PIN_21
Version
v2.0.9
IDE Name
Arduino IDE
Operating System
Windows 11
Flash frequency
No idea
PSRAM enabled
yes
Upload speed
115200
Description
I elaborate the esp-rainmaker\examples\switch example. Originally it works fine, but after adding a new device with his callback the switch callback become the same as new device callback.
Sketch
Debug Message
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
The text was updated successfully, but these errors were encountered: