Skip to content
This repository has been archived by the owner on May 20, 2023. It is now read-only.

Commit

Permalink
大更新,以适配ESPHome 1.14版本
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel-0-0 committed Nov 24, 2019
1 parent 27a2037 commit 2719167
Show file tree
Hide file tree
Showing 9 changed files with 704 additions and 513 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
doc/dc1引脚功能及走线定位.xlsx
image/PSD
自动git更新.bat
自动git更新.bat
.DS_Store
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ ioBroker是基于nodejs的物联网的集成平台,为物联网设备提供核

## 可能存在的BUG
- 可能会出现开关重置的现象,怀疑是wifi模块重启或者是CAT9554驱动的bug,未验证
- 电量统计可能会有问题,还需测试优化

## 正在进行中
- 迁移到新版ESPHOME
Expand Down
54 changes: 54 additions & 0 deletions esphome/components/cat9554/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import i2c
from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED

DEPENDENCIES = ['i2c']
MULTI_CONF = True

cat9554_ns = cg.esphome_ns.namespace('cat9554')
CAT9554GPIOMode = cat9554_ns.enum('CAT9554GPIOMode')
CAT9554_GPIO_MODES = {
'INPUT': CAT9554GPIOMode.CAT9554_INPUT,
'OUTPUT': CAT9554GPIOMode.CAT9554_OUTPUT,
}

CAT9554Component = cat9554_ns.class_('CAT9554Component', cg.Component, i2c.I2CDevice)
CAT9554GPIOPin = cat9554_ns.class_('CAT9554GPIOPin', cg.GPIOPin)

CONF_CAT9554 = 'cat9554'
CONFIG_SCHEMA = cv.Schema({
cv.Required(CONF_ID): cv.declare_id(CAT9554Component),
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20))


def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield i2c.register_i2c_device(var, config)


def validate_cat9554_gpio_mode(value):
value = cv.string(value)
return cv.enum(CAT9554_GPIO_MODES, upper=True)(value)


CAT9554_OUTPUT_PIN_SCHEMA = cv.Schema({
cv.Required(CONF_CAT9554): cv.use_id(CAT9554Component),
cv.Required(CONF_NUMBER): cv.int_,
cv.Optional(CONF_MODE, default="OUTPUT"): validate_cat9554_gpio_mode,
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
})
CAT9554_INPUT_PIN_SCHEMA = cv.Schema({
cv.Required(CONF_CAT9554): cv.use_id(CAT9554Component),
cv.Required(CONF_NUMBER): cv.int_,
cv.Optional(CONF_MODE, default="INPUT"): validate_cat9554_gpio_mode,
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
})


@pins.PIN_SCHEMA_REGISTRY.register('cat9554', (CAT9554_OUTPUT_PIN_SCHEMA, CAT9554_INPUT_PIN_SCHEMA))
def cat9554_pin_to_code(config):
parent = yield cg.get_variable(config[CONF_CAT9554])
yield CAT9554GPIOPin.new(parent, config[CONF_NUMBER], config[CONF_MODE], config[CONF_INVERTED])
102 changes: 102 additions & 0 deletions esphome/components/cat9554/cat9554.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "cat9554.h"
#include "esphome/core/log.h"

namespace esphome {
namespace cat9554 {

static const char *TAG = "cat9554";

void CAT9554Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up CAT9554...");
if (!this->read_gpio_()) {
ESP_LOGE(TAG, "CAT9554 not available under 0x%02X", this->address_);
this->mark_failed();
return;
}

this->write_gpio_();
this->read_gpio_();
}
void CAT9554Component::dump_config() {
ESP_LOGCONFIG(TAG, "CAT9554:");
LOG_I2C_DEVICE(this)
if (this->is_failed()) {
ESP_LOGE(TAG, "Communication with CAT9554 failed!");
}
}
bool CAT9554Component::digital_read(uint8_t pin) {
this->read_gpio_();
return this->input_mask_ & (1 << pin);
}
void CAT9554Component::digital_write(uint8_t pin, bool value) {
if (value) {
this->output_mask_ |= (1 << pin);
} else {
this->output_mask_ &= ~(1 << pin);
}

this->write_gpio_();
}
void CAT9554Component::pin_mode(uint8_t pin, uint8_t mode) {
switch (mode) {
case CAT9554_INPUT:
// Clear mode mask bit
this->mode_mask_ |= (1 << pin);
// Write GPIO to enable input mode
this->write_gpio_();
break;
case CAT9554_OUTPUT:
// Set mode mask bit
this->mode_mask_ &= ~(1 << pin);
break;
default:
break;
}
}
bool CAT9554Component::read_gpio_() {
if (this->is_failed())
return false;
bool success;
uint8_t data[2];
success = this->read_bytes_raw(data, 1);
this->input_mask_ = data[0];

if (!success) {
this->status_set_warning();
return false;
}
this->status_clear_warning();
return true;
}
bool CAT9554Component::write_gpio_() {
if (this->is_failed())
return false;

uint16_t value = 0;
// Pins in OUTPUT mode and where pin is HIGH.
value |= this->mode_mask_ & this->output_mask_;
// Pins in INPUT mode must also be set here
value |= ~this->mode_mask_;

uint8_t data[2];
data[0] = value;
data[1] = value >> 8;
if (!this->write_bytes_raw(data, 1)) {
this->status_set_warning();
return false;
}

this->status_clear_warning();
return true;
}
float CAT9554Component::get_setup_priority() const { return setup_priority::IO; }

void CAT9554GPIOPin::setup() { this->pin_mode(this->mode_); }
bool CAT9554GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
void CAT9554GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
void CAT9554GPIOPin::pin_mode(uint8_t mode) { this->parent_->pin_mode(this->pin_, mode); }
CAT9554GPIOPin::CAT9554GPIOPin(CAT9554Component *parent, uint8_t pin, uint8_t mode, bool inverted)
: GPIOPin(pin, mode, inverted), parent_(parent) {}

} // namespace cat9554
} // namespace esphome
62 changes: 62 additions & 0 deletions esphome/components/cat9554/cat9554.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

#include "esphome/core/component.h"
#include "esphome/core/esphal.h"
#include "esphome/components/i2c/i2c.h"

namespace esphome {
namespace cat9554 {

/// Modes for CAT9554 pins
enum CAT9554GPIOMode : uint8_t {
CAT9554_INPUT = INPUT,
CAT9554_OUTPUT = OUTPUT,
};

class CAT9554Component : public Component, public i2c::I2CDevice {
public:
CAT9554Component() = default;

/// Check i2c availability and setup masks
void setup() override;
/// Helper function to read the value of a pin.
bool digital_read(uint8_t pin);
/// Helper function to write the value of a pin.
void digital_write(uint8_t pin, bool value);
/// Helper function to set the pin mode of a pin.
void pin_mode(uint8_t pin, uint8_t mode);

float get_setup_priority() const override;

void dump_config() override;

protected:
bool read_gpio_();

bool write_gpio_();

/// Mask for the pin mode - 1 means output, 0 means input
uint16_t mode_mask_{0x00};
/// The mask to write as output state - 1 means HIGH, 0 means LOW
uint16_t output_mask_{0x00};
/// The state read in read_gpio_ - 1 means HIGH, 0 means LOW
uint16_t input_mask_{0x00};

};

/// Helper class to expose a CAT9554 pin as an internal input GPIO pin.
class CAT9554GPIOPin : public GPIOPin {
public:
CAT9554GPIOPin(CAT9554Component *parent, uint8_t pin, uint8_t mode, bool inverted = false);

void setup() override;
void pin_mode(uint8_t mode) override;
bool digital_read() override;
void digital_write(bool value) override;

protected:
CAT9554Component *parent_;
};

} // namespace cat9554
} // namespace esphome
File renamed without changes
32 changes: 15 additions & 17 deletions yaml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@

