Skip to content

Commit

Permalink
Update main.c
Browse files Browse the repository at this point in the history
  • Loading branch information
TIT8 committed Dec 18, 2023
1 parent 1548f10 commit 3ae4fd4
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "mqtt_client.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "cJSON.h"


// Remember to panic and reboot (in the config) if the watchdog was triggered
Expand All @@ -50,8 +49,6 @@ static EventGroupHandle_t s_wifi_event_group;
static const char *TAG = "mqtt";
static const char *TAG2 = "wifi station";
static int s_retry_num = 0;
volatile bool state = 0;
volatile bool connection = false;
volatile bool disconnected = true;
volatile bool fail = false;

Expand Down Expand Up @@ -84,11 +81,9 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
{
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
while (!connection && !xQueueSend(mqtt_evt_queue, &client, 0)) {}
disconnected = false;
msg_id = esp_mqtt_client_subscribe(client, "<YOUR_SHELLY_ID>/status/switch:0", 2);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
esp_mqtt_client_publish(client, "<YOUR_SHELLY_ID>/command/switch:0", "status_update", 0, 2, 0);
break;

case MQTT_EVENT_DISCONNECTED:
Expand All @@ -113,23 +108,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
cJSON *json = cJSON_Parse(event->data);
if (json != NULL)
{
char *rendered = cJSON_PrintUnformatted(json);
printf("DATA_json=%.*s\r\n", event->data_len, rendered);
cJSON *output = cJSON_GetObjectItem(json, "output");
if (output != NULL && cJSON_IsBool(output))
{
state = output->valueint;
printf("output=%d\r\n", state);
}
}
else
{
printf("DATA=%.*s\r\n", event->data_len, event->data);
}
cJSON_Delete(json);
printf("DATA=%.*s\r\n", event->data_len, event->data);
break;

case MQTT_EVENT_ERROR:
Expand Down Expand Up @@ -191,8 +170,11 @@ static void gpio_task(void *arg)
int msg_id;
esp_mqtt_client_handle_t client;

while (!xQueueReceive(mqtt_evt_queue, &client, portMAX_DELAY)) {}
connection = true;
// portMAX_DELAY is reduntant, because the sending happens before the receiving
// but it's better to block to be sure. Delete the queue after receiving,
// in this way no memory get wasted
xQueueReceive(mqtt_evt_queue, &client, portMAX_DELAY);
vQueueDelete(mqtt_evt_queue);

for (;;)
{
Expand All @@ -208,7 +190,9 @@ static void gpio_task(void *arg)
{
if (!current)
{
msg_id = esp_mqtt_client_publish(client, "<YOUR_SHELLY_ID>/command/switch:0", state ? "off" : "on", 0, 2, 0);
// https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Switch#mqtt-control
msg_id = esp_mqtt_client_publish(client, "<YOUR_SHELLY_ID>/command/switch:0", "toggle", 0, 2, 0);

// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/mqtt.html#_CPPv423esp_mqtt_client_publish24esp_mqtt_client_handle_tPKcPKciii
if (msg_id == -1)
{
Expand All @@ -219,11 +203,6 @@ static void gpio_task(void *arg)
ESP_ERROR_CHECK(esp_mqtt_client_reconnect(client));
}
}
else if (msg_id != -1 && msg_id != -2)
{
// Save the state for redundancy
state = !state;
}
}
current = 1;
}
Expand All @@ -232,6 +211,7 @@ static void gpio_task(void *arg)
current = 0;
}
}

button_last = button_current;

// Yield control to the idle task on core 1 if the task priority is setted above 0
Expand All @@ -249,8 +229,18 @@ static void mqtt_app_start(void)
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);
mqtt_evt_queue = xQueueCreate(1, sizeof(client));
esp_mqtt_client_start(client);

// Send data to gpio task and handle failure from here, so gpio task should not worry
mqtt_evt_queue = xQueueCreate(1, sizeof(client));
if (mqtt_evt_queue != NULL)
{
if (!xQueueSend(mqtt_evt_queue, &client, (TickType_t)10)) esp_restart();
}
else
{
esp_restart();
}
}


Expand Down Expand Up @@ -350,8 +340,8 @@ void app_main(void)
ESP_ERROR_CHECK(gpio_config(&io_conf));

// Due to the majority of built-in tasks pinned on core 0, the gpio task run on core 1
// Since there's only one application task, its priority on core 1 is setted to 0,
// so when RTOS tick, the idle task can run and feed the watchdog timer.
// Since there's only one application task, its priority on core 1 is setted to 0.
// So when RTOS tick, the idle task can run and feed the watchdog timer.
// https://docs.espressif.com/projects/esp-idf/en/v5.0/esp32/api-guides/performance/speed.html#choosing-application-task-priorities
xTaskCreatePinnedToCore(gpio_task, "gpio_task", 4096, NULL, 0, NULL, 1);
}

0 comments on commit 3ae4fd4

Please sign in to comment.