|
| 1 | +// This example demonstrates the ESP RainMaker with a standard Switch device. |
| 2 | +#include "RMaker.h" |
| 3 | +#include "WiFi.h" |
| 4 | +#include "WiFiProv.h" |
| 5 | +#include "AppInsights.h" |
| 6 | + |
| 7 | +#define DEFAULT_POWER_MODE true |
| 8 | +const char *service_name = "PROV_1234"; |
| 9 | +const char *pop = "abcd1234"; |
| 10 | + |
| 11 | +// GPIO for push button |
| 12 | +#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C6 |
| 13 | +static int gpio_0 = 9; |
| 14 | +static int gpio_switch = 7; |
| 15 | +#else |
| 16 | +// GPIO for virtual device |
| 17 | +static int gpio_0 = 0; |
| 18 | +static int gpio_switch = 16; |
| 19 | +#endif |
| 20 | + |
| 21 | +/* Variable for reading pin status*/ |
| 22 | +bool switch_state = true; |
| 23 | + |
| 24 | +// The framework provides some standard device types like switch, lightbulb, |
| 25 | +// fan, temperaturesensor. |
| 26 | +static Switch *my_switch = NULL; |
| 27 | + |
| 28 | +// WARNING: sysProvEvent is called from a separate FreeRTOS task (thread)! |
| 29 | +void sysProvEvent(arduino_event_t *sys_event) { |
| 30 | + switch (sys_event->event_id) { |
| 31 | + case ARDUINO_EVENT_PROV_START: |
| 32 | +#if CONFIG_IDF_TARGET_ESP32S2 |
| 33 | + Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop); |
| 34 | + WiFiProv.printQR(service_name, pop, "softap"); |
| 35 | +#else |
| 36 | + Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop); |
| 37 | + WiFiProv.printQR(service_name, pop, "ble"); |
| 38 | +#endif |
| 39 | + break; |
| 40 | + case ARDUINO_EVENT_PROV_INIT: WiFiProv.disableAutoStop(10000); break; |
| 41 | + case ARDUINO_EVENT_PROV_CRED_SUCCESS: WiFiProv.endProvision(); break; |
| 42 | + default: ; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx) { |
| 47 | + const char *device_name = device->getDeviceName(); |
| 48 | + const char *param_name = param->getParamName(); |
| 49 | + |
| 50 | + if (strcmp(param_name, "Power") == 0) { |
| 51 | + Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name); |
| 52 | + switch_state = val.val.b; |
| 53 | + (switch_state == false) ? digitalWrite(gpio_switch, LOW) : digitalWrite(gpio_switch, HIGH); |
| 54 | + param->updateAndReport(val); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void setup() { |
| 59 | + Serial.begin(115200); |
| 60 | + pinMode(gpio_0, INPUT); |
| 61 | + pinMode(gpio_switch, OUTPUT); |
| 62 | + digitalWrite(gpio_switch, DEFAULT_POWER_MODE); |
| 63 | + |
| 64 | + Node my_node; |
| 65 | + my_node = RMaker.initNode("ESP RainMaker Node"); |
| 66 | + |
| 67 | + // Initialize switch device |
| 68 | + my_switch = new Switch("Switch", &gpio_switch); |
| 69 | + if (!my_switch) { |
| 70 | + return; |
| 71 | + } |
| 72 | + // Standard switch device |
| 73 | + my_switch->addCb(write_callback); |
| 74 | + |
| 75 | + // Add switch device to the node |
| 76 | + my_node.addDevice(*my_switch); |
| 77 | + |
| 78 | + // This is optional |
| 79 | + RMaker.enableOTA(OTA_USING_TOPICS); |
| 80 | + // If you want to enable scheduling, set time zone for your region using |
| 81 | + // setTimeZone(). The list of available values are provided here |
| 82 | + // https://rainmaker.espressif.com/docs/time-service.html |
| 83 | + // RMaker.setTimeZone("Asia/Shanghai"); |
| 84 | + // Alternatively, enable the Timezone service and let the phone apps set the |
| 85 | + // appropriate timezone |
| 86 | + RMaker.enableTZService(); |
| 87 | + |
| 88 | + RMaker.enableSchedule(); |
| 89 | + |
| 90 | + RMaker.enableScenes(); |
| 91 | + // Enable ESP Insights. Insteads of using the default http transport, this function will |
| 92 | + // reuse the existing MQTT connection of Rainmaker, thereby saving memory space. |
| 93 | + initAppInsights(); |
| 94 | + |
| 95 | + RMaker.enableSystemService(SYSTEM_SERV_FLAGS_ALL, 2, 2, 2); |
| 96 | + |
| 97 | +#if CONFIG_IDF_TARGET_ESP32S2 |
| 98 | + WiFiProv.initProvision(NETWORK_PROV_SCHEME_SOFTAP, NETWORK_PROV_SCHEME_HANDLER_NONE); |
| 99 | +#else |
| 100 | + WiFiProv.initProvision(NETWORK_PROV_SCHEME_BLE, NETWORK_PROV_SCHEME_HANDLER_FREE_BTDM); |
| 101 | +#endif |
| 102 | + |
| 103 | + RMaker.start(); |
| 104 | + |
| 105 | + WiFi.onEvent(sysProvEvent); // Will call sysProvEvent() from another thread. |
| 106 | +#if CONFIG_IDF_TARGET_ESP32S2 |
| 107 | + WiFiProv.beginProvision(NETWORK_PROV_SCHEME_SOFTAP, NETWORK_PROV_SCHEME_HANDLER_NONE, NETWORK_PROV_SECURITY_1, pop, service_name); |
| 108 | +#else |
| 109 | + WiFiProv.beginProvision(NETWORK_PROV_SCHEME_BLE, NETWORK_PROV_SCHEME_HANDLER_FREE_BTDM, NETWORK_PROV_SECURITY_1, pop, service_name); |
| 110 | +#endif |
| 111 | +} |
| 112 | + |
| 113 | +void loop() { |
| 114 | + if (digitalRead(gpio_0) == LOW) { // Push button pressed |
| 115 | + |
| 116 | + // Key debounce handling |
| 117 | + delay(100); |
| 118 | + int startTime = millis(); |
| 119 | + while (digitalRead(gpio_0) == LOW) { |
| 120 | + delay(50); |
| 121 | + } |
| 122 | + int endTime = millis(); |
| 123 | + |
| 124 | + if ((endTime - startTime) > 10000) { |
| 125 | + // If key pressed for more than 10secs, reset all |
| 126 | + Serial.printf("Reset to factory.\n"); |
| 127 | + RMakerFactoryReset(2); |
| 128 | + } else if ((endTime - startTime) > 3000) { |
| 129 | + Serial.printf("Reset Wi-Fi.\n"); |
| 130 | + // If key pressed for more than 3secs, but less than 10, reset Wi-Fi |
| 131 | + RMakerWiFiReset(2); |
| 132 | + } else { |
| 133 | + // Toggle device state |
| 134 | + switch_state = !switch_state; |
| 135 | + Serial.printf("Toggle State to %s.\n", switch_state ? "true" : "false"); |
| 136 | + if (my_switch) { |
| 137 | + my_switch->updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state); |
| 138 | + } |
| 139 | + (switch_state == false) ? digitalWrite(gpio_switch, LOW) : digitalWrite(gpio_switch, HIGH); |
| 140 | + } |
| 141 | + } |
| 142 | + delay(100); |
| 143 | +} |
0 commit comments