https request works
This commit is contained in:
52
main/main.c
52
main/main.c
@@ -29,16 +29,17 @@ static const httpd_uri_t root = {
|
|||||||
.handler = root_get_handler
|
.handler = root_get_handler
|
||||||
};
|
};
|
||||||
|
|
||||||
/* static const httpd_uri_t bme280_url = { */
|
static const httpd_uri_t bme280_uri = {
|
||||||
/* .uri = "/bme280", */
|
.uri = "/bme280",
|
||||||
/* .method = HTTP_GET, */
|
.method = HTTP_GET,
|
||||||
/* .handler = bme280_readout_handler */
|
.handler = bme280_readout_handler
|
||||||
/* }; */
|
};
|
||||||
|
|
||||||
|
|
||||||
void setup_i2c(void);
|
void setup_i2c(void);
|
||||||
int8_t init_bme280(void);
|
int8_t init_bme280(void);
|
||||||
int8_t bme280_readout();
|
int8_t bme280_readout();
|
||||||
|
int8_t lastReadoutStatus = 1;
|
||||||
extern struct bme280_dev bme280; // from bme280_sup.c
|
extern struct bme280_dev bme280; // from bme280_sup.c
|
||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
@@ -74,26 +75,26 @@ void app_main(void)
|
|||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
// take BME280 readings.
|
// take BME280 readings.
|
||||||
bme280_readout();
|
lastReadoutStatus = bme280_readout();
|
||||||
vTaskDelay(1600/portTICK_PERIOD_MS);
|
vTaskDelay(1600/portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct bme280_data comp_data;
|
struct bme280_data comp_data;
|
||||||
int8_t bme280_readout(){
|
int8_t bme280_readout(){
|
||||||
int8_t rslt;
|
int8_t ret;
|
||||||
char msgbuf[128];
|
char msgbuf[128];
|
||||||
rslt = bme280_set_sensor_mode(BME280_NORMAL_MODE, &bme280);
|
ret = bme280_set_sensor_mode(BME280_NORMAL_MODE, &bme280);
|
||||||
bme280.delay_ms(40);
|
bme280.delay_ms(40);
|
||||||
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, &bme280);
|
ret = bme280_get_sensor_data(BME280_ALL, &comp_data, &bme280);
|
||||||
if (rslt != BME280_OK)
|
if (ret != BME280_OK)
|
||||||
ESP_LOGI(TAG, "bme280_get_sensor_data() returned %d", rslt);
|
ESP_LOGI(TAG, "bme280_get_sensor_data() returned %d", ret);
|
||||||
|
|
||||||
snprintf(msgbuf, sizeof(msgbuf), "%.1f C, %0.2f hPa, %0.1f%%",
|
snprintf(msgbuf, sizeof(msgbuf), "%.1f C, %0.1f hPa, %0.1f%%",
|
||||||
comp_data.temperature, comp_data.pressure / 100., comp_data.humidity);
|
comp_data.temperature, comp_data.pressure / 100., comp_data.humidity);
|
||||||
ESP_LOGI(TAG, "%s", msgbuf);
|
ESP_LOGI(TAG, "%s", msgbuf);
|
||||||
|
|
||||||
return rslt;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t root_get_handler(httpd_req_t *req)
|
static esp_err_t root_get_handler(httpd_req_t *req)
|
||||||
@@ -106,19 +107,19 @@ static esp_err_t root_get_handler(httpd_req_t *req)
|
|||||||
|
|
||||||
static esp_err_t bme280_readout_handler(httpd_req_t *req)
|
static esp_err_t bme280_readout_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
/* char buffer[128]; */
|
char buffer[128];
|
||||||
httpd_resp_set_type(req, "text/json");
|
httpd_resp_set_type(req, "text/json");
|
||||||
|
int8_t ret = bme280_readout();
|
||||||
/* if (sht31_readTempHum()) { */
|
if (ret == 0) {
|
||||||
/* float h = sht31_readHumidity(); */
|
snprintf(buffer, sizeof(buffer), "{\"status\": \"ok\", \"sensor\": \"bme280\", \"temp\": %.1f, \"humidity\": %.1f, \"pressure\": %0.1f}",
|
||||||
/* float t = sht31_readTemperature(); */
|
comp_data.temperature, comp_data.humidity, comp_data.pressure/100.);
|
||||||
/* ESP_LOGI(TAG, "Humidity, Temp(c) : %.f, %.f", h, t); */
|
ESP_LOGI(TAG, "%s", buffer);
|
||||||
/* sprintf(buffer, "{\"sensor\": \"sht31\", \"values\": { \"humidity\": %f, \"temp\": %f, \"units\": \"celsius\" } }", h, t); */
|
httpd_resp_send(req, buffer, -1);
|
||||||
/* httpd_resp_send(req, buffer, -1); */
|
}
|
||||||
/* } else { */
|
else {
|
||||||
/* ESP_LOGI(TAG, "sht31_readTempHum : failed"); */
|
ESP_LOGE(TAG, "bme280_readout failed");
|
||||||
/* httpd_resp_send(req, "{\"error\":\"failed to read sensor\"}", -1); */
|
httpd_resp_send(req, "{\"status\": failed, \"error\": \"could not read from sensor\"}", -1);
|
||||||
/* } */
|
}
|
||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -151,6 +152,7 @@ static httpd_handle_t start_webserver(void)
|
|||||||
// Set URI handlers
|
// Set URI handlers
|
||||||
ESP_LOGI(TAG, "Registering URI handlers");
|
ESP_LOGI(TAG, "Registering URI handlers");
|
||||||
httpd_register_uri_handler(server, &root);
|
httpd_register_uri_handler(server, &root);
|
||||||
|
httpd_register_uri_handler(server, &bme280_uri);
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user