/* Rainmaker Multi-Device Example Original code source: Espressif Rainmaker example code. Created on: Jan 6, 2024 Modified on: Jan 6, 2024 Edited by: Alexander Bobkov This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. NOTES: Compiled with ESP-IDF v.5.1.2; ESP-Rainmaker 21-Nov-2022 Specify chip ESP32S2 in a file settings.json Ensure that SDK Configuration has setting set CONFIG_IDF_TARGET="esp32s2" Ensure that log output is set to a Serial console (from UART to USB CDC) => Channel for console output: USB CDC Rainmaker default MQTT port 443 (i.e. unencrypted), protocol 3.1.1 ESP Insights enabled esp32s2 -> ESP Prog USB Make initial flash with DFU mode selected (Device Firmware Update) [https://docs.espressif.com/projects/esp-idf/en/v4.3/esp32s2/api-guides/usb-console.html] Flash with UART Channel for console output: USB CDC Stay in bootloader after flash UART1 (not UART0) */ #include #include #include #include #include #include #include #include #include #include #include #include #include "sys_status.h" #include "app_priv.h" static const char *TAG = "app_main"; // A group of devices that a part of this node esp_rmaker_device_t *switch_device; esp_rmaker_device_t *light_device; esp_rmaker_device_t *fan_device; esp_rmaker_device_t *temp_sensor_device; // Additional devices esp_rmaker_device_t *switch_1; esp_rmaker_device_t *switch_2; esp_rmaker_device_t *temp_sensor; /* Callback to handle commands received from the RainMaker cloud */ static esp_err_t write_cb(const esp_rmaker_device_t *device, const esp_rmaker_param_t *param, const esp_rmaker_param_val_t val, void *priv_data, esp_rmaker_write_ctx_t *ctx) { if (ctx) { // Write status update to the serial terminal. ESP_LOGI(TAG, "Received write request via : %s", esp_rmaker_device_cb_src_to_str(ctx->src)); } const char *device_name = esp_rmaker_device_get_name(device); const char *param_name = esp_rmaker_param_get_name(param); // Compare parameter name that made call back against initiated devices if (strcmp(param_name, ESP_RMAKER_DEF_POWER_NAME) == 0) { ESP_LOGI(TAG, "Received value = %s for %s - %s", val.val.b? "true" : "false", device_name, param_name); if (strcmp(device_name, "Switch") == 0) { app_driver_set_state(val.val.b); } } else if (strcmp(param_name, ESP_RMAKER_DEF_BRIGHTNESS_NAME) == 0) { ESP_LOGI(TAG, "Received value = %d for %s - %s", val.val.i, device_name, param_name); } else if (strcmp(param_name, ESP_RMAKER_DEF_SPEED_NAME) == 0) { ESP_LOGI(TAG, "Received value = %d for %s - %s", val.val.i, device_name, param_name); } else { /* Silently ignoring invalid params */ return ESP_OK; } esp_rmaker_param_update_and_report(param, val); return ESP_OK; } void app_main() { /* Initialize Application specific hardware drivers and * set initial state. */ app_driver_init(); app_driver_set_state(DEFAULT_SWITCH_POWER); sys_status_init(); /* Initialize NVS. */ /* Non-volatile storage (NVS) library is designed to store key-value pairs in flash. */ esp_err_t err = nvs_flash_init(); if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); err = nvs_flash_init(); } ESP_ERROR_CHECK( err ); /* Initialize Wi-Fi. Note that, this should be called before esp_rmaker_node_init() */ app_wifi_init(); /* Initialize the ESP RainMaker Agent. * Note that this should be called after app_wifi_init() but before app_wifi_start() * */ esp_rmaker_config_t rainmaker_cfg = { .enable_time_sync = false, }; esp_rmaker_node_t *node = esp_rmaker_node_init(&rainmaker_cfg, "ESP32S2 RainMaker MultiDevice", "ESP32S2 Multi Device"); if (!node) { ESP_LOGE(TAG, "Could not initialise node. Aborting!!!"); vTaskDelay(5000/portTICK_PERIOD_MS); abort(); } /* Create a Switch device and add the relevant parameters to it */ switch_device = esp_rmaker_switch_device_create("Switch", NULL, DEFAULT_SWITCH_POWER); esp_rmaker_device_add_cb(switch_device, write_cb, NULL); esp_rmaker_node_add_device(node, switch_device); /* Create a custom Switch device for switch_1 and add the relevant parameters to it */ switch_1 = esp_rmaker_switch_device_create("Switch #1", NULL, DEFAULT_SWITCH_POWER); esp_rmaker_device_add_cb(switch_1, write_cb, NULL); esp_rmaker_node_add_device(node, switch_1); /* Create a Light device and add the relevant parameters to it */ light_device = esp_rmaker_lightbulb_device_create("Light", NULL, DEFAULT_LIGHT_POWER); esp_rmaker_device_add_cb(light_device, write_cb, NULL); esp_rmaker_device_add_param(light_device, esp_rmaker_brightness_param_create(ESP_RMAKER_DEF_BRIGHTNESS_NAME, DEFAULT_LIGHT_BRIGHTNESS)); esp_rmaker_device_add_attribute(light_device, "Serial Number", "012345"); esp_rmaker_device_add_attribute(light_device, "MAC", "xx:yy:zz:aa:bb:cc"); esp_rmaker_node_add_device(node, light_device); /* Create a Fan device and add the relevant parameters to it */ fan_device = esp_rmaker_fan_device_create("Fan", NULL, DEFAULT_FAN_POWER); esp_rmaker_device_add_cb(fan_device, write_cb, NULL); esp_rmaker_device_add_param(fan_device, esp_rmaker_speed_param_create(ESP_RMAKER_DEF_SPEED_NAME, DEFAULT_FAN_SPEED)); esp_rmaker_node_add_device(node, fan_device); /* Create a Temperature Sensor device and add the relevant parameters to it */ temp_sensor_device = esp_rmaker_temp_sensor_device_create("Temperature Sensor", NULL, app_get_current_temperature()); esp_rmaker_node_add_device(node, temp_sensor_device); /* Enable OTA */ esp_rmaker_ota_enable_default(); /* Enable timezone service which will be require for setting appropriate timezone * from the phone apps for scheduling to work correctly. * For more information on the various ways of setting timezone, please check * https://rainmaker.espressif.com/docs/time-service.html. */ esp_rmaker_timezone_service_enable(); /* Enable scheduling. */ esp_rmaker_schedule_enable(); /* Enable Scenes */ esp_rmaker_scenes_enable(); /* Enable Insights. Requires CONFIG_ESP_INSIGHTS_ENABLED=y */ app_insights_enable(); /* Start the ESP RainMaker Agent */ esp_rmaker_start(); /* Start the Wi-Fi. * If the node is provisioned, it will start connection attempts, * else, it will start Wi-Fi provisioning. The function will return * after a connection has been successfully established */ err = app_wifi_start(POP_TYPE_RANDOM); if (err != ESP_OK) { ESP_LOGE(TAG, "Could not start Wifi. Aborting!!!"); vTaskDelay(5000/portTICK_PERIOD_MS); abort(); } }