kinda working now maybe

modbus
Ondřej Hruška 3 years ago
parent ace9fc33d0
commit 8e9913dae5
  1. 2
      components/dhcp_wd/src/dhcp_wd.c
  2. BIN
      components/fileserver/files/embed/favicon.ico
  3. 7
      components/fileserver/files/embed/index.html
  4. 8
      main/Kconfig.projbuild
  5. 1
      main/app_main.c
  6. 111
      main/co2_sensor.c
  7. 2
      main/console/commands/cmd_pw.c
  8. 4
      main/console/commands/cmd_wifi.c
  9. 36
      main/periph_init.c
  10. 12
      main/voc_sensor.c
  11. 11
      main/wifi_conn.c
  12. 20
      sdkconfig

@ -1,3 +1,5 @@
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#include <string.h>
#include "esp_log.h"
#include "dhcp_wd.h"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

@ -33,7 +33,12 @@ thead tr { border-bottom: 2px solid black; }
<body>
<h1>ESP node {version}</h1>
<a href="/reboot">Restart node</a>
<p>Air quality monitor</p>
<ul>
<li><a href="/sample">Sample</a>
<li><a href="/reboot">Restart node</a>
</ul>
<script>
var kv_re = /^(-?[0-9.-]+)\s+(.+)$/;

@ -9,8 +9,12 @@ menu "App Configuration"
int "I2C0 SCL"
default 17
config PIN_SENSEAIR_NRDY
int "SenseAir nRDY"
config PIN_I2C_SDA1
int "I2C0 SDA"
default 5
config PIN_I2C_SCL1
int "I2C0 SCL"
default 18
endmenu

