📅  最后修改于: 2023-12-03 15:14:54.319000             🧑  作者: Mango
ESP Rainmaker is a platform from Espressif Systems that enables users to quickly develop and prototype IoT solutions. ESP Rainmaker provides an easy-to-use interface for configuring and managing Wi-Fi and cloud services on Espressif’s ESP32 and ESP8266 microcontrollers.
In this tutorial, we will learn how to control an LED connected to an ESP32 microcontroller using ESP Rainmaker and C Programming Language.
Connect the LED to GPIO 2 of the ESP32 through a 330-ohm resistor.
We will use the ESP-IDF software development kit to write C code for controlling the LED.
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "esp_system.h"
#include "driver/gpio.h"
#include "esp_rmaker_core.h"
#include "esp_rmaker_device.h"
#include "esp_rmaker_light.h"
/* GPIO for the LED */
#define LED_GPIO 2
esp_rmaker_device_t *light_device;
/* Callback for updating the state of the LED */
esp_err_t led_write_cb(const char *dev_name, const char *prop_name, esp_rmaker_param_val_t val, void *priv_data)
{
gpio_set_level(LED_GPIO, val.val.b);
return ESP_OK;
}
/* Initialise the LED */
void led_init(void)
{
/* Configure the GPIO for the LED */
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1 << LED_GPIO);
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
gpio_config(&io_conf);
/* Create a light device and add it to the ESP Rainmaker node */
light_device = esp_rmaker_device_create("LED", "light", NULL);
esp_rmaker_device_add_cb(light_device, led_write_cb, NULL);
esp_rmaker_light_service_create_and_add_to_device(light_device, true, 100);
}
void app_main(void)
{
/* Initialize the ESP Rainmaker agent */
esp_rmaker_node_init();
/* Initialise the LED */
led_init();
/* Start the ESP Rainmaker agent task */
esp_rmaker_node_start();
printf("ESP Rainmaker LED - C Programming Language\n");
}
First, we include the necessary header files. We then define the GPIO pin for the LED. We create a function led_write_cb
that is called when the state of the LED changes. This function receives the new state as a Boolean value and updates the state of the LED using the GPIO library. We then create a function led_init
to initialise the LED and add it as a light device to the ESP Rainmaker node.
In app_main
, we initialise the ESP Rainmaker agent, initialise the LED and start the ESP Rainmaker agent task.
make menuconfig
.rainmaker/examples/c/led
and run the command make flash
.In this tutorial, we learned how to use ESP Rainmaker and C Programming Language to control an LED connected to an ESP32 microcontroller. By leveraging the power of ESP Rainmaker, we were able to quickly set up a Wi-Fi and cloud-connected device without having to write extensive code.