Skip to content

Commit 14bc2ea

Browse files
committed
added bh1750 sensor support with adaptation to the changed driver
1 parent bb67059 commit 14bc2ea

3 files changed

+151
-0
lines changed

src/mgos_homeassistant.c

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "mgos_homeassistant_barometer.h"
2222
#include "mgos_homeassistant_gpio.h"
2323
#include "mgos_homeassistant_si7021.h"
24+
#include "mgos_homeassistant_bh1750.h"
2425
#include "mgos_mqtt.h"
2526

2627
static struct mgos_homeassistant *s_homeassistant = NULL;
@@ -126,6 +127,19 @@ bool mgos_homeassistant_fromjson(struct mgos_homeassistant *ha, const char *json
126127
#endif
127128
}
128129

130+
while ((h = json_next_elem(json, strlen(json), h, ".provider.bh1750", &idx, &val)) != NULL) {
131+
#ifdef MGOS_HAVE_BH1750
132+
if (!mgos_homeassistant_bh1750_fromjson(ha, val)) {
133+
LOG(LL_WARN, ("Failed to add object from provider bh1750, index %d, json "
134+
"follows:%.*s",
135+
idx, (int) val.len, val.ptr));
136+
}
137+
#else
138+
LOG(LL_ERROR, ("provider.bh1750 config found: Add bh1750-i2c to mos.yml, "
139+
"skipping .. "));
140+
#endif
141+
}
142+
129143
while ((h = json_next_elem(json, strlen(json), h, ".provider.barometer", &idx, &val)) != NULL) {
130144
#ifdef MGOS_HAVE_BAROMETER
131145
if (!mgos_homeassistant_barometer_fromjson(ha, val)) {

src/mgos_homeassistant_bh1750.c

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2020 Mircho Mirev <[email protected]>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
#ifdef MGOS_HAVE_BH1750
19+
#include <math.h>
20+
#include "mgos.h"
21+
#include "mgos_homeassistant_bh1750.h"
22+
23+
static void bh1750_timer(void *user_data) {
24+
struct mgos_homeassistant_object *o = (struct mgos_homeassistant_object *) user_data;
25+
if (!o) return;
26+
mgos_homeassistant_object_send_status(o);
27+
}
28+
29+
static void bh1750_stat_light(struct mgos_homeassistant_object *o, struct json_out *json) {
30+
struct mgos_homeassistant_bh1750 *d = NULL;
31+
float lux = NAN;
32+
33+
if (!o || !json) return;
34+
if (!(d = (struct mgos_homeassistant_bh1750 *) o->user_data)) return;
35+
36+
lux = mgos_bh1750_read_lux(d->dev, NULL);
37+
json_printf(json, "%.1f", lux);
38+
//initiate another measurment
39+
mgos_bh1750_set_config( d->dev, MGOS_BH1750_MODE_ONCE_HIGH_RES, MGOS_BH1750_MTIME_DEFAULT );
40+
}
41+
42+
static void bh1750_pre_remove_cb(struct mgos_homeassistant_object *o) {
43+
struct mgos_homeassistant_bh1750 *d = NULL;
44+
if (!o) return;
45+
if (!(d = (struct mgos_homeassistant_bh1750 *) o->user_data)) return;
46+
mgos_clear_timer(d->timer);
47+
if (d->dev) mgos_bh1750_free(d->dev);
48+
free(o->user_data);
49+
o->user_data = NULL;
50+
}
51+
52+
bool mgos_homeassistant_bh1750_fromjson(struct mgos_homeassistant *ha, struct json_token val) {
53+
int i2caddr = -1;
54+
int period = 60;
55+
bool ret = false;
56+
struct mgos_homeassistant_object *o = NULL;
57+
struct mgos_homeassistant_bh1750 *d = NULL;
58+
char object_name[20];
59+
char *name = NULL;
60+
char *nameptr = NULL;
61+
62+
if (!ha) goto exit;
63+
if (!(d = calloc(1, sizeof(*d)))) goto exit;
64+
65+
json_scanf(val.ptr, val.len, "{i2caddr:%d,period:%d,name:%Q}", &i2caddr, &period, &name);
66+
d->dev = mgos_bh1750_create(i2caddr);
67+
68+
if (!d->dev) {
69+
LOG(LL_ERROR, ("Could not create bh1750 at i2caddr=%d", i2caddr));
70+
goto exit;
71+
}
72+
73+
//initiate a one time measurement
74+
mgos_bh1750_set_config( d->dev, MGOS_BH1750_MODE_ONCE_HIGH_RES, MGOS_BH1750_MTIME_DEFAULT );
75+
76+
if (!name) {
77+
mgos_homeassistant_object_generate_name(ha, "bh1750_", object_name, sizeof(object_name));
78+
nameptr = object_name;
79+
} else {
80+
nameptr = name;
81+
}
82+
83+
o = mgos_homeassistant_object_add(ha, nameptr, COMPONENT_SENSOR, NULL, NULL, d);
84+
if (!o) {
85+
LOG(LL_ERROR, ("Could not add object %s to homeassistant", nameptr));
86+
goto exit;
87+
}
88+
o->pre_remove_cb = bh1750_pre_remove_cb;
89+
90+
// if (!mgos_homeassistant_object_class_add(o, "illuminance", "\"unit_of_measurement\":\"lx\"", bh1750_stat_light)) {
91+
if (!mgos_homeassistant_object_class_add(o, "illuminance", "\"unit_of_measurement\":\"lx\"", bh1750_stat_light)) {
92+
LOG(LL_ERROR, ("Could not add 'illuminance' class to object %s", nameptr));
93+
goto exit;
94+
}
95+
96+
if (period > 0) d->timer = mgos_set_timer(period * 1000, true, bh1750_timer, o);
97+
98+
ret = true;
99+
LOG(LL_DEBUG, ("Successfully created object %s", nameptr));
100+
exit:
101+
if (name) free(name);
102+
if (!ret && o) mgos_homeassistant_object_remove(&o);
103+
return ret;
104+
}
105+
106+
#endif // MGOS_HAVE_bh1750_I2C

src/mgos_homeassistant_bh1750.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2020 Mircho Mirev <[email protected]>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
#ifdef MGOS_HAVE_BH1750
19+
#include <strings.h>
20+
#include <stdbool.h>
21+
22+
#include "mgos_bh1750.h"
23+
#include "mgos_homeassistant.h"
24+
25+
struct mgos_homeassistant_bh1750 {
26+
struct mgos_bh1750 *dev;
27+
mgos_timer_id timer;
28+
};
29+
30+
bool mgos_homeassistant_bh1750_fromjson(struct mgos_homeassistant *ha, struct json_token val);
31+
#endif // MGOS_HAVE_BH1750

0 commit comments

Comments
 (0)