add mqtt, decouple i2c and sensor
This commit is contained in:
70
main.c
70
main.c
@@ -4,6 +4,8 @@
|
||||
#include "ch32v20xhw.h"
|
||||
#include "ethernetif.h"
|
||||
#include "gxht30_hw_i2c.h"
|
||||
#include "ha_mqtt.h"
|
||||
#include "hw_i2c.h"
|
||||
#include "lwip/apps/httpd.h"
|
||||
#include "lwip/dhcp.h"
|
||||
#include "lwip/init.h"
|
||||
@@ -26,8 +28,7 @@
|
||||
#define PLL_MULTIPLIER 15
|
||||
|
||||
#define STATS_PRINT_INTERVAL_MS 10000
|
||||
#define SENSOR_READ_INTERVAL_MS 5000
|
||||
#define STATUS_READ_INTERVAL_MS 30000
|
||||
#define SENSOR_READ_INTERVAL_MS 60000
|
||||
|
||||
struct netif g_netif;
|
||||
static volatile int g_httpd_is_initialized = 0;
|
||||
@@ -181,17 +182,17 @@ void ethernetif_print_stats(void) {
|
||||
int main() {
|
||||
SystemInit();
|
||||
set_sysclk_to_120mhz_from_hse();
|
||||
print_clock_registers();
|
||||
|
||||
systick_init();
|
||||
led_init();
|
||||
lwip_stack_init();
|
||||
|
||||
gxht30_i2c_init();
|
||||
i2c_init();
|
||||
|
||||
gxht30_soft_reset(GXHT30_I2C_ADDR_DEFAULT);
|
||||
Delay_Ms(10);
|
||||
|
||||
ha_mqtt_init();
|
||||
|
||||
uint32_t last_led_toggle_time = 0;
|
||||
uint32_t last_link_poll_time = 0;
|
||||
#if LWIP_STATS
|
||||
@@ -203,9 +204,8 @@ int main() {
|
||||
uint32_t last_status_read_time = 0;
|
||||
|
||||
GXHT30_Data sensor_data = {0};
|
||||
GXHT30_Status sensor_status = {0};
|
||||
|
||||
printf("GXHT30 Sensor initialized\n");
|
||||
printf("System initialized. Main loop starting.\n");
|
||||
|
||||
while (1) {
|
||||
ethernetif_input(&g_netif);
|
||||
@@ -223,60 +223,30 @@ int main() {
|
||||
}
|
||||
#endif
|
||||
|
||||
// Read sensor data periodically
|
||||
ha_mqtt_check_and_reconnect();
|
||||
|
||||
if (millis() - last_sensor_read_time > SENSOR_READ_INTERVAL_MS) {
|
||||
if (gxht30_read_data(GXHT30_I2C_ADDR_DEFAULT, &sensor_data) ==
|
||||
GXHT30_OK) {
|
||||
int16_t temp_int = (int16_t)(sensor_data.temperature * 100);
|
||||
int16_t hum_int = (int16_t)(sensor_data.humidity * 100);
|
||||
// int32_t temp_int = sensor_data.temperature_x100 / 100;
|
||||
// int32_t temp_frac = sensor_data.temperature_x100 % 100;
|
||||
// if (temp_frac < 0) temp_frac = -temp_frac;
|
||||
|
||||
int16_t temp_whole = temp_int / 100;
|
||||
int16_t temp_decimal = temp_int % 100;
|
||||
if (temp_decimal < 0) temp_decimal = -temp_decimal;
|
||||
// int32_t hum_int = sensor_data.humidity_x100 / 100;
|
||||
// int32_t hum_frac = sensor_data.humidity_x100 % 100;
|
||||
|
||||
int16_t hum_whole = hum_int / 100;
|
||||
int16_t hum_decimal = hum_int % 100;
|
||||
// printf("Read Sensor -> Temp: %ld.%02ld C, Hum: %ld.%02ld %%\n",
|
||||
// temp_int, temp_frac, hum_int, hum_frac);
|
||||
ha_mqtt_publish_sensor_data(sensor_data.temperature_x100,
|
||||
sensor_data.humidity_x100);
|
||||
|
||||
printf("Temperature: %d.%02d C | Humidity: %d.%02d %%\n", temp_whole,
|
||||
temp_decimal, hum_whole, hum_decimal);
|
||||
} else {
|
||||
printf("Sensor error: %d\n", sensor_data.error);
|
||||
printf("Failed to read sensor. Error: %d\n", sensor_data.error);
|
||||
}
|
||||
|
||||
last_sensor_read_time = millis();
|
||||
}
|
||||
|
||||
// Read status register periodically
|
||||
if (millis() - last_status_read_time > STATUS_READ_INTERVAL_MS) {
|
||||
if (gxht30_read_status(GXHT30_I2C_ADDR_DEFAULT, &sensor_status) ==
|
||||
GXHT30_OK) {
|
||||
printf("Status: ");
|
||||
|
||||
if (sensor_status.alert_pending) printf("ALERT ");
|
||||
if (sensor_status.heater_on) printf("HEATER_ON ");
|
||||
if (sensor_status.humidity_alert) printf("HUM_ALERT ");
|
||||
if (sensor_status.temperature_alert) printf("TEMP_ALERT ");
|
||||
if (sensor_status.reset_detected) printf("RESET ");
|
||||
if (sensor_status.command_status) printf("CMD_ERR ");
|
||||
if (sensor_status.crc_status) printf("CRC_ERR ");
|
||||
|
||||
if (sensor_status.raw_status == 0x0010) {
|
||||
printf("OK (reset detected on startup)");
|
||||
} else if (sensor_status.raw_status == 0x0000) {
|
||||
printf("OK");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
// Clear reset flag after first read if you want
|
||||
// if (sensor_status.reset_detected) {
|
||||
// gxht30_clear_status(GXHT30_I2C_ADDR_DEFAULT);
|
||||
// }
|
||||
} else {
|
||||
printf("Status read error\n");
|
||||
}
|
||||
last_status_read_time = millis();
|
||||
}
|
||||
|
||||
// uint32_t now = millis();
|
||||
// if (now - last_led_toggle_time > LED_TOGGLE_INTERVAL_MS) {
|
||||
// if (led_state) {
|
||||
|
||||
Reference in New Issue
Block a user