-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.c
161 lines (140 loc) · 5.64 KB
/
application.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <application.h>
#define BATTERY_UPDATE_INTERVAL (60 * 60 * 1000) // 1 hour
#define UPDATE_INTERVAL (10 * 1000) // 10 seconds
#define PUB_NO_CHANGE_INTERVAL (15 * 60 * 1000) // 15 minutes
#define TEMPERATURE_TAG_PUB_VALUE_CHANGE 0.2f
#define HUMIDITY_TAG_PUB_VALUE_CHANGE 5.0f
#define LUX_METER_TAG_PUB_VALUE_CHANGE 25.0f
#define BAROMETER_TAG_PUB_VALUE_CHANGE 20.0f
struct {
event_param_t temperature;
event_param_t humidity;
event_param_t illuminance;
event_param_t pressure;
} params;
// Humidity instance
bc_tag_humidity_t humidity;
event_param_t humidity_event_param = { .next_pub = 0 };
void battery_event_handler(bc_module_battery_event_t event, void *event_param)
{
(void) event_param;
float voltage;
if (event == BC_MODULE_BATTERY_EVENT_UPDATE)
{
if (bc_module_battery_get_voltage(&voltage))
{
bc_radio_pub_battery(&voltage);
}
int charge_level;
if (bc_module_battery_get_charge_level(&charge_level))
{
bc_radio_pub_int("battery/-/charge-level", &charge_level);
}
}
}
void climate_module_event_handler(bc_module_climate_event_t event, void *event_param)
{
(void) event_param;
float value;
if (event == BC_MODULE_CLIMATE_EVENT_UPDATE_THERMOMETER)
{
if (bc_module_climate_get_temperature_celsius(&value))
{
if ((fabs(value - params.temperature.value) >= TEMPERATURE_TAG_PUB_VALUE_CHANGE) || (params.temperature.next_pub < bc_scheduler_get_spin_tick()))
{
bc_radio_pub_temperature(BC_RADIO_PUB_CHANNEL_R1_I2C0_ADDRESS_DEFAULT, &value);
params.temperature.value = value;
params.temperature.next_pub = bc_scheduler_get_spin_tick() + PUB_NO_CHANGE_INTERVAL;
}
}
}
else if (event == BC_MODULE_CLIMATE_EVENT_UPDATE_HYGROMETER)
{
if (bc_module_climate_get_humidity_percentage(&value))
{
if ((fabs(value - params.humidity.value) >= HUMIDITY_TAG_PUB_VALUE_CHANGE) || (params.humidity.next_pub < bc_scheduler_get_spin_tick()))
{
bc_radio_pub_humidity(BC_RADIO_PUB_CHANNEL_R3_I2C0_ADDRESS_DEFAULT, &value);
params.humidity.value = value;
params.humidity.next_pub = bc_scheduler_get_spin_tick() + PUB_NO_CHANGE_INTERVAL;
}
}
}
else if (event == BC_MODULE_CLIMATE_EVENT_UPDATE_LUX_METER)
{
if (bc_module_climate_get_illuminance_lux(&value))
{
if (value < 1)
{
value = 0;
}
if ((fabs(value - params.illuminance.value) >= LUX_METER_TAG_PUB_VALUE_CHANGE) || (params.illuminance.next_pub < bc_scheduler_get_spin_tick()) ||
((value == 0) && (params.illuminance.value != 0)) || ((value > 1) && (params.illuminance.value == 0)))
{
bc_radio_pub_luminosity(BC_RADIO_PUB_CHANNEL_R1_I2C0_ADDRESS_DEFAULT, &value);
params.illuminance.value = value;
params.illuminance.next_pub = bc_scheduler_get_spin_tick() + PUB_NO_CHANGE_INTERVAL;
}
}
}
else if (event == BC_MODULE_CLIMATE_EVENT_UPDATE_BAROMETER)
{
if (bc_module_climate_get_pressure_pascal(&value))
{
if ((fabs(value - params.pressure.value) >= BAROMETER_TAG_PUB_VALUE_CHANGE) || (params.pressure.next_pub < bc_scheduler_get_spin_tick()))
{
float meter;
if (!bc_module_climate_get_altitude_meter(&meter))
{
return;
}
bc_radio_pub_barometer(BC_RADIO_PUB_CHANNEL_R1_I2C0_ADDRESS_DEFAULT, &value, &meter);
params.pressure.value = value;
params.pressure.next_pub = bc_scheduler_get_spin_tick() + PUB_NO_CHANGE_INTERVAL;
}
}
}
}
void humidity_tag_event_handler(bc_tag_humidity_t *self, bc_tag_humidity_event_t event, void *event_param)
{
float value;
event_param_t *param = (event_param_t *)event_param;
if (event == BC_TAG_HUMIDITY_EVENT_UPDATE)
{
if (bc_tag_humidity_get_humidity_percentage(self, &value))
{
if ((fabs(value - param->value) >= HUMIDITY_TAG_PUB_VALUE_CHANGE) || (param->next_pub < bc_scheduler_get_spin_tick()))
{
bc_radio_pub_humidity(param->channel, &value);
param->value = value;
param->next_pub = bc_scheduler_get_spin_tick() + PUB_NO_CHANGE_INTERVAL;
}
}
}
}
void application_init(void)
{
bc_radio_init(BC_RADIO_MODE_NODE_SLEEPING);
// Initialize battery
bc_module_battery_init();
bc_module_battery_set_event_handler(battery_event_handler, NULL);
bc_module_battery_set_update_interval(BATTERY_UPDATE_INTERVAL);
// Initialize climate module
bc_module_climate_init();
bc_module_climate_set_update_interval_all_sensors(UPDATE_INTERVAL);
bc_module_climate_set_event_handler(climate_module_event_handler, NULL);
bc_module_climate_measure_all_sensors();
#ifdef USE_HUMIDITY_TAG
// Initialize humidity tag
humidity_event_param.channel = BC_RADIO_PUB_CHANNEL_R3_I2C1_ADDRESS_DEFAULT;
bc_tag_humidity_init(&humidity, BC_TAG_HUMIDITY_REVISION_R3, BC_I2C_I2C1, BC_TAG_HUMIDITY_I2C_ADDRESS_DEFAULT);
bc_tag_humidity_set_update_interval(&humidity, UPDATE_INTERVAL);
bc_tag_humidity_set_event_handler(&humidity, humidity_tag_event_handler, &humidity_event_param);
#endif
#ifdef USE_HUMIDITY_TAG
bc_radio_pairing_request("climate-monitor-tags", VERSION);
#endif
#ifndef USE_HUMIDITY_TAG
bc_radio_pairing_request("climate-monitor-kit", VERSION);
#endif
}