Add BH1750 sensor

main
Nigreon 2024-07-21 01:24:01 +02:00
parent 8eea54771b
commit 1ee7df8078
6 changed files with 279 additions and 100 deletions

50
src/bh1750.c Normal file
View File

@ -0,0 +1,50 @@
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(bh1750);
#include <zephyr/kernel.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/device.h>
#include <stdbool.h>
#include <zephyr/drivers/sensor.h>
#include <math.h>
#include "bh1750.h"
#if !DT_HAS_COMPAT_STATUS_OKAY(rohm_bh1750)
#error "No rohm,bh1750 compatible node found in the device tree"
#endif
const struct device *const bh = DEVICE_DT_GET_ANY(rohm_bh1750);
int bh1750_setup()
{
if (!device_is_ready(bh) || bh1750_probe()) {
printk("Device %s is not ready.\n", bh->name);
return -1;
}
return 0;
};
int bh1750_probe()
{
if(sensor_sample_fetch(bh))
{
printk("Failed to fetch sample from BH1750 device\n");
return -1;
}
return 0;
}
double bh1750_ReadLight()
{
struct sensor_value lux;
sensor_channel_get(bh, SENSOR_CHAN_LIGHT, &lux);
return sensor_value_to_double(&lux);
}

3
src/bh1750.h Normal file
View File

@ -0,0 +1,3 @@
int bh1750_setup();
int bh1750_probe();
double bh1750_ReadLight();

View File