### 1、下载固件配置文件(下方右键另存为)
> #### 固件版本定义:
> - [dc1_homeassistant](https://github.com/Samuel-0-0/phicomm_dc1-esphome/raw/master/yaml/dc1_homeassistant.yaml):相对稳定版本,用于接入Home Assistant
> - [dc1_homeassistant_test](https://github.com/Samuel-0-0/phicomm_dc1-esphome/raw/master/yaml/dc1_homeassistant_test.yaml):测试版本,用于接入Home Assistant
> - dc1_mqtt:相对稳定版本,用于接入mqtt平台
> - dc1_mqtt_test:测试版本,用于接入mqtt平台
> - [dc1_homeassistant](https://github.com/Samuel-0-0/phicomm_dc1-esphome/raw/master/yaml/dc1_homeassistant.yaml):用于接入Home Assistant
> - [dc1_mqtt](https://github.com/Samuel-0-0/phicomm_dc1-esphome/raw/master/yaml/dc1_mqtt.yaml):用于接入mqtt平台

```
配置文件对应版本更新历史
dc1_homeassistant:
v2019.11.24.001:
迁移到1.14版本ESPHome
v2019.03.28.002:
编译固件前请更新esphome及esphome-core到最新版本!
1、优化按钮,解决重启问题
dc1_homeassistant_test:
dc1_mqtt:
v2019.11.24.001:
迁移到1.14版本ESPHome
v2019.08.26.001
感谢[yaming116](https://github.com/yaming116) QQ昵称:花开堪折枝的mqtt版本修改及测试,使用mqtt时禁止使用api!
dc1_mqtt_test:无
```

Expand All @@ -34,8 +36,8 @@ dc1_mqtt_test:无
```
#--------------------- 只需要改这下面的内容 ---------------------
substitutions:
#WiFi芯片版本,型号中带B的为csm64f02_b,不带B的为csm64f02
board_model: csm64f02
#WiFi芯片版本,请勿修改
board_model: esp01_1m
#设备名称(多个dc1改成不一样的)
device_name: phicomm_dc1
#WiFi_SSID名称
Expand All @@ -45,7 +47,7 @@ substitutions:
#如果SSID是隐藏的,设置为true
wifi_fast_connect: 'false'
#WiFi离线多久后重启设备,秒s/分钟min/小时h,不需要此功能设置为0s
wifi_reboot_timeout: 600s
wifi_reboot_timeout: 0s
#OTA密码
ota_password: '123456'
#与客户端(如Home Assistant)失去连接多久后重启设备,秒s/分钟min/小时h,不需要此功能设置为0s
Expand All @@ -55,20 +57,14 @@ substitutions:
#--------------------- 只需要改这上面的内容 ---------------------
```

关于WiFi模组版本的选择,请查看下图,找出自己对应的芯片版本即可。
看红色箭头的位置,带B的为csm64f02_b,不带B的为csm64f02

![image](https://github.com/Samuel-0-0/phicomm_dc1-esphome/blob/master/yaml/%E6%A8%A1%E7%BB%84%E5%9E%8B%E5%8F%B7%E9%80%89%E6%8B%A9.jpg?raw=true)

### 3、搭建编译环境及刷固件
**因platformio需要python2.7的环境,所以python2.7为必须。**

- Windows

[点此查看](https://github.com/Samuel-0-0/esphome-tools-dc1/tree/master)

- MacOS

> MacOS自带python2.7,所以无需再安装。
**以下方法暂时不可用,请勿使用**

1. [从此处下载esphome](https://github.com/Samuel-0-0/esphome)(打开页面后,右上角「 Clone or download 」 → 「 Download ZIP 」),下载后解压缩。

Expand All @@ -94,6 +90,8 @@ esphome xxxxx.yaml upload
> 与MacOS类似,参考MacOS的方法
- Docker
**以下方法暂时不可用,请勿使用**

> 注意!多个配置文件放在一起除了改文件名还要改device_name: XXX,不能出现device_name重复的现象
1. 安装[官方esphome容器](https://hub.docker.com/r/esphome/esphome)
2. 执行如下命令:
Expand Down
21 changes: 3 additions & 18 deletions yaml/dc1_homeassistant.yaml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,19 @@ esphome:
board: $board_model
#状态恢复保持
esp8266_restore_from_flash: yes
#--------------指定esphome_core来源------------------
esphome_core_version:
#指定本地来源
#local: path/to/esphome-core
#指定外部来源
repository: https://github.com/Samuel-0-0/esphome-core.git
#指定分支
branch: dc1

#----------------------------------------------------
#指定编译临时文件存放位置
build_path: build\$device_name
#指定arduino版本
arduino_version: 2.5.0
#-----------------platform相关设置-------------------
platformio_options:
#指定platform-espressif8266来源
platform: https://github.com/Samuel-0-0/platform-espressif8266.git#dc1
#----------------------------------------------------

#--------------------- 只需要改这下面的内容 ---------------------
substitutions:
#WiFi芯片版本,型号中带B的为csm64f02_b,不带B的为csm64f02
board_model: csm64f02
board_model: esp01_1m
#设备名称(多个dc1改成不一样的)
device_name: phicomm_dc1
#WiFi_SSID名称
wifi_ssid: '2L'
wifi_ssid: '777'
#WiFi密码
wifi_password: '1122334455'
#如果SSID是隐藏的,设置为true
Expand Down Expand Up @@ -93,7 +79,6 @@ i2c:
cat9554:
- id: cat9554_hub
address: 0x20
irq: GPIO4

uart:
rx_pin: GPIO13
Expand Down

0 comments on commit 2719167

Please sign in to comment.