107 lines
3.1 KiB
C
107 lines
3.1 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 heating_logic_enabled = true;
|
|
volatile uint8_t temperature_threshold = 21;
|
|
volatile uint8_t floor_temp_threshold = 24;
|
|
volatile uint32_t send_interval_minutes = 10;
|
|
|
|
/* 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);
|
|
|
|
/* Try MLX sensor first for both room and floor temperature */
|
|
floor_temp = get_stable_value_mlx90614(MLX90614_IR_TEMP_ADDR);
|
|
if (mlx90614_sensor_available)
|
|
{
|
|
/* Use MLX90614 ambient temperature as room temperature */
|
|
temp = get_stable_value_mlx90614(MLX90614_INTERNAL_TEMP_ADDR);
|
|
printk("hum: %d.%02d%% (SHT4x)\n", (int)humidity, (int)(humidity * 100) % 100);
|
|
printk("temp: %d.%02d °C (MLX ambient)\n", (int)temp, (int)(temp * 100) % 100);
|
|
}
|
|
else
|
|
{
|
|
/* Fallback to SHT4x temperature if MLX not available */
|
|
temp = get_stable_value_sht4x(SENSOR_CHAN_AMBIENT_TEMP);
|
|
printk("hum: %d.%02d%% (SHT4x)\n", (int)humidity, (int)(humidity * 100) % 100);
|
|
printk("temp: %d.%02d °C (SHT4x fallback)\n", (int)temp, (int)(temp * 100) % 100);
|
|
}
|
|
if (mlx90614_sensor_available)
|
|
{
|
|
LOG_INF("MLX stable measurement: %d.%02d °C", (int)floor_temp, (int)(floor_temp * 100) % 100);
|
|
}
|
|
else
|
|
{
|
|
LOG_WRN("MLX sensor not available");
|
|
}
|
|
|
|
printk("floor_temp: %d.%02d °C (available: %s)\n",
|
|
(int)floor_temp, (int)(floor_temp * 100) % 100,
|
|
mlx90614_sensor_available ? "YES" : "NO");
|
|
|
|
/* Set relais based on temperature if heating logic is enabled */
|
|
if (heating_logic_enabled)
|
|
{
|
|
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 (heating_enabled: %d, temp_th: %d°C, floor_th: %d°C)\n",
|
|
relais_state, heating_logic_enabled, 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(K_MINUTES(send_interval_minutes));
|
|
}
|
|
} |