mirror of
https://github.com/alexandrebobkov/ESP-Nodes.git
synced 2024-10-05 20:47:50 -04:00
163 lines
5.0 KiB
C
163 lines
5.0 KiB
C
/* Blink Example
|
|
|
|
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.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <sys/param.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "driver/gpio.h"
|
|
#include "esp_log.h"
|
|
#include "led_strip.h"
|
|
#include "sdkconfig.h"
|
|
|
|
#include "mqtt_client.h"
|
|
#include "esp_system.h"
|
|
#include "esp_event.h"
|
|
#include "esp_ota_ops.h"
|
|
#include "esp_partition.h"
|
|
#include "esp_flash_partitions.h"
|
|
#include "esp_netif.h"
|
|
#include "esp_tls.h"
|
|
#include "nvs_flash.h"
|
|
|
|
static const char *TAG = "ESP32 MQTT SSL node";
|
|
static const uint8_t mqtt_eclipseprojects_io_pem_start[] = "";
|
|
extern const uint8_t mqtt_eclipseprojects_io_pem_start[] asm("_binary_mqtt_exlipseprojects_io_pem_start");
|
|
extern const uint8_t mqtt_eclipseprojects_io_pem_end[] asm("_binary_mqtt_eclipseprojects_io_pem_end");
|
|
|
|
/* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
|
|
or you can edit the following line and set a number here.
|
|
*/
|
|
#define BLINK_GPIO CONFIG_BLINK_GPIO
|
|
|
|
static uint8_t s_led_state = 0;
|
|
|
|
#ifdef CONFIG_BLINK_LED_STRIP
|
|
|
|
static led_strip_handle_t led_strip;
|
|
|
|
static void blink_led(void)
|
|
{
|
|
/* If the addressable LED is enabled */
|
|
if (s_led_state) {
|
|
/* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
|
|
led_strip_set_pixel(led_strip, 0, 16, 16, 16);
|
|
/* Refresh the strip to send data */
|
|
led_strip_refresh(led_strip);
|
|
} else {
|
|
/* Set all LED off to clear all pixels */
|
|
led_strip_clear(led_strip);
|
|
}
|
|
}
|
|
|
|
static void configure_led(void)
|
|
{
|
|
ESP_LOGI(TAG, "Example configured to blink addressable LED!");
|
|
/* LED strip initialization with the GPIO and pixels number*/
|
|
led_strip_config_t strip_config = {
|
|
.strip_gpio_num = BLINK_GPIO,
|
|
.max_leds = 1, // at least one LED on board
|
|
};
|
|
#if CONFIG_BLINK_LED_STRIP_BACKEND_RMT
|
|
led_strip_rmt_config_t rmt_config = {
|
|
.resolution_hz = 10 * 1000 * 1000, // 10MHz
|
|
.flags.with_dma = false,
|
|
};
|
|
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
|
|
#elif CONFIG_BLINK_LED_STRIP_BACKEND_SPI
|
|
led_strip_spi_config_t spi_config = {
|
|
.spi_bus = SPI2_HOST,
|
|
.flags.with_dma = true,
|
|
};
|
|
ESP_ERROR_CHECK(led_strip_new_spi_device(&strip_config, &spi_config, &led_strip));
|
|
#else
|
|
#error "unsupported LED strip backend"
|
|
#endif
|
|
/* Set all LED off to clear all pixels */
|
|
led_strip_clear(led_strip);
|
|
}
|
|
|
|
#elif CONFIG_BLINK_LED_GPIO
|
|
|
|
static void blink_led(void)
|
|
{
|
|
/* Set the GPIO level according to the state (LOW or HIGH)*/
|
|
gpio_set_level(BLINK_GPIO, s_led_state);
|
|
}
|
|
|
|
static void send_binary(esp_mqtt_client_handle_t client)
|
|
{
|
|
esp_partition_mmap_handle_t out_handle;
|
|
const void *binary_address;
|
|
const esp_partition_t *partition = esp_ota_get_running_partition();
|
|
|
|
esp_partition_mmap(partition, 0, partition->size, ESP_PARTITION_MMAP_DATA, &binary_address, &out_handle);
|
|
|
|
int binary_size = MIN(1024, partition->size);
|
|
int msg_id = esp_mqtt_client_publish(client, "/esp32/binary", binary_address, binary_size, 0, 0);
|
|
ESP_LOGI(TAG, "binary sent with msg_id=%d", msg_id);
|
|
}
|
|
|
|
static void configure_led(void)
|
|
{
|
|
ESP_LOGI(TAG, "Example configured to blink GPIO LED!");
|
|
gpio_reset_pin(BLINK_GPIO);
|
|
/* Set the GPIO as a push/pull output */
|
|
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
|
|
}
|
|
|
|
#else
|
|
#error "unsupported LED type"
|
|
#endif
|
|
|
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, uint32_t event_id, void *event_data)
|
|
{
|
|
esp_mqtt_event_handle_t event = event_data;
|
|
esp_mqtt_client_handle_t client = event->client;
|
|
int msg_id;
|
|
}
|
|
|
|
static void mqtt_app_start(void) {
|
|
const esp_mqtt_client_config_t mqtt_cfg = {
|
|
.broker = {
|
|
.address.uri = "10.100.50.16",
|
|
.verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start
|
|
},
|
|
};
|
|
|
|
ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size());
|
|
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
|
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
|
|
esp_mqtt_client_start(client);
|
|
|
|
send_binary(client);
|
|
}
|
|
|
|
void app_main(void)
|
|
{
|
|
|
|
ESP_LOGI(TAG, "[APP] Startup ...");
|
|
ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes ", esp_get_free_heap_size());
|
|
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
|
/* Configure the peripheral according to the LED type */
|
|
configure_led();
|
|
|
|
while (1) {
|
|
ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");
|
|
blink_led();
|
|
/* Toggle the LED state */
|
|
s_led_state = !s_led_state;
|
|
vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);
|
|
}
|
|
mqtt_app_start();
|
|
}
|