82 lines
2.3 KiB
C
82 lines
2.3 KiB
C
#include <zephyr/kernel.h>
|
|
#include <zephyr/logging/log.h>
|
|
#include <zephyr/device.h>
|
|
#include <zephyr/devicetree.h>
|
|
#include <zephyr/sys/printk.h>
|
|
#include <zephyr/drivers/sensor/sht4x.h>
|
|
#include <zephyr/drivers/gpio.h>
|
|
#include <stdio.h>
|
|
|
|
#include <lora/lorawan.h>
|
|
#include <sensors/mlx90614.h>
|
|
#include <sensors/sht4x.h>
|
|
#include <gpio.h>
|
|
|
|
LOG_MODULE_REGISTER(g2h_heat_main);
|
|
|
|
volatile bool relais_state = false;
|
|
volatile bool relais_manual_override = false;
|
|
volatile uint8_t temperature_threshold = 21;
|
|
volatile uint8_t floor_temp_threshold = 24;
|
|
|
|
/* MAIN */
|
|
int main(void)
|
|
{
|
|
printk("=== Device starting ===\n");
|
|
LOG_INF("G2H Heat Control starting...");
|
|
|
|
/* Initialization */
|
|
init_gpio();
|
|
init_mlx90614();
|
|
init_sht4x();
|
|
init_lorawan();
|
|
// join_network_otaa();
|
|
join_network_abp();
|
|
|
|
float humidity, temp, floor_temp;
|
|
|
|
while (true)
|
|
{
|
|
/* Get stable values from sensors */
|
|
humidity = get_stable_value_sht4x(SENSOR_CHAN_HUMIDITY);
|
|
temp = get_stable_value_sht4x(SENSOR_CHAN_AMBIENT_TEMP);
|
|
printk("hum: %d.%02d%%\n", (int)humidity, (int)(humidity * 100) % 100);
|
|
printk("temp: %d.%02d °C\n", (int)temp, (int)(temp * 100) % 100);
|
|
floor_temp = get_stable_value_mlx90614(MLX90614_IR_TEMP_ADDR);
|
|
printk("floor_temp: %d.%02d °C\n", (int)floor_temp, (int)(floor_temp * 100) % 100);
|
|
|
|
/* Set relais based on temperature if not manually overridden */
|
|
if (!relais_manual_override)
|
|
{
|
|
bool temp_condition = temp < temperature_threshold;
|
|
bool floor_condition = true; // Default: ignore floor temp if sensor not available
|
|
|
|
// Only check floor temperature if MLX sensor is working
|
|
if (mlx90614_sensor_available)
|
|
{
|
|
floor_condition = floor_temp < floor_temp_threshold;
|
|
}
|
|
|
|
if (temp_condition && floor_condition)
|
|
{
|
|
relais_state = 1; // Heat on below thresholds
|
|
}
|
|
else
|
|
{
|
|
relais_state = 0; // Heat off if any threshold exceeded
|
|
}
|
|
set_relais_state(relais_state);
|
|
}
|
|
printk("relais: %d (manual: %d, temp_th: %d°C, floor_th: %d°C)\n",
|
|
relais_state, relais_manual_override, temperature_threshold, floor_temp_threshold);
|
|
|
|
/* Pack and send values */
|
|
char data[7];
|
|
create_data_package(data, temp, floor_temp, humidity, relais_state);
|
|
LOG_HEXDUMP_DBG(data, 7, "data");
|
|
send_data_packet(data, 7);
|
|
|
|
/* Delay until next cycle */
|
|
k_sleep(SEND_INTERVALL);
|
|
}
|
|
} |