本帖最后由 honglei 于 2022-6-18 12:12 编辑
感谢建议
我给家里的所有DIY智能设备写了一个基础框架,比较复杂;代码比较多;个人也懒,很多模块没有完善,我完善下再发git吧
整体上用了MQTT Discovery(https://www.home-assistant.io/docs/mqtt/discovery/);然后通过IRremoteESP8266来获取和发送
空调(恒温器)文档:https://www.home-assistant.io/integrations/climate.mqtt/
先分享下空调部分的,如下
#include "device_daikin.h"
#include "debug.h"
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <IRrecv.h>
#include <IRutils.h>
#include <ir_Daikin.h>
namespace VIMEE_DEVICE_CLIMATE {
//Tag
static char *TAG = "Daikin";
//IR输出IO
static uint16_t IRout = 5;
static uint16_t IRin = 4;
IRDaikin152 ac = IRDaikin152(IRout);
IRrecv irrecv = IRrecv(IRin, 1024, 50, true);
decode_results results;
bool lock = false;
//初始化
device_daikin::device_daikin (String nm, String dc) {
this->init(nm, dc);
//模式
char *ml[] = {"auto","off","dry","cool","heat","fan_only"};
set_mode_list(ml, 6);
//风扇模式
char *fl[] = {"low", "middle-low", "medium", "middle-high", "high", "auto", "quiet"};
set_fan_list(fl, 7);
//预设模式
char *pl[] = {"econo","comfort","powerful"};
set_preset_list(pl, 3);
//默认导风模式
char *sl[] = {"on","off","v-on", "h-on"};
set_swing_list(sl, 4);
}
//启动方法
void device_daikin::device_setup () {
ac.begin();
irrecv.enableIRIn();
}
void device_daikin::IR_recv (decode_results *results) {
if (results->bits != 152) {
return;
}
if (lock) {
lock = false;
return;
}
ac.setRaw(results->state);
//调试接收信息
VIMEE_DEBUG::print("{RECV} " + ac.toString(), 3, TAG);
//设置模式
if (ac.getPower() == 0) {
change_mode(get_mode_list(1));
} else if (ac.getMode() == 6) {
change_mode(get_mode_list(5));
} else {
change_mode(get_mode_list(ac.getMode()));
}
//设置预设
change_preset("none");
if (ac.getPowerful() == 1) {
change_preset("powerful");
}
if (ac.getEcono() == 1) {
change_preset("econo");
}
//设置风扇
if (ac.getFan() < 10) {
change_fan(get_fan_list(ac.getFan()-1));
} else {
change_fan(get_fan_list(ac.getFan()-5));
}
//设置导风
change_swing("off");
if (ac.getSwingV() == 1 && ac.getSwingH() == 1) {
change_swing("on");
} else if (ac.getSwingV() == 1) {
change_swing("v-on");
} else if (ac.getSwingH() == 1) {
change_swing("h-on");
}
//设置温度
change_temp(ac.getTemp());
//发送状态
mqtt_send_state();
}
void device_daikin::device_loop () {
if (irrecv.decode(&results)) {
IR_recv(&results);
yield();
}
}
void device_daikin::action(){
IR_send();
mqtt_send_state();
}
void device_daikin::IR_send(){
//获取模式
String m = get_mode();
if(m != "off"){
//打开
ac.on();
//设置模式
if (m == "cool") {
ac.setMode(kDaikinCool);
} else if (m == "heat") {
ac.setMode(kDaikinHeat);
} else if (m == "dry") {
ac.setMode(kDaikinDry);
} else if (m == "fan_only") {
ac.setMode(kDaikinFan);
} else {
ac.setMode(kDaikinAuto);
}
//预设模式
String p = get_preset();
if (p == "powerful") {
ac.setPowerful(true);
} else if(p == "econo") {
ac.setEcono(true);
}
//设置温度
ac.setTemp(get_temp());
//设置风扇
String f = get_fan();
if (f == "low"){
ac.setFan(1);
} else if (f == "middle-low"){
ac.setFan(2);
} else if (f == "medium"){
ac.setFan(3);
} else if (f == "middle-high"){
ac.setFan(4);
} else if (f == "high"){
ac.setFan(5);
} else if (f == "quiet"){
ac.setFan(kDaikinFanQuiet);
} else {
ac.setFan(kDaikinFanAuto);
}
//设置风摆
String s = get_swing();
ac.setSwingV(false);
ac.setSwingH(false);
if (s == "on") {
ac.setSwingV(true);
ac.setSwingH(true);
} else if (s == "v-on") {
ac.setSwingV(true);
} else if (s == "h-on") {
ac.setSwingH(true);
}
} else {
//关闭
ac.off();
}
//调试发送信息
VIMEE_DEBUG::print("{SEND}" + ac.toString(), 3, TAG);
//锁定接收
lock = true;
ac.send();
}
}
|