Mid progress
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
idf_component_register(SRCS "station_example_main.c"
|
||||
PRIV_REQUIRES esp_wifi nvs_flash esp_adc esp_http_client
|
||||
PRIV_REQUIRES esp_wifi nvs_flash esp_adc esp_http_client esp_timer
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
Voltage measurement device over WiFi (batched, ~30 Hz with jitter)
|
||||
Voltage summary device (~20 Hz, 1000 samples)
|
||||
Sends: start_ms, end_ms, [max, mean+std, mean, mean-std, min]
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
@@ -7,6 +8,8 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include <time.h>
|
||||
#include "math.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
@@ -15,14 +18,13 @@
|
||||
#include "esp_adc/adc_oneshot.h"
|
||||
#include "esp_adc/adc_cali_scheme.h"
|
||||
#include "esp_http_client.h"
|
||||
#include "esp_timer.h"
|
||||
|
||||
/* ===================== CONSTS ===================== */
|
||||
|
||||
#define SERVER_URL "http://192.168.178.157:8080/data"
|
||||
|
||||
#define SAMPLE_RATE_HZ 20
|
||||
#define SAMPLE_PERIOD_MS (1000 / SAMPLE_RATE_HZ)
|
||||
#define BATCH_SIZE 100
|
||||
#define NUM_MEASUREMENTS 100000
|
||||
|
||||
#define CHANNEL ADC_CHANNEL_6
|
||||
#define CALIBRATION_FACTOR 1600.0f
|
||||
@@ -38,7 +40,6 @@ static const char *TAG = "VoltageSensor";
|
||||
/* ===================== WIFI ===================== */
|
||||
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
@@ -67,7 +68,6 @@ static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
void wifi_init_sta(void)
|
||||
{
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
@@ -137,6 +137,9 @@ static void adc_init(void)
|
||||
}
|
||||
}
|
||||
|
||||
const double upper = 0.24107260247486464 * CALIBRATION_FACTOR + CALIBRATION_FACTOR;
|
||||
const double lower = -0.2247819992266048 * CALIBRATION_FACTOR + CALIBRATION_FACTOR;
|
||||
|
||||
static float read_voltage(void)
|
||||
{
|
||||
int raw, mv = 0;
|
||||
@@ -144,24 +147,24 @@ static float read_voltage(void)
|
||||
if (cali_enabled) {
|
||||
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(cali_handle, raw, &mv));
|
||||
}
|
||||
return (mv - CALIBRATION_FACTOR) / CALIBRATION_FACTOR;
|
||||
|
||||
return (float)mv;
|
||||
// return (mv - lower) / (upper - lower) * 2.0 * 325.0 - 325.0;
|
||||
}
|
||||
|
||||
/* ===================== HTTP ===================== */
|
||||
|
||||
static void send_voltage_batch(esp_http_client_handle_t client,
|
||||
const float *data,
|
||||
size_t n)
|
||||
static void send_summary(esp_http_client_handle_t client,
|
||||
double start_ms, double end_ms,
|
||||
double values[5])
|
||||
{
|
||||
char payload[1024];
|
||||
char *p = payload;
|
||||
|
||||
p += sprintf(p, "{\"voltages\":[");
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
p += sprintf(p, "%.4f%s", data[i],
|
||||
(i + 1 < n) ? "," : "");
|
||||
}
|
||||
p += sprintf(p, "]}");
|
||||
char payload[256];
|
||||
snprintf(payload, sizeof(payload),
|
||||
"{\"start_ms\":%.0f,"
|
||||
"\"end_ms\":%.0f,"
|
||||
"\"values\":[%.6f,%.6f,%.6f,%.6f,%.6f]}",
|
||||
start_ms, end_ms,
|
||||
values[0], values[1], values[2], values[3], values[4]);
|
||||
|
||||
esp_http_client_set_post_field(client, payload, strlen(payload));
|
||||
esp_http_client_perform(client);
|
||||
@@ -174,28 +177,45 @@ static void measure_task(void *arg)
|
||||
esp_http_client_config_t cfg = {
|
||||
.url = SERVER_URL,
|
||||
.method = HTTP_METHOD_POST,
|
||||
.timeout_ms = 3000,
|
||||
.timeout_ms = 5000,
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&cfg);
|
||||
esp_http_client_set_header(client, "Content-Type", "application/json");
|
||||
|
||||
float batch[BATCH_SIZE];
|
||||
size_t count = 0;
|
||||
|
||||
TickType_t last_wake = xTaskGetTickCount();
|
||||
|
||||
while (1) {
|
||||
batch[count++] = read_voltage();
|
||||
double sum = 0, ssum = 0, max = -1e9, min = 1e9;
|
||||
int i = 0;
|
||||
double start_us = esp_timer_get_time();
|
||||
double start_ms = start_us / 1000.0;
|
||||
|
||||
if (count == BATCH_SIZE) {
|
||||
send_voltage_batch(client, batch, count);
|
||||
count = 0;
|
||||
}
|
||||
for (i = 0; i < NUM_MEASUREMENTS; i++) {
|
||||
float m = read_voltage();
|
||||
sum += m;
|
||||
ssum += m * m;
|
||||
if (m > max) max = m;
|
||||
if (m < min) min = m;
|
||||
|
||||
int jitter = rand() % 41; // 0..40 ms
|
||||
vTaskDelayUntil(&last_wake,
|
||||
pdMS_TO_TICKS(SAMPLE_PERIOD_MS + jitter));
|
||||
vTaskDelay(pdMS_TO_TICKS(2));
|
||||
}
|
||||
|
||||
double end_us = esp_timer_get_time();
|
||||
double end_ms = end_us / 1000.0;
|
||||
|
||||
double mean = sum / NUM_MEASUREMENTS;
|
||||
double variance = (ssum / NUM_MEASUREMENTS) - (mean * mean);
|
||||
double std = variance > 0 ? sqrt(variance) : 0;
|
||||
|
||||
double values[5];
|
||||
values[0] = max;
|
||||
values[1] = mean + std;
|
||||
values[2] = mean;
|
||||
values[3] = mean - std;
|
||||
values[4] = min;
|
||||
|
||||
send_summary(client, start_ms, end_ms, values);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
}
|
||||
}
|
||||
|
||||
/* ===================== MAIN ===================== */
|
||||
|
||||
Reference in New Issue
Block a user