https request works

This commit is contained in:
2020-09-13 20:34:16 -05:00
parent b5864c04e2
commit dbf96f7a62

View File

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