Compare commits
2 Commits
42af035ae6
...
2421ae31a5
Author | SHA1 | Date | |
---|---|---|---|
2421ae31a5 | |||
b9ffab5c2b |
@ -122,6 +122,17 @@ void read_sensor2(int32_t* temp, uint32_t* pressure, uint32_t* humidity) {
|
||||
*humidity = comp_data.humidity;
|
||||
}
|
||||
|
||||
double read_light_sensor() {
|
||||
uint8_t raw_high = 0;
|
||||
|
||||
i2c_read(0x4a, 0x03, &raw_high, 1);
|
||||
|
||||
uint8_t exp = raw_high >> 4;
|
||||
uint8_t mant = raw_high & 0b00001111;
|
||||
|
||||
return pow(2, exp) * mant * 0.72;
|
||||
}
|
||||
|
||||
void init_sensors()
|
||||
{
|
||||
// INIT SENSOR
|
||||
|
@ -11,9 +11,11 @@
|
||||
#include "bme280.h"
|
||||
#include "bme280_defs.h"
|
||||
#include "driver/i2c.h"
|
||||
#include "math.h"
|
||||
|
||||
void init_sensors();
|
||||
void read_sensor2(int32_t* temp, uint32_t* pressure, uint32_t* humidity);
|
||||
void read_sensor(int32_t* temp, uint32_t* pressure, uint32_t* humidity);
|
||||
double read_light_sensor();
|
||||
|
||||
#endif /* COMPONENTS_SENSORS_SENSORS_H_ */
|
||||
|
@ -1,3 +1,3 @@
|
||||
idf_component_register(SRCS "server.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES esp_http_server freertos)
|
||||
REQUIRES mqtt freertos)
|
@ -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);
|
||||
}
|
||||
|
@ -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_ */
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user