Delete some of the sdkconfig that I think are not needed anymore also made basic measurements working

- Still todo is the real calibration and testing
- The continous logging to the server works
This commit is contained in:
2025-12-20 20:22:59 +01:00
parent 57b81d01b1
commit 0efab1c8b3
10 changed files with 101 additions and 38 deletions

View File

@@ -1,12 +1,9 @@
/* WiFi station Example
/*
Voltage measurement device over wifi from WiFi station Example
*/
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
@@ -22,9 +19,19 @@
#include "esp_adc/adc_continuous.h"
#include "esp_adc/adc_cali.h"
#include "esp_adc/adc_oneshot.h"
#include "esp_adc/adc_cali.h"
#include "esp_adc/adc_cali_scheme.h"
#include "esp_http_client.h"
/* ===================== CONSTS ===================== */
#define SERVER_URL "http://192.168.178.157:8080/data"
#define RMS_WINDOW_MS 300
#define CHANNEL ADC_CHANNEL_6
// TODO: Determine this
#define CALIBRATION_FACTOR 1650.0f
/* The examples use WiFi configuration that you can set via project configuration menu
@@ -63,6 +70,11 @@
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
#endif
// Tag for logging
static const char *TAG = "VoltageSensor";
/* ===================== WIFI ===================== */
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
@@ -72,11 +84,9 @@ static EventGroupHandle_t s_wifi_event_group;
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
static const char *TAG = "wifi station";
static int s_retry_num = 0;
// Internal wifi event_handler
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
@@ -99,6 +109,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
}
}
// Initialize the wifi
void wifi_init_sta(void)
{
s_wifi_event_group = xEventGroupCreate();
@@ -165,31 +176,95 @@ void wifi_init_sta(void)
}
}
void send_voltage(float v)
// Function to send the voltage to a remote server
void send_voltage(esp_http_client_handle_t client, float v)
{
// Simple payload for the go server
char payload[64];
snprintf(payload, sizeof(payload),
"{\"voltage\":%.2f}", v);
snprintf(payload, sizeof(payload), "{\"voltage\":%.2f}", v);
esp_http_client_set_post_field(client, payload, strlen(payload));
esp_http_client_perform(client);
}
/* ===================== ADC ===================== */
static adc_oneshot_unit_handle_t adc_handle;
static adc_cali_handle_t cali_handle;
static bool cali_enabled = false;
static void adc_init(void)
{
// Setup oneshot
adc_oneshot_unit_init_cfg_t unit_cfg = {
.unit_id = ADC_UNIT_1,
.ulp_mode = ADC_ULP_MODE_DISABLE,
};
ESP_ERROR_CHECK(adc_oneshot_new_unit(&unit_cfg, &adc_handle));
adc_oneshot_chan_cfg_t chan_cfg = {
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_DEFAULT,
};
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, CHANNEL, &chan_cfg));
// Calibration of the adc
adc_cali_scheme_ver_t scheme;
if (adc_cali_check_scheme(&scheme) != ESP_OK) {
ESP_LOGW(TAG, "ADC calibration not supported");
return;
}
if (scheme & ADC_CALI_SCHEME_VER_LINE_FITTING) {
adc_cali_line_fitting_config_t cfg = {
.unit_id = ADC_UNIT_1,
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_DEFAULT,
};
ESP_ERROR_CHECK(adc_cali_create_scheme_line_fitting(&cfg, &cali_handle));
cali_enabled = true;
}
}
static float read_voltage_mv(void)
{
int raw;
int mv = 0;
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, CHANNEL, &raw));
if (cali_enabled) {
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(cali_handle, raw, &mv));
}
ESP_LOGI(TAG, "Raw measurement from sensor to calibrated value: %d -> %d", raw, mv);
return (mv - CALIBRATION_FACTOR) / CALIBRATION_FACTOR;
}
/* ===================== MAIN ===================== */
static void measure_task(void *arg)
{
// Setup client to be reused
esp_http_client_config_t cfg = {
.url = SERVER_URL,
.method = HTTP_METHOD_POST,
.timeout_ms = 2000,
};
esp_http_client_handle_t client = esp_http_client_init(&cfg);
esp_http_client_set_header(client, "Content-Type", "application/json");
esp_http_client_handle_t client =
esp_http_client_init(&cfg);
// Main measure loop
while (1) {
float v = read_voltage_mv();
vTaskDelay(pdMS_TO_TICKS(50)); // Does this do anything?
send_voltage(client, v);
vTaskDelay(pdMS_TO_TICKS(RMS_WINDOW_MS));
}
esp_http_client_set_header(
client, "Content-Type", "application/json");
esp_http_client_set_post_field(
client, payload, strlen(payload));
esp_http_client_perform(client);
// Cleanup after task
esp_http_client_cleanup(client);
}
void app_main(void)
{
//Initialize NVS
@@ -208,5 +283,8 @@ void app_main(void)
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta();
send_voltage(0.5);
adc_init();
ESP_LOGI(TAG, "Start measure");
xTaskCreate(measure_task, "measure", 4096, NULL, 5, NULL);
}

View File

@@ -1,2 +0,0 @@
CONFIG_IDF_TARGET="esp32c3"
CONFIG_ESP32C3_REV_MIN_101=y

View File

@@ -1 +0,0 @@
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=y

View File

@@ -1,2 +0,0 @@
CONFIG_IDF_TARGET="esp32c2"
CONFIG_XTAL_FREQ_26=y

View File

@@ -1,3 +0,0 @@
CONFIG_IDF_TARGET="esp32c2"
CONFIG_XTAL_FREQ_26=y
CONFIG_ESP32C2_REV_MIN_200=y

View File

@@ -1 +0,0 @@
CONFIG_ESP_WIFI_11R_SUPPORT=y

View File

@@ -1,2 +0,0 @@
CONFIG_ESP_WIFI_11KV_SUPPORT=y
CONFIG_ESP_WIFI_RRM_SUPPORT=y

View File

@@ -1,2 +0,0 @@
CONFIG_ESP_WIFI_11KV_SUPPORT=y
CONFIG_ESP_WIFI_WNM_SUPPORT=y

View File

@@ -1 +0,0 @@
CONFIG_ESP_WIFI_ENABLE_WPA3_SAE=n

View File

@@ -1 +0,0 @@
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n