Skip to content

Commit

Permalink
added bh1750 sensor support with adaptation to the changed driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Mircho committed Aug 2, 2021
1 parent bb67059 commit 14bc2ea
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/mgos_homeassistant.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "mgos_homeassistant_barometer.h"
#include "mgos_homeassistant_gpio.h"
#include "mgos_homeassistant_si7021.h"
#include "mgos_homeassistant_bh1750.h"
#include "mgos_mqtt.h"

static struct mgos_homeassistant *s_homeassistant = NULL;
Expand Down Expand Up @@ -126,6 +127,19 @@ bool mgos_homeassistant_fromjson(struct mgos_homeassistant *ha, const char *json
#endif
}

while ((h = json_next_elem(json, strlen(json), h, ".provider.bh1750", &idx, &val)) != NULL) {
#ifdef MGOS_HAVE_BH1750
if (!mgos_homeassistant_bh1750_fromjson(ha, val)) {
LOG(LL_WARN, ("Failed to add object from provider bh1750, index %d, json "
"follows:%.*s",
idx, (int) val.len, val.ptr));
}
#else
LOG(LL_ERROR, ("provider.bh1750 config found: Add bh1750-i2c to mos.yml, "
"skipping .. "));
#endif
}

while ((h = json_next_elem(json, strlen(json), h, ".provider.barometer", &idx, &val)) != NULL) {
#ifdef MGOS_HAVE_BAROMETER
if (!mgos_homeassistant_barometer_fromjson(ha, val)) {
Expand Down
106 changes: 106 additions & 0 deletions src/mgos_homeassistant_bh1750.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2020 Mircho Mirev <mircho.mirev@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#ifdef MGOS_HAVE_BH1750
#include <math.h>
#include "mgos.h"
#include "mgos_homeassistant_bh1750.h"

static void bh1750_timer(void *user_data) {
struct mgos_homeassistant_object *o = (struct mgos_homeassistant_object *) user_data;
if (!o) return;
mgos_homeassistant_object_send_status(o);
}

static void bh1750_stat_light(struct mgos_homeassistant_object *o, struct json_out *json) {
struct mgos_homeassistant_bh1750 *d = NULL;
float lux = NAN;

if (!o || !json) return;
if (!(d = (struct mgos_homeassistant_bh1750 *) o->user_data)) return;

lux = mgos_bh1750_read_lux(d->dev, NULL);
json_printf(json, "%.1f", lux);
//initiate another measurment
mgos_bh1750_set_config( d->dev, MGOS_BH1750_MODE_ONCE_HIGH_RES, MGOS_BH1750_MTIME_DEFAULT );
}

static void bh1750_pre_remove_cb(struct mgos_homeassistant_object *o) {
struct mgos_homeassistant_bh1750 *d = NULL;
if (!o) return;
if (!(d = (struct mgos_homeassistant_bh1750 *) o->user_data)) return;
mgos_clear_timer(d->timer);
if (d->dev) mgos_bh1750_free(d->dev);
free(o->user_data);
o->user_data = NULL;
}

bool mgos_homeassistant_bh1750_fromjson(struct mgos_homeassistant *ha, struct json_token val) {
int i2caddr = -1;
int period = 60;
bool ret = false;
struct mgos_homeassistant_object *o = NULL;
struct mgos_homeassistant_bh1750 *d = NULL;
char object_name[20];
char *name = NULL;
char *nameptr = NULL;

if (!ha) goto exit;
if (!(d = calloc(1, sizeof(*d)))) goto exit;

json_scanf(val.ptr, val.len, "{i2caddr:%d,period:%d,name:%Q}", &i2caddr, &period, &name);
d->dev = mgos_bh1750_create(i2caddr);

if (!d->dev) {
LOG(LL_ERROR, ("Could not create bh1750 at i2caddr=%d", i2caddr));
goto exit;
}

//initiate a one time measurement
mgos_bh1750_set_config( d->dev, MGOS_BH1750_MODE_ONCE_HIGH_RES, MGOS_BH1750_MTIME_DEFAULT );

if (!name) {
mgos_homeassistant_object_generate_name(ha, "bh1750_", object_name, sizeof(object_name));
nameptr = object_name;
} else {
nameptr = name;
}

o = mgos_homeassistant_object_add(ha, nameptr, COMPONENT_SENSOR, NULL, NULL, d);
if (!o) {
LOG(LL_ERROR, ("Could not add object %s to homeassistant", nameptr));
goto exit;
}
o->pre_remove_cb = bh1750_pre_remove_cb;

// if (!mgos_homeassistant_object_class_add(o, "illuminance", "\"unit_of_measurement\":\"lx\"", bh1750_stat_light)) {
if (!mgos_homeassistant_object_class_add(o, "illuminance", "\"unit_of_measurement\":\"lx\"", bh1750_stat_light)) {
LOG(LL_ERROR, ("Could not add 'illuminance' class to object %s", nameptr));
goto exit;
}

if (period > 0) d->timer = mgos_set_timer(period * 1000, true, bh1750_timer, o);

ret = true;
LOG(LL_DEBUG, ("Successfully created object %s", nameptr));
exit:
if (name) free(name);
if (!ret && o) mgos_homeassistant_object_remove(&o);
return ret;
}

#endif // MGOS_HAVE_bh1750_I2C
31 changes: 31 additions & 0 deletions src/mgos_homeassistant_bh1750.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2020 Mircho Mirev <mircho.mirev@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once
#ifdef MGOS_HAVE_BH1750
#include <strings.h>
#include <stdbool.h>

#include "mgos_bh1750.h"
#include "mgos_homeassistant.h"

struct mgos_homeassistant_bh1750 {
struct mgos_bh1750 *dev;
mgos_timer_id timer;
};

bool mgos_homeassistant_bh1750_fromjson(struct mgos_homeassistant *ha, struct json_token val);
#endif // MGOS_HAVE_BH1750

0 comments on commit 14bc2ea

Please sign in to comment.