Ran clang-format
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#include "dht22.h"
|
#include "dht22.h"
|
||||||
#include "sdkconfig.h"
|
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "sdkconfig.h"
|
||||||
|
|
||||||
#define DATA_GPIO CONFIG_DHT22_DATA_GPIO
|
#define DATA_GPIO CONFIG_DHT22_DATA_GPIO
|
||||||
|
|
||||||
@@ -27,7 +27,8 @@ dht22_error dht22_read() {
|
|||||||
|
|
||||||
// TODO: Actually figure out how to read the data from the chip.
|
// TODO: Actually figure out how to read the data from the chip.
|
||||||
|
|
||||||
// TODO test that data[x] << 8 will be promoted to 16bits otherwise I'm shifting them to just zeros
|
// TODO test that data[x] << 8 will be promoted to 16bits otherwise I'm
|
||||||
|
// shifting them to just zeros
|
||||||
_dht22_relative_humidity = data[0] << 8 | data[1];
|
_dht22_relative_humidity = data[0] << 8 | data[1];
|
||||||
_dht22_temperature = (data[2] & 0x7f) << 8 | data[3];
|
_dht22_temperature = (data[2] & 0x7f) << 8 | data[3];
|
||||||
if (data[2] & 0x80) {
|
if (data[2] & 0x80) {
|
||||||
@@ -43,9 +44,5 @@ dht22_error dht22_read() {
|
|||||||
return DHT22_OK;
|
return DHT22_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
deci_percent dht22_relative_humidity() {
|
deci_percent dht22_relative_humidity() { return _dht22_relative_humidity; }
|
||||||
return _dht22_relative_humidity;
|
deci_degrees_c dht22_temperature() { return _dht22_temperature; }
|
||||||
}
|
|
||||||
deci_degrees_c dht22_temperature() {
|
|
||||||
return _dht22_temperature;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -15,5 +15,3 @@ void dht22_handle_error();
|
|||||||
deci_percent dht22_relative_humidity();
|
deci_percent dht22_relative_humidity();
|
||||||
deci_degrees_c dht22_temperature();
|
deci_degrees_c dht22_temperature();
|
||||||
dht22_error dht22_read();
|
dht22_error dht22_read();
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#include "wifi.h"
|
#include "wifi.h"
|
||||||
#include <string.h>
|
|
||||||
#include "freertos/FreeRTOS.h"
|
|
||||||
#include "esp_sntp.h"
|
|
||||||
#include "esp_wifi.h"
|
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_sntp.h"
|
||||||
|
#include "esp_wifi.h"
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
static const char *TAG = "WIFI";
|
static const char *TAG = "WIFI";
|
||||||
|
|
||||||
@@ -13,22 +13,18 @@ static bool ntp_time_obtained = false;
|
|||||||
|
|
||||||
#define WIFI_CONNECTED_BIT (1 << 0)
|
#define WIFI_CONNECTED_BIT (1 << 0)
|
||||||
|
|
||||||
bool has_ntp_time_obtained_once() {
|
bool has_ntp_time_obtained_once() { return ntp_time_obtained; }
|
||||||
return ntp_time_obtained;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void event_handler(void *arg, esp_event_base_t event_base,
|
static void event_handler(void *arg, esp_event_base_t event_base,
|
||||||
int32_t event_id, void* event_data)
|
int32_t event_id, void *event_data) {
|
||||||
{
|
|
||||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||||
esp_wifi_connect();
|
esp_wifi_connect();
|
||||||
ESP_LOGI(TAG, "connecting...");
|
ESP_LOGI(TAG, "connecting...");
|
||||||
}
|
} else if (event_base == WIFI_EVENT &&
|
||||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||||
esp_wifi_connect();
|
esp_wifi_connect();
|
||||||
ESP_LOGI(TAG, "connect to the AP fail - trying to reconnect");
|
ESP_LOGI(TAG, "connect to the AP fail - trying to reconnect");
|
||||||
}
|
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
||||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
||||||
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||||
@@ -48,19 +44,14 @@ void start_wifi() {
|
|||||||
|
|
||||||
esp_event_handler_instance_t instance_any_id;
|
esp_event_handler_instance_t instance_any_id;
|
||||||
esp_event_handler_instance_t instance_got_ip;
|
esp_event_handler_instance_t instance_got_ip;
|
||||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
||||||
ESP_EVENT_ANY_ID,
|
WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
|
||||||
&event_handler,
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
||||||
NULL,
|
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
|
||||||
&instance_any_id));
|
|
||||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
|
||||||
IP_EVENT_STA_GOT_IP,
|
|
||||||
&event_handler,
|
|
||||||
NULL,
|
|
||||||
&instance_got_ip));
|
|
||||||
|
|
||||||
wifi_config_t wifi_config = {
|
wifi_config_t wifi_config = {
|
||||||
.sta = {
|
.sta =
|
||||||
|
{
|
||||||
.ssid = CONFIG_WIFI_SSID,
|
.ssid = CONFIG_WIFI_SSID,
|
||||||
.password = CONFIG_WIFI_PASSWORD,
|
.password = CONFIG_WIFI_PASSWORD,
|
||||||
.failure_retry_cnt = 20,
|
.failure_retry_cnt = 20,
|
||||||
@@ -73,7 +64,8 @@ void start_wifi() {
|
|||||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
||||||
|
|
||||||
// Wait for WiFi to connect
|
// Wait for WiFi to connect
|
||||||
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdFALSE,
|
||||||
|
portMAX_DELAY);
|
||||||
esp_sntp_setservername(0, "pool.ntp.org");
|
esp_sntp_setservername(0, "pool.ntp.org");
|
||||||
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||||
sntp_set_sync_status(SNTP_SYNC_STATUS_RESET);
|
sntp_set_sync_status(SNTP_SYNC_STATUS_RESET);
|
||||||
@@ -83,7 +75,6 @@ void start_wifi() {
|
|||||||
setenv("TZ", "GMT0BST,M3.5.0/1,M10.5.0", 1);
|
setenv("TZ", "GMT0BST,M3.5.0/1,M10.5.0", 1);
|
||||||
tzset();
|
tzset();
|
||||||
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < 20; ++i) {
|
for (uint32_t i = 0; i < 20; ++i) {
|
||||||
if (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) {
|
if (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) {
|
||||||
ESP_LOGI(TAG, "... waiting for time update");
|
ESP_LOGI(TAG, "... waiting for time update");
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
// The idea with this function is to check if NTP time has been obtained
|
// The idea with this function is to check if NTP time has been obtained
|
||||||
// at least once - and therefore time is at least vaguely accurate
|
// at least once - and therefore time is at least vaguely accurate
|
||||||
// I want to call this when saving data to know whether the time is valid/trusted
|
// I want to call this when saving data to know whether the time is
|
||||||
// at all
|
// valid/trusted at all
|
||||||
bool has_ntp_time_obtained_once();
|
bool has_ntp_time_obtained_once();
|
||||||
|
|
||||||
// Probably should rename this to "start networking" or something because
|
// Probably should rename this to "start networking" or something because
|
||||||
|
|||||||
43
main/main.c
43
main/main.c
@@ -5,8 +5,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "dht22.h"
|
#include "dht22.h"
|
||||||
#include "sdkconfig.h"
|
|
||||||
#include "wifi.h"
|
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_sntp.h"
|
#include "esp_sntp.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
@@ -14,6 +12,8 @@
|
|||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include "portmacro.h"
|
#include "portmacro.h"
|
||||||
|
#include "sdkconfig.h"
|
||||||
|
#include "wifi.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -38,9 +38,7 @@ typedef struct {
|
|||||||
#ifdef CONFIG_PMS5003_ENABLED
|
#ifdef CONFIG_PMS5003_ENABLED
|
||||||
#include "pms5003.h"
|
#include "pms5003.h"
|
||||||
|
|
||||||
void
|
void read_from_pms5003() {
|
||||||
read_from_pms5003()
|
|
||||||
{
|
|
||||||
while (run_pms5003) {
|
while (run_pms5003) {
|
||||||
// TODO: actually collect some data
|
// TODO: actually collect some data
|
||||||
ESP_LOGI(TAG, "Got PMS5003 data!");
|
ESP_LOGI(TAG, "Got PMS5003 data!");
|
||||||
@@ -79,14 +77,16 @@ void read_from_dht22() {
|
|||||||
latest_datapoint.rh = dht22_relative_humidity();
|
latest_datapoint.rh = dht22_relative_humidity();
|
||||||
|
|
||||||
if (has_ntp_time_obtained_once()) {
|
if (has_ntp_time_obtained_once()) {
|
||||||
// I _think_ I only want to be doing that rolling average and saving if/when
|
// I _think_ I only want to be doing that rolling average and saving
|
||||||
// we've got an actual time to do that with. I think that'd avoid some weird
|
// if/when we've got an actual time to do that with. I think that'd
|
||||||
// bugs (time changing from 1970 -> current time within a 10 min period)
|
// avoid some weird bugs (time changing from 1970 -> current time within
|
||||||
|
// a 10 min period)
|
||||||
time_t epoch;
|
time_t epoch;
|
||||||
time(&epoch);
|
time(&epoch);
|
||||||
struct tm *timeinfo = localtime(&epoch);
|
struct tm *timeinfo = localtime(&epoch);
|
||||||
|
|
||||||
// There must be a better way - I couldn't figure out how to do it nicely all at once
|
// There must be a better way - I couldn't figure out how to do it
|
||||||
|
// nicely all at once
|
||||||
latest_datapoint.time.myt_sec = timeinfo->tm_sec;
|
latest_datapoint.time.myt_sec = timeinfo->tm_sec;
|
||||||
latest_datapoint.time.myt_min = timeinfo->tm_min;
|
latest_datapoint.time.myt_min = timeinfo->tm_min;
|
||||||
latest_datapoint.time.myt_hour = timeinfo->tm_hour;
|
latest_datapoint.time.myt_hour = timeinfo->tm_hour;
|
||||||
@@ -102,20 +102,23 @@ void read_from_dht22() {
|
|||||||
(latest_datapoint.rh - ten_minute_rolling_average.rh) /
|
(latest_datapoint.rh - ten_minute_rolling_average.rh) /
|
||||||
num_samples_last_ten_mins;
|
num_samples_last_ten_mins;
|
||||||
ten_minute_rolling_average.temp +=
|
ten_minute_rolling_average.temp +=
|
||||||
(latest_datapoint.temp - ten_minute_rolling_average.temp) / num_samples_last_ten_mins;
|
(latest_datapoint.temp - ten_minute_rolling_average.temp) /
|
||||||
|
num_samples_last_ten_mins;
|
||||||
}
|
}
|
||||||
num_samples_last_ten_mins += 1;
|
num_samples_last_ten_mins += 1;
|
||||||
|
|
||||||
// This should be: if (number of samples in 10 mins taken)
|
// This should be: if (number of samples in 10 mins taken)
|
||||||
// This _will_ break if sampled too quickly, because we'll have more samples per 10 minutes
|
// This _will_ break if sampled too quickly, because we'll have more
|
||||||
// than can be stored in a uint32 - I'll calculate what that is and perhaps raise a compile
|
// samples per 10 minutes than can be stored in a uint32 - I'll
|
||||||
// error if too high.
|
// calculate what that is and perhaps raise a compile error if too high.
|
||||||
if (num_samples_last_ten_mins >=
|
if (num_samples_last_ten_mins >=
|
||||||
(CONFIG_DHT22_PERIOD_POLL * 1000 * 60 * 10)) {
|
(CONFIG_DHT22_PERIOD_POLL * 1000 * 60 * 10)) {
|
||||||
num_samples_last_ten_mins = 0;
|
num_samples_last_ten_mins = 0;
|
||||||
// TODO: Write the data out somewhere - into RAM maybe
|
// TODO: Write the data out somewhere - into RAM maybe
|
||||||
// TODO: maybe push the data somewhere - or put a flag up that data can be pushed
|
// TODO: maybe push the data somewhere - or put a flag up that data
|
||||||
// ^ could do a cute Semaphore thingy here - I don't think I've actually used one before
|
// can be pushed
|
||||||
|
// ^ could do a cute Semaphore thingy here - I don't think I've
|
||||||
|
// actually used one before
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,9 +127,7 @@ void read_from_dht22() {
|
|||||||
}
|
}
|
||||||
#endif /* CONFIG DHT22_ENABLED */
|
#endif /* CONFIG DHT22_ENABLED */
|
||||||
|
|
||||||
void
|
void app_main(void) {
|
||||||
app_main(void)
|
|
||||||
{
|
|
||||||
esp_err_t ret = nvs_flash_init();
|
esp_err_t ret = nvs_flash_init();
|
||||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
||||||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
@@ -136,8 +137,7 @@ app_main(void)
|
|||||||
ESP_ERROR_CHECK(ret);
|
ESP_ERROR_CHECK(ret);
|
||||||
|
|
||||||
#ifdef CONFIG_DHT22_ENABLED
|
#ifdef CONFIG_DHT22_ENABLED
|
||||||
xTaskCreate(read_from_dht22,
|
xTaskCreate(read_from_dht22, "DHT22",
|
||||||
"DHT22",
|
|
||||||
4 * 1024, // honestly I have _no_ idea
|
4 * 1024, // honestly I have _no_ idea
|
||||||
NULL,
|
NULL,
|
||||||
5, // no idea either
|
5, // no idea either
|
||||||
@@ -147,8 +147,7 @@ app_main(void)
|
|||||||
|
|
||||||
#ifdef CONFIG_PMS5003_ENABLED
|
#ifdef CONFIG_PMS5003_ENABLED
|
||||||
|
|
||||||
xTaskCreate(read_from_pms5003,
|
xTaskCreate(read_from_pms5003, "PMS5003",
|
||||||
"PMS5003",
|
|
||||||
4 * 1024, // honestly I have _no_ idea
|
4 * 1024, // honestly I have _no_ idea
|
||||||
NULL,
|
NULL,
|
||||||
5, // no idea either
|
5, // no idea either
|
||||||
|
|||||||
Reference in New Issue
Block a user