Created
January 5, 2025 14:01
-
-
Save GOROman/571ec4d6b38ee27ea9494a32ff9732ae to your computer and use it in GitHub Desktop.
ESP-IDF で MQTT を使って Adafruit IOとやりとりするクソコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <stdio.h> | |
| #include "esp_system.h" | |
| #include "freertos/FreeRTOS.h" | |
| #include "freertos/task.h" | |
| #include "mqtt_client.h" | |
| #define BROKER_HOST "io.adafruit.com" | |
| #define BROKER_PORT 1883 | |
| #define BROKER_USER "yourname" | |
| #define BROKER_PASS "aio_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
| #define BROKER_URI "mqtt://" BROKER_USER ":" BROKER_PASS "@" BROKER_HOST | |
| #define FEED "GOROman/feeds/mini4wd." | |
| static esp_mqtt_client_handle_t client; | |
| void mqtt_send(const char *feed, const char *data) { | |
| if (client == NULL) { | |
| return; | |
| } | |
| char name[100]; | |
| strncpy(name, FEED, sizeof(name)); | |
| strncat(name, feed, sizeof(name) - strlen(name) - 1); | |
| printf("[MQTT] publish: %s=%s\n", name, data); | |
| esp_mqtt_client_publish(client, name, data, 0, 0, 0); | |
| } | |
| static void mqtt_event_handler(void *handler_args, esp_event_base_t base, | |
| int32_t event_id, void *event_data) { | |
| esp_mqtt_event_handle_t e = (esp_mqtt_event_handle_t)event_data; | |
| switch ((esp_mqtt_event_id_t)event_id) { | |
| case MQTT_EVENT_CONNECTED: | |
| printf("[MQTT_EVENT_CONNECTED]\n"); | |
| // Subscribe | |
| esp_mqtt_client_subscribe(e->client, FEED "led-r", 0); | |
| esp_mqtt_client_subscribe(e->client, FEED "led-g", 0); | |
| esp_mqtt_client_subscribe(e->client, FEED "led-b", 0); | |
| esp_mqtt_client_subscribe(e->client, FEED "talk", 0); | |
| esp_mqtt_client_subscribe(e->client, FEED "spk-volume", 0); | |
| esp_mqtt_client_subscribe(e->client, FEED "mic-volume", 0); | |
| break; | |
| case MQTT_EVENT_DISCONNECTED: | |
| printf("[MQTT_EVENT_DISCONNECTED]\n"); | |
| break; | |
| case MQTT_EVENT_SUBSCRIBED: | |
| printf("[MQTT_EVENT_SUBSCRIBED]\n"); | |
| break; | |
| case MQTT_EVENT_UNSUBSCRIBED: | |
| printf("[MQTT_EVENT_UNSUBSCRIBED]\n"); | |
| break; | |
| case MQTT_EVENT_DATA: { | |
| printf("[MQTT_EVENT_DATA]\n"); | |
| printf("TOPIC=%.*s\r\n", e->topic_len, e->topic); | |
| printf("DATA=%.*s\r\n", e->data_len, e->data); | |
| char buf[e->data_len + 1] = {0}; | |
| memcpy(buf, e->data, e->data_len); | |
| if (strncmp(e->topic, FEED "led-r", e->topic_len) == 0) { | |
| const int value = atoi(buf); | |
| printf("LED R:[%d]\n", value); | |
| led_set_color_r(value); | |
| } else if (strncmp(e->topic, FEED "led-g", e->topic_len) == 0) { | |
| const int value = atoi(buf); | |
| printf("LED G:[%d]\n", value); | |
| led_set_color_g(value); | |
| } else if (strncmp(e->topic, FEED "led-b", e->topic_len) == 0) { | |
| const int value = atoi(buf); | |
| printf("LED B:[%d]\n", value); | |
| led_set_color_b(value); | |
| } else if (strncmp(e->topic, FEED "spk-volume", e->topic_len) == 0) { | |
| const int value = atoi(buf); | |
| printf("Spk Volume: %d\n", value); | |
| media_set_spk_volume(value); | |
| } else if (strncmp(e->topic, FEED "mic-volume", e->topic_len) == 0) { | |
| const int value = atoi(buf); | |
| printf("Mic Volume: %d\n", value); | |
| media_set_mic_volume(value); | |
| } else if (strncmp(e->topic, FEED "talk", e->topic_len) == 0) { | |
| printf("TALK!: %s\n", buf); | |
| openai_talk(buf); | |
| } | |
| } break; | |
| default: | |
| printf("MQTT event %ld\n", event_id); | |
| break; | |
| } | |
| } | |
| // MQTT開始 | |
| void mqtt_app_start(void) { | |
| esp_mqtt_client_config_t mqtt_cfg = { | |
| .broker = {.address = | |
| { | |
| .uri = BROKER_URI, | |
| }}, | |
| }; | |
| printf("MQTT client initialized\n"); | |
| client = esp_mqtt_client_init(&mqtt_cfg); | |
| printf("MQTT client register\n"); | |
| esp_mqtt_client_register_event(client, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, | |
| mqtt_event_handler, NULL); | |
| printf("MQTT client start\n"); | |
| esp_mqtt_client_start(client); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ESP-MQTT
https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/protocols/mqtt.html