server + getting sensor id + json in esp32
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
idf_component_register(SRCS "thermo_sensor.c" "ygg_http_routes.c" "ygg_bmp280.c" "../../common/esp32/ygg_wifi.c" "../../common/esp32/ygg_http_server.c"
|
||||
idf_component_register(SRCS "thermo_sensor.c" "ygg_http_routes.c" "ygg_bmp280.c" "ygg_http_req.c" "../../common/esp32/ygg_wifi.c" "../../common/esp32/ygg_http_server.c"
|
||||
INCLUDE_DIRS "." "../../common/esp32")
|
||||
|
||||
@@ -15,3 +15,4 @@ dependencies:
|
||||
# # All dependencies of `main` are public by default.
|
||||
# public: true
|
||||
esp-idf-lib/bmp280: '*'
|
||||
espressif/cjson: '*'
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#include <freertos/task.h>
|
||||
#include <esp_system.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
@@ -11,6 +10,7 @@
|
||||
#include "ygg_wifi.h"
|
||||
#include "ygg_http_server.h"
|
||||
#include "ygg_bmp280.h"
|
||||
#include "ygg_http_req.h"
|
||||
|
||||
|
||||
static const char *TAG = "thermo_sensor";
|
||||
@@ -21,8 +21,11 @@ bmp280_t dev_bmp_280;
|
||||
|
||||
void main_loop(void *pvParameters) {
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
vTaskDelay(pdMS_TO_TICKS(10000));
|
||||
ygg_bmp280_read_sensor(&dev_bmp_280);
|
||||
ygg_send_sensor_redouts(last_temperature,
|
||||
last_pressure,
|
||||
last_humidity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "cJSON.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_http_client.h"
|
||||
#include "ygg_http_req.h"
|
||||
|
||||
#define GET_RESP_MAX_SIZE 4096
|
||||
|
||||
static const char *TAG = "YGG_HTTP_REQ";
|
||||
static char GET_RESP[GET_RESP_MAX_SIZE];
|
||||
|
||||
esp_err_t _http_event_handler(esp_http_client_event_t *evt) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
char* ygg_send_get(const char *url) {
|
||||
size_t total = 0;
|
||||
|
||||
esp_http_client_config_t config = {
|
||||
.url = url,
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
esp_err_t err = esp_http_client_open(client, 0);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "HTTP open error: %s", esp_err_to_name(err));
|
||||
esp_http_client_cleanup(client);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
esp_http_client_fetch_headers(client);
|
||||
|
||||
while (total < GET_RESP_MAX_SIZE - 1) {
|
||||
int len = esp_http_client_read(
|
||||
client,
|
||||
GET_RESP + total,
|
||||
GET_RESP_MAX_SIZE - 1 - total
|
||||
);
|
||||
if (len <= 0) {
|
||||
break;
|
||||
}
|
||||
total += len;
|
||||
}
|
||||
GET_RESP[total] = '\0';
|
||||
|
||||
ESP_LOGI(TAG, "Status HTTP = %d",
|
||||
esp_http_client_get_status_code(client));
|
||||
|
||||
ESP_LOGI(TAG, "Response: %s", GET_RESP);
|
||||
|
||||
esp_http_client_close(client);
|
||||
esp_http_client_cleanup(client);
|
||||
|
||||
return GET_RESP;
|
||||
}
|
||||
|
||||
void ygg_send_post(const char *url, char *post_data) {
|
||||
esp_http_client_config_t config = {
|
||||
.url = url,
|
||||
.event_handler = _http_event_handler,
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
|
||||
esp_http_client_set_method(client, HTTP_METHOD_POST);
|
||||
esp_http_client_set_header(client, "Content-Type", "application/json");
|
||||
esp_http_client_set_post_field(client, post_data, strlen(post_data));
|
||||
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Status HTTP = %d, Content-Length = %"PRId64,
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP connection error: %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
int ygg_get_sensor_id(const char *sensor_name,
|
||||
const char *url);
|
||||
|
||||
void ygg_send_sensor_redouts(float temp, float press, float hum) {
|
||||
const char *url = "http://192.168.0.236:5000/api/sensors/1/readouts";
|
||||
char post_data[64];
|
||||
|
||||
int sensor_id = ygg_get_sensor_id("bmp280", "http://192.168.0.236:5000");
|
||||
|
||||
snprintf(post_data, sizeof(post_data),
|
||||
"{\"type\":\"temperature\", \"value\":%.2f}", temp);
|
||||
ygg_send_post(url, post_data);
|
||||
|
||||
snprintf(post_data, sizeof(post_data),
|
||||
"{\"type\":\"pressure\", \"value\":%.2f}", press);
|
||||
ygg_send_post(url, post_data);
|
||||
|
||||
snprintf(post_data, sizeof(post_data),
|
||||
"{\"type\":\"humidity\", \"value\":%.2f}", hum);
|
||||
ygg_send_post(url, post_data);
|
||||
}
|
||||
|
||||
char* ygg_get_sensors(const char *base_url) {
|
||||
char url[128];
|
||||
snprintf(url, sizeof(url),
|
||||
"%s/api/sensors", base_url);
|
||||
char *sensors = ygg_send_get(url);
|
||||
return sensors;
|
||||
}
|
||||
|
||||
int ygg_find_sensor(char *sensors, const char *sensor_name) {
|
||||
cJSON *root = cJSON_Parse(sensors);
|
||||
cJSON *item;
|
||||
|
||||
cJSON_ArrayForEach(item, root) {
|
||||
cJSON *id = cJSON_GetObjectItem(item, "id");
|
||||
cJSON *item_name = cJSON_GetObjectItem(item, "name");
|
||||
|
||||
if (cJSON_IsNumber(id) &&
|
||||
cJSON_IsString(item_name) &&
|
||||
strcmp(item_name->valuestring, sensor_name) == 0) {
|
||||
|
||||
int result = id->valueint;
|
||||
cJSON_Delete(root);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ygg_get_sensor_id(const char *sensor_name,
|
||||
const char *url) {
|
||||
char *sensors = ygg_get_sensors(url);
|
||||
int id = ygg_find_sensor(sensors, sensor_name);
|
||||
ESP_LOGI(TAG, "Sensor ID: %d", id);
|
||||
return 0;
|
||||
//if (id == -1) {
|
||||
// id = ygg_add_sensor(url, sensor_name);
|
||||
//}
|
||||
//return id;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef YGG_HTTP_REQ_H
|
||||
#define YGG_HTTP_REQ_H
|
||||
|
||||
void ygg_send_sensor_redouts(float temp, float press, float hum);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user