This commit is contained in:
2025-11-10 12:34:10 +06:00
parent e448282aed
commit 80478c7400
3 changed files with 369 additions and 1 deletions

72
main.c
View File

@@ -3,6 +3,7 @@
#include "ch32fun.h"
#include "ch32v20xhw.h"
#include "ethernetif.h"
#include "gxht30_hw_i2c.h"
#include "lwip/apps/httpd.h"
#include "lwip/dhcp.h"
#include "lwip/init.h"
@@ -25,6 +26,8 @@
#define PLL_MULTIPLIER 15
#define STATS_PRINT_INTERVAL_MS 10000
#define SENSOR_READ_INTERVAL_MS 5000
#define STATUS_READ_INTERVAL_MS 30000
struct netif g_netif;
static volatile int g_httpd_is_initialized = 0;
@@ -178,10 +181,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();
gxht30_soft_reset(GXHT30_I2C_ADDR_DEFAULT);
Delay_Ms(10);
uint32_t last_led_toggle_time = 0;
uint32_t last_link_poll_time = 0;
#if LWIP_STATS
@@ -189,6 +199,14 @@ int main() {
#endif
int led_state = 0;
uint32_t last_sensor_read_time = 0;
uint32_t last_status_read_time = 0;
GXHT30_Data sensor_data = {0};
GXHT30_Status sensor_status = {0};
printf("GXHT30 Sensor initialized\n");
while (1) {
ethernetif_input(&g_netif);
sys_check_timeouts();
@@ -205,6 +223,60 @@ int main() {
}
#endif
// Read sensor data periodically
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);
int16_t temp_whole = temp_int / 100;
int16_t temp_decimal = temp_int % 100;
if (temp_decimal < 0) temp_decimal = -temp_decimal;
int16_t hum_whole = hum_int / 100;
int16_t hum_decimal = hum_int % 100;
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);
}
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) {