ESP RainMaker is an end-to-end solution offered by Espressif to enable remote control and monitoring for ESP32-S2 and ESP32 based products without any configuration required in the Cloud. The primary components of this solution are:
- Claiming Service (to get the Cloud connectivity credentials)
- RainMaker library (i.e. this library, to develop the firmware)
It stops the ESP RainMaker agent which was started using `RMaker.start()`.
```
esp_err_t stop()
```
* **Return**
1. ESP_OK : On success
2. Error in case of failure
### RMaker.deinitNode()
It deinitializes the ESP RainMaker agent and the node created using `RMaker.initNode()`.
```
esp_err_t deinitNode(Node node)
```
* **Parameter**
1.`node` : Node object created using `RMaker.initNode()`
* **Return**
1. ESP_OK : On success
2. Error in case of failure
### RMaker.enableOTA()
It enables OTA as per the ESP RainMaker Specification. For more details refer ESP RainMaker documentation. check [here](https://rainmaker.espressif.com/docs/ota.html)
```
esp_err_t enableOTA(ota_type_t type);
```
* **Parameter**
1.`type` : The OTA workflow type.
- OTA_USING_PARAMS
- OTA_USING_TOPICS
* **Return**
1. ESP_OK : On success
2. Error in case of failure
### RMaker.enableSchedule()
This API enables the scheduling service for the node. For more information, check [here](https://rainmaker.espressif.com/docs/scheduling.html).
```
esp_err_t enableSchedule();
```
* **Return**
1. ESP_OK : On success
2. Error in case of failure
### RMaker.setTimeZone()
This API set's the timezone as a user friendly location string. Check [here](https://rainmaker.espressif.com/docs/time-service.html) for a list of valid values.
```
esp_err_t setTimeZone(const char *tz);
```
* **Parameter**
1. `tz' : Valid values as specified in documentation.
* **Return**
1. ESP_OK : On success
2. Error in case of failure
> NOTE : default value is "Asia/Shanghai".
> This API comes into picture only when working with scheduling.
## ESP RainMaker NODE APIs
`Node` class expose API's for node.
> NOTE : my_node is the object of Node class.
### my_node.getNodeID()
Itreturns the unique node_id assigned to the node. This node_id is usually the MAC address of the board.
```
char * getNodeID()
```
* **Return**
1.`char * ` : Pointer to a NULL terminated node_id string.
### my_node.getNodeInfo()
It returns pointer to the node_info_t as configured during node initialisation.
```
node_info_t * getNodeInfo();
```
* **Return**
1.`node_info_t` : Pointer to the structure node_info_t on success.
2.`NULL` : On failure.
* **ESP RainMaker node info**
It has following data member
1. char * name
2. char * type
3. char * fw_version
4. char * model
### my_node.addNodeAttr()
It adds a new attribute as the metadata to the node.
1.`dev_name` : Unique device name by default it is "switch" for switch device.
2.`priv_data` : Private data associated with the device. This will be passed to the callbacks.
3.`power` : It is the value that can be set for primary parameter.
- Sample example for standard device.
```
Switch switch1;
Switch switch2("switch2", NULL, true);
```
`"switch2"` : Name for standard device.
`NULL` : Private data for the device, which will be used in callback.
`true` : Default value for the primary param, in case of switch it is power.
> NOTE : No parameter are compulsory for standard devices. However if you are creating two objects of same standard class then in that case you will have to set the device name, if not then both device will have same name which is set by default, hence device will not get create. Device name should be unique for each device.
### my_device.getDeviceName()
It returns the name of the Device.
```
const char * getDeviceName();
```
* **Return**
1.`char *`: Returns Device name.
> NOTE : Each device on the node should have unique device name.
### my_device.addDeviceAttr()
It adds attribute to the device. Device attributes are reported only once after a boot-up as part of the node configuration. Eg. Serial Number
It deletes the device created using parameterized constructor. This device should be first removed from the node using `my_node.removeDevice(my_device)`.
```
esp_err_t deleteDevice();
```
* **Return**
1.`ESP_OK` : On success
2. Error in case of failure
### my_device.addXParam()
It adds standard parameter to the device.
> NOTE : X is the default name by which parameter is referred, you can specify your own name to each parameter.
> Default
> Eg. `my_device.addPowerParam(true)` here power parameter is referred with name Power.
> Eg. `my_device.addHueParam(12)` here hue parameter is referred with name Hue.
> You can specify your own name to each parameter
> Eg. `my_device.addNameParam("NickName")` here name parameter is referred with name NickName.
> Eg. `my_device.addPowerParam(true, "FanPower")` here power parameter is referred with name FanPower.
> NOTE : Care should be taken while accessing name of parameter. Above mentioned are the two ways using which default name of parameters can be accessed. Either LHS or RHS.
### my_device.assignPrimaryParam()
It assigns a parameter (already added using addXParam() or addParam()) as a primary parameter, which can be used by clients (phone apps specifically) to give prominence to it.
2.`value` : Value to be updated. It can be int, bool, char * , float.
* **Return**
1.`ESP_OK` : On success
2. Error in case of failure
### my_device.addCb()
It registers read and write callback for the device which will be invoked as per requests received from the cloud (or other paths as may be added in future).
1.`write_cb` : Function with signature [ func_name(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx); ]
2.`read_cb` : Function with signature [ func_name(Device *device, Param *param, void *priv_data, read_ctx_t *ctx); ]
* **param_val_t val**
Value can be accessed as below
1.`bool` : val.val.b
2.`integer` : val.val.i
3.`float` : val.val.f
4.`char *` : val.val.s
## ESP RainMaker PARAM API's
`Param` class expose API's for creating custom parameters for the devices and report and update values associated with parameter to the ESP RainMaker cloud. Parameterized constructor is defined which creates custom parameter.
2.`param_type` : Type of the parameter. It is optional can be kept NULL.
3.`val` : Define the default value for the parameter. It should be defined using `value(int ival)` , `value(bool bval)` , `value(float fval)` , `value(char *sval)`.
4.`properties` : Properties of the parameter, which will be a logical OR of flags.
> NOTE : Parameter created using Param class should be added to the device using `my_device.addParam(my_param);`
### my_param.addUIType()
Add a UI type to the parameter. This will be used by the Phone apps (or other clients) to render appropriate UI for the given parameter. Please refer the RainMaker documentation [here](https://rainmaker.espressif.com/docs/standard-types.html#ui-elements) for supported UI Types.
```
esp_err_t addUIType(const char *ui_type);
```
* **Paramter**
1.`ui_type` : String describing the UI Type.
* Standard UI Types
* ESP_RMAKER_UI_TOGGLE
* ESP_RMAKER_UI_SLIDER
* ESP_RMAKER_UI_DROPDOWN
* ESP_RMAKER_UI_TEXT
* **Returns**
1. ESP_OK : On success.
2. Error in case of failure.
### my_param.addBounds()
Add bounds for an integer/float parameter. This can be used to add bounds (min/max values) for a given integer/float parameter. Eg. brightness will have bounds as 0 and 100 if it is a percentage.
`Sample example : my_param.addBounds(value(0), value(100), value(5));`
### my_param.updateAndReport()
It updates the parameter and report it to ESP RainMaker cloud. This is called in callback.
```
esp_err_t updateAndReport(param_val_t val);
```
* **Parameters**
1.`val` : New value of the parameter
* **Return**
1. ESP_OK : On success.
2. Error in case of failure.
> NOTE :
> - This API should always be called inside device write callback, if you aimed at updating n reporting parameter values, changed via RainMaker Client (Phone App), to the ESP RainMaker cloud.
> - If not called then paramter values will not be updated to the ESP RainMaker cloud.
### printQR()
This API displays QR code, which is used in provisioning.