main 应用
main.c
#include "esp_random.h"
#include "led_strip.h"
#include "nvs_param.h"
#include "button.h"
#include "wifi.h"
#include "mqtt.h"
void app_main(void)
{
led_strip_init();
nvs_param_init();
button_init();
// 设置最近记录的颜色
led_color_t color_nvs;
esp_err_t err = get_led_color(&color_nvs);
if (err == ESP_OK)
{
led_strip_update(color_nvs);
}
while (1)
{
uint8_t event_id = button_wait_action();
if (BUTTON_TYPE_LONG == event_id) // 长按按键
{
// 启动 wifi
wifi_start();
// 连接 mqtt 服务器
mqtt_init();
}
else if (BUTTON_TYPE_SHORT == event_id) // 短按按键
{
// 设置随机颜色
uint32_t random_val = esp_random();
led_color_t color_random = {
.red = random_val & 0xFF,
.green = (random_val >> 8) & 0xFF,
.blue = (random_val >> 16) & 0xFF,
};
led_strip_update(color_random);
// 记录颜色值
set_led_color(&color_random);
// 向 mqtt 服务器发送消息
if (mqtt_is_connected())
{
char message[512] = {0};
const char* message_fmt = "{\"red\": %d,\"green\": %d,\"blue\": %d}";
sprintf(message, message_fmt, color_random.red, color_random.green, color_random.blue);
mqtt_publish(message);
}
}
}
}
浙公网安备 33010602011771号