From d5d20f456df2c82ce5293f5e361b402b6a265a6d Mon Sep 17 00:00:00 2001 From: Fernando Garcia Date: Thu, 2 Mar 2023 13:55:25 -0300 Subject: [PATCH 1/7] Add switch example using array --- .../examples/RMakerSwitchArray/README.md | 36 +++ .../RMakerSwitchArray/RMakerSwitchArray.ino | 224 ++++++++++++++++++ 2 files changed, 260 insertions(+) create mode 100644 libraries/RainMaker/examples/RMakerSwitchArray/README.md create mode 100644 libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino diff --git a/libraries/RainMaker/examples/RMakerSwitchArray/README.md b/libraries/RainMaker/examples/RMakerSwitchArray/README.md new file mode 100644 index 00000000000..8ac23ba8f81 --- /dev/null +++ b/libraries/RainMaker/examples/RMakerSwitchArray/README.md @@ -0,0 +1,36 @@ +# ESP RainMaker 8 Switches using Array + +This example demonstrates how to build a device with 8 light switches to be used with ESP RainMaker. + + +## What to expect in this example? + +- This virtual switches will work in parallel with physical two state light switch. +- The physical switch will change the state of relay while is pressed or when released and the same will reflect on the phone app. +- After compiling and flashing the example, add your device using the [ESP RainMaker phone apps](https://rainmaker.espressif.com/docs/quick-links.html#phone-apps) by scanning the QR code. + +### Output + +``` +[ 80][I][RMaker.cpp:17] event_handler(): RainMaker Initialised. +[ 96][I][WiFiProv.cpp:149] beginProvision(): Already Provisioned +[ 96][I][WiFiProv.cpp:153] beginProvision(): Attempting connect to AP: XXXXXX + +[ 199][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay1, Param Name : Power, Val : false +[ 199][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay2, Param Name : Power, Val : false +[ 207][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay3, Param Name : Power, Val : false +[ 223][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay4, Param Name : Power, Val : false +[ 232][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay5, Param Name : Power, Val : false +[ 241][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay6, Param Name : Power, Val : false +[ 251][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay7, Param Name : Power, Val : false +[ 260][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay8, Param Name : Power, Val : false +Light switch 1 released, relayState: 1 +[359585][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay1, Param Name : Power, Val : true +Light switch 1 pressed, relayState: 0 +[363695][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay1, Param Name : Power, Val : false +[390384][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay1, Param Name : Power, Val : true +[392021][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay1, Param Name : Power, Val : false +``` + +### Factory reset +- Press and Hold the Boot button for more than 10 seconds and then release to reset to factory defaults. diff --git a/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino b/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino new file mode 100644 index 00000000000..b639dda21d5 --- /dev/null +++ b/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino @@ -0,0 +1,224 @@ +// This example demonstrates the ESP RainMaker with a standard Switch device. +#include "RMaker.h" +#include "WiFi.h" +#include "WiFiProv.h" +#include +#include + +#define NUM_RELAYS 8 + +#if CONFIG_IDF_TARGET_ESP32C3 + const byte resetPin = 9; +#else + const byte resetPin = 0; +#endif + +const char nodeName[] = "ESP32_Relay_16"; +const char *service_name = "PROV_1234"; +const char *pop = "abcd1234"; +bool firstPress = true; + +Preferences pref; + +using namespace ace_button; + +AceButton lightSwitch[NUM_RELAYS]; + +static Switch myRelay[NUM_RELAYS]; + +struct INFOS +{ + const char *deviceName; + uint8_t relayPin; + bool relayState; + uint8_t switchPin; +}; + +#define INITIAL_STATE LOW // Initial state = HIGH to active LOW relay model. + +INFOS info[NUM_RELAYS] = { + { "Relay1", 4, INITIAL_STATE, 26}, + { "Relay2", 5, INITIAL_STATE, 27}, + { "Relay3", 18, INITIAL_STATE, 32}, + { "Relay4", 19, INITIAL_STATE, 33}, + { "Relay5", 21, INITIAL_STATE, 13}, + { "Relay6", 22, INITIAL_STATE, 12}, + { "Relay7", 23, INITIAL_STATE, 14}, + { "Relay8", 25, INITIAL_STATE, 15} +}; + +void sysProvEvent(arduino_event_t *sys_event) +{ + 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 + 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; + } + } +} + +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 *param_name = param->getParamName(); + + if (strcmp(param_name, ESP_RMAKER_DEF_POWER_NAME) == 0) + { + for (byte i = 0; i < NUM_RELAYS; i++) + { + if (strcmp(device_name, info[i].deviceName) == 0) + { + info[i].relayState = val.val.b; + digitalWrite(info[i].relayPin, info[i].relayState); + myRelay[i].updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, info[i].relayState); + pref.putBool(info[i].deviceName, info[i].relayState); + break; + } + } + } +} + +void getLastState() +{ + for (byte i = 0; i < NUM_RELAYS; i++) + { + info[i].relayState = pref.getBool(info[i].deviceName, info[i].relayState); + digitalWrite(info[i].relayPin, info[i].relayState); + myRelay[i].updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, info[i].relayState); + } +} + +void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) +{ + uint8_t numSwitch = button->getId(); + + switch (eventType) + { + case AceButton::kEventPressed: + { + info[numSwitch].relayState = !info[numSwitch].relayState; + Serial.print(F("Light switch ")); + Serial.print(numSwitch + 1); + Serial.print(F(" pressed, relayState: ")); + Serial.println(info[numSwitch].relayState); + digitalWrite(info[numSwitch].relayPin, info[numSwitch].relayState); + myRelay[numSwitch].updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, info[numSwitch].relayState); + pref.putBool(info[numSwitch].deviceName, info[numSwitch].relayState); + break; + } + case AceButton::kEventReleased: + { + info[numSwitch].relayState = !info[numSwitch].relayState; + Serial.print(F("Light switch ")); + Serial.print(numSwitch + 1); + Serial.print(F(" released, relayState: ")); + Serial.println(info[numSwitch].relayState); + digitalWrite(info[numSwitch].relayPin, info[numSwitch].relayState); + myRelay[numSwitch].updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, info[numSwitch].relayState); + pref.putBool(info[numSwitch].deviceName, info[numSwitch].relayState); + break; + } + } +} + +void setup() +{ + Serial.begin(115200); + + pinMode(resetPin, INPUT); + + pref.begin(nodeName, false); + + // Configure the ButtonConfig with the event handler, and enable all higher level events. + ButtonConfig* buttonConfig = ButtonConfig::getSystemButtonConfig(); + + buttonConfig->setEventHandler(handleEvent); + buttonConfig->setFeature(ButtonConfig::kFeatureClick); + buttonConfig->setFeature(ButtonConfig::kFeatureLongPress); + + Node my_node; + my_node = RMaker.initNode(nodeName); + + for (byte i = 0; i < NUM_RELAYS; i++) + { + pinMode(info[i].relayPin, OUTPUT); + digitalWrite(info[i].relayPin, info[i].relayState); + + myRelay[i] = Switch(info[i].deviceName); + myRelay[i].addCb(write_callback); + my_node.addDevice(myRelay[i]); + + pinMode(info[i].switchPin, INPUT_PULLUP); + lightSwitch[i].init(info[i].switchPin, HIGH, i); + } + + // 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(); + + RMaker.start(); + + WiFi.onEvent(sysProvEvent); + #if CONFIG_IDF_TARGET_ESP32S2 + WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name); + #else + WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name); + #endif + + getLastState(); // Get the last state of Relays +} + +void loop() +{ + for (int i = 0; i < NUM_RELAYS; i++) + { + lightSwitch[i].check(); + } + + if(digitalRead(resetPin) == LOW) // Push button pressed + { + delay(100); // Key debounce handling + int startTime = millis(); + while(digitalRead(resetPin) == LOW) + { + delay(50); + } + int endTime = millis(); + + if ((endTime - startTime) > 10000) // If key pressed for more than 10secs, reset all + { + Serial.printf("Factory reset!"); + RMakerFactoryReset(2); + } + } +} From 37aa9938348163653c3f95c88e641fb1dab3a754 Mon Sep 17 00:00:00 2001 From: Fernando Garcia Date: Thu, 2 Mar 2023 19:49:53 -0300 Subject: [PATCH 2/7] Add more comments --- .../RMakerSwitchArray/RMakerSwitchArray.ino | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino b/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino index b639dda21d5..658b784758c 100644 --- a/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino +++ b/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino @@ -1,4 +1,4 @@ -// This example demonstrates the ESP RainMaker with a standard Switch device. +// This example demonstrates the ESP RainMaker with a standard light switch device. #include "RMaker.h" #include "WiFi.h" #include "WiFiProv.h" @@ -22,8 +22,10 @@ Preferences pref; using namespace ace_button; +// Define the buttons in an array using the default constructor. AceButton lightSwitch[NUM_RELAYS]; +// The framework provides some standard device types like switch, lightbulb, fan, temperaturesensor. static Switch myRelay[NUM_RELAYS]; struct INFOS @@ -36,6 +38,7 @@ struct INFOS #define INITIAL_STATE LOW // Initial state = HIGH to active LOW relay model. +// Define the device name, relay pin, relay default status and switch pin to an array. INFOS info[NUM_RELAYS] = { { "Relay1", 4, INITIAL_STATE, 26}, { "Relay2", 5, INITIAL_STATE, 27}, @@ -92,7 +95,11 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p { info[i].relayState = val.val.b; digitalWrite(info[i].relayPin, info[i].relayState); + + // Publish the new state to all listeners myRelay[i].updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, info[i].relayState); + + // Save the relay state. pref.putBool(info[i].deviceName, info[i].relayState); break; } @@ -149,9 +156,10 @@ void setup() pinMode(resetPin, INPUT); + // Starts the configuration file where the state will be saved. pref.begin(nodeName, false); - // Configure the ButtonConfig with the event handler, and enable all higher level events. + // Configure the ButtonConfig with the event handler, enable click and long press events. ButtonConfig* buttonConfig = ButtonConfig::getSystemButtonConfig(); buttonConfig->setEventHandler(handleEvent); @@ -164,13 +172,23 @@ void setup() for (byte i = 0; i < NUM_RELAYS; i++) { pinMode(info[i].relayPin, OUTPUT); + + // Turn off all relays. digitalWrite(info[i].relayPin, info[i].relayState); - myRelay[i] = Switch(info[i].deviceName); + // Initialize switch device + myRelay[i] = Switch(info[i].deviceName); + + // Specify the callback function to relays. myRelay[i].addCb(write_callback); + + //Add switch device to the node. my_node.addDevice(myRelay[i]); + // Switch uses the built-in pull up resistor. pinMode(info[i].switchPin, INPUT_PULLUP); + + // initialize the corresponding AceButton. lightSwitch[i].init(info[i].switchPin, HIGH, i); } @@ -195,7 +213,8 @@ void setup() WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name); #endif - getLastState(); // Get the last state of Relays + // Get the last state of Relays. + getLastState(); } void loop() @@ -205,9 +224,11 @@ void loop() lightSwitch[i].check(); } - if(digitalRead(resetPin) == LOW) // Push button pressed + // Push button pressed. + if(digitalRead(resetPin) == LOW) { - delay(100); // Key debounce handling + // Key debounce handling. + delay(100); int startTime = millis(); while(digitalRead(resetPin) == LOW) { @@ -215,7 +236,8 @@ void loop() } int endTime = millis(); - if ((endTime - startTime) > 10000) // If key pressed for more than 10secs, reset all + // If key pressed for more than 10secs, reset all. + if ((endTime - startTime) > 10000) { Serial.printf("Factory reset!"); RMakerFactoryReset(2); From 6c648d1af1453ade800df12883b27f1d07b13ef3 Mon Sep 17 00:00:00 2001 From: Fernando Garcia Date: Sat, 4 Mar 2023 09:44:35 -0300 Subject: [PATCH 3/7] Add libraries dependencies to readme --- libraries/RainMaker/examples/RMakerSwitchArray/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libraries/RainMaker/examples/RMakerSwitchArray/README.md b/libraries/RainMaker/examples/RMakerSwitchArray/README.md index 8ac23ba8f81..2c486eaaf24 100644 --- a/libraries/RainMaker/examples/RMakerSwitchArray/README.md +++ b/libraries/RainMaker/examples/RMakerSwitchArray/README.md @@ -9,6 +9,9 @@ This example demonstrates how to build a device with 8 light switches to be used - The physical switch will change the state of relay while is pressed or when released and the same will reflect on the phone app. - After compiling and flashing the example, add your device using the [ESP RainMaker phone apps](https://rainmaker.espressif.com/docs/quick-links.html#phone-apps) by scanning the QR code. +### Libraries dependencies + - [AceButton](https://github.com/bxparks/AceButton) + ### Output ``` From 8900a3898a80ddfb9498b7bd01deb770da4fd59b Mon Sep 17 00:00:00 2001 From: Fernando Garcia Date: Sun, 5 Mar 2023 10:12:42 -0300 Subject: [PATCH 4/7] Improve sysProvEvent function --- .../RMakerSwitchArray/RMakerSwitchArray.ino | 53 +++++++++++++++---- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino b/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino index 658b784758c..902785014bb 100644 --- a/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino +++ b/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino @@ -54,6 +54,17 @@ void sysProvEvent(arduino_event_t *sys_event) { switch (sys_event->event_id) { + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + { + Serial.print("\nConnected IP address : "); + Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); + break; + } + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + { + Serial.println("\nDisconnected. Connecting to the AP again... "); + break; + } case ARDUINO_EVENT_PROV_START: { #if CONFIG_IDF_TARGET_ESP32S2 @@ -65,14 +76,36 @@ void sysProvEvent(arduino_event_t *sys_event) #endif break; } - case ARDUINO_EVENT_PROV_INIT: + case ARDUINO_EVENT_PROV_CRED_RECV: { - wifi_prov_mgr_disable_auto_stop(10000); + Serial.println("\nReceived Wi-Fi credentials"); + Serial.print("\tSSID : "); + Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); + Serial.print("\tPassword : "); + Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); + break; + } + case ARDUINO_EVENT_PROV_CRED_FAIL: + { + Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); + if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) + { + Serial.println("\nWi-Fi AP password incorrect"); + } + else + { + Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); + } break; } case ARDUINO_EVENT_PROV_CRED_SUCCESS: { - wifi_prov_mgr_stop_provisioning(); + Serial.println("\nProvisioning Successful"); + break; + } + case ARDUINO_EVENT_PROV_END: + { + Serial.println("\nProvisioning Ends"); break; } default: @@ -176,13 +209,13 @@ void setup() // Turn off all relays. digitalWrite(info[i].relayPin, info[i].relayState); - // Initialize switch device - myRelay[i] = Switch(info[i].deviceName); + // Initialize switch device + myRelay[i] = Switch(info[i].deviceName); // Specify the callback function to relays. myRelay[i].addCb(write_callback); - //Add switch device to the node. + // Add switch device to the node. my_node.addDevice(myRelay[i]); // Switch uses the built-in pull up resistor. @@ -214,7 +247,7 @@ void setup() #endif // Get the last state of Relays. - getLastState(); + getLastState(); } void loop() @@ -225,10 +258,10 @@ void loop() } // Push button pressed. - if(digitalRead(resetPin) == LOW) + if(digitalRead(resetPin) == LOW) { // Key debounce handling. - delay(100); + delay(100); int startTime = millis(); while(digitalRead(resetPin) == LOW) { @@ -237,7 +270,7 @@ void loop() int endTime = millis(); // If key pressed for more than 10secs, reset all. - if ((endTime - startTime) > 10000) + if ((endTime - startTime) > 10000) { Serial.printf("Factory reset!"); RMakerFactoryReset(2); From 565d6d17be3b6cad25d818234dee7704ccc403bb Mon Sep 17 00:00:00 2001 From: Fernando Garcia Date: Sun, 5 Mar 2023 17:33:05 -0300 Subject: [PATCH 5/7] Apply changes requested in #7932 Add ARDUINO_EVENT_PROV_INIT and ARDUINO_EVENT_PROV_CRED_SUCCESS back --- .../examples/RMakerSwitchArray/RMakerSwitchArray.ino | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino b/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino index 902785014bb..f8fa9aebd70 100644 --- a/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino +++ b/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino @@ -76,6 +76,16 @@ void sysProvEvent(arduino_event_t *sys_event) #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; + } case ARDUINO_EVENT_PROV_CRED_RECV: { Serial.println("\nReceived Wi-Fi credentials"); From cdc3b81bccbda1cd2597ba25eef1f68b44a9aaf8 Mon Sep 17 00:00:00 2001 From: Fernando Garcia Date: Sun, 5 Mar 2023 10:27:28 -0300 Subject: [PATCH 6/7] Improve sysProvEvent function --- .../examples/RMakerCustom/RMakerCustom.ino | 69 ++++++++++++++---- .../RMakerCustomAirCooler.ino | 69 ++++++++++++++---- .../RMakerSonoffDualR3/RMakerSonoffDualR3.ino | 71 ++++++++++++++----- .../examples/RMakerSwitch/RMakerSwitch.ino | 71 +++++++++++++++---- 4 files changed, 223 insertions(+), 57 deletions(-) diff --git a/libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino b/libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino index 04492039519..dc14997d322 100644 --- a/libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino +++ b/libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino @@ -26,24 +26,67 @@ static Device *my_device = NULL; void sysProvEvent(arduino_event_t *sys_event) { - switch (sys_event->event_id) { + switch (sys_event->event_id) + { + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + { + Serial.print("\nConnected IP address : "); + Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); + break; + } + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + { + Serial.println("\nDisconnected. Connecting to the AP again... "); + break; + } 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 + { + #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 break; - case ARDUINO_EVENT_PROV_INIT: - wifi_prov_mgr_disable_auto_stop(10000); + } + case ARDUINO_EVENT_PROV_CRED_RECV: + { + Serial.println("\nReceived Wi-Fi credentials"); + Serial.print("\tSSID : "); + Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); + Serial.print("\tPassword : "); + Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); break; + } + case ARDUINO_EVENT_PROV_CRED_FAIL: + { + Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); + if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) + { + Serial.println("\nWi-Fi AP password incorrect"); + } + else + { + Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); + } + break; + } case ARDUINO_EVENT_PROV_CRED_SUCCESS: - wifi_prov_mgr_stop_provisioning(); + { + Serial.println("\nProvisioning Successful"); break; - default:; - } + } + case ARDUINO_EVENT_PROV_END: + { + Serial.println("\nProvisioning Ends"); + break; + } + default: + { + break; + } + } } void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx) diff --git a/libraries/RainMaker/examples/RMakerCustomAirCooler/RMakerCustomAirCooler.ino b/libraries/RainMaker/examples/RMakerCustomAirCooler/RMakerCustomAirCooler.ino index 04c070cf21b..c824ba4eaaf 100644 --- a/libraries/RainMaker/examples/RMakerCustomAirCooler/RMakerCustomAirCooler.ino +++ b/libraries/RainMaker/examples/RMakerCustomAirCooler/RMakerCustomAirCooler.ino @@ -43,24 +43,67 @@ static Device *my_device = NULL; void sysProvEvent(arduino_event_t *sys_event) { - switch (sys_event->event_id) { + switch (sys_event->event_id) + { + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + { + Serial.print("\nConnected IP address : "); + Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); + break; + } + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + { + Serial.println("\nDisconnected. Connecting to the AP again... "); + break; + } 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 + { + #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 break; - case ARDUINO_EVENT_PROV_INIT: - wifi_prov_mgr_disable_auto_stop(10000); + } + case ARDUINO_EVENT_PROV_CRED_RECV: + { + Serial.println("\nReceived Wi-Fi credentials"); + Serial.print("\tSSID : "); + Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); + Serial.print("\tPassword : "); + Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); break; + } + case ARDUINO_EVENT_PROV_CRED_FAIL: + { + Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); + if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) + { + Serial.println("\nWi-Fi AP password incorrect"); + } + else + { + Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); + } + break; + } case ARDUINO_EVENT_PROV_CRED_SUCCESS: - wifi_prov_mgr_stop_provisioning(); + { + Serial.println("\nProvisioning Successful"); break; - default:; - } + } + case ARDUINO_EVENT_PROV_END: + { + Serial.println("\nProvisioning Ends"); + break; + } + default: + { + break; + } + } } void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx) diff --git a/libraries/RainMaker/examples/RMakerSonoffDualR3/RMakerSonoffDualR3.ino b/libraries/RainMaker/examples/RMakerSonoffDualR3/RMakerSonoffDualR3.ino index 4cc78f2a0b3..4f4d629e254 100644 --- a/libraries/RainMaker/examples/RMakerSonoffDualR3/RMakerSonoffDualR3.ino +++ b/libraries/RainMaker/examples/RMakerSonoffDualR3/RMakerSonoffDualR3.ino @@ -36,28 +36,67 @@ static Switch *my_switch2 = NULL; void sysProvEvent(arduino_event_t *sys_event) { - switch (sys_event->event_id) { + switch (sys_event->event_id) + { + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + { + Serial.print("\nConnected IP address : "); + Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); + break; + } + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + { + Serial.println("\nDisconnected. Connecting to the AP again... "); + break; + } 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 + { + #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 break; - case ARDUINO_EVENT_WIFI_STA_CONNECTED: - Serial.printf("\nConnected to Wi-Fi!\n"); - digitalWrite(gpio_led, true); + } + case ARDUINO_EVENT_PROV_CRED_RECV: + { + Serial.println("\nReceived Wi-Fi credentials"); + Serial.print("\tSSID : "); + Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); + Serial.print("\tPassword : "); + Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); break; - case ARDUINO_EVENT_PROV_INIT: - wifi_prov_mgr_disable_auto_stop(10000); + } + case ARDUINO_EVENT_PROV_CRED_FAIL: + { + Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); + if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) + { + Serial.println("\nWi-Fi AP password incorrect"); + } + else + { + Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); + } break; + } case ARDUINO_EVENT_PROV_CRED_SUCCESS: - wifi_prov_mgr_stop_provisioning(); + { + Serial.println("\nProvisioning Successful"); break; - default:; - } + } + case ARDUINO_EVENT_PROV_END: + { + Serial.println("\nProvisioning Ends"); + break; + } + default: + { + break; + } + } } diff --git a/libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino b/libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino index 32a1a28601f..844890ed1a7 100644 --- a/libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino +++ b/libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino @@ -26,26 +26,67 @@ static Switch *my_switch = NULL; void sysProvEvent(arduino_event_t *sys_event) { - switch (sys_event->event_id) { + switch (sys_event->event_id) + { + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + { + Serial.print("\nConnected IP address : "); + Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); + break; + } + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + { + Serial.println("\nDisconnected. Connecting to the AP again... "); + break; + } 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 + { + #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 break; - case ARDUINO_EVENT_PROV_INIT: - wifi_prov_mgr_disable_auto_stop(10000); + } + case ARDUINO_EVENT_PROV_CRED_RECV: + { + Serial.println("\nReceived Wi-Fi credentials"); + Serial.print("\tSSID : "); + Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); + Serial.print("\tPassword : "); + Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); break; + } + case ARDUINO_EVENT_PROV_CRED_FAIL: + { + Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); + if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) + { + Serial.println("\nWi-Fi AP password incorrect"); + } + else + { + Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); + } + break; + } case ARDUINO_EVENT_PROV_CRED_SUCCESS: - wifi_prov_mgr_stop_provisioning(); + { + Serial.println("\nProvisioning Successful"); break; - default:; - } + } + case ARDUINO_EVENT_PROV_END: + { + Serial.println("\nProvisioning Ends"); + break; + } + default: + { + break; + } + } } void write_callback(Device *device, Param *param, const param_val_t val, From 3767e2991b32e109ffca7f3d921e20d8964bde92 Mon Sep 17 00:00:00 2001 From: Fernando Garcia Date: Sun, 5 Mar 2023 17:18:57 -0300 Subject: [PATCH 7/7] Changes requested --- .../examples/RMakerCustom/RMakerCustom.ino | 39 +-- .../RMakerCustomAirCooler.ino | 39 +-- .../RMakerSonoffDualR3/RMakerSonoffDualR3.ino | 39 +-- .../examples/RMakerSwitch/RMakerSwitch.ino | 39 +-- .../examples/RMakerSwitchArray/README.md | 39 --- .../RMakerSwitchArray/RMakerSwitchArray.ino | 289 ------------------ 6 files changed, 56 insertions(+), 428 deletions(-) delete mode 100644 libraries/RainMaker/examples/RMakerSwitchArray/README.md delete mode 100644 libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino diff --git a/libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino b/libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino index dc14997d322..0c58dd2cb41 100644 --- a/libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino +++ b/libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino @@ -26,41 +26,37 @@ static Device *my_device = NULL; void sysProvEvent(arduino_event_t *sys_event) { - switch (sys_event->event_id) - { + switch (sys_event->event_id){ case ARDUINO_EVENT_WIFI_STA_GOT_IP: - { Serial.print("\nConnected IP address : "); Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); break; - } case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: - { Serial.println("\nDisconnected. Connecting to the AP again... "); break; - } 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 +#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 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; case ARDUINO_EVENT_PROV_CRED_RECV: - { Serial.println("\nReceived Wi-Fi credentials"); Serial.print("\tSSID : "); Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); Serial.print("\tPassword : "); Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); break; - } case ARDUINO_EVENT_PROV_CRED_FAIL: - { Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) { @@ -71,21 +67,14 @@ void sysProvEvent(arduino_event_t *sys_event) Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); } break; - } case ARDUINO_EVENT_PROV_CRED_SUCCESS: - { Serial.println("\nProvisioning Successful"); break; - } case ARDUINO_EVENT_PROV_END: - { Serial.println("\nProvisioning Ends"); break; - } default: - { break; - } } } diff --git a/libraries/RainMaker/examples/RMakerCustomAirCooler/RMakerCustomAirCooler.ino b/libraries/RainMaker/examples/RMakerCustomAirCooler/RMakerCustomAirCooler.ino index c824ba4eaaf..4c26d2ad00d 100644 --- a/libraries/RainMaker/examples/RMakerCustomAirCooler/RMakerCustomAirCooler.ino +++ b/libraries/RainMaker/examples/RMakerCustomAirCooler/RMakerCustomAirCooler.ino @@ -43,41 +43,37 @@ static Device *my_device = NULL; void sysProvEvent(arduino_event_t *sys_event) { - switch (sys_event->event_id) - { + switch (sys_event->event_id){ case ARDUINO_EVENT_WIFI_STA_GOT_IP: - { Serial.print("\nConnected IP address : "); Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); break; - } case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: - { Serial.println("\nDisconnected. Connecting to the AP again... "); break; - } 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 +#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 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; case ARDUINO_EVENT_PROV_CRED_RECV: - { Serial.println("\nReceived Wi-Fi credentials"); Serial.print("\tSSID : "); Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); Serial.print("\tPassword : "); Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); break; - } case ARDUINO_EVENT_PROV_CRED_FAIL: - { Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) { @@ -88,21 +84,14 @@ void sysProvEvent(arduino_event_t *sys_event) Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); } break; - } case ARDUINO_EVENT_PROV_CRED_SUCCESS: - { Serial.println("\nProvisioning Successful"); break; - } case ARDUINO_EVENT_PROV_END: - { Serial.println("\nProvisioning Ends"); break; - } default: - { break; - } } } diff --git a/libraries/RainMaker/examples/RMakerSonoffDualR3/RMakerSonoffDualR3.ino b/libraries/RainMaker/examples/RMakerSonoffDualR3/RMakerSonoffDualR3.ino index 4f4d629e254..00e060857a2 100644 --- a/libraries/RainMaker/examples/RMakerSonoffDualR3/RMakerSonoffDualR3.ino +++ b/libraries/RainMaker/examples/RMakerSonoffDualR3/RMakerSonoffDualR3.ino @@ -36,41 +36,37 @@ static Switch *my_switch2 = NULL; void sysProvEvent(arduino_event_t *sys_event) { - switch (sys_event->event_id) - { + switch (sys_event->event_id){ case ARDUINO_EVENT_WIFI_STA_GOT_IP: - { Serial.print("\nConnected IP address : "); Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); break; - } case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: - { Serial.println("\nDisconnected. Connecting to the AP again... "); break; - } 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 +#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 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; case ARDUINO_EVENT_PROV_CRED_RECV: - { Serial.println("\nReceived Wi-Fi credentials"); Serial.print("\tSSID : "); Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); Serial.print("\tPassword : "); Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); break; - } case ARDUINO_EVENT_PROV_CRED_FAIL: - { Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) { @@ -81,21 +77,14 @@ void sysProvEvent(arduino_event_t *sys_event) Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); } break; - } case ARDUINO_EVENT_PROV_CRED_SUCCESS: - { Serial.println("\nProvisioning Successful"); break; - } case ARDUINO_EVENT_PROV_END: - { Serial.println("\nProvisioning Ends"); break; - } default: - { break; - } } } diff --git a/libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino b/libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino index 844890ed1a7..e3537695b62 100644 --- a/libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino +++ b/libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino @@ -26,41 +26,37 @@ static Switch *my_switch = NULL; void sysProvEvent(arduino_event_t *sys_event) { - switch (sys_event->event_id) - { + switch (sys_event->event_id){ case ARDUINO_EVENT_WIFI_STA_GOT_IP: - { Serial.print("\nConnected IP address : "); Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); break; - } case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: - { Serial.println("\nDisconnected. Connecting to the AP again... "); break; - } 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 +#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 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; case ARDUINO_EVENT_PROV_CRED_RECV: - { Serial.println("\nReceived Wi-Fi credentials"); Serial.print("\tSSID : "); Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); Serial.print("\tPassword : "); Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); break; - } case ARDUINO_EVENT_PROV_CRED_FAIL: - { Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) { @@ -71,21 +67,14 @@ void sysProvEvent(arduino_event_t *sys_event) Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); } break; - } case ARDUINO_EVENT_PROV_CRED_SUCCESS: - { Serial.println("\nProvisioning Successful"); break; - } case ARDUINO_EVENT_PROV_END: - { Serial.println("\nProvisioning Ends"); break; - } default: - { break; - } } } diff --git a/libraries/RainMaker/examples/RMakerSwitchArray/README.md b/libraries/RainMaker/examples/RMakerSwitchArray/README.md deleted file mode 100644 index 2c486eaaf24..00000000000 --- a/libraries/RainMaker/examples/RMakerSwitchArray/README.md +++ /dev/null @@ -1,39 +0,0 @@ -# ESP RainMaker 8 Switches using Array - -This example demonstrates how to build a device with 8 light switches to be used with ESP RainMaker. - - -## What to expect in this example? - -- This virtual switches will work in parallel with physical two state light switch. -- The physical switch will change the state of relay while is pressed or when released and the same will reflect on the phone app. -- After compiling and flashing the example, add your device using the [ESP RainMaker phone apps](https://rainmaker.espressif.com/docs/quick-links.html#phone-apps) by scanning the QR code. - -### Libraries dependencies - - [AceButton](https://github.com/bxparks/AceButton) - -### Output - -``` -[ 80][I][RMaker.cpp:17] event_handler(): RainMaker Initialised. -[ 96][I][WiFiProv.cpp:149] beginProvision(): Already Provisioned -[ 96][I][WiFiProv.cpp:153] beginProvision(): Attempting connect to AP: XXXXXX - -[ 199][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay1, Param Name : Power, Val : false -[ 199][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay2, Param Name : Power, Val : false -[ 207][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay3, Param Name : Power, Val : false -[ 223][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay4, Param Name : Power, Val : false -[ 232][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay5, Param Name : Power, Val : false -[ 241][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay6, Param Name : Power, Val : false -[ 251][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay7, Param Name : Power, Val : false -[ 260][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay8, Param Name : Power, Val : false -Light switch 1 released, relayState: 1 -[359585][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay1, Param Name : Power, Val : true -Light switch 1 pressed, relayState: 0 -[363695][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay1, Param Name : Power, Val : false -[390384][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay1, Param Name : Power, Val : true -[392021][I][RMakerDevice.cpp:163] updateAndReportParam(): Device : Relay1, Param Name : Power, Val : false -``` - -### Factory reset -- Press and Hold the Boot button for more than 10 seconds and then release to reset to factory defaults. diff --git a/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino b/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino deleted file mode 100644 index f8fa9aebd70..00000000000 --- a/libraries/RainMaker/examples/RMakerSwitchArray/RMakerSwitchArray.ino +++ /dev/null @@ -1,289 +0,0 @@ -// This example demonstrates the ESP RainMaker with a standard light switch device. -#include "RMaker.h" -#include "WiFi.h" -#include "WiFiProv.h" -#include -#include - -#define NUM_RELAYS 8 - -#if CONFIG_IDF_TARGET_ESP32C3 - const byte resetPin = 9; -#else - const byte resetPin = 0; -#endif - -const char nodeName[] = "ESP32_Relay_16"; -const char *service_name = "PROV_1234"; -const char *pop = "abcd1234"; -bool firstPress = true; - -Preferences pref; - -using namespace ace_button; - -// Define the buttons in an array using the default constructor. -AceButton lightSwitch[NUM_RELAYS]; - -// The framework provides some standard device types like switch, lightbulb, fan, temperaturesensor. -static Switch myRelay[NUM_RELAYS]; - -struct INFOS -{ - const char *deviceName; - uint8_t relayPin; - bool relayState; - uint8_t switchPin; -}; - -#define INITIAL_STATE LOW // Initial state = HIGH to active LOW relay model. - -// Define the device name, relay pin, relay default status and switch pin to an array. -INFOS info[NUM_RELAYS] = { - { "Relay1", 4, INITIAL_STATE, 26}, - { "Relay2", 5, INITIAL_STATE, 27}, - { "Relay3", 18, INITIAL_STATE, 32}, - { "Relay4", 19, INITIAL_STATE, 33}, - { "Relay5", 21, INITIAL_STATE, 13}, - { "Relay6", 22, INITIAL_STATE, 12}, - { "Relay7", 23, INITIAL_STATE, 14}, - { "Relay8", 25, INITIAL_STATE, 15} -}; - -void sysProvEvent(arduino_event_t *sys_event) -{ - switch (sys_event->event_id) - { - case ARDUINO_EVENT_WIFI_STA_GOT_IP: - { - Serial.print("\nConnected IP address : "); - Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); - break; - } - case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: - { - Serial.println("\nDisconnected. Connecting to the AP again... "); - break; - } - 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 - 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; - } - case ARDUINO_EVENT_PROV_CRED_RECV: - { - Serial.println("\nReceived Wi-Fi credentials"); - Serial.print("\tSSID : "); - Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); - Serial.print("\tPassword : "); - Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); - break; - } - case ARDUINO_EVENT_PROV_CRED_FAIL: - { - Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n"); - if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR) - { - Serial.println("\nWi-Fi AP password incorrect"); - } - else - { - Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()"); - } - break; - } - case ARDUINO_EVENT_PROV_CRED_SUCCESS: - { - Serial.println("\nProvisioning Successful"); - break; - } - case ARDUINO_EVENT_PROV_END: - { - Serial.println("\nProvisioning Ends"); - break; - } - default: - { - break; - } - } -} - -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 *param_name = param->getParamName(); - - if (strcmp(param_name, ESP_RMAKER_DEF_POWER_NAME) == 0) - { - for (byte i = 0; i < NUM_RELAYS; i++) - { - if (strcmp(device_name, info[i].deviceName) == 0) - { - info[i].relayState = val.val.b; - digitalWrite(info[i].relayPin, info[i].relayState); - - // Publish the new state to all listeners - myRelay[i].updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, info[i].relayState); - - // Save the relay state. - pref.putBool(info[i].deviceName, info[i].relayState); - break; - } - } - } -} - -void getLastState() -{ - for (byte i = 0; i < NUM_RELAYS; i++) - { - info[i].relayState = pref.getBool(info[i].deviceName, info[i].relayState); - digitalWrite(info[i].relayPin, info[i].relayState); - myRelay[i].updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, info[i].relayState); - } -} - -void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) -{ - uint8_t numSwitch = button->getId(); - - switch (eventType) - { - case AceButton::kEventPressed: - { - info[numSwitch].relayState = !info[numSwitch].relayState; - Serial.print(F("Light switch ")); - Serial.print(numSwitch + 1); - Serial.print(F(" pressed, relayState: ")); - Serial.println(info[numSwitch].relayState); - digitalWrite(info[numSwitch].relayPin, info[numSwitch].relayState); - myRelay[numSwitch].updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, info[numSwitch].relayState); - pref.putBool(info[numSwitch].deviceName, info[numSwitch].relayState); - break; - } - case AceButton::kEventReleased: - { - info[numSwitch].relayState = !info[numSwitch].relayState; - Serial.print(F("Light switch ")); - Serial.print(numSwitch + 1); - Serial.print(F(" released, relayState: ")); - Serial.println(info[numSwitch].relayState); - digitalWrite(info[numSwitch].relayPin, info[numSwitch].relayState); - myRelay[numSwitch].updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, info[numSwitch].relayState); - pref.putBool(info[numSwitch].deviceName, info[numSwitch].relayState); - break; - } - } -} - -void setup() -{ - Serial.begin(115200); - - pinMode(resetPin, INPUT); - - // Starts the configuration file where the state will be saved. - pref.begin(nodeName, false); - - // Configure the ButtonConfig with the event handler, enable click and long press events. - ButtonConfig* buttonConfig = ButtonConfig::getSystemButtonConfig(); - - buttonConfig->setEventHandler(handleEvent); - buttonConfig->setFeature(ButtonConfig::kFeatureClick); - buttonConfig->setFeature(ButtonConfig::kFeatureLongPress); - - Node my_node; - my_node = RMaker.initNode(nodeName); - - for (byte i = 0; i < NUM_RELAYS; i++) - { - pinMode(info[i].relayPin, OUTPUT); - - // Turn off all relays. - digitalWrite(info[i].relayPin, info[i].relayState); - - // Initialize switch device - myRelay[i] = Switch(info[i].deviceName); - - // Specify the callback function to relays. - myRelay[i].addCb(write_callback); - - // Add switch device to the node. - my_node.addDevice(myRelay[i]); - - // Switch uses the built-in pull up resistor. - pinMode(info[i].switchPin, INPUT_PULLUP); - - // initialize the corresponding AceButton. - lightSwitch[i].init(info[i].switchPin, HIGH, i); - } - - // 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(); - - RMaker.start(); - - WiFi.onEvent(sysProvEvent); - #if CONFIG_IDF_TARGET_ESP32S2 - WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name); - #else - WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name); - #endif - - // Get the last state of Relays. - getLastState(); -} - -void loop() -{ - for (int i = 0; i < NUM_RELAYS; i++) - { - lightSwitch[i].check(); - } - - // Push button pressed. - if(digitalRead(resetPin) == LOW) - { - // Key debounce handling. - delay(100); - int startTime = millis(); - while(digitalRead(resetPin) == LOW) - { - delay(50); - } - int endTime = millis(); - - // If key pressed for more than 10secs, reset all. - if ((endTime - startTime) > 10000) - { - Serial.printf("Factory reset!"); - RMakerFactoryReset(2); - } - } -}