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

Polish sensor support in Home Automation Arduino sketch #29

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Home Assistant: add sensor device_class
hPa, and not Pa, is a supported pressure unit, so adjust the values
accordingly.

See https://www.home-assistant.io/integrations/sensor/#device-class
  • Loading branch information
Thynix committed Nov 1, 2021
commit f71ea91d18c01837162d0f6700ac6fd776a7ced0
31 changes: 16 additions & 15 deletions Arduino/Examples/Home_Assistant/Home_Assistant.ino
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ char password[] = "PUT WIFI PASSWORD HERE IN QUOTES"; // network password
WiFiClient client;

// Buffers for assembling http POST requests
char postBuffer[450] = {0};
char postBuffer[512] = {0};
char fieldBuffer[70] = {0};

// Structs for data
Expand All @@ -77,23 +77,23 @@ SoundData_t soundData = {0};
// dashboard in Home Assistant. The icons can be chosen from
// https://cdn.materialdesignicons.com/5.3.45/
// (remove the "mdi-" part from the icon name).
// The attribute fields are: {name, unit, icon, decimal places}
HA_Attributes_t pressure = {"Pressure","Pa","weather-cloudy",0};
HA_Attributes_t humidity = {"Humidity","%","water-percent",1};
HA_Attributes_t illuminance = {"Illuminance","lx","white-balance-sunny",2};
HA_Attributes_t soundLevel = {"Sound level","dBA","microphone",1};
HA_Attributes_t peakAmplitude = {"Sound peak","mPa","waveform",2};
HA_Attributes_t AQI = {"Air Quality Index"," ","thought-bubble-outline",1};
HA_Attributes_t AQ_assessment = {"Air quality assessment","","flower-tulip",0};
// The attribute fields are: {name, device_class, unit, icon, decimal places}
HA_Attributes_t pressure = {"Pressure", "pressure", "hPa", "weather-cloudy", 1};
HA_Attributes_t humidity = {"Humidity", "humidity", "%", "water-percent", 1};
HA_Attributes_t illuminance = {"Illuminance", "illuminance", "lx", "white-balance-sunny", 2};
HA_Attributes_t soundLevel = {"Sound level", "None", "dBA", "microphone", 1};
HA_Attributes_t peakAmplitude = {"Sound peak", "None", "mPa", "waveform", 2};
HA_Attributes_t AQI = {"Air Quality Index", "aqi", "", "thought-bubble-outline", 1};
HA_Attributes_t AQ_assessment = {"Air quality assessment", "None", "", "flower-tulip", 0};
#if (PARTICLE_SENSOR == PARTICLE_SENSOR_PPD42)
HA_Attributes_t particulates = {"Particle concentration","ppL","chart-bubble",0};
HA_Attributes_t particulates = {"Particle concentration", "pm10", "ppL", "chart-bubble", 0};
#else
HA_Attributes_t particulates = {"Particle concentration",SDS011_UNIT_SYMBOL,"chart-bubble",2};
HA_Attributes_t particulates = {"Particle concentration", "pm25", SDS011_UNIT_SYMBOL, "chart-bubble", 2};
#endif
#ifdef USE_FAHRENHEIT
HA_Attributes_t temperature = {"Temperature",FAHRENHEIT_SYMBOL,"thermometer",1};
HA_Attributes_t temperature = {"Temperature", "temperature", FAHRENHEIT_SYMBOL, "thermometer", 1};
#else
HA_Attributes_t temperature = {"Temperature",CELSIUS_SYMBOL,"thermometer",1};
HA_Attributes_t temperature = {"Temperature", "temperature", CELSIUS_SYMBOL, "thermometer", 1};
#endif


Expand Down Expand Up @@ -144,7 +144,7 @@ void loop() {

// Send data to Home Assistant
sendNumericData(&temperature, (uint32_t) T_intPart, T_fractionalPart, isPositive);
sendNumericData(&pressure, (uint32_t) airData.P_Pa, 0, true);
sendNumericData(&pressure, (uint32_t) roundf(airData.P_Pa / 100.0), 0, true);
sendNumericData(&humidity, (uint32_t) airData.H_pc_int, airData.H_pc_fr_1dp, true);
sendNumericData(&illuminance, (uint32_t) lightData.illum_lux_int, lightData.illum_lux_fr_2dp, true);
sendNumericData(&soundLevel, (uint32_t) soundData.SPL_dBA_int, soundData.SPL_dBA_fr_1dp, true);
Expand Down Expand Up @@ -206,12 +206,13 @@ void http_POST_Home_Assistant(const HA_Attributes_t * attributes, const char * v
// Assemble the JSON content string:
sprintf(postBuffer, "{\"state\":%s,\"attributes\":{"
"\"unique_id\":\"" SENSOR_NAME "\","
"\"device_class\":\"%s\","
"\"unit_of_measurement\":\"%s\","
"\"friendly_name\":\"%s\","
"\"icon\":\"mdi:%s\"}"
"}",
valueText,
attributes->unit, attributes->name, attributes->icon);
attributes->device_class, attributes->unit, attributes->name, attributes->icon);

sprintf(fieldBuffer,"Content-Length: %u", strlen(postBuffer));
client.println(fieldBuffer);
Expand Down
1 change: 1 addition & 0 deletions Arduino/Metriful_Sensor/Metriful_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ typedef struct {
// Struct used in the Home Assistant example
typedef struct {
const char * name;
const char * device_class;
const char * unit;
const char * icon;
uint8_t decimalPlaces;
Expand Down