@ -14,6 +14,7 @@
#include <zephyr/sys/printk.h> #include <zephyr/sys/printk.h>
#include <zephyr/sys/byteorder.h> #include <zephyr/sys/byteorder.h>
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
//#include <zephyr/sys/sys_heap.h>
#include <zephyr/settings/settings.h> #include <zephyr/settings/settings.h>
@ -27,6 +28,7 @@
#include <zephyr/devicetree.h> #include <zephyr/devicetree.h>
#include <zephyr/drivers/sensor.h> #include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio.h>
#include <zephyr/debug/thread_analyzer.h>
#include <math.h> #include <math.h>
@ -37,6 +39,7 @@ LOG_MODULE_REGISTER(main);
#include "sht4x.h" #include "sht4x.h"
#include "bmp180.h" #include "bmp180.h"
#include "sds011.h" #include "sds011.h"
#include "bh1750.h"
#include "hal/nrf_gpiote.h" #include "hal/nrf_gpiote.h"
static K_FIFO_DEFINE(fifo_adv_data); static K_FIFO_DEFINE(fifo_adv_data);
@ -44,11 +47,12 @@ static K_FIFO_DEFINE(fifo_adv_data);
static K_SEM_DEFINE(ble_init_ok, 0, 1); static K_SEM_DEFINE(ble_init_ok, 0, 1);
static K_SEM_DEFINE(adv_send_ok, 0, 1); static K_SEM_DEFINE(adv_send_ok, 0, 1);
#define BTHOME_MAXELT 9 #define BTHOME_MAXELT 10
enum captor_type { BATTERY, BATTERYVDD, TEMPERATURE, PRESSURE, VOLTAGE, VOLTAGEVDD, PM25, PM10, HUMIDITY }; enum captor_type { BATTERY, BATTERYVDD, TEMPERATURE, PRESSURE, ILLUMINANCE, VOLTAGE, VOLTAGEVDD, PM25, PM10, HUMIDITY };
uint8_t eltid[BTHOME_MAXELT] = { 0x01, 0x01, 0x02, 0x04, 0x0c, 0x0c, 0x0d, 0x0e, 0x2e }; uint8_t eltid[BTHOME_MAXELT] = { 0x01, 0x01, 0x02, 0x04, 0x05, 0x0c, 0x0c, 0x0d, 0x0e, 0x2e };
uint8_t eltsize[BTHOME_MAXELT] = { 1, 1, 2, 3, 2, 2, 2, 2, 1}; uint8_t eltsize[BTHOME_MAXELT] = { 1, 1, 2, 3, 3, 2, 2, 2, 2, 1};
uint8_t eltenabled[BTHOME_MAXELT] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; uint8_t eltenabled[BTHOME_MAXELT] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
uint8_t eltenabled2[BTHOME_MAXELT] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
struct bthome_adv_t { struct bthome_adv_t {
void *fifo_reserved; void *fifo_reserved;
@ -59,11 +63,17 @@ struct bthome_adv_t {
uint32_t data_u32; uint32_t data_u32;
}; };
uint8_t mfg_bthome_tosend = 1;
static uint8_t mfg_bthome_data[28]; static uint8_t mfg_bthome_data[28];
uint8_t mfg_bthome_sizetosend = 0; uint8_t mfg_bthome_sizetosend = 0;
uint8_t mfg_bthome_numelt = 0; uint8_t mfg_bthome_numelt = 0;
uint8_t bthome_datapos[BTHOME_MAXELT]; uint8_t bthome_datapos[BTHOME_MAXELT];
//uint8_t bthome_datapos[] = { 4, 6, 9, 12, 14, 18, 21 };
static uint8_t mfg_bthome_data2[28];
uint8_t mfg_bthome_sizetosend2 = 0;
uint8_t mfg_bthome_numelt2 = 0;
uint8_t bthome_datapos2[BTHOME_MAXELT];
@ -122,8 +132,8 @@ struct k_work_q workqueue_i2c;
*/ */
{ 10000, 2800 }, { 10000, 2800 },
{ 90, 2600 }, { 9000, 2600 },
{ 50, 2440 }, { 5000, 2440 },
{ 0, 2300 }, { 0, 2300 },
}; };
@ -195,10 +205,11 @@ struct k_work_q workqueue_i2c;
struct bt_data ad[] = { struct bt_data ad[] = {
//BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_SVC_DATA16, mfg_bthome_data, mfg_bthome_sizetosend), BT_DATA(BT_DATA_SVC_DATA16, mfg_bthome_data, mfg_bthome_sizetosend),
}; };
err = bt_le_adv_start(BT_LE_ADV_PARAM(BT_LE_ADV_OPT_USE_IDENTITY | BT_LE_ADV_OPT_USE_NAME, BT_GAP_ADV_SLOW_INT_MIN,BT_GAP_ADV_SLOW_INT_MAX, NULL), ad, ARRAY_SIZE(ad), NULL, 0); // err = bt_le_adv_start(BT_LE_ADV_PARAM(BT_LE_ADV_OPT_USE_IDENTITY | BT_LE_ADV_OPT_USE_NAME, BT_GAP_ADV_SLOW_INT_MIN,BT_GAP_ADV_SLOW_INT_MAX, NULL), ad, ARRAY_SIZE(ad), NULL, 0);
err = bt_le_adv_start(BT_LE_ADV_PARAM(BT_LE_ADV_OPT_USE_IDENTITY, BT_GAP_ADV_SLOW_INT_MIN,BT_GAP_ADV_SLOW_INT_MAX, NULL), ad, ARRAY_SIZE(ad), NULL, 0);
if (err) { if (err) {
printk("Advertising failed to start (err %d)\n", err); printk("Advertising failed to start (err %d)\n", err);
return; return;
@ -300,6 +311,50 @@ void mfg_bthome_build()
} }
} }
void mfg_bthome_build2()
{
for(int i = 0; i < BTHOME_MAXELT; i++)
{
if(eltenabled2[i] == 1)
{
mfg_bthome_numelt2++;
mfg_bthome_data2[mfg_bthome_sizetosend2] = eltid[i];
bthome_datapos2[i] = mfg_bthome_sizetosend2;
mfg_bthome_sizetosend2 = mfg_bthome_sizetosend2 + eltsize[i] + 1;
}
}
}
static void work_publish_adv_handler(struct k_work *work)
{
struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_SVC_DATA16, mfg_bthome_data, mfg_bthome_sizetosend),
};
struct bt_data ad2[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_SVC_DATA16, mfg_bthome_data2, mfg_bthome_sizetosend2),
};
if(mfg_bthome_tosend == 1) {
printk("Update ADV2\n");
bt_le_adv_update_data(ad2, ARRAY_SIZE(ad2), NULL, 0);
mfg_bthome_tosend = 2;
} else {
printk("Update ADV1\n");
bt_le_adv_update_data(ad, ARRAY_SIZE(ad), NULL, 0);
mfg_bthome_tosend = 1;
}
}
K_WORK_DEFINE(work_publish_adv, work_publish_adv_handler);
void timer_publish_adv_handler(struct k_timer *stm)
{
k_work_submit(&work_publish_adv);
}
K_TIMER_DEFINE(timer_publish_adv, timer_publish_adv_handler, NULL);
void publish_adv_thread() void publish_adv_thread()
{ {
bool adv_activate = false; bool adv_activate = false;
@ -324,20 +379,21 @@ uint8_t eltreceived[BTHOME_MAXELT] = { 0, 0, 0, 0, 0, 0, 0 };
numeltreceived++; numeltreceived++;
eltreceived[request->type] = 1; eltreceived[request->type] = 1;
} }
//LOG_INF("Fill BT for type %d", request->type);
if(request->type == VOLTAGE) if(request->type == VOLTAGE)
{ {
mfg_bthome_data[bthome_datapos[VOLTAGE]+2] = request->data_u16 >> 8; mfg_bthome_data2[bthome_datapos2[VOLTAGE]+2] = request->data_u16 >> 8;
mfg_bthome_data[bthome_datapos[VOLTAGE]+1] = request->data_u16; mfg_bthome_data2[bthome_datapos2[VOLTAGE]+1] = request->data_u16;
} else if(request->type == BATTERY) } else if(request->type == BATTERY)
{ {
mfg_bthome_data[bthome_datapos[BATTERY]+1] = request->data_u8; mfg_bthome_data2[bthome_datapos2[BATTERY]+1] = request->data_u8;
} else if(request->type == VOLTAGEVDD) } else if(request->type == VOLTAGEVDD)
{ {
mfg_bthome_data[bthome_datapos[VOLTAGEVDD]+2] = request->data_u16 >> 8; mfg_bthome_data2[bthome_datapos2[VOLTAGEVDD]+2] = request->data_u16 >> 8;
mfg_bthome_data[bthome_datapos[VOLTAGEVDD]+1] = request->data_u16; mfg_bthome_data2[bthome_datapos2[VOLTAGEVDD]+1] = request->data_u16;
} else if(request->type == BATTERYVDD) } else if(request->type == BATTERYVDD)
{ {
mfg_bthome_data[bthome_datapos[BATTERYVDD]+1] = request->data_u8; mfg_bthome_data2[bthome_datapos2[BATTERYVDD]+1] = request->data_u8;
} else if(request->type == TEMPERATURE) } else if(request->type == TEMPERATURE)
{ {
mfg_bthome_data[bthome_datapos[TEMPERATURE]+2] = request->data_s16 >> 8; mfg_bthome_data[bthome_datapos[TEMPERATURE]+2] = request->data_s16 >> 8;
@ -350,6 +406,11 @@ uint8_t eltreceived[BTHOME_MAXELT] = { 0, 0, 0, 0, 0, 0, 0 };
mfg_bthome_data[bthome_datapos[PRESSURE]+3] = request->data_u32 >> 16; mfg_bthome_data[bthome_datapos[PRESSURE]+3] = request->data_u32 >> 16;
mfg_bthome_data[bthome_datapos[PRESSURE]+2] = request->data_u32 >> 8; mfg_bthome_data[bthome_datapos[PRESSURE]+2] = request->data_u32 >> 8;
mfg_bthome_data[bthome_datapos[PRESSURE]+1] = request->data_u32; mfg_bthome_data[bthome_datapos[PRESSURE]+1] = request->data_u32;
} else if(request->type == ILLUMINANCE)
{
mfg_bthome_data[bthome_datapos[ILLUMINANCE]+3] = request->data_u32 >> 16;
mfg_bthome_data[bthome_datapos[ILLUMINANCE]+2] = request->data_u32 >> 8;
mfg_bthome_data[bthome_datapos[ILLUMINANCE]+1] = request->data_u32;
} else if(request->type == PM25) } else if(request->type == PM25)
{ {
mfg_bthome_data[bthome_datapos[PM25]+2] = request->data_u16 >> 8; mfg_bthome_data[bthome_datapos[PM25]+2] = request->data_u16 >> 8;
@ -362,60 +423,74 @@ uint8_t eltreceived[BTHOME_MAXELT] = { 0, 0, 0, 0, 0, 0, 0 };
k_free(request); k_free(request);
} }
if(numeltreceived == (mfg_bthome_numelt + mfg_bthome_numelt2)) {
if(adv_activate == false)
{
bt_ready();
adv_activate = true;
k_timer_start(&timer_publish_adv, K_SECONDS(150), K_SECONDS(150));
}
}
/*
if(numeltreceived == mfg_bthome_numelt) { if(numeltreceived == mfg_bthome_numelt) {
//LOG_HEXDUMP_INF(mfg_bthome_data, mfg_bthome_sizetosend, "mfg_bthome_data");
//printk("Send ADV Size %d\n", mfg_bthome_sizetosend);
if(adv_activate == false) if(adv_activate == false)
{ {
bt_ready(); bt_ready();
adv_activate = true; adv_activate = true;
} else { } else {
printk("Update ADV\n");
struct bt_data ad[] = { struct bt_data ad[] = {
//BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_SVC_DATA16, mfg_bthome_data, mfg_bthome_sizetosend), BT_DATA(BT_DATA_SVC_DATA16, mfg_bthome_data, mfg_bthome_sizetosend),
}; };
bt_le_adv_update_data(ad, ARRAY_SIZE(ad), NULL, 0); bt_le_adv_update_data(ad, ARRAY_SIZE(ad), NULL, 0);
printk("Update ADV\n");
//k_msleep(50);
} }
} }
*/
} }
} }
K_THREAD_DEFINE(publish_adv_thread_id, STACKSIZE, publish_adv_thread, NULL, NULL, NULL, PRIORITY, 0, 0); K_THREAD_DEFINE(publish_adv_thread_id, STACKSIZE, publish_adv_thread, NULL, NULL, NULL, PRIORITY, 0, 0);
/*void print_heap_info(void)
{
extern struct k_heap _system_heap;
struct sys_memory_stats stats;
sys_heap_runtime_stats_get(&_system_heap.heap, &stats);
printk("\n");
printk("INFO: Allocated Heap = %zu\n", stats.allocated_bytes);
printk("INFO: Free Heap = %zu\n", stats.free_bytes);
printk("INFO: Max Allocated Heap = %zu\n", stats.max_allocated_bytes);
printk("\n");
return;
}*/
static void work_battery_handler(struct k_work *work) static void work_battery_handler(struct k_work *work)
{ {
struct bthome_adv_t bthome_adv; struct bthome_adv_t *bthome_adv_voltage = k_malloc(sizeof(*bthome_adv_voltage));
size_t size = sizeof(struct bthome_adv_t); bthome_adv_voltage->type = VOLTAGE;
bthome_adv.type = VOLTAGE;
battery_setup(); battery_setup();
bthome_adv.data_u16 = (uint16_t) battery_sample(); bthome_adv_voltage->data_u16 = (uint16_t) battery_sample();
char *mem_ptr = k_malloc(size); k_fifo_put(&fifo_adv_data, bthome_adv_voltage);
memcpy(mem_ptr, &bthome_adv, size);
k_fifo_put(&fifo_adv_data, mem_ptr);
bthome_adv.type = BATTERY; struct bthome_adv_t *bthome_adv_battery = k_malloc(sizeof(*bthome_adv_battery));
bthome_adv.data_u8 = round(battery_level_pptt(bthome_adv.data_u16, battery_levels)/100); bthome_adv_battery->type = BATTERY;
char *mem_ptr2 = k_malloc(size); bthome_adv_battery->data_u8 = round(battery_level_pptt(bthome_adv_voltage->data_u16, battery_levels)/100);
memcpy(mem_ptr2, &bthome_adv, size); k_fifo_put(&fifo_adv_data, bthome_adv_battery);
k_fifo_put(&fifo_adv_data, mem_ptr2); printk("Battery: %d mV (%d%%)\n",bthome_adv_voltage->data_u16, bthome_adv_battery->data_u8);
k_sem_give(&adv_send_ok);
printk("Battery: %d mV (%d%%)\n",bthome_adv.data_u16, bthome_adv.data_u8);
bthome_adv.type = VOLTAGEVDD; struct bthome_adv_t *bthome_adv_voltagevdd = k_malloc(sizeof(*bthome_adv_voltagevdd));
bthome_adv_voltagevdd->type = VOLTAGEVDD;
batteryvdd_setup(); batteryvdd_setup();
bthome_adv.data_u16 = (uint16_t) batteryvdd_sample(); bthome_adv_voltagevdd->data_u16 = (uint16_t) batteryvdd_sample();
char *mem_ptr3 = k_malloc(size); k_fifo_put(&fifo_adv_data, bthome_adv_voltagevdd);
memcpy(mem_ptr3, &bthome_adv, size);
k_fifo_put(&fifo_adv_data, mem_ptr3);
bthome_adv.type = BATTERYVDD; struct bthome_adv_t *bthome_adv_batteryvdd = k_malloc(sizeof(*bthome_adv_batteryvdd));
bthome_adv.data_u8 = round(battery_level_pptt(bthome_adv.data_u16, battery_nimh_levels)/100); bthome_adv_batteryvdd->type = BATTERYVDD;
char *mem_ptr4 = k_malloc(size); bthome_adv_batteryvdd->data_u8 = round(battery_level_pptt(bthome_adv_voltagevdd->data_u16, battery_nimh_levels)/100);
memcpy(mem_ptr4, &bthome_adv, size); k_fifo_put(&fifo_adv_data, bthome_adv_batteryvdd);
k_fifo_put(&fifo_adv_data, mem_ptr4); printk("Battery VDD: %d mV (%d%%)\n",bthome_adv_voltagevdd->data_u16, bthome_adv_batteryvdd->data_u8);
k_sem_give(&adv_send_ok); k_sem_give(&adv_send_ok);
printk("Battery VDD: %d mV (%d%%)\n",bthome_adv.data_u16, bthome_adv.data_u8);
} }
K_WORK_DEFINE(work_battery, work_battery_handler); K_WORK_DEFINE(work_battery, work_battery_handler);
@ -427,22 +502,20 @@ void timer_battery_handler(struct k_timer *stm)
static void work_temperature_handler(struct k_work *work) static void work_temperature_handler(struct k_work *work)
{ {
struct bthome_adv_t bthome_adv; struct bthome_adv_t *bthome_adv_temperature = k_malloc(sizeof(*bthome_adv_temperature));
sht4x_probe(); sht4x_probe();
bthome_adv.type = TEMPERATURE; bthome_adv_temperature->type = TEMPERATURE;
bthome_adv.data_s16 = (int16_t)(sht4x_ReadTempeature()*100); bthome_adv_temperature->data_s16 = (int16_t)(sht4x_ReadTempeature()*100);
size_t size = sizeof(struct bthome_adv_t); k_fifo_put(&fifo_adv_data, bthome_adv_temperature);
char *mem_ptr = k_malloc(size); printk("Temperature: %d°\n",bthome_adv_temperature->data_s16);
memcpy(mem_ptr, &bthome_adv, size);
k_fifo_put(&fifo_adv_data, mem_ptr);
printk("Temperature: %d°\n",bthome_adv.data_s16); struct bthome_adv_t *bthome_adv_humidity = k_malloc(sizeof(*bthome_adv_humidity));
bthome_adv.type = HUMIDITY; bthome_adv_humidity->type = HUMIDITY;
bthome_adv.data_u8 = (uint8_t)(sht4x_ReadHumidity()); bthome_adv_humidity->data_u8 = (uint8_t)(sht4x_ReadHumidity());
char *mem_ptr2 = k_malloc(size); k_fifo_put(&fifo_adv_data, bthome_adv_humidity);
memcpy(mem_ptr2, &bthome_adv, size);
k_fifo_put(&fifo_adv_data, mem_ptr2);
k_sem_give(&adv_send_ok); k_sem_give(&adv_send_ok);
printk("Humidity: %d pc \n",bthome_adv.data_u8); printk("Humidity: %d pc \n",bthome_adv_humidity->data_u8);
} }
K_WORK_DEFINE(work_temperature, work_temperature_handler); K_WORK_DEFINE(work_temperature, work_temperature_handler);
@ -454,42 +527,50 @@ void timer_temperature_handler(struct k_timer *stm)
static void work_pressure_handler(struct k_work *work) static void work_pressure_handler(struct k_work *work)
{ {
struct bthome_adv_t bthome_adv; struct bthome_adv_t *bthome_adv_pressure = k_malloc(sizeof(*bthome_adv_pressure));
bthome_adv.type = PRESSURE; bthome_adv_pressure->type = PRESSURE;
bthome_adv.data_u32 = bmp180_readPressure(); bthome_adv_pressure->data_u32 = bmp180_readPressure();
size_t size = sizeof(struct bthome_adv_t); k_fifo_put(&fifo_adv_data, bthome_adv_pressure);
char *mem_ptr = k_malloc(size);
memcpy(mem_ptr, &bthome_adv, size);
k_fifo_put(&fifo_adv_data, mem_ptr);
k_sem_give(&adv_send_ok); k_sem_give(&adv_send_ok);
printk("Pressure: %d Pa \n",bthome_adv.data_u32); printk("Pressure: %d Pa \n",bthome_adv_pressure->data_u32);
} }
K_WORK_DEFINE(work_pressure, work_pressure_handler); K_WORK_DEFINE(work_pressure, work_pressure_handler);
void timer_pressure_handler(struct k_timer *stm) void timer_pressure_handler(struct k_timer *stm)
{ {
k_work_submit_to_queue(&workqueue_i2c, &work_pressure); k_work_submit_to_queue(&workqueue_i2c, &work_pressure);
} }
K_TIMER_DEFINE(timer_pressure, timer_pressure_handler, NULL); K_TIMER_DEFINE(timer_pressure, timer_pressure_handler, NULL);
static void work_illuminance_handler(struct k_work *work)
{
struct bthome_adv_t *bthome_adv_illuminance = k_malloc(sizeof(*bthome_adv_illuminance));
bh1750_probe();
bthome_adv_illuminance->type = ILLUMINANCE;
bthome_adv_illuminance->data_u32 = (uint32_t) (bh1750_ReadLight()*100);
k_fifo_put(&fifo_adv_data, bthome_adv_illuminance);
k_sem_give(&adv_send_ok);
printk("Illuminance: %d lux \n",bthome_adv_illuminance->data_u32);
}
K_WORK_DEFINE(work_illuminance, work_illuminance_handler);
void timer_illuminance_handler(struct k_timer *stm)
{
k_work_submit_to_queue(&workqueue_i2c, &work_illuminance);
}
K_TIMER_DEFINE(timer_illuminance, timer_illuminance_handler, NULL);
static void work_pms_handler(struct k_work *work) static void work_pms_handler(struct k_work *work)
{ {
sds011_probe(); sds011_probe();
struct bthome_adv_t bthome_adv; struct bthome_adv_t *bthome_adv_pm25 = k_malloc(sizeof(*bthome_adv_pm25));
bthome_adv.type = PM25; bthome_adv_pm25->type = PM25;
bthome_adv.data_u16 = sds011_ReadPM25(); bthome_adv_pm25->data_u16 = sds011_ReadPM25();
size_t size = sizeof(struct bthome_adv_t); k_fifo_put(&fifo_adv_data, bthome_adv_pm25);
char *mem_ptr = k_malloc(size); printk("PM25: %d\n",bthome_adv_pm25->data_u16);
memcpy(mem_ptr, &bthome_adv, size); struct bthome_adv_t *bthome_adv_pm10 = k_malloc(sizeof(*bthome_adv_pm10));
k_fifo_put(&fifo_adv_data, mem_ptr); bthome_adv_pm10->type = PM10;
printk("PM25: %d\n",bthome_adv.data_u16); bthome_adv_pm10->data_u16 = sds011_ReadPM10();
bthome_adv.type = PM10; k_fifo_put(&fifo_adv_data, bthome_adv_pm10);
bthome_adv.data_u16 = sds011_ReadPM10(); printk("PM10: %d\n",bthome_adv_pm10->data_u16);
char *mem_ptr2 = k_malloc(size);
memcpy(mem_ptr2, &bthome_adv, size);
k_fifo_put(&fifo_adv_data, mem_ptr2);
printk("PM10: %d\n",bthome_adv.data_u16);
k_sem_give(&adv_send_ok); k_sem_give(&adv_send_ok);
} }
K_WORK_DEFINE(work_pms, work_pms_handler); K_WORK_DEFINE(work_pms, work_pms_handler);
@ -503,7 +584,7 @@ void timer_pms_handler(struct k_timer *stm)
int main(void) int main(void)
{ {
printk("Hello, It's TuxOutdooor\n"); LOG_WRN("Hello, It's TuxOutdooor");
int err; int err;
// bt_passkey_set(192847); // bt_passkey_set(192847);
@ -512,6 +593,12 @@ void timer_pms_handler(struct k_timer *stm)
mfg_bthome_data[2]=0x40; mfg_bthome_data[2]=0x40;
mfg_bthome_sizetosend=3; mfg_bthome_sizetosend=3;
mfg_bthome_data2[0]=0xd2;
mfg_bthome_data2[1]=0xfc;
mfg_bthome_data2[2]=0x40;
mfg_bthome_sizetosend2=3;
for(int i = 0; i < BTHOME_MAXELT; i++) for(int i = 0; i < BTHOME_MAXELT; i++)
{ {
bthome_datapos[i] = 255; bthome_datapos[i] = 255;
@ -537,6 +624,7 @@ k_work_queue_start(&workqueue_i2c, stack_wq_i2c, K_THREAD_STACK_SIZEOF(stack_wq_
k_work_queue_init(&workqueue_pms); k_work_queue_init(&workqueue_pms);
k_work_queue_start(&workqueue_pms, stack_wq_pms, K_THREAD_STACK_SIZEOF(stack_wq_pms), PRIORITY, NULL); k_work_queue_start(&workqueue_pms, stack_wq_pms, K_THREAD_STACK_SIZEOF(stack_wq_pms), PRIORITY, NULL);
k_msleep(250);
if(sht4x_setup() == 0) { if(sht4x_setup() == 0) {
printk("SHT4x enabled\n"); printk("SHT4x enabled\n");
@ -548,7 +636,8 @@ k_work_queue_start(&workqueue_pms, stack_wq_pms, K_THREAD_STACK_SIZEOF(stack_wq_
printk("SHT4x disabled\n"); printk("SHT4x disabled\n");
} }
k_msleep(50); k_msleep(250);
if(bmp180_setup() == 0) { if(bmp180_setup() == 0) {
printk("BMP180 enabled\n"); printk("BMP180 enabled\n");
@ -559,7 +648,9 @@ k_work_queue_start(&workqueue_pms, stack_wq_pms, K_THREAD_STACK_SIZEOF(stack_wq_
printk("BMP180 disabled\n"); printk("BMP180 disabled\n");
} }
k_msleep(50);
k_msleep(250);
if(sds011_setup() == 0) { if(sds011_setup() == 0) {
printk("SDS011 enabled\n"); printk("SDS011 enabled\n");
k_work_submit_to_queue(&workqueue_pms, &work_pms); k_work_submit_to_queue(&workqueue_pms, &work_pms);
@ -570,17 +661,27 @@ k_work_queue_start(&workqueue_pms, stack_wq_pms, K_THREAD_STACK_SIZEOF(stack_wq_
printk("SDS011 disabled\n"); printk("SDS011 disabled\n");
} }
k_msleep(2000);
if(bh1750_setup() == 0) {
printk("BH1750 enabled\n");
k_work_submit_to_queue(&workqueue_i2c, &work_illuminance);
k_timer_start(&timer_illuminance, K_SECONDS(300), K_SECONDS(300));
eltenabled[ILLUMINANCE] = 1;
} else {
printk("BH1750 disabled\n");
}
k_msleep(50); k_msleep(250);
k_work_submit(&work_battery); k_work_submit(&work_battery);
k_timer_start(&timer_battery, K_SECONDS(3600), K_SECONDS(3600)); k_timer_start(&timer_battery, K_SECONDS(3600), K_SECONDS(3600));
eltenabled[BATTERY] = 1; eltenabled2[BATTERY] = 1;
eltenabled[VOLTAGE] = 1; eltenabled2[VOLTAGE] = 1;
eltenabled[BATTERYVDD] = 1; eltenabled2[BATTERYVDD] = 1;
eltenabled[VOLTAGEVDD] = 1; eltenabled2[VOLTAGEVDD] = 1;
mfg_bthome_build(); mfg_bthome_build();
mfg_bthome_build2();
k_sem_give(&ble_init_ok); k_sem_give(&ble_init_ok);
/* for(;;) /* for(;;)

View File

@ -1,6 +1,7 @@
#include <sys/_stdint.h> #include <sys/_stdint.h>
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <zephyr/drivers/uart.h> #include <zephyr/drivers/uart.h>
#include <zephyr/drivers/gpio.h>
#include "sds011.h" #include "sds011.h"
@ -33,6 +34,10 @@ static K_FIFO_DEFINE(fifo_pms);
#define WARMUPTIME_SDS_MS 15000 #define WARMUPTIME_SDS_MS 15000
#define READINGTIME_SDS_MS 5000 #define READINGTIME_SDS_MS 5000
#define GPIO_PORT DT_NODELABEL(gpio0)
#define GPIO_PIN 22
const struct device *devgpio = DEVICE_DT_GET(GPIO_PORT);
uint16_t sds_pm10; uint16_t sds_pm10;
uint16_t sds_pm25; uint16_t sds_pm25;
@ -49,7 +54,7 @@ uint32_t getTsSDS()
void send_uart_cmd(uint8_t *buf) void send_uart_cmd(uint8_t *buf)
{ {
//int msg_len = strlen(buf); //int msg_len = strlen(buf);
printk("Send to uart\n"); //printk("Send to uart\n");
for (int i = 0; i < 19; i++) { for (int i = 0; i < 19; i++) {
uart_poll_out(uart_dev, buf[i]); uart_poll_out(uart_dev, buf[i]);
@ -118,11 +123,15 @@ int sds011_setup()
} }
uart_irq_rx_enable(uart_dev); uart_irq_rx_enable(uart_dev);
send_uart_cmd(SDS_WAKEUPCMD); gpio_pin_configure(devgpio, GPIO_PIN, GPIO_OUTPUT | GPIO_OPEN_DRAIN);
k_msleep(2000);
gpio_pin_set(devgpio, GPIO_PIN, 0);
//send_uart_cmd(SDS_WAKEUPCMD);
k_msleep(5000);
if(uart_data_received == true) { if(uart_data_received == true) {
return 0; return 0;
} else { } else {
gpio_pin_set(devgpio, GPIO_PIN, 1);
return -1; return -1;
} }
} }
@ -132,7 +141,8 @@ int sds011_setup()
int sds011_probe() int sds011_probe()
{ {
uint32_t ts_start_measure; uint32_t ts_start_measure;
send_uart_cmd(SDS_WAKEUPCMD); gpio_pin_set(devgpio, GPIO_PIN, 0);
//send_uart_cmd(SDS_WAKEUPCMD);
pms_warming = true; pms_warming = true;
pms_enable = true; pms_enable = true;
k_msleep(WARMUPTIME_SDS_MS); k_msleep(WARMUPTIME_SDS_MS);
@ -175,17 +185,18 @@ int sds011_probe()
printk("p10: %d p25: %d\n", pm10_serial, pm25_serial); printk("p10: %d p25: %d\n", pm10_serial, pm25_serial);
} }
k_free(pms_data_read);
} else { } else {
send_uart_cmd(SDS_SLEEPCMD); send_uart_cmd(SDS_SLEEPCMD);
pms_enable = false; pms_enable = false;
} }
k_free(pms_data_read);
} else { } else {
break; break;
} }
} }
gpio_pin_set(devgpio, GPIO_PIN, 1);
if (sds_val_count > 2) { if (sds_val_count > 2) {
sds_pm10_sum = sds_pm10_sum - sds_pm10_min - sds_pm10_max; sds_pm10_sum = sds_pm10_sum - sds_pm10_min - sds_pm10_max;

View File

@ -32,6 +32,11 @@
reg = <0x44>; reg = <0x44>;
repeatability = <2>; repeatability = <2>;
}; };
bh1750@23 {
status = "okay";
compatible = "rohm,bh1750";
reg = <0x23>;
};
}; };
&i2c1 { &i2c1 {

View File

@ -29,13 +29,22 @@ CONFIG_UART_CONSOLE=n
CONFIG_RTT_CONSOLE=y CONFIG_RTT_CONSOLE=y
CONFIG_STDOUT_CONSOLE=y CONFIG_STDOUT_CONSOLE=y
#CONFIG_THREAD_ANALYZER=y
#CONFIG_THREAD_ANALYZER_USE_PRINTK=y
#CONFIG_THREAD_ANALYZER_AUTO=y
#CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=5
#CONFIG_THREAD_NAME=y
#CONFIG_DEBUG_INFO=y
CONFIG_ADC=y CONFIG_ADC=y
CONFIG_GPIO=y CONFIG_GPIO=y
CONFIG_SERIAL=y CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_BOARD_ENABLE_DCDC=n CONFIG_BOARD_ENABLE_DCDC=n
CONFIG_HEAP_MEM_POOL_SIZE=512 CONFIG_HEAP_MEM_POOL_SIZE=2048
#CONFIG_SYS_HEAP_RUNTIME_STATS=y
CONFIG_NEWLIB_LIBC=y CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_NANO=y CONFIG_NEWLIB_LIBC_NANO=y