Skip to content

Instantly share code, notes, and snippets.

@cpq
Last active September 27, 2019 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpq/43ff470eedddd30d91571c8da6a58c67 to your computer and use it in GitHub Desktop.
Save cpq/43ff470eedddd30d91571c8da6a58c67 to your computer and use it in GitHub Desktop.
ESP-IDF mDash skeleton firmware code
// Copyright (c) Cesanta Software Limited
// All rights reserved
#include "main.h"
#define REPORT_FREQUENCY_MS 5000
// This function is called by the main superloop every 10ms.
// We trigger shadow update each REPORT_FREQUENCY_MS milliseconds.
// The other way to do it is to start a dedicated FreeRTOS task.
static void tick(int64_t uptime_ms) {
static int64_t next;
if (next < uptime_ms) {
next = uptime_ms - (uptime_ms % REPORT_FREQUENCY_MS) + REPORT_FREQUENCY_MS;
if (mDashGetState() == MDASH_CONNECTED) {
// Update shadow with the current values
mDashShadowUpdate("{\"state\":{\"reported\":{\"ram_free\":%lu}}}",
mDashGetFreeRam());
}
}
}
void app_main() {
mDashBegin();
// If a device recovered after a crash, do not start firmware logic
// because it could crash again, thus fall into a crash-loop and make
// a device unusable and un-recoverable.
// Instead, wait for a possible recovery action.
// Then reboot (this will change the crash reason) to try the firmware again.
if (esp_reset_reason() == ESP_RST_PANIC) {
int seconds = 120;
MLOG(LL_CRIT, "Restarting after crash. Sleeping for %d min", seconds);
vTaskDelay(seconds * 1000 / portTICK_PERIOD_MS); // Sleep
esp_restart(); // And restart again
}
// Using superloop instead of RTOS tasks
for (;;) {
vTaskDelay(10 / portTICK_PERIOD_MS); // Sleep 10ms
mDashCLI(getchar()); // Handle CLI input
tick(esp_timer_get_time() / 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment