From 1b8d242505b73b28ceaf50f7140eedf5da1897d3 Mon Sep 17 00:00:00 2001 From: Brandon Davidson Date: Wed, 13 Feb 2019 02:20:22 -0800 Subject: [PATCH] Enable use of alternate hardware UARTs for logging (#427) * Enable use of alternate hardware UARTs for logging Enable use of Serial1 on ESP8266 and Serial1/Serial2 on ESP32 for logging. This is frequently done on ESP8266 to allow use of Serial for UART TX+RX, while maintaining logging output on Serial1 which is TX-only via GPIO2. * ESPHOMELIB_UART -> UART_SELECTION_UART; HW_UART -> HARDWARE_UART * Add test3 to travis; remove test4 * Set DEBUG_ESP_PORT based on logger UART setting --- .travis.yml | 1 + esphomeyaml/components/logger.py | 41 +++++-- esphomeyaml/const.py | 1 + tests/test3.yaml | 177 +++++++++++++++++++++++++++++++ 4 files changed, 213 insertions(+), 7 deletions(-) create mode 100644 tests/test3.yaml diff --git a/.travis.yml b/.travis.yml index c7d33285f6..6eb2f480f0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,6 +22,7 @@ matrix: script: - esphomeyaml tests/test1.yaml compile - esphomeyaml tests/test2.yaml compile + - esphomeyaml tests/test3.yaml compile #- python: "3.5.3" # env: TARGET=Test3.5 # install: pip install -U https://github.com/platformio/platformio-core/archive/develop.zip && pip install -e . && pip install flake8==3.6.0 pylint==2.2.2 pillow diff --git a/esphomeyaml/components/logger.py b/esphomeyaml/components/logger.py index ad713a4c2d..df212cb673 100644 --- a/esphomeyaml/components/logger.py +++ b/esphomeyaml/components/logger.py @@ -5,11 +5,10 @@ import voluptuous as vol from esphomeyaml.automation import ACTION_REGISTRY, LambdaAction import esphomeyaml.config_validation as cv from esphomeyaml.const import CONF_ARGS, CONF_BAUD_RATE, CONF_FORMAT, CONF_ID, CONF_LEVEL, \ - CONF_LOGS, CONF_TAG, CONF_TX_BUFFER_SIZE + CONF_LOGS, CONF_TAG, CONF_TX_BUFFER_SIZE, CONF_HARDWARE_UART from esphomeyaml.core import EsphomeyamlError, Lambda, CORE from esphomeyaml.cpp_generator import Pvariable, RawExpression, add, process_lambda, statement from esphomeyaml.cpp_types import App, Component, esphomelib_ns, global_ns, void - from esphomeyaml.py_compat import text_type LOG_LEVELS = { @@ -33,10 +32,36 @@ LOG_LEVEL_TO_ESP_LOG = { LOG_LEVEL_SEVERITY = ['NONE', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'VERBOSE', 'VERY_VERBOSE'] +UART_SELECTION_ESP32 = ['UART0', 'UART1', 'UART2'] + +UART_SELECTION_ESP8266 = ['UART0', 'UART0_SWAP', 'UART1'] + +HARDWARE_UART_TO_UART_SELECTION = { + 'UART0': global_ns.UART_SELECTION_UART0, + 'UART0_SWAP': global_ns.UART_SELECTION_UART0_SWAP, + 'UART1': global_ns.UART_SELECTION_UART1, + 'UART2': global_ns.UART_SELECTION_UART2, +} + +HARDWARE_UART_TO_SERIAL = { + 'UART0': 'Serial', + 'UART0_SWAP': 'Serial', + 'UART1': 'Serial1', + 'UART2': 'Serial2', +} + # pylint: disable=invalid-name is_log_level = cv.one_of(*LOG_LEVELS, upper=True) +def uart_selection(value): + if CORE.is_esp32: + return cv.one_of(*UART_SELECTION_ESP32, upper=True)(value) + if CORE.is_esp8266: + return cv.one_of(*UART_SELECTION_ESP8266, upper=True)(value) + raise NotImplementedError + + def validate_local_no_higher_than_global(value): global_level = value.get(CONF_LEVEL, 'DEBUG') for tag, level in value.get(CONF_LOGS, {}).items(): @@ -51,7 +76,8 @@ LogComponent = esphomelib_ns.class_('LogComponent', Component) CONFIG_SCHEMA = vol.All(vol.Schema({ cv.GenerateID(): cv.declare_variable_id(LogComponent), vol.Optional(CONF_BAUD_RATE, default=115200): cv.positive_int, - vol.Optional(CONF_TX_BUFFER_SIZE): cv.validate_bytes, + vol.Optional(CONF_TX_BUFFER_SIZE, default=512): cv.validate_bytes, + vol.Optional(CONF_HARDWARE_UART, default='UART0'): uart_selection, vol.Optional(CONF_LEVEL): is_log_level, vol.Optional(CONF_LOGS): vol.Schema({ cv.string: is_log_level, @@ -60,10 +86,10 @@ CONFIG_SCHEMA = vol.All(vol.Schema({ def to_code(config): - rhs = App.init_log(config.get(CONF_BAUD_RATE)) + rhs = App.init_log(config.get(CONF_BAUD_RATE), + config.get(CONF_TX_BUFFER_SIZE), + HARDWARE_UART_TO_UART_SELECTION[config.get(CONF_HARDWARE_UART)]) log = Pvariable(config[CONF_ID], rhs) - if CONF_TX_BUFFER_SIZE in config: - add(log.set_tx_buffer_size(config[CONF_TX_BUFFER_SIZE])) if CONF_LEVEL in config: add(log.set_global_log_level(LOG_LEVELS[config[CONF_LEVEL]])) for tag, level in config.get(CONF_LOGS, {}).items(): @@ -79,7 +105,8 @@ def required_build_flags(config): is_at_least_verbose = this_severity >= verbose_severity has_serial_logging = config.get(CONF_BAUD_RATE) != 0 if CORE.is_esp8266 and has_serial_logging and is_at_least_verbose: - flags.append(u"-DDEBUG_ESP_PORT=Serial") + debug_serial_port = HARDWARE_UART_TO_SERIAL[config.get(CONF_HARDWARE_UART)] + flags.append(u"-DDEBUG_ESP_PORT={}".format(debug_serial_port)) flags.append(u"-DLWIP_DEBUG") DEBUG_COMPONENTS = { 'HTTP_CLIENT', diff --git a/esphomeyaml/const.py b/esphomeyaml/const.py index 7d073f7413..797033ed04 100644 --- a/esphomeyaml/const.py +++ b/esphomeyaml/const.py @@ -264,6 +264,7 @@ CONF_DELAYED_OFF = 'delayed_off' CONF_UUID = 'uuid' CONF_TYPE = 'type' CONF_SPI_ID = 'spi_id' +CONF_HARDWARE_UART = 'hardware_uart' CONF_UART_ID = 'uart_id' CONF_UID = 'uid' CONF_TX_PIN = 'tx_pin' diff --git a/tests/test3.yaml b/tests/test3.yaml new file mode 100644 index 0000000000..5432a97053 --- /dev/null +++ b/tests/test3.yaml @@ -0,0 +1,177 @@ +esphomeyaml: + name: $devicename + platform: ESP8266 + board: esp07 + # Use this for testing while developing: + # esphomelib_version: + # local: ~/path/to/esphomelib + build_path: build/test3 + +substitutions: + devicename: test3 + +api: + +i2c: + sda: 4 + scl: 5 + scan: False + +spi: + clk_pin: GPIO12 + mosi_pin: GPIO13 + miso_pin: GPIO14 + +uart: + tx_pin: GPIO1 + rx_pin: GPIO3 + baud_rate: 115200 + +ota: + safe_mode: True + port: 3286 + +logger: + hardware_uart: UART1 + level: DEBUG + +web_server: + +deep_sleep: + run_duration: 20s + sleep_duration: 50s + +sensor: + - platform: pmsx003 + type: PMSX003 + pm_1_0: + name: "PM 1.0 Concentration" + pm_2_5: + name: "PM 2.5 Concentration" + pm_10_0: + name: "PM 10.0 Concentration" + - platform: pmsx003 + type: PMS5003T + pm_2_5: + name: "PM 2.5 Concentration" + temperature: + name: "PMS Temperature" + humidity: + name: "PMS Humidity" + - platform: pmsx003 + type: PMS5003ST + pm_2_5: + name: "PM 2.5 Concentration" + temperature: + name: "PMS Temperature" + humidity: + name: "PMS Humidity" + formaldehyde: + name: "PMS Formaldehyde Concentration" + - platform: cse7766 + voltage: + name: "CSE7766 Voltage" + current: + name: "CSE7766 Current" + power: + name: "CSE776 Power" + - platform: apds9960 + type: proximity + name: APDS9960 Proximity + - platform: apds9960 + type: clear + name: APDS9960 Clear + - platform: apds9960 + type: red + name: APDS9960 Red + - platform: apds9960 + type: green + name: APDS9960 Green + - platform: apds9960 + type: blue + name: APDS9960 Blue + - platform: homeassistant + entity_id: sensor.hello_world + id: ha_hello_world + +time: +- platform: homeassistant + +apds9960: + address: 0x20 + update_interval: 60s + +binary_sensor: + - platform: apds9960 + direction: up + name: APDS9960 Up + - platform: apds9960 + direction: down + name: APDS9960 Down + - platform: apds9960 + direction: left + name: APDS9960 Left + - platform: apds9960 + direction: right + name: APDS9960 Right + - platform: homeassistant + entity_id: binary_sensor.hello_world + id: ha_hello_world_binary + +remote_receiver: + pin: GPIO12 + dump: [] + +status_led: + pin: GPIO2 + +text_sensor: + - platform: version + name: "Esphomelib Version" + icon: mdi:icon + id: version_sensor + on_value: + - lambda: !lambda |- + ESP_LOGD("main", "The state is %s=%s", x.c_str(), id(version_sensor).state.c_str()); + - script.execute: my_script + - homeassistant.service: + service: notify.html5 + data: + title: New Humidity + data_template: + message: The humidity is {{ my_variable }}%. + variables: + my_variable: |- + return id(version_sensor).state; + - platform: template + name: "Template Text Sensor" + lambda: |- + return {"Hello World"}; + - platform: homeassistant + entity_id: sensor.hello_world2 + id: ha_hello_world2 + +script: + - id: my_script + then: + - lambda: 'ESP_LOGD("main", "Hello World!");' + +stepper: + - platform: uln2003 + id: my_stepper + pin_a: GPIO12 + pin_b: GPIO13 + pin_c: GPIO14 + pin_d: GPIO15 + sleep_when_done: no + step_mode: HALF_STEP + max_speed: 250 steps/s + + # Optional: + acceleration: inf + deceleration: inf + +interval: + interval: 5s + then: + - logger.log: "Interval Run"