代码: 全选
#define PLUGIN_099
#define PLUGIN_ID_099 99
#define PLUGIN_NAME_099 "PLANTOWER HCHO"
#define PLUGIN_VALUENAME1_099 "HCHO"
#define PLUGIN_READ_TIMEOUT 3000
boolean Plugin_099_init = false;
#include <SoftwareSerial.h>
SoftwareSerial *Plugin_099_SoftSerial;
// 9-bytes CMD HCHO read command
byte tcoCmdReadHCHO[7] = {0x42,0x4d,0x01,0x00,0x00,0x00,0x90};
byte tcoResp[9]; // 9 bytes bytes response
boolean Plugin_099(byte function, struct EventStruct *event, String& string)
{
bool success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_099;
Device[deviceCount].Type = DEVICE_TYPE_DUAL;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_099);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_099));
break;
}
case PLUGIN_INIT:
{
Plugin_099_SoftSerial = new SoftwareSerial(Settings.TaskDevicePin1[event->TaskIndex], Settings.TaskDevicePin2[event->TaskIndex]);
Plugin_099_SoftSerial->begin(9600);
addLog(LOG_LEVEL_INFO, F("HCHO: Init OK "));
timerSensor[event->TaskIndex] = millis();
Plugin_099_init = true;
success = true;
break;
}
case PLUGIN_READ:
{
if (Plugin_099_init)
{
//send read HCHO command
int nbBytesSent = Plugin_099_SoftSerial->write(tcoCmdReadHCHO, 7);
if (nbBytesSent != 7) {
String log = F("HCHO: Error, nb bytes sent != 9 : ");
log += nbBytesSent;
addLog(LOG_LEVEL_INFO, log);
}
memset(tcoResp, 0, sizeof(tcoResp));
long start = millis();
int counter = 0;
while (((millis() - start) < PLUGIN_READ_TIMEOUT) && (counter < 9)) {
if (Plugin_099_SoftSerial->available() > 0) {
tcoResp[counter++] = Plugin_099_SoftSerial->read();
} else {
delay(2);
}
}
if (counter < 7){
addLog(LOG_LEVEL_INFO, F("HCHO: Error, timeout while trying to read"));
}
float hcho = 0;
int i;
signed int temp = 0;
unsigned int s = 0;
float u = 0;
byte crc = 0;
float hch=0;
for (i = 2; i < 30; i++);
if (tcoResp[0] == 0x42) {
//calculate CO2 HCHO
unsigned int tcoRespHigh = (unsigned int) tcoResp[6];
unsigned int tcoRespLow = (unsigned int) tcoResp[7];
hch = (256*tcoRespHigh) + tcoRespLow;
if(4==tcoResp[5])
{hcho=hch/1000;}
if(3==tcoResp[5])
{hcho=hch/100;}
if(2==tcoResp[5])
{hcho=hch/10;}
if(1==tcoResp[5])
{hcho=hch;}
success = true;
UserVar[event->BaseVarIndex] = (float)hcho;
String log = F("HCHO: ");
// Log values in all cases
log += F("HCHO value: ");
log += hcho;
addLog(LOG_LEVEL_INFO, log);
break;
}
}
break;
}
}
return success;
}