Added SNTP and start of data logging
This commit is contained in:
51
main/main.c
51
main/main.c
@@ -7,13 +7,14 @@
|
||||
#include "dht22.h"
|
||||
#include "wifi.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_sntp.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/projdefs.h"
|
||||
#include "freertos/task.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "portmacro.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static const char* TAG = "YASPAM";
|
||||
@@ -24,6 +25,15 @@ static volatile bool run_pms5003 = true;
|
||||
static TaskHandle_t dht22_handle = NULL;
|
||||
static TaskHandle_t pms5003_handle = NULL;
|
||||
|
||||
typedef struct {
|
||||
uint8_t myt_sec; // 0 -> 61
|
||||
uint8_t myt_min; // 0 -> 69
|
||||
uint8_t myt_hour; // 0 -> 23
|
||||
uint8_t myt_day; // 1 -> 31
|
||||
uint8_t myt_month; // 0 -> 11 !!
|
||||
uint8_t myt_year; // years since 1900
|
||||
} my_time;
|
||||
|
||||
#ifdef CONFIG_PMS5003_ENABLED
|
||||
#include "pms5003.h"
|
||||
|
||||
@@ -42,19 +52,48 @@ read_from_pms5003()
|
||||
#ifdef CONFIG_DHT22_ENABLED
|
||||
#include "dht22.h"
|
||||
|
||||
void
|
||||
read_from_dht22()
|
||||
{
|
||||
typedef struct {
|
||||
deci_percent rh;
|
||||
deci_degrees_c temp;
|
||||
my_time time;
|
||||
} my_dht22_data;
|
||||
|
||||
my_dht22_data latest_datapoint;
|
||||
|
||||
my_dht22_data ten_minute_rolling_average;
|
||||
uint16_t num_samples_last_ten_mins = 0;
|
||||
|
||||
void read_from_dht22() {
|
||||
while (run_dht) {
|
||||
// Data reading is based on time, so need to block interrups etc
|
||||
// I _think_ these are freeRTOS task interrupts - I wonder if
|
||||
// I need to also block ESP interrupts? Should I even do that?
|
||||
portDISABLE_INTERRUPTS();
|
||||
int ret = dht22_read();
|
||||
portENABLE_INTERRUPTS();
|
||||
if (ret != DHT22_OK) {
|
||||
dht22_handle_error(ret);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Got DHT22 data!");
|
||||
// TODO: Write the data out somewhere - into RAM maybe?
|
||||
latest_datapoint.temp = dht22_temperature();
|
||||
latest_datapoint.rh = dht22_relative_humidity();
|
||||
|
||||
if (has_ntp_time_obtained_once()) {
|
||||
time_t epoch;
|
||||
time(&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
|
||||
latest_datapoint.time.myt_sec = timeinfo->tm_sec;
|
||||
latest_datapoint.time.myt_min = timeinfo->tm_min;
|
||||
latest_datapoint.time.myt_hour = timeinfo->tm_hour;
|
||||
latest_datapoint.time.myt_day = timeinfo->tm_mday;
|
||||
latest_datapoint.time.myt_month = timeinfo->tm_mon;
|
||||
latest_datapoint.time.myt_year = timeinfo->tm_year;
|
||||
// TODO: update ten minute rolling average
|
||||
//
|
||||
// TODO: Write the data out somewhere - into RAM maybe - I think only every 10 minutes
|
||||
// though
|
||||
}
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(CONFIG_DHT22_PERIOD_POLL));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user