Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic Temperature and Humidty PayloadConvert functions #917

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/payload.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class PayloadConvert {
void addVoltage(uint16_t value);
void addGPS(gpsStatus_t value);
void addBME(bmeStatus_t value);
void addTempHum(float temperature, float humidity);
void addButton(uint8_t value);
void addSensor(uint8_t[]);
void addTime(time_t value);
Expand Down
33 changes: 33 additions & 0 deletions src/payload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ void PayloadConvert::addBME(bmeStatus_t value) {
#endif
}

void addTempHum(float temperature, float humidity) {
int16_t temperature = (int16_t)(temperature); // float -> int
uint16_t humidity = (uint16_t)(humidity); // float -> int
buffer[cursor++] = highByte(temperature);
buffer[cursor++] = lowByte(temperature);
buffer[cursor++] = highByte(humidity);
buffer[cursor++] = lowByte(humidity);
}

void PayloadConvert::addSDS(sdsStatus_t sds) {
#if (HAS_SDS011)
char tempBuffer[10 + 1];
Expand Down Expand Up @@ -218,6 +227,11 @@ void PayloadConvert::addBME(bmeStatus_t value) {
#endif
}

void PayloadConvert::addTempHum(float temperature, float humidity) {
writeFloat(temperature);
writeFloat(humidity);
}

void PayloadConvert::addSDS(sdsStatus_t sds) {
#if (HAS_SDS011)
writeUint16((uint16_t)(sds.pm10 * 10));
Expand Down Expand Up @@ -471,6 +485,25 @@ void PayloadConvert::addBME(bmeStatus_t value) {
#endif // HAS_BME
}

void addTempHum(float temperature, float humidity) {
// data value conversions to meet cayenne data type definition
// 0.1°C per bit => -3276,7 .. +3276,7 °C
int16_t temp = temperature * 10;
// 0.5% per bit => 0 .. 128 %C
uint16_t hum = humidity * 2;
#if (PAYLOAD_ENCODER == 3)
buffer[cursor++] = LPP_TEMPERATURE_CHANNEL;
#endif
buffer[cursor++] = LPP_TEMPERATURE; // 2 bytes 0.1 °C Signed MSB
buffer[cursor++] = highByte(temperature);
buffer[cursor++] = lowByte(temperature);
#if (PAYLOAD_ENCODER == 3)
buffer[cursor++] = LPP_HUMIDITY_CHANNEL;
#endif
buffer[cursor++] = LPP_HUMIDITY; // 1 byte 0.5 % Unsigned
buffer[cursor++] = humidity;
}

void PayloadConvert::addButton(uint8_t value) {
#ifdef HAS_BUTTON
#if (PAYLOAD_ENCODER == 3)
Expand Down