(v2) add GPIOs to WebUI as configurable options (LED, Rx, Tx, Dallas sensor) #466

This commit is contained in:
proddy
2020-08-29 15:16:19 +02:00
parent 844267eeef
commit 24d5da36a6
16 changed files with 142 additions and 73 deletions

View File

@@ -76,6 +76,54 @@ function EMSESPSettingsControllerForm(props: EMSESPSettingsControllerFormProps)
<MenuItem value={0x0F}>Time Module (0x0F)</MenuItem>
<MenuItem value={0x12}>Alarm Module (0x12)</MenuItem>
</SelectValidator>
<TextValidator
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:40']}
errorMessages={['Rx GPIO is required', "Must be a number", "Must be 0 or higher", "Max value is 255"]}
name="rx_gpio"
label="Rx GPIO pin"
fullWidth
variant="outlined"
value={data.rx_gpio}
type="number"
onChange={handleValueChange('rx_gpio')}
margin="normal"
/>
<TextValidator
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:40']}
errorMessages={['Tx GPIO is required', "Must be a number", "Must be 0 or higher", "Max value is 255"]}
name="tx_gpio"
label="Tx GPIO pin"
fullWidth
variant="outlined"
value={data.tx_gpio}
type="number"
onChange={handleValueChange('tx_gpio')}
margin="normal"
/>
<TextValidator
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:40']}
errorMessages={['LED GPIO is required', "Must be a number", "Must be 0 or higher", "Max value is 255"]}
name="led_gpio"
label="LED GPIO pin (0=none)"
fullWidth
variant="outlined"
value={data.led_gpio}
type="number"
onChange={handleValueChange('led_gpio')}
margin="normal"
/>
<TextValidator
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:40']}
errorMessages={['Dallas GPIO is required', "Must be a number", "Must be 0 or higher", "Max value is 255"]}
name="dallas_gpio"
label="Dallas GPIO pin (0=none)"
fullWidth
variant="outlined"
value={data.dallas_gpio}
type="number"
onChange={handleValueChange('dallas_gpio')}
margin="normal"
/>
<BlockFormControlLabel
control={
<Checkbox

View File

@@ -8,6 +8,10 @@ export interface EMSESPSettings {
shower_timer: boolean;
shower_alert: boolean;
hide_led: boolean;
rx_gpio: number;
tx_gpio : number;
dallas_gpio : number;
led_gpio : number;
}
export enum busConnectionStatus {

View File

@@ -26,7 +26,7 @@ namespace emsesp {
/*
* init UART0 driver
*/
void EMSuart::start(uint8_t tx_mode) {
void EMSuart::start(uint8_t tx_mode, uint8_t rx_gpio, uint8_t tx_gpio) {
}
/*

View File

@@ -31,7 +31,7 @@ class EMSuart {
EMSuart() = default;
~EMSuart() = default;
static void start(uint8_t tx_mode);
static void start(uint8_t tx_mode, uint8_t rx_gpio, uint8_t tx_gpio);
static void stop();
static void restart();
static void send_poll(uint8_t data);

View File

@@ -37,6 +37,10 @@ void EMSESPSettings::read(EMSESPSettings & settings, JsonObject & root) {
root["shower_timer"] = settings.shower_timer;
root["shower_alert"] = settings.shower_alert;
root["hide_led"] = settings.hide_led;
root["rx_gpio"] = settings.rx_gpio;
root["tx_gpio"] = settings.tx_gpio;
root["dallas_gpio"] = settings.dallas_gpio;
root["led_gpio"] = settings.led_gpio;
}
StateUpdateResult EMSESPSettings::update(JsonObject & root, EMSESPSettings & settings) {
@@ -49,6 +53,10 @@ StateUpdateResult EMSESPSettings::update(JsonObject & root, EMSESPSettings & set
settings.shower_timer = root["shower_timer"] | EMSESP_DEFAULT_SHOWER_TIMER;
settings.shower_alert = root["shower_alert"] | EMSESP_DEFAULT_SHOWER_ALERT;
settings.hide_led = root["hide_led"] | EMSESP_DEFAULT_HIDE_LED;
settings.rx_gpio = root["rx_gpio"] | EMSESP_DEFAULT_RX_GPIO;
settings.tx_gpio = root["tx_gpio"] | EMSESP_DEFAULT_TX_GPIO;
settings.dallas_gpio = root["dallas_gpio"] | EMSESP_DEFAULT_DALLAS_GPIO;
settings.led_gpio = root["led_gpio"] | EMSESP_DEFAULT_LED_GPIO;
return StateUpdateResult::CHANGED;
}

View File

@@ -35,6 +35,25 @@
#define EMSESP_DEFAULT_SHOWER_ALERT false
#define EMSESP_DEFAULT_HIDE_LED false
// Default GPIO PIN definitions
#if defined(ESP8266)
#define EMSESP_DEFAULT_RX_GPIO 13 // UART0, swapped
#define EMSESP_DEFAULT_TX_GPIO 15 // UART0, swapped
#define EMSESP_DEFAULT_DALLAS_GPIO 14 // D5
#define EMSESP_DEFAULT_LED_GPIO 2 // onboard LED
#elif defined(ESP32)
#define EMSESP_DEFAULT_RX_GPIO 23 // D7 on Wemos D1-32, OR 17 for UART2 on Lolin D32
#define EMSESP_DEFAULT_TX_GPIO 5 // D8 on Wemos D1-32, OR 16 for UART2 on Lolin D32
#define EMSESP_DEFAULT_DALLAS_GPIO 18 // 18 on Wemos D1-32, 14 on LOLIN D32
#define EMSESP_DEFAULT_LED_GPIO 2 // 2 on Wemos D1-32, 5 on LOLIN D32
#else
// for standalone
#define EMSESP_DEFAULT_RX_GPIO 0
#define EMSESP_DEFAULT_TX_GPIO 0
#define EMSESP_DEFAULT_DALLAS_GPIO 0
#define EMSESP_DEFAULT_LED_GPIO 0
#endif
namespace emsesp {
enum MQTT_format : uint8_t { SINGLE = 1, NESTED, HA, CUSTOM };
@@ -50,6 +69,10 @@ class EMSESPSettings {
int8_t syslog_level; // uuid::log::Level
uint32_t syslog_mark_interval;
String syslog_host;
uint8_t rx_gpio;
uint8_t tx_gpio;
uint8_t dallas_gpio;
uint8_t led_gpio;
static void read(EMSESPSettings & settings, JsonObject & root);
static StateUpdateResult update(JsonObject & root, EMSESPSettings & settings);

View File

@@ -142,14 +142,15 @@ void EMSESP::watch_id(uint16_t watch_id) {
// resets all counters and bumps the UART
// this is called when the tx_mode is persisted in the FS either via Web UI or the console
void EMSESP::init_tx() {
// get the tx_mode
uint8_t tx_mode;
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { tx_mode = settings.tx_mode; });
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) {
tx_mode = settings.tx_mode;
#ifndef EMSESP_FORCE_SERIAL
EMSuart::stop();
EMSuart::start(tx_mode);
EMSuart::start(tx_mode, settings.rx_gpio, settings.tx_gpio);
#endif
});
txservice_.start(); // sends out request to EMS bus for all devices

View File

@@ -36,7 +36,9 @@ void Sensors::start() {
reload();
#ifndef EMSESP_STANDALONE
bus_.begin(SENSOR_GPIO);
if (dallas_gpio_) {
bus_.begin(dallas_gpio_);
}
#endif
}
@@ -47,6 +49,8 @@ void Sensors::reload() {
mqtt_format_ = settings.mqtt_format; // single, nested or ha
});
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { dallas_gpio_ = settings.dallas_gpio; });
if (mqtt_format_ == MQTT_format::HA) {
for (uint8_t i = 0; i < MAX_SENSORS; registered_ha_[i++] = false)
;

View File

@@ -64,16 +64,6 @@ class Sensors {
const std::vector<Device> devices() const;
private:
#if defined(ESP8266)
static constexpr uint8_t SENSOR_GPIO = 14; // D5
#elif defined(ESP32)
#ifdef WEMOS_D1_32
static constexpr uint8_t SENSOR_GPIO = 18; // Wemos D1-32 for compatibility D5
#else
static constexpr uint8_t SENSOR_GPIO = 14; // D5 is LED on wemos lolin D32, so use GPIO14
#endif
#endif
static constexpr uint8_t MAX_SENSORS = 20;
enum class State { IDLE, READING, SCANNING };
@@ -118,6 +108,7 @@ class Sensors {
uint8_t mqtt_format_;
uint8_t retrycnt_ = 0;
uint8_t dallas_gpio_ = 0;
};
} // namespace emsesp

View File

@@ -34,6 +34,7 @@ uint32_t System::heap_start_ = 0;
int System::reset_counter_ = 0;
bool System::upload_status_ = false;
bool System::hide_led_ = false;
uint8_t System::led_gpio_ = 0;
// send on/off to a gpio pin
// value: true = HIGH, false = LOW
@@ -155,9 +156,10 @@ void System::start() {
void System::set_led() {
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) {
hide_led_ = settings.hide_led;
if (LED_GPIO) {
pinMode(LED_GPIO, OUTPUT); // LED_GPIO 0 means disabled
digitalWrite(LED_GPIO, hide_led_ ? !LED_ON : LED_ON); // LED on, for ever
led_gpio_ = settings.led_gpio;
if (led_gpio_) {
pinMode(led_gpio_, OUTPUT); // 0 means disabled
digitalWrite(led_gpio_, hide_led_ ? !LED_ON : LED_ON); // LED on, for ever
}
});
}
@@ -265,8 +267,8 @@ void System::system_check() {
// if it was unhealthy but now we're better, make sure the LED is solid again cos we've been healed
if (!system_healthy_) {
system_healthy_ = true;
if (LED_GPIO) {
digitalWrite(LED_GPIO, hide_led_ ? !LED_ON : LED_ON); // LED on, for ever
if (led_gpio_) {
digitalWrite(led_gpio_, hide_led_ ? !LED_ON : LED_ON); // LED on, for ever
}
}
}
@@ -275,7 +277,7 @@ void System::system_check() {
// flashes the LED
void System::led_monitor() {
if (!LED_GPIO) {
if (!led_gpio_) {
return;
}
@@ -286,7 +288,7 @@ void System::led_monitor() {
// if bus_not_connected or network not connected, start flashing
if (!system_healthy_) {
digitalWrite(LED_GPIO, !digitalRead(LED_GPIO));
digitalWrite(led_gpio_, !digitalRead(led_gpio_));
}
}
}

View File

@@ -76,19 +76,15 @@ class System {
// internal LED
#ifndef EMSESP_NO_LED
#if defined(ESP8266)
static constexpr uint8_t LED_GPIO = 2;
static constexpr uint8_t LED_ON = LOW;
#elif defined(ESP32)
#ifdef WEMOS_D1_32
static constexpr uint8_t LED_GPIO = 2; // on Wemos D1-32
static constexpr uint8_t LED_ON = HIGH;
#else
static constexpr uint8_t LED_GPIO = 5;
static constexpr uint8_t LED_ON = LOW;
#endif
#endif
#else
static constexpr uint8_t LED_GPIO = 0; // no LED
static constexpr uint8_t LED_ON = 0;
#endif
@@ -106,8 +102,6 @@ class System {
static uint32_t heap_start_;
static int reset_counter_;
uint32_t last_heartbeat_ = 0;
// OTA
static bool upload_status_; // true if we're in the middle of a OTA firmware upload
// settings
@@ -116,6 +110,7 @@ class System {
uint8_t syslog_level_;
uint32_t syslog_mark_interval_;
String syslog_host_;
static uint8_t led_gpio_;
};
} // namespace emsesp

View File

@@ -80,7 +80,6 @@ void IRAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) {
portEXIT_CRITICAL(&mux);
}
void IRAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() {
if (emsTxBufLen == 0) {
return;
@@ -106,7 +105,7 @@ void IRAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() {
/*
* init UART driver
*/
void EMSuart::start(const uint8_t tx_mode) {
void EMSuart::start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio) {
if (tx_mode_ != 0xFF) { // uart already initialized
tx_mode_ = tx_mode;
restart();
@@ -121,8 +120,9 @@ void EMSuart::start(const uint8_t tx_mode) {
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
};
uart_param_config(EMSUART_UART, &uart_config);
uart_set_pin(EMSUART_UART, EMSUART_TXPIN, EMSUART_RXPIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_set_pin(EMSUART_UART, tx_gpio, rx_gpio, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
EMS_UART.int_ena.val = 0; // disable all intr.
EMS_UART.int_clr.val = 0xFFFFFFFF; // clear all intr. flags
EMS_UART.idle_conf.tx_brk_num = 10; // breaklength 10 bit

View File

@@ -66,16 +66,6 @@
#define EMSUART_TX_WAIT_PLUS (EMSUART_TX_BIT_TIME * 20) // 2080
#define EMSUART_TX_BRK_PLUS (EMSUART_TX_BIT_TIME * 11)
// customize the GPIO pins for RX and TX here
#ifdef WEMOS_D1_32
#define EMSUART_RXPIN 23 // 17 is UART2 RX. Use 23 for D7 on a Wemos D1-32 mini for backwards compatabilty
#define EMSUART_TXPIN 5 // 16 is UART2 TX. Use 5 for D8 on a Wemos D1-32 mini for backwards compatabilty
#else
#define EMSUART_RXPIN 17 // 17 is UART2 RX. Use 23 for D7 on a Wemos D1-32 mini for backwards compatabilty
#define EMSUART_TXPIN 16 // 16 is UART2 TX. Use 5 for D8 on a Wemos D1-32 mini for backwards compatabilty
#endif
namespace emsesp {
#define EMS_TX_STATUS_OK 1
@@ -86,7 +76,7 @@ class EMSuart {
EMSuart() = default;
~EMSuart() = default;
static void start(const uint8_t tx_mode);
static void start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio);
static void send_poll(const uint8_t data);
static void stop();
static void restart();

View File

@@ -113,7 +113,7 @@ void ICACHE_FLASH_ATTR EMSuart::emsuart_flush_fifos() {
/*
* init UART0 driver
*/
void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) {
void ICACHE_FLASH_ATTR EMSuart::start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio) {
if (tx_mode_ != 0xFF) { // it's a restart no need to configure uart
tx_mode_ = tx_mode;
restart();
@@ -161,8 +161,11 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) {
// disable esp debug which will go to Tx and mess up the line - see https://github.com/espruino/Espruino/issues/655
system_set_os_print(0);
// swap Rx and Tx pins to use GPIO13 (D7) and GPIO15 (D8) respectively
system_uart_swap();
if (tx_gpio == 1 && rx_gpio == 3) {
system_uart_de_swap();
} else if (tx_gpio == 15 && rx_gpio == 13) {
system_uart_swap(); // swap Rx and Tx pins to use GPIO13 (D7) and GPIO15 (D8) respectively
}
ETS_UART_INTR_ATTACH(emsuart_rx_intr_handler, nullptr);
drop_next_rx = true;

View File

@@ -71,7 +71,7 @@ class EMSuart {
EMSuart() = default;
~EMSuart() = default;
static void ICACHE_FLASH_ATTR start(uint8_t tx_mode);
static void ICACHE_FLASH_ATTR start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio);
static void ICACHE_FLASH_ATTR stop();
static void ICACHE_FLASH_ATTR restart();
static void ICACHE_FLASH_ATTR send_poll(uint8_t data);

View File

@@ -1 +1 @@
#define EMSESP_APP_VERSION "2.0.0b13"
#define EMSESP_APP_VERSION "2.0.0b14"