与c++代码漫长的一天后,我最终与非工作的解决方案。我调整的主要代码,替代德国变量和相应的英语的分离到他们的子字符串的消息。
我试图通过传感器数据与一个if语句,但挣扎。这是我目前的进展和一些描述如果有人能指出我错过了(@danielw吗?
)。
# include“esphome。h”# include <字符串> / / # include”传感器。使用名称空间std h”;类HausbusRead:公共PollingComponent UARTDevice{公众:HausbusRead (UARTComponent *父母):PollingComponent (1000), UARTDevice(父){}TextSensor * LastHausBusMessage = new TextSensor () * LastHausBusID = new TextSensor () * LastHausBusSensorType = new TextSensor () * LastHausBusSensorID = new TextSensor () * LastHausBusSensorState = new TextSensor () * LastHausBusSensorValue = new TextSensor ();传感器* Hausbus_taster_1395_brightness =新的传感器();传感器* Hausbus_taster_1395_temperature =新的传感器();传感器* Hausbus_taster_1395_humidity =新的传感器();char RS485Message [36] = {};std:: string Message = " ", device_id = " ", sensor_type = " ", sensor_id = " ", sensor_state = " ", sensor_value = " ", outputsensor = " ";int msg_len = 0;int point_counter = 0; void setup() override { //pinMode(12, Message); // no longer necessary with RS3485 (only needed for DE/RE) //pinMode(14, Message); // no longer necessary with RS3485 (only needed for DE/RE) }//end of setup void update() override { while (available() > 0) { //digitalWrite(12, LOW); // no longer necessary with RS3485 (only needed for DE/RE) //digitalWrite(14, LOW); // no longer necessary with RS3485 (only needed for DE/RE) byte incomingByte = read(); // get last byte from UART switch (incomingByte) // is it the last or the first Byte? { case 0xFD: // every message starts with 0xFD, this means here starts a new message msg_len = 0; point_counter = 0; RS485Message[msg_len] = incomingByte; // add the incoming byte to the array break; case 0xFE: // every message ends with 0xFE, this means the message ends here for (int i = 1; i <=msg_len; i++) // create a string from the array for the HAss Sensor { Message = Message + RS485Message[i]; }//end of for outputsensor = "Hausbus_taster_" + device_id + "_"; if (sensor_type == "BRS") { outputsensor = outputsensor + "brightness"; } else if (sensor_type == "TMP") { outputsensor = outputsensor + "temperature"; } else if (sensor_type == "RHD") { outputsensor = outputsensor + "humidity"; } outputsensor->publish_state(sensor_value); LastHausBusMessage->publish_state(Message); // push the strings to the sensors (currently for debugging purposes only) LastHausBusID->publish_state(device_id); LastHausBusSensorType->publish_state(sensor_type); LastHausBusSensorID->publish_state(sensor_id); LastHausBusSensorState->publish_state(sensor_state); LastHausBusSensorValue->publish_state(sensor_value); Message = ""; // Reset the strings device_id = ""; sensor_type = ""; sensor_id = ""; sensor_state = ""; sensor_value = ""; outputsensor = ""; break; case '.': // Count the . chars to separate the substrings of the message point_counter++; // no break, we want to go to default after increasing the . counter default: // in case it is not the beginning oder the end of the message msg_len = msg_len + 1; RS485Message[msg_len] = incomingByte; // add the last byte to the array if (msg_len == 35) { msg_len = 0; // stupidest way of error handling so far Message = ""; }//end of if if(RS485Message[msg_len] != '.') // jump over the . chars, they are useless { switch (point_counter) { case 0: // 1st substring is the DeviceID device_id = device_id + RS485Message[msg_len]; break; case 1: // 2nd substring is the sensor type sensor_type = sensor_type + RS485Message[msg_len]; break; case 2: // 3rd substring is the sensor id sensor_id = sensor_id + RS485Message[msg_len]; break; case 3: // 4th substring is the sensor state sensor_state = sensor_state + RS485Message[msg_len]; break; case 4: // 5th substring is the sensor value sensor_value = sensor_value + RS485Message[msg_len]; break; }//end of switch }//end of if break; }//end of switch }//end of while }//end of update };//end of class
正如之前提到的,我想我必须定义所有Multitaster的所有传感器TextSensors
或传感器
在c++代码(目前只有一个设备有三个传感器是在上面的代码中定义)。我意识到,每个开关都是一个传感器,所以每个Multitaster 9传感器定义,有22个设备,我最终在近220个变量,多数工作都是复制粘贴和excel魔法,它不是太难。一般比例是非常糟糕的,尽管它是静态的,因此接受的。
但是我有点失去了一部分,我想发布电流传感器的值
outputsensor = " Hausbus_taster_ " + device_id +“_”;如果(sensor_type = =“集团”){outputsensor = outputsensor +“亮度”;}else if (sensor_type = = TMP) {outputsensor = outputsensor +“温度”;}else if (sensor_type = = RHD) {outputsensor = outputsensor +“湿度”;}outputsensor - > publish_state (sensor_value);
在我看来这是一个非常优雅的解决方案,使用变量,只发布之前我从UART读什么——例如温度的值19日,5
传感器的Hausbus_taster_1395_temperature
。
来完成,我想“创建”正确的传感器名称(即正确的变量名称发布)在运行时和——在那之后发布。我没有运气谷歌一下,如何将一个字符串值转换为被解释为一个变量的名字,也许我在寻找错误的搜索词,这叫一个程序员的语言怎么样?
另外上面的处理代码,我不确定如何处理yaml中代码的输出。目前我使用类似
传感器:平台:定制λ:| -汽车my_hausbus = new Hausbusread (id (HausbusUART));App.register_component (my_hausbus);返回{my_hausbus - > Hausbus_taster_1395_brightness, my_hausbus - > Hausbus_taster_1395_temperature, my_hausbus - > Hausbus_taster_1395_humidity};传感器:名字:“Hausbus_taster_1395_brightness”——名字:“Hausbus_taster_1395_temperature”——名字:“Hausbus_taster_1395_humidity”
得到的值在HA相应的传感器。但这尺度比在变量定义。我真的应该返回220值吗?重要,只有一个220的值,如Hausbus_taster_1395_temperature = 19日5
,其他219个值仅仅是忽略了在这种情况下吗?
再次:请原谅我有时很愚蠢的问题,也许我应该投入更多的时间在google,但是很难,当你不知道正确的术语。我希望我的无知会帮助别人有一天同样的问题。