Just formatting
This commit is contained in:
5
.clang-format
Normal file
5
.clang-format
Normal file
@@ -0,0 +1,5 @@
|
||||
BasedOnStyle: LLVM
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: Never
|
||||
|
||||
@@ -3,22 +3,22 @@
|
||||
Sends: start_ms, end_ms, [max, mean+std, mean, mean-std, min]
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#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"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_adc/adc_cali.h"
|
||||
#include "esp_adc/adc_oneshot.h"
|
||||
#include "esp_adc/adc_cali_scheme.h"
|
||||
#include "esp_adc/adc_oneshot.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_http_client.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/task.h"
|
||||
#include "math.h"
|
||||
#include "nvs_flash.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
/* ===================== CONSTS ===================== */
|
||||
|
||||
@@ -29,8 +29,8 @@
|
||||
#define CHANNEL ADC_CHANNEL_6
|
||||
#define CALIBRATION_FACTOR 1600.0f
|
||||
|
||||
#define EXAMPLE_ESP_WIFI_SSID "Jaguar"
|
||||
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
|
||||
#define EXAMPLE_ESP_WIFI_SSID "Jaguar"
|
||||
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
|
||||
|
||||
#define ESP_WIFI_SAE_MODE WPA3_SAE_PWE_BOTH
|
||||
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
|
||||
@@ -41,13 +41,12 @@ static const char *TAG = "VoltageSensor";
|
||||
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
static int s_retry_num = 0;
|
||||
|
||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data) {
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
} else if (event_base == WIFI_EVENT &&
|
||||
@@ -58,15 +57,13 @@ static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
} else {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
} else if (event_base == IP_EVENT &&
|
||||
event_id == IP_EVENT_STA_GOT_IP) {
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_init_sta(void)
|
||||
{
|
||||
void wifi_init_sta(void) {
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
@@ -78,27 +75,25 @@ void wifi_init_sta(void)
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
||||
WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL,
|
||||
&instance_any_id));
|
||||
WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
||||
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL,
|
||||
&instance_got_ip));
|
||||
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = EXAMPLE_ESP_WIFI_SSID,
|
||||
.password = EXAMPLE_ESP_WIFI_PASS,
|
||||
.threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
|
||||
.sae_pwe_h2e = ESP_WIFI_SAE_MODE,
|
||||
},
|
||||
.sta =
|
||||
{
|
||||
.ssid = EXAMPLE_ESP_WIFI_SSID,
|
||||
.password = EXAMPLE_ESP_WIFI_PASS,
|
||||
.threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
|
||||
.sae_pwe_h2e = ESP_WIFI_SAE_MODE,
|
||||
},
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
}
|
||||
|
||||
@@ -108,8 +103,7 @@ 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)
|
||||
{
|
||||
static void adc_init(void) {
|
||||
adc_oneshot_unit_init_cfg_t unit_cfg = {
|
||||
.unit_id = ADC_UNIT_1,
|
||||
.ulp_mode = ADC_ULP_MODE_DISABLE,
|
||||
@@ -137,36 +131,32 @@ static void adc_init(void)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const double upper = 1931.009445512987;
|
||||
const double lower = 1371.4123048766235;
|
||||
const double nominal_v = 230.0;
|
||||
|
||||
static float read_voltage(void)
|
||||
{
|
||||
static float read_voltage(void) {
|
||||
int raw, 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));
|
||||
}
|
||||
|
||||
// return (float)mv;
|
||||
// return (float)mv;
|
||||
return (mv - lower) / (upper - lower) * 2.0 * nominal_v - nominal_v;
|
||||
}
|
||||
|
||||
/* ===================== HTTP ===================== */
|
||||
|
||||
static void send_summary(esp_http_client_handle_t client,
|
||||
double start_ms, double end_ms,
|
||||
double values[5])
|
||||
{
|
||||
static void send_summary(esp_http_client_handle_t client, double start_ms,
|
||||
double end_ms, double values[5]) {
|
||||
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]);
|
||||
"{\"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,8 +164,7 @@ static void send_summary(esp_http_client_handle_t client,
|
||||
|
||||
/* ===================== TASK ===================== */
|
||||
|
||||
static void measure_task(void *arg)
|
||||
{
|
||||
static void measure_task(void *arg) {
|
||||
esp_http_client_config_t cfg = {
|
||||
.url = SERVER_URL,
|
||||
.method = HTTP_METHOD_POST,
|
||||
@@ -185,49 +174,49 @@ static void measure_task(void *arg)
|
||||
esp_http_client_set_header(client, "Content-Type", "application/json");
|
||||
|
||||
while (1) {
|
||||
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;
|
||||
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;
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(2));
|
||||
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));
|
||||
}
|
||||
|
||||
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 ===================== */
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
void app_main(void) {
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
wifi_init_sta();
|
||||
adc_init();
|
||||
|
||||
xTaskCreate(measure_task, "measure", 4096, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user