增加I2C 使用SHT40温湿度传感器上传TCP客户端数据
This commit is contained in:
79
app/main.c
79
app/main.c
@ -10,6 +10,7 @@
|
||||
#include "lwip/inet_chksum.h"
|
||||
#include <string.h>
|
||||
#include "lwip/prot/ip4.h"
|
||||
#include "sht40.h"
|
||||
|
||||
/* Utility macros */
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
@ -422,6 +423,9 @@ static void tcp_client_entry(void *parameter)
|
||||
reconnect_count = 0;
|
||||
reconnect_delay = TCP_CLIENT_INIT_RECONNECT_DELAY;
|
||||
|
||||
int report_interval = 5000; // 5秒上报一次温湿度数据
|
||||
osal_tick_t last_report_time = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* Check Link Status */
|
||||
@ -438,6 +442,59 @@ static void tcp_client_entry(void *parameter)
|
||||
}
|
||||
}
|
||||
|
||||
/* 定期读取并上报温湿度数据 */
|
||||
osal_tick_t current_time = osal_tick_get();
|
||||
if (current_time - last_report_time >= report_interval)
|
||||
{
|
||||
float temperature = 0.0f, humidity = 0.0f;
|
||||
int result = sht40_read_temperature_humidity(&temperature, &humidity);
|
||||
|
||||
if (result == 0)
|
||||
{
|
||||
char sensor_data[64];
|
||||
int temp_int = (int)(temperature * 100);
|
||||
int hum_int = (int)(humidity * 100);
|
||||
|
||||
/* 处理负值温度显示 */
|
||||
int temp_integer = temp_int / 100;
|
||||
int temp_decimal = temp_int % 100;
|
||||
if (temp_decimal < 0) temp_decimal = -temp_decimal;
|
||||
|
||||
int hum_integer = hum_int / 100;
|
||||
int hum_decimal = hum_int % 100;
|
||||
if (hum_decimal < 0) hum_decimal = -hum_decimal;
|
||||
|
||||
if (temp_integer >= 0)
|
||||
{
|
||||
snprintf(sensor_data, sizeof(sensor_data), "TEMP=+%d.%02d℃,HUM=%d.%02d%%RH\n",
|
||||
temp_integer, temp_decimal, hum_integer, hum_decimal);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(sensor_data, sizeof(sensor_data), "TEMP=%d.%02d℃,HUM=%d.%02d%%RH\n",
|
||||
temp_integer, temp_decimal, hum_integer, hum_decimal);
|
||||
}
|
||||
|
||||
if (send(sock, sensor_data, strlen(sensor_data), 0) < 0)
|
||||
{
|
||||
if (errno != EWOULDBLOCK)
|
||||
{
|
||||
osal_log_e("Send sensor data failed: %d", errno);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_log_i("Sent sensor data: %s", sensor_data);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_log_e("Failed to read sensor data, result: %d", result);
|
||||
}
|
||||
last_report_time = current_time;
|
||||
}
|
||||
|
||||
/* Wait for data with timeout */
|
||||
fd_set rset;
|
||||
struct timeval tv;
|
||||
@ -588,6 +645,27 @@ int main(void)
|
||||
rt_kprintf("Failed to create thread 'eth_input'\n");
|
||||
}
|
||||
|
||||
/* Initialize SHT40 sensor */
|
||||
if (sht40_init() != 0)
|
||||
{
|
||||
osal_log_e("SHT40 sensor initialization failed");
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_log_i("SHT40 sensor initialized successfully");
|
||||
|
||||
/* Use heater once during initialization for self-calibration */
|
||||
osal_log_i("Performing SHT40 self-calibration using heater...");
|
||||
if (sht40_heater_enable(1) == 0) // Use medium power (110mW)
|
||||
{
|
||||
osal_log_i("SHT40 self-calibration completed successfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
osal_log_e("SHT40 self-calibration failed");
|
||||
}
|
||||
}
|
||||
|
||||
/* Create TCP Client Thread */
|
||||
tid = osal_thread_create("tcp_client", tcp_client_entry, NULL, 2048, 15);
|
||||
if (tid != NULL)
|
||||
@ -621,3 +699,4 @@ int main(void)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user