Skip to content

Instantly share code, notes, and snippets.

@cpq
Created April 29, 2025 14:20
Show Gist options
  • Save cpq/e59fe469381e0bbb38dafa5c4e1d3d96 to your computer and use it in GitHub Desktop.
Save cpq/e59fe469381e0bbb38dafa5c4e1d3d96 to your computer and use it in GitHub Desktop.
ESP32C6 dashboard
// SPDX-FileCopyrightText: 2024 Cesanta Software Limited
// SPDX-License-Identifier: GPL-2.0-only or commercial
// Generated by Mongoose Wizard, https://mongoose.ws/wizard/
#include "mongoose_glue.h"
#include "wifi.h"
#include "driver/gpio.h"
#include "driver/temperature_sensor.h"
#include "nvs_flash.h"
temperature_sensor_handle_t temp_sensor = NULL;
nvs_handle_t nvs_h;
extern void wifi_init(const char *ssid, const char *pass);
#define LED1 GPIO_NUM_15
void my_get_leds(struct leds *leds) {
leds->led1 = !gpio_get_level(LED1);
}
void my_set_leds(struct leds *leds) {
gpio_set_level(LED1, !leds->led1);
}
void my_get_state(struct state *state) {
float t = 0;
ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor, &t));
state->temperature = t;
state->ram_total = heap_caps_get_total_size(MALLOC_CAP_DEFAULT);
state->ram_used = heap_caps_get_free_size(MALLOC_CAP_DEFAULT);
}
void my_get_settings(struct settings *settings) {
size_t size = sizeof(settings->device_name);
if (nvs_get_str(nvs_h, "dn", settings->device_name, &size) != ESP_OK) {
strncpy(settings->device_name, "MyDevice", sizeof(settings->device_name));
}
}
void my_set_settings(struct settings *settings) {
nvs_set_str(nvs_h, "dn", settings->device_name);
nvs_commit(nvs_h);
}
void app_main() {
wifi_init(WIFI_SSID, WIFI_PASS); // This blocks until connected
mongoose_init();
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << LED1), // Select the pin
.mode = GPIO_MODE_INPUT_OUTPUT, // Set as output mode
.pull_up_en = GPIO_PULLUP_DISABLE, // No pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // No pull-down
.intr_type = GPIO_INTR_DISABLE, // No interrupts
};
gpio_config(&io_conf);
mongoose_set_http_handlers("leds", my_get_leds, my_set_leds);
temperature_sensor_config_t tc = TEMPERATURE_SENSOR_CONFIG_DEFAULT(0, 70);
temperature_sensor_install(&tc, &temp_sensor);
temperature_sensor_enable(temp_sensor);
mongoose_set_http_handlers("state", my_get_state, NULL);
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(nvs_open("nvs", NVS_READWRITE, &nvs_h));
mongoose_set_http_handlers("settings", my_get_settings, my_set_settings);
for (;;) {
mongoose_poll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment