Last active
April 29, 2022 23:56
-
-
Save shimarin/2919318cf7a4626af8c12f3c87b085e7 to your computer and use it in GitHub Desktop.
This file contains 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
#include <cstring> | |
#include <string> | |
#include <stdio.h> | |
#include "sdkconfig.h" | |
#include "freertos/FreeRTOS.h" | |
#include "freertos/event_groups.h" | |
#include "driver/gpio.h" | |
#include "esp_wifi.h" | |
#include "esp_log.h" | |
#include "nvs_flash.h" | |
#include "lwip/ip6_addr.h" | |
#include "mqtt_client.h" | |
static const char* TAG = "mqtt-relay"; | |
static const char* SSID = "YOUR-SSID"; | |
static const char* PASSWORD = "WIFI-PASSWORD"; | |
static const char* MQTT_URI = "mqtt://your-mqtt-host"; | |
static const char* MQTT_TOPIC = "relay"; | |
static const auto GPIO_PIN = GPIO_NUM_13; | |
extern "C" { | |
void app_main(void) | |
{ | |
ESP_ERROR_CHECK(nvs_flash_init()); | |
ESP_ERROR_CHECK(esp_event_loop_create_default()); | |
// Establish network connection | |
{ | |
ESP_ERROR_CHECK(esp_netif_init()); | |
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | |
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); | |
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, | |
[](void* arg, esp_event_base_t, int32_t event_id, void*) { | |
if (event_id == WIFI_EVENT_STA_START || event_id == WIFI_EVENT_STA_DISCONNECTED) { | |
esp_wifi_connect(); | |
} else if (event_id == WIFI_EVENT_STA_CONNECTED) { | |
esp_netif_create_ip6_linklocal((esp_netif_t*)arg); | |
} | |
}, esp_netif_create_default_wifi_sta())); | |
auto event_group = xEventGroupCreate(); | |
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, | |
[](void* arg, esp_event_base_t, int32_t, void*) { | |
xEventGroupSetBits((EventGroupHandle_t)arg, 1); | |
}, event_group)); | |
wifi_config_t wifi; | |
memset(&wifi, 0, sizeof(wifi)); | |
strcpy((char*)wifi.sta.ssid, SSID); | |
strcpy((char*)wifi.sta.password, PASSWORD); | |
wifi.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; | |
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); | |
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi) ); | |
ESP_ERROR_CHECK(esp_wifi_start() ); | |
xEventGroupWaitBits(event_group, 1, pdFALSE, pdFALSE, portMAX_DELAY); | |
} | |
// Initialize GPIO | |
{ | |
gpio_config_t config; | |
config.intr_type = GPIO_INTR_DISABLE; | |
config.mode = GPIO_MODE_OUTPUT; | |
config.pin_bit_mask = (1ULL << GPIO_PIN); | |
config.pull_down_en = GPIO_PULLDOWN_ENABLE; | |
config.pull_up_en = GPIO_PULLUP_DISABLE; | |
ESP_ERROR_CHECK(gpio_config(&config)); | |
} | |
// Establish MQTT connection and subscribe topic | |
esp_mqtt_client_handle_t mqtt_client = NULL; | |
{ | |
esp_mqtt_client_config_t mqtt_cfg; | |
memset(&mqtt_cfg, 0, sizeof(mqtt_cfg)); | |
mqtt_cfg.uri = MQTT_URI; | |
mqtt_client = esp_mqtt_client_init(&mqtt_cfg); | |
ESP_ERROR_CHECK(esp_mqtt_client_register_event(mqtt_client, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, | |
[](void *arg, esp_event_base_t, int32_t event_id, void *event_data) { | |
auto event = (esp_mqtt_event_handle_t)event_data; | |
if (event->event_id == MQTT_EVENT_CONNECTED) { | |
esp_mqtt_client_subscribe((esp_mqtt_client_handle_t)arg, MQTT_TOPIC, 0); | |
} else if (event->event_id == MQTT_EVENT_DATA) { | |
auto topic = std::string(event->topic, event->topic_len); | |
if (topic != MQTT_TOPIC) return; | |
//else | |
auto data = std::string(event->data, event->data_len); | |
if (data == "on") gpio_set_level(GPIO_PIN, 0); | |
else if (data == "off") gpio_set_level(GPIO_PIN, 1); | |
} | |
}, mqtt_client)); | |
ESP_ERROR_CHECK(esp_mqtt_client_start(mqtt_client)); | |
} | |
// Main loop | |
while (true) { | |
esp_mqtt_client_publish(mqtt_client, MQTT_TOPIC, "PING", 4, 0, 0); | |
vTaskDelay(5000 / portTICK_PERIOD_MS); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment