1
0

Add mqtt server for brightness values

This commit is contained in:
Christian Loch 2021-02-11 00:31:02 +01:00
parent b9ffab5c2b
commit 2421ae31a5
4 changed files with 63 additions and 51 deletions

View File

@ -1,3 +1,3 @@
idf_component_register(SRCS "server.c"
INCLUDE_DIRS "."
REQUIRES esp_http_server freertos)
REQUIRES mqtt freertos)

View File

@ -8,63 +8,73 @@
#include "server.h"
static const char *TAG = "server";
char cur_value_str[255] = "No measurements yet!";
esp_mqtt_client_handle_t client;
void server_set_values(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw,
int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw)
static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
{
sprintf(cur_value_str, "<html><body><p>%d %d %d</p><p>%d %d %d</p></body></html>",
temp_raw, pressure_raw, humidity_raw, temp2_raw, pressure2_raw, humidity2_raw);
}
esp_mqtt_client_handle_t client = event->client;
int msg_id;
// your_context_t *context = event->context;
switch (event->event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
break;
case MQTT_EVENT_SUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data);
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
/*if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
static esp_err_t get_handler(httpd_req_t *req)
{
char* buf;
size_t buf_len;
/* Get header value string length and allocate memory for length + 1,
* extra byte for null termination */
buf_len = httpd_req_get_hdr_value_len(req, "Host") + 1;
if (buf_len > 1) {
buf = malloc(buf_len);
/* Copy null terminated value string into buffer */
if (httpd_req_get_hdr_value_str(req, "Host", buf, buf_len) == ESP_OK) {
ESP_LOGI(TAG, "Found header => Host: %s", buf);
}
free(buf);
}*/
break;
default:
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
break;
}
const char* resp_str = cur_value_str;
httpd_resp_send(req, resp_str, strlen(resp_str));
return ESP_OK;
}
static httpd_uri_t uri = {
.uri = "/",
.method = HTTP_GET,
.handler = get_handler,
};
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
mqtt_event_handler_cb(event_data);
}
static httpd_handle_t start_webserver(void)
void send_mqtt_update(double lux, const char* topic)
{
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
// Start the httpd server
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
// Set URI handlers
ESP_LOGI(TAG, "Registering URI handlers");
httpd_register_uri_handler(server, &uri);
return server;
}
ESP_LOGI(TAG, "Error starting server!");
return NULL;
char lux_str[317] = "\0";
snprintf(lux_str, 317, "%f", lux);
esp_mqtt_client_publish(client, topic, lux_str, 0, 0, 0);
}
void init_server()
{
start_webserver();
//start_webserver();
esp_mqtt_client_config_t mqtt_cfg = {
.uri = "mqtt://192.168.0.101",
};
client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
esp_mqtt_client_start(client);
}

View File

@ -13,11 +13,11 @@
#include <freertos/task.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_http_server.h>
#include <sys/param.h>
#include "mqtt_client.h"
void init_server();
void server_set_values(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw,
int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw);
void send_mqtt_update(double lux, const char* topic);
#endif /* COMPONENTS_SERVER_SERVER_H_ */

View File

@ -31,8 +31,10 @@ void app_main(void)
get_time(&time);
read_sensor(&temp, &pressure, &humidity);
read_sensor2(&temp2, &pressure2, &humidity2);
server_set_values(temp, pressure, humidity, temp2, pressure2, humidity2);
//server_set_values(temp, pressure, humidity, temp2, pressure2, humidity2);
update_data(temp, pressure, humidity, temp2, pressure2, humidity2, time);
double lux = read_light_sensor();
send_mqtt_update(lux, "wzHelligkeit");
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}