Started on DS18B20 and PMS5003

Didn't realise DS18B20 was 1-Wire

Will probably just find a library for that one - can't be arsed
doing more bit banging
This commit is contained in:
2023-11-01 21:12:39 +00:00
parent 8aeade9922
commit 2b117b4b33
8 changed files with 132 additions and 46 deletions

View File

@@ -1,13 +1,17 @@
#include "dht22.h" #include "dht22.h"
#include "esp_log.h"
#include "hal/gpio_types.h"
#include "sdkconfig.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include <rom/ets_sys.h> #include "esp_log.h"
#include "esp_timer.h" #include "esp_timer.h"
#include "freertos/portmacro.h" #include "freertos/portmacro.h"
#include "hal/gpio_types.h"
#include "sdkconfig.h"
#include <rom/ets_sys.h>
#ifndef CONFIG_DHT22_DATA_GPIO
#define DATA_GPIO 2
#else
#define DATA_GPIO CONFIG_DHT22_DATA_GPIO #define DATA_GPIO CONFIG_DHT22_DATA_GPIO
#endif /* CONFIG_DHT22_DATA_GPIO */
#if DATA_GPIO == -1 #if DATA_GPIO == -1
#error "Please define your DHT22 GPIO Data Pin" #error "Please define your DHT22 GPIO Data Pin"
@@ -70,7 +74,8 @@ dht22_error dht22_read() {
gpio_set_direction(DATA_GPIO, GPIO_MODE_INPUT); gpio_set_direction(DATA_GPIO, GPIO_MODE_INPUT);
// Wait here until it goes high DHT "responds" after being pulled high // Wait here until it goes high DHT "responds" after being pulled high
// The 40 is pretty arbitrary - but there's probably a problem if it goes higher // The 40 is pretty arbitrary - but there's probably a problem if it goes
// higher
uint32_t elapsed_time; uint32_t elapsed_time;
elapsed_time = wait_for_signal_to_change_from(1); elapsed_time = wait_for_signal_to_change_from(1);
if (elapsed_time == DHT22_TIMEOUT_ERROR) { if (elapsed_time == DHT22_TIMEOUT_ERROR) {

View File

@@ -5,11 +5,11 @@ menu "DS18B20"
config DS18B20_GPIO_PIN config DS18B20_GPIO_PIN
int "GPIO Pin used for 1-Wire DS18B20" int "GPIO Pin used for 1-Wire DS18B20"
default 10 default 15
depends on DS18B20_ENABLED depends on DS18B20_ENABLED
config DS18B20_PERIOD_POLL config DS18B20_PERIOD_POLL
int "Period to poll the DS18B20 in seconds" int "Period to poll the DS18B20 in seconds"
default 8000 default 8
depends on DS18B20_ENABLED depends on DS18B20_ENABLED
endmenu endmenu

View File

@@ -1,4 +1,12 @@
int test() { #include "ds18b20.h"
int i = 5; #include "driver/gpio.h"
return i; #include "sdkconfig.h"
#ifdef CONFIG_DS18B20_ENABLED
int ds18b20_read(void) {
esp_err_t ret = gpio_set_pull_mode(CONFIG_DS18B20_GPIO_PIN, GPIO_PULLUP_ONLY);
return DS18B20_OK;
} }
#endif /* CONFIG_DS18B20_ENABLED */

View File

@@ -0,0 +1,7 @@
#pragma once
int ds18b20_read(void);
typedef enum {
DS18B20_OK,
} ds18b20_error;

View File

@@ -3,13 +3,23 @@ menu "PMS5003"
bool "PMS5003 Enabled" bool "PMS5003 Enabled"
default y default y
config PMS5003_UART config PMS5003_UART_PORT
int "PMS5003 UART Port" int "PMS5003 UART PORT"
default 1 default 0
depends on PMS5003_ENABLED
config PMS5003_UART_TX
int "PMS5003 UART Tx GPIO Pin"
default 4
depends on PMS5003_ENABLED
config PMS5003_UART_RX
int "PMS5003 UART Rx GPIO Pin"
default 5
depends on PMS5003_ENABLED depends on PMS5003_ENABLED
config PMS5003_PERIOD_POLL config PMS5003_PERIOD_POLL
int "Period to poll the PMS5003 in ms" int "Period to poll the PMS5003 in seconds"
default 8000 default 8
depends on PMS5003_ENABLED depends on PMS5003_ENABLED
endmenu endmenu

View File

@@ -1,4 +1,29 @@
int test() { #include "pms5003.h"
int i = 5; #include "driver/uart.h"
return i; #include "hal/uart_types.h"
#define START_CHAR_1 0x42
#define START_CHAR_2 0x4d
#define DEFAULT_BAUD 9600
static const char* TAG = "PMS5003";
pms5003_err pms5003_init() {
const uart_port_t uart_num = CONFIG_PMS5003_UART_PORT;
uart_set_pin(uart_num, CONFIG_PMS5003_UART_TX, CONFIG_PMS5003_UART_RX, -1, -1);
uart_config_t uart_config = {
.baud_rate = DEFAULT_BAUD,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 122,
};
return PMS5003_OK;
}
pms5003_err pms5003_read(void) {
return PMS5003_OK;
} }

View File

@@ -0,0 +1,5 @@
#pragma once
typedef enum { PMS5003_OK } pms5003_err;
pms5003_err pms5003_read(void);

View File

@@ -14,18 +14,20 @@
#include "portmacro.h" #include "portmacro.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "wifi.h" #include "wifi.h"
#include <inttypes.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <inttypes.h>
static const char *TAG = "YASPAM"; static const char *TAG = "YASPAM";
static volatile bool run_dht = true; static volatile bool run_dht = true;
static volatile bool run_pms5003 = true; static volatile bool run_pms5003 = true;
static volatile bool run_ds18b20 = true;
static TaskHandle_t dht22_handle = NULL; static TaskHandle_t dht22_handle = NULL;
static TaskHandle_t pms5003_handle = NULL; static TaskHandle_t pms5003_handle = NULL;
static TaskHandle_t ds18b20_handle = NULL;
typedef struct { typedef struct {
uint8_t myt_sec; // 0 -> 61 uint8_t myt_sec; // 0 -> 61
@@ -36,14 +38,25 @@ typedef struct {
uint8_t myt_year; // years since 1900 uint8_t myt_year; // years since 1900
} my_time; } my_time;
#ifdef CONFIG_DS18B20_ENABLED
#include "ds18b20.h"
void read_from_ds18b20() {
ESP_LOGI(TAG, "Starting DS18B20 Task");
while (run_pms5003) {
int ret = ds18b20_read();
vTaskDelay(pdMS_TO_TICKS(CONFIG_DS18B20_PERIOD_POLL * 1000));
}
}
#endif /* CONFIG_PMS5003_ENABLED */
#ifdef CONFIG_PMS5003_ENABLED #ifdef CONFIG_PMS5003_ENABLED
#include "pms5003.h" #include "pms5003.h"
void read_from_pms5003() { void read_from_pms5003() {
while (run_pms5003) { while (run_pms5003) {
// TODO: actually collect some data int ret = pms5003_read();
ESP_LOGI(TAG, "Got PMS5003 data!");
// TODO: Write the data out somewhere
vTaskDelay(pdMS_TO_TICKS(CONFIG_PMS5003_PERIOD_POLL * 1000)); vTaskDelay(pdMS_TO_TICKS(CONFIG_PMS5003_PERIOD_POLL * 1000));
} }
} }
@@ -64,7 +77,7 @@ void read_from_dht22() {
// From datasheet: // From datasheet:
// - Don't start within 1 second of powering on // - Don't start within 1 second of powering on
vTaskDelay(pdMS_TO_TICKS(1000)); vTaskDelay(pdMS_TO_TICKS(1000));
ESP_LOGI(TAG, "Starting DHT22 Task");
uint32_t num_samples_last_ten_mins = 0; uint32_t num_samples_last_ten_mins = 0;
float relative_humidity_tmp; float relative_humidity_tmp;
float temperature_tmp; float temperature_tmp;
@@ -77,6 +90,9 @@ void read_from_dht22() {
} else { } else {
latest_datapoint.temp = dht22_temperature(); latest_datapoint.temp = dht22_temperature();
latest_datapoint.rh = dht22_relative_humidity(); latest_datapoint.rh = dht22_relative_humidity();
ESP_LOGI(TAG,
"Sucesfully obtained DHT22 data (temp: %"PRIi16" rh: %"PRIu16")",
latest_datapoint.temp, latest_datapoint.rh);
// No point collecting rolling average if we don't know what the time is // No point collecting rolling average if we don't know what the time is
if (has_ntp_time_obtained_once()) { if (has_ntp_time_obtained_once()) {
@@ -85,21 +101,22 @@ void read_from_dht22() {
temperature_tmp = latest_datapoint.temp; temperature_tmp = latest_datapoint.temp;
} else { } else {
relative_humidity_tmp += relative_humidity_tmp +=
((float) latest_datapoint.rh - relative_humidity_tmp) / ((float)latest_datapoint.rh - relative_humidity_tmp) /
((float) num_samples_last_ten_mins + 1.0); ((float)num_samples_last_ten_mins + 1.0);
temperature_tmp += temperature_tmp += ((float)latest_datapoint.temp - temperature_tmp) /
((float) latest_datapoint.temp - temperature_tmp ) / ((float)num_samples_last_ten_mins + 1.0);
((float) num_samples_last_ten_mins + 1.0);
} }
num_samples_last_ten_mins += 1; num_samples_last_ten_mins += 1;
ESP_LOGI(TAG, "latest_datapoint.temp: %"PRIi16, latest_datapoint.temp); ESP_LOGI(TAG, "latest_datapoint.temp: %" PRIi16, latest_datapoint.temp);
ESP_LOGI(TAG, "ten_minute_average.temp: %f, num_samples: %"PRIu32, temperature_tmp, num_samples_last_ten_mins); ESP_LOGI(TAG, "ten_minute_average.temp: %f, num_samples: %" PRIu32,
temperature_tmp, num_samples_last_ten_mins);
// This should be: if (10 minutes of sample taken) // This should be: if (10 minutes of sample taken)
// TODO: check max samples / second that can fit in 10 minutes // TODO: check max samples / second that can fit in 10 minutes
if (num_samples_last_ten_mins >= if (num_samples_last_ten_mins >=
((10 * 60) / (CONFIG_DHT22_PERIOD_POLL))) { ((10 * 60) / (CONFIG_DHT22_PERIOD_POLL))) {
ESP_LOGW(TAG, "10 minutes of samples collected. Average temp: %f", temperature_tmp); ESP_LOGW(TAG, "10 minutes of samples collected. Average temp: %f",
temperature_tmp);
num_samples_last_ten_mins = 0; num_samples_last_ten_mins = 0;
time_t epoch; time_t epoch;
@@ -116,12 +133,14 @@ void read_from_dht22() {
ten_minute_average.time.myt_month = timeinfo->tm_mon; ten_minute_average.time.myt_month = timeinfo->tm_mon;
ten_minute_average.time.myt_year = timeinfo->tm_year; ten_minute_average.time.myt_year = timeinfo->tm_year;
// TODO: Consider rounding this number - although, .1 of a degree C // TODO: Consider rounding this number - although, .1 of a degree C...
// no one is going to miss that. Probably the calculation require to // no one is going to miss that. Probably the calculation require to
// do the rounding would affect the temperature that much anyways haha // do the rounding would affect the temperature that much anyways
// haha
ten_minute_average.temp = temperature_tmp; ten_minute_average.temp = temperature_tmp;
ten_minute_average.rh = relative_humidity_tmp; ten_minute_average.rh = relative_humidity_tmp;
ESP_LOGW(TAG, "ten_min_avg.temp %" PRIi16 ", .rh %" PRIu16, ten_minute_average.temp, ten_minute_average.rh); ESP_LOGW(TAG, "ten_min_avg.temp %" PRIi16 ", .rh %" PRIu16,
ten_minute_average.temp, ten_minute_average.rh);
// 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 // TODO: maybe push the data somewhere - or put a flag up that data
@@ -163,6 +182,13 @@ void app_main(void) {
&pms5003_handle); &pms5003_handle);
#endif /* CONFIG_PMS5003_ENABLED */ #endif /* CONFIG_PMS5003_ENABLED */
start_wifi();
#ifdef CONFIG_DS18B20_ENABLED
xTaskCreate(read_from_ds18b20, "DS18B20",
4 * 1024, // honestly I have _no_ idea
NULL,
5, // no idea either
&ds18b20_handle);
#endif /* CONFIG DS18B20_ENABLED */
// start_wifi();
} }