@ -61,6 +61,7 @@ void app_main(void) {
xTaskCreatePinnedToCore(voc_read_task, "VOC", 4096, NULL, PRIO_NORMAL, NULL, 1);
xTaskCreatePinnedToCore(co2_read_task, "CO2", 4096, NULL, PRIO_NORMAL, NULL, 1);
// xTaskCreate(co2_read_task, "CO2", 4096, NULL, PRIO_NORMAL, NULL);
console_init(NULL);
register_console_commands();

@ -1,3 +1,5 @@
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#include "co2_sensor.h"
#include <esp_log.h>
@ -6,6 +8,7 @@
#include <freertos/task.h>
#include <driver/i2c.h>
#include <sys/time.h>
#include <hal/i2c_hal.h>
#include "voc_sensor.h"
#include "i2c_utils.h"
#include "periph_init.h"
@ -14,28 +17,20 @@
static const char *TAG = "co2";
#define CO2_ADDR 104
#define CO2_I2C_NUM I2C_NUM_0
#define CO2_I2C_NUM I2C_NUM_1
#define TIMEOUT_MS 500
#define SEMA_TIMEOUT_MS 2000
static esp_err_t do_reg_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, uint32_t timeout_ms) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_reg_read(cmd, CO2_ADDR, reg_addr, reg_data, length);
esp_err_t ret = i2c_master_cmd_begin(CO2_I2C_NUM, cmd, pdMS_TO_TICKS(TIMEOUT_MS));
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "I2C read error: %s, devaddr %x, reg %d, len %d", esp_err_to_name(ret), CO2_ADDR, reg_addr, length);
}
ESP_LOG_BUFFER_HEXDUMP(TAG, reg_data, length, ESP_LOG_DEBUG);
return ret;
}
// this works, but the bus is fucked up
bool wake_up() {
esp_err_t suc;
#define TRY(x) suc=x; if(suc!=ESP_OK) return suc;
// for (int i = 0; i < 10; i++) {
for (int i = 0; i < 5; i++) {
ESP_LOGD(TAG, "Wake up try %d", i);
uint8_t dummy;
i2c_cmd_handle_t chain = i2c_cmd_link_create();
i2c_cmd_handle_t chain;
chain = i2c_cmd_link_create();
TRY(i2c_master_start(chain));
TRY(i2c_master_write_byte(chain, (CO2_ADDR << 1) | I2C_MASTER_WRITE, false));
TRY(i2c_master_write_byte(chain, 0x30, false));
@ -43,32 +38,56 @@ bool wake_up() {
TRY(i2c_master_write_byte(chain, (CO2_ADDR << 1) | I2C_MASTER_READ, false)); // TODO expect ack?
TRY(i2c_master_read(chain, &dummy, 1, I2C_MASTER_LAST_NACK));
TRY(i2c_master_stop(chain));
/*esp_err_t ret = */ i2c_master_cmd_begin(CO2_I2C_NUM, chain, pdMS_TO_TICKS(TIMEOUT_MS));
i2c_master_cmd_begin(CO2_I2C_NUM, chain, pdMS_TO_TICKS(70));
i2c_cmd_link_delete(chain);
// if (ret == ESP_OK) {
// ets_delay_us(5000);
ets_delay_us(12000);
chain = i2c_cmd_link_create();
TRY(i2c_master_start(chain));
TRY(i2c_master_write_byte(chain, (CO2_ADDR << 1) | I2C_MASTER_WRITE, false));
TRY(i2c_master_write_byte(chain, 0x30, false));
TRY(i2c_master_start(chain));
TRY(i2c_master_write_byte(chain, (CO2_ADDR << 1) | I2C_MASTER_READ, true)); // TODO expect ack?
TRY(i2c_master_read(chain, &dummy, 1, I2C_MASTER_LAST_NACK));
TRY(i2c_master_stop(chain));
suc = i2c_master_cmd_begin(CO2_I2C_NUM, chain, pdMS_TO_TICKS(80));
i2c_cmd_link_delete(chain);
if (suc == ESP_OK) {
ESP_LOGD(TAG, "Woken up at try %d", i);
return true;
// }
//// }
// ESP_LOGE(TAG, "Fail to wake up");
// return false;
}
}
ESP_LOGE(TAG, "Fail to wake up");
return false;
#undef TRY
}
static esp_err_t reg_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, uint32_t timeout_ms) {
BaseType_t suc = xSemaphoreTake(g_mux_i2c, pdMS_TO_TICKS(SEMA_TIMEOUT_MS));
if (suc != pdPASS) {
ESP_LOGE(TAG, "Sema fail");
return ESP_ERR_TIMEOUT;
static esp_err_t do_reg_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, uint32_t timeout_ms) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_reg_read(cmd, CO2_ADDR, reg_addr, reg_data, length);
esp_err_t ret = i2c_master_cmd_begin(CO2_I2C_NUM, cmd, pdMS_TO_TICKS(TIMEOUT_MS));
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "I2C read error: %s, devaddr %x, reg %d, len %d", esp_err_to_name(ret), CO2_ADDR, reg_addr, length);
}
ESP_LOG_BUFFER_HEXDUMP(TAG, reg_data, length, ESP_LOG_DEBUG);
return ret;
}
static esp_err_t reg_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, uint32_t timeout_ms) {
// BaseType_t suc = xSemaphoreTake(g_mux_i2c, pdMS_TO_TICKS(SEMA_TIMEOUT_MS));
// if (suc != pdPASS) {
// ESP_LOGE(TAG, "Sema fail");
// return ESP_ERR_TIMEOUT;
// }
if (!wake_up()) {
xSemaphoreGive(g_mux_i2c);
// xSemaphoreGive(g_mux_i2c);
return ESP_ERR_TIMEOUT;
}
esp_err_t rv = do_reg_read(reg_addr, reg_data, length, timeout_ms);
xSemaphoreGive(g_mux_i2c);
vTaskDelay(pdMS_TO_TICKS(12));
// xSemaphoreGive(g_mux_i2c);
// ets_delay_us(12000);
return rv;
}
@ -84,18 +103,18 @@ static esp_err_t do_reg_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_
}
static esp_err_t reg_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, uint32_t timeout_ms) {
BaseType_t suc = xSemaphoreTake(g_mux_i2c, pdMS_TO_TICKS(SEMA_TIMEOUT_MS));
if (suc != pdPASS) {
ESP_LOGE(TAG, "Sema fail");
return ESP_ERR_TIMEOUT;
}
// BaseType_t suc = xSemaphoreTake(g_mux_i2c, pdMS_TO_TICKS(SEMA_TIMEOUT_MS));
// if (suc != pdPASS) {
// ESP_LOGE(TAG, "Sema fail");
// return ESP_ERR_TIMEOUT;
// }
if (!wake_up()) {
xSemaphoreGive(g_mux_i2c);
// xSemaphoreGive(g_mux_i2c);
return ESP_ERR_TIMEOUT;
}
esp_err_t rv = do_reg_write(reg_addr, reg_data, length, timeout_ms);
xSemaphoreGive(g_mux_i2c);
vTaskDelay(pdMS_TO_TICKS(12));
// xSemaphoreGive(g_mux_i2c);
// ets_delay_us(12000);
return rv;
}
@ -104,18 +123,18 @@ void co2_read_task(void *param) {
// continuous is the default
for (int retry = 1; retry <= 10; retry++) {
vTaskDelay(pdMS_TO_TICKS(500));
uint8_t pld[] = {0x00, 0x05, 0x00, 0x05};
if (ESP_OK == reg_write(0x96, pld, 4, TIMEOUT_MS)) {
ESP_LOGI(TAG, "CO2 init OK");
break;
}
vTaskDelay(pdMS_TO_TICKS(50));
}
bool init_suc = false;
vTaskDelay(pdMS_TO_TICKS(250));
while (1) {
vTaskDelay(pdMS_TO_TICKS(5000));
vTaskDelay(pdMS_TO_TICKS(2000));
uint8_t data[4] = {};
@ -126,7 +145,7 @@ void co2_read_task(void *param) {
ESP_LOGI(TAG, "CO2 ppm %d, raw %d", filtered, unfiltered);
if (pdPASS == xSemaphoreTake(g_mux_data_report, pdMS_TO_TICKS(1000))) {
if (pdPASS == xSemaphoreTake(g_mux_data_report, pdMS_TO_TICKS(750))) {
if (filtered > 400 && filtered < 5000) {
g_data_report.co2_ppm = (float) filtered;
g_data_report.co2_ready = true;

@ -37,7 +37,7 @@ static int cmd_set(console_ctx_t *ctx, cmd_signature_t *reg)
return CONSOLE_OK;
}
strncpy(g_Settings.console_pw, args.pw->sval[0], CONSOLE_PW_LEN);
strncpy(g_Settings.console_pw, args.pw->sval[0], CONSOLE_PW_LEN-1);
console_printf("Access pw set to: \"%.*s\"\n", CONSOLE_PW_LEN, args.pw->sval[0]);
settings_persist(SETTINGS_console_pw);
return 0;

@ -228,9 +228,9 @@ static int cmd_sta_forget(console_ctx_t *ctx, cmd_signature_t *reg)
static bool wifi_join(const char* ssid, const char* pass, int timeout_ms)
{
wifi_config_t wifi_config = {};
strncpy((char*) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
strncpy((char*) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid)-1);
if (pass) {
strncpy((char*) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
strncpy((char*) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password) - 1);
}
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );

@ -5,19 +5,16 @@
static const char *TAG = "periph_init";
SemaphoreHandle_t g_mux_i2c;
esp_err_t periph_init() {
esp_err_t rv;
g_mux_i2c = xSemaphoreCreateMutex();
assert(g_mux_i2c);
g_mux_data_report = xSemaphoreCreateMutex(); // XXX move elsewhere
assert(g_mux_data_report);
ESP_LOGI(TAG, "Init I2C");
// VOC i2c
{
int i2c_master_port = I2C_NUM_0;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
@ -37,15 +34,30 @@ esp_err_t periph_init() {
ESP_LOGE(TAG, "Err in i2c_driver_install");
return rv;
}
}
gpio_config_t gconf = {
.pin_bit_mask = (1 << CONFIG_PIN_SENSEAIR_NRDY),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE, // TODO
// senseair i2c
{
int i2c_master_port = I2C_NUM_1;
i2c_config_t conf2 = {
.mode = I2C_MODE_MASTER,
.sda_io_num = CONFIG_PIN_I2C_SDA1,
.sda_pullup_en = GPIO_PULLUP_ENABLE, // is this enough?
.scl_io_num = CONFIG_PIN_I2C_SCL1,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000,
};
gpio_config(&gconf);
rv = i2c_param_config(i2c_master_port, &conf2);
if (rv != ESP_OK) {
ESP_LOGE(TAG, "Err in i2c_param_config");
return rv;
}
rv = i2c_driver_install(i2c_master_port, conf2.mode, 0, 0, 0);
if (rv != ESP_OK) {
ESP_LOGE(TAG, "Err in i2c_driver_install");
return rv;
}
}
return ESP_OK;
}

@ -71,11 +71,6 @@ static BME68X_INTF_RET_TYPE user_i2c_read(uint8_t reg_addr, uint8_t *reg_data, u
if (length == 0) {
return BME68X_OK;
}
BaseType_t suc = xSemaphoreTake(g_mux_i2c, pdMS_TO_TICKS(VOC_SEMA_TO_MS));
if (suc != pdPASS) {
ESP_LOGE(TAG, "Sema fail");
return -1;
}
struct sensor_itf *itf = intf_ptr;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_reg_read(cmd, itf->dev_addr, reg_addr, reg_data, length);
@ -85,7 +80,6 @@ static BME68X_INTF_RET_TYPE user_i2c_read(uint8_t reg_addr, uint8_t *reg_data, u
ESP_LOGE(TAG, "I2C read error: %s, devaddr %x, reg %d, len %d", esp_err_to_name(ret), itf->dev_addr, reg_addr, length);
}
ESP_LOG_BUFFER_HEXDUMP(TAG, reg_data, length, ESP_LOG_DEBUG);
xSemaphoreGive(g_mux_i2c);
return ret == ESP_OK ? BME68X_OK : BME68X_E_COM_FAIL;
}
@ -94,11 +88,6 @@ static BME68X_INTF_RET_TYPE user_i2c_write(uint8_t reg_addr, const uint8_t *reg_
if (length == 0) {
return BME68X_OK;
}
BaseType_t suc = xSemaphoreTake(g_mux_i2c, pdMS_TO_TICKS(VOC_SEMA_TO_MS));
if (suc != pdPASS) {
ESP_LOGE(TAG, "Sema fail");
return -1;
}
struct sensor_itf *itf = intf_ptr;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_reg_write(cmd, itf->dev_addr, reg_addr, reg_data, length);
@ -107,7 +96,6 @@ static BME68X_INTF_RET_TYPE user_i2c_write(uint8_t reg_addr, const uint8_t *reg_
if (ret != ESP_OK) {
ESP_LOGE(TAG, "I2C write error: %s, devaddr %x, reg %d, len %d", esp_err_to_name(ret), itf->dev_addr, reg_addr, length);
}
xSemaphoreGive(g_mux_i2c);
return ret == ESP_OK ? BME68X_OK : BME68X_E_COM_FAIL;
}

@ -36,13 +36,14 @@ static void event_handler(void* arg, esp_event_base_t event_base,
dhcp_watchdog_notify(dhcp_wd, DHCP_WD_NOTIFY_DISCONNECTED);
}
if (s_retry_num < WIFI_MAX_RETRY) {
esp_wifi_connect();
// if (s_retry_num < WIFI_MAX_RETRY) {
// esp_wifi_disconnect();
esp_wifi_connect(); // eventually, reconnect works, but it takes a while
s_retry_num++;
ESP_LOGI(TAG, "Retry to connect (try %d)", s_retry_num+1);
} else {
xEventGroupSetBits(g_wifi_event_group, WIFI_FAIL_BIT);
}
// } else {
// xEventGroupSetBits(g_wifi_event_group, WIFI_FAIL_BIT);
// }
} else if (event_base == IP_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
if (dhcp_wd) {
dhcp_watchdog_notify(dhcp_wd, DHCP_WD_NOTIFY_CONNECTED);

@ -134,7 +134,8 @@ CONFIG_PARTITION_TABLE_MD5=y
#
CONFIG_PIN_I2C_SDA0=16
CONFIG_PIN_I2C_SCL0=17
CONFIG_PIN_SENSEAIR_NRDY=18
CONFIG_PIN_I2C_SDA1=5
CONFIG_PIN_I2C_SCL1=18
# end of Pin mapping
#
@ -148,19 +149,20 @@ CONFIG_CONSOLE_PW_LEN=32
#
# Compiler options
#
CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y
# CONFIG_COMPILER_OPTIMIZATION_DEFAULT is not set
# CONFIG_COMPILER_OPTIMIZATION_SIZE is not set
# CONFIG_COMPILER_OPTIMIZATION_PERF is not set
CONFIG_COMPILER_OPTIMIZATION_PERF=y
# CONFIG_COMPILER_OPTIMIZATION_NONE is not set
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y
# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT is not set
# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE is not set
# CONFIG_COMPILER_CXX_EXCEPTIONS is not set
# CONFIG_COMPILER_CXX_RTTI is not set
CONFIG_COMPILER_STACK_CHECK_MODE_NONE=y
# CONFIG_COMPILER_STACK_CHECK_MODE_NORM is not set
# CONFIG_COMPILER_STACK_CHECK_MODE_NONE is not set
CONFIG_COMPILER_STACK_CHECK_MODE_NORM=y
# CONFIG_COMPILER_STACK_CHECK_MODE_STRONG is not set
# CONFIG_COMPILER_STACK_CHECK_MODE_ALL is not set
CONFIG_COMPILER_STACK_CHECK=y
# CONFIG_COMPILER_WARN_WRITE_STRINGS is not set
# CONFIG_COMPILER_DISABLE_GCC8_WARNINGS is not set
# CONFIG_COMPILER_DUMP_RTL_FILES is not set
@ -641,7 +643,6 @@ CONFIG_FREERTOS_USE_TRACE_FACILITY=y
CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS=y
CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID=y
# CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS is not set
CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER=y
CONFIG_FREERTOS_CHECK_MUTEX_GIVEN_BY_OWNER=y
# CONFIG_FREERTOS_CHECK_PORT_CRITICAL_COMPLIANCE is not set
# CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH is not set
@ -1185,16 +1186,17 @@ CONFIG_MONITOR_BAUD_115200B=y
# CONFIG_MONITOR_BAUD_OTHER is not set
CONFIG_MONITOR_BAUD_OTHER_VAL=115200
CONFIG_MONITOR_BAUD=115200
CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG is not set
# CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is not set
CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y
# CONFIG_OPTIMIZATION_ASSERTIONS_SILENT is not set
# CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED is not set
# CONFIG_CXX_EXCEPTIONS is not set
CONFIG_STACK_CHECK_NONE=y
# CONFIG_STACK_CHECK_NORM is not set
# CONFIG_STACK_CHECK_NONE is not set
CONFIG_STACK_CHECK_NORM=y
# CONFIG_STACK_CHECK_STRONG is not set
# CONFIG_STACK_CHECK_ALL is not set
CONFIG_STACK_CHECK=y
# CONFIG_WARN_WRITE_STRINGS is not set
# CONFIG_DISABLE_GCC8_WARNINGS is not set
# CONFIG_ESP32_APPTRACE_DEST_TRAX is not set

Loading…
Cancel
Save