tiaanv / esphome_gy906

esphome example for the uart based GY-601 optical temperature sensor.
4 stars 1 forks source link

Custom request #2

Open tiaanv opened 3 years ago

tiaanv commented 3 years ago

Hi Sir, I’m newbie on ESPhome custom components and not very familiar with C++. I have a dehumidifier with a serial communication interface, want integrate HASSIO by ESPhome. Trying to learn your code, and reference to others on github, after week trying but unfortunately fail until now, came look for your help.

I can get message by Serial Debugger Tools: send { 0x01, 0x03, 0x00, 0x2A, 0x00, 0x02, 0xE5, 0xC3 } respond { 01 03 04 00 50 00 3C FA 33} 00, 50, 00, 3C this 4 bytes are factory set value, RH 60% (0x3C) stop, RH 80% (0x50) start. send { 0x01, 0x03, 0x00, 0x30, 0x00, 0x05, 0x85, 0xC6 } respond { 01 03 0A 00 15 00 3F 00 15 00 15 00 04 04 E1 } first 00, 15 will be detected ambient temperature 21°C; 00, 3F will be detected relative humidity 63 RH%; second and third 00, 15, 00, 15 will be the semicon refrigeration board cool side 21°C, hot side 21°C(machine suspend now); 00, 04 is the machine working status, (4 or 0 = suspend, 3 = on)

Want to have these 7 sensors and update every 10 seconds, looking forward your assist or advise.

_Originally posted by @UJStudio in https://github.com/tiaanv/esphome_gy906/issues/1#issuecomment-741017259_

tiaanv commented 3 years ago

I moved this to a separate issue, as it is in no way related to the OP

tiaanv commented 3 years ago

I unfortunately don't have the time or the hardware to dev this, but the principles are there... If you break the code down you should be able to figure it out.

UJStudio commented 3 years ago

I got it work today, the following code hope can help someone.

`#include "esphome.h"

class mydh1 : public PollingComponent, public UARTDevice { Sensor xsetMax {nullptr}; Sensor xsetMin {nullptr}; Sensor xambTem {nullptr}; Sensor xambHum {nullptr}; Sensor xsemiColTem {nullptr}; Sensor xsemiHotTem {nullptr}; Sensor *xrunStatus {nullptr};

public: mydh1(UARTComponent parent, Sensor setMax, Sensor setMin, Sensor ambTem, Sensor ambHum, Sensor semiColTem, Sensor semiHotTem, Sensor runStatus) : UARTDevice(parent) , xsetMax(setMax), xsetMin(setMin), xambTem(ambTem), xambHum(ambHum), xsemiColTem(semiColTem), xsemiHotTem(semiHotTem), xrunStatus(runStatus) {}

byte cmdGetSetMinMax[8] = { 0x01, 0x03, 0x00, 0x2A, 0x00, 0x02, 0xE5, 0xC3 }; byte cmdGetPrtStatus[8] = { 0x01, 0x03, 0x00, 0x30, 0x00, 0x05, 0x85, 0xC6 }; unsigned char Re_buf_MinMax[9],Re_buf_Status[15],counter=0; int setMinVal=0,setMaxVal=0,ambTemVal=0,ambHumVal=0,semiColTemVal=0,semiHotTemVal=0; unsigned int runStatusVal=0;

void setup() override { this->set_update_interval(5000); flush(); }

void loop() override { }

void update() override {

write_array(cmdGetSetMinMax,sizeof(cmdGetSetMinMax));
delay(1000);
while (available()) {
    Re_buf_MinMax[counter]=(unsigned char)read();
    if(counter==0&&Re_buf_MinMax[0]!=0x01) return;    
    counter++;
}
counter = 0;

setMaxVal = Re_buf_MinMax[6];
setMinVal = Re_buf_MinMax[4];
if (xsetMax != nullptr) xsetMax->publish_state(setMaxVal);
if (xsetMin != nullptr) xsetMin->publish_state(setMinVal);

write_array(cmdGetPrtStatus,sizeof(cmdGetPrtStatus));
delay(1000);
while (available()) {
    Re_buf_Status[counter]=(unsigned char)read();
    if(counter==0&&Re_buf_Status[0]!=0x01) return;    
    counter++;       
}
counter = 0;

ambTemVal = Re_buf_Status[4];
ambHumVal = Re_buf_Status[6];
semiHotTemVal = Re_buf_Status[10];
semiColTemVal = Re_buf_Status[8];
runStatusVal = Re_buf_Status[12];

if (xambTem != nullptr) xambTem->publish_state(ambTemVal);
if (xambHum != nullptr) xambHum->publish_state(ambHumVal);
if (xsemiColTem != nullptr) xsemiColTem->publish_state(semiColTemVal);
if (xsemiHotTem != nullptr) xsemiHotTem->publish_state(semiHotTemVal);
if (xrunStatus != nullptr) xrunStatus->publish_state(runStatusVal);

} };`

UJStudio commented 3 years ago

Thanks your share & help. @tiaanv