Skip to content

Fixed switch rename issue in examples espressif/esp-rainmaker#152 #7171

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 1 commit into from
Aug 29, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ LightSwitch switch_ch1 = {gpio_switch1, false};
LightSwitch switch_ch2 = {gpio_switch2, false};

//The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
static Switch my_switch1("Switch_ch1", &gpio_relay1);
static Switch my_switch2("Switch_ch2", &gpio_relay2);
static Switch my_switch1;
static Switch my_switch2;

void sysProvEvent(arduino_event_t *sys_event)
{
switch (sys_event->event_id) {
switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
#else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap");
#endif
#endif
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.printf("\nConnected to Wi-Fi!\n");
Expand All @@ -60,18 +60,18 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p
const char *param_name = param->getParamName();

if(strcmp(device_name, "Switch_ch1") == 0) {

Serial.printf("Lightbulb = %s\n", val.val.b? "true" : "false");

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_ch1 = val.val.b;
(switch_state_ch1 == false) ? digitalWrite(gpio_relay1, LOW) : digitalWrite(gpio_relay1, HIGH);
param->updateAndReport(val);
}

} else if(strcmp(device_name, "Switch_ch2") == 0) {

Serial.printf("Switch value = %s\n", val.val.b? "true" : "false");

if(strcmp(param_name, "Power") == 0) {
Expand All @@ -80,9 +80,9 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p
(switch_state_ch2 == false) ? digitalWrite(gpio_relay2, LOW) : digitalWrite(gpio_relay2, HIGH);
param->updateAndReport(val);
}

}

}

void ARDUINO_ISR_ATTR isr(void* arg) {
Expand All @@ -92,8 +92,9 @@ void ARDUINO_ISR_ATTR isr(void* arg) {

void setup()
{

uint32_t chipId = 0;

Serial.begin(115200);

// Configure the input GPIOs
Expand All @@ -102,7 +103,7 @@ void setup()
attachInterruptArg(switch_ch1.pin, isr, &switch_ch1, CHANGE);
pinMode(switch_ch2.pin, INPUT_PULLUP);
attachInterruptArg(switch_ch2.pin, isr, &switch_ch2, CHANGE);

// Set the Relays GPIOs as output mode
pinMode(gpio_relay1, OUTPUT);
pinMode(gpio_relay2, OUTPUT);
Expand All @@ -112,20 +113,24 @@ void setup()
digitalWrite(gpio_relay2, DEFAULT_POWER_MODE);
digitalWrite(gpio_led, false);

Node my_node;
Node my_node;
my_node = RMaker.initNode("Sonoff Dual R3");

//Initialize switch device
my_switch1 = Switch("Switch_ch1", &gpio_relay1);
my_switch2 = Switch("Switch_ch2", &gpio_relay2);

//Standard switch device
my_switch1.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_switch2);

//This is optional
//This is optional
RMaker.enableOTA(OTA_USING_PARAMS);
//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 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
Expand Down
19 changes: 11 additions & 8 deletions libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ static int gpio_switch = 16;
bool switch_state = true;

//The framework provides some standard device types like switch, lightbulb, fan, temperaturesensor.
static Switch my_switch("Switch", &gpio_switch);
static Switch my_switch;

void sysProvEvent(arduino_event_t *sys_event)
{
switch (sys_event->event_id) {
switch (sys_event->event_id) {
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");
#else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
#endif
#endif
break;
default:;
}
Expand All @@ -59,18 +59,21 @@ void setup()
pinMode(gpio_switch, OUTPUT);
digitalWrite(gpio_switch, DEFAULT_POWER_MODE);

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

//Initialize switch device
my_switch = Switch("Switch", &gpio_switch);

//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);

//This is optional
//This is optional
RMaker.enableOTA(OTA_USING_PARAMS);
//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 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
Expand Down