Skip to content

Commit

Permalink
Add deep sleep functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-vdm committed Oct 25, 2023
1 parent c0b93f4 commit ad23787
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion firmware/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"configurations": [
{
"name": "ESP-IDF",
"compilerPath": "/home/snips/.espressif/tools/riscv32-esp-elf/esp-12.2.0_20230208/riscv32-esp-elf/bin/riscv32-esp-elf-gcc",
"compilerPath": "${config:idf.toolsPath}/tools/riscv32-esp-elf/esp-12.2.0_20230208/riscv32-esp-elf/bin/riscv32-esp-elf-gcc",
"includePath": [
"${config:idf.espIdfPath}/components/**",
"${config:idf.espIdfPathWin}/components/**",
Expand Down
4 changes: 3 additions & 1 deletion firmware/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"idf.portWin": "COM4",
"idf.flashType": "UART",
"files.associations": {
"*.php7": "php",
"esp_event.h": "c",
"array": "c",
"string": "c",
Expand All @@ -26,7 +27,8 @@
"deque": "cpp",
"vector": "cpp",
"mutex": "cpp",
"*.tcc": "cpp"
"*.tcc": "cpp",
"esp_sleep.h": "c"
},
"idf.port": "/dev/ttyACM0"
}
4 changes: 3 additions & 1 deletion firmware/main/EventParticipationTrendsFirmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef struct {
uint8_t mac[6];
} newPacket;

#define QUEUE_SIZE 5000
#define QUEUE_SIZE 1000
QueueHandle_t packetQueue;

std::mutex macs_mutex;
Expand Down Expand Up @@ -74,8 +74,10 @@ static void SendBufferAsSingleJsonArray(void*){
data += "]}";
macs.clear();
macs_mutex.unlock();
esp_wifi_set_promiscuous(false);
mqtt_publish_sensor(data.c_str());
flashLed();
esp_wifi_set_promiscuous(true);
vTaskDelete(NULL);
}

Expand Down
14 changes: 14 additions & 0 deletions firmware/main/mqtt.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "mqtt.h"
#include "utils.h"
#include "esp_sleep.h"

const char *MQTT_TAG = "MQTT_H";
static esp_mqtt_client_handle_t mqtt_client = NULL;
Expand Down Expand Up @@ -45,8 +46,16 @@ static void mqtt_event_handler(void *args, esp_event_base_t base, int32_t event_
ESP_LOGW(MQTT_TAG, "MQTT Client has disconnected... Trying to reconnect");
esp_mqtt_client_reconnect(mqtt_client);
mqtt_client_connected = false;
while (!mqtt_client_connected)
{
ESP_LOGI(MQTT_TAG, "Waiting for MQTT connection...");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
esp_mqtt_client_subscribe(mqtt_client, "/reset", 0);
esp_mqtt_client_subscribe(mqtt_client, "/sleep", 0);
break;
case MQTT_EVENT_ERROR:
esp_restart();
ESP_LOGE(MQTT_TAG, "MQTT Client has encountered an Error...");
break;
case MQTT_EVENT_DATA:
Expand All @@ -61,6 +70,10 @@ static void mqtt_event_handler(void *args, esp_event_base_t base, int32_t event_
ESP_LOGI(MQTT_TAG, "Resetting device...");
esp_restart();
}
if(strcmp(topic_buffer, "/sleep") == 0){
ESP_LOGI(MQTT_TAG, "Going to sleep...");
esp_deep_sleep_start();
}
free(topic_buffer);
free(data_buffer);
break;
Expand Down Expand Up @@ -105,5 +118,6 @@ esp_mqtt_client_handle_t INITIALIZE_MQTT(bool wait_for_connection)
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
esp_mqtt_client_subscribe(mqtt_client, "/reset", 0);
esp_mqtt_client_subscribe(mqtt_client, "/sleep", 0);
return mqtt_client;
}

0 comments on commit ad23787

Please sign in to comment.