演示视频
项目简介
本项目是基于RA4M2完成了环境网关设备,传感器HS3003采集温湿度,温湿度通过wifi模块周期性上传到TCP服务器平台;
调试环境
windows10;
MDK5;
RASC4.0.0;
DAP调试器;
软件实现逻辑
上电之后,做系统初始化,初始化完成之后WIFI自动连接TCP服务端,并将温湿度信息发送给TCP服务端。实现环境网关。
hs3003.c
#include "hs3003.h"
#include "stdio.h"
typedef enum e_demo_sequence
{
DEMO_SEQUENCE_1 = (1),
DEMO_SEQUENCE_2,
DEMO_SEQUENCE_3,
DEMO_SEQUENCE_4,
DEMO_SEQUENCE_5,
DEMO_SEQUENCE_6,
} demo_sequence_t;
typedef enum e_hs3003_callback_status
{
HS3003_CALLBACK_STATUS_WAIT = (0),
HS3003_CALLBACK_STATUS_SUCCESS,
HS3003_CALLBACK_STATUS_REPEAT,
} hs3003_callback_status_t;
#define SENSOR_TEMP_RANGE_MAX (80)
#define SENSOR_TEMP_RANGE_MIN (-10)
#define SENSOR_HUMI_RANGE_MAX (100)
#define SENSOR_HUMI_RANGE_MIN (0)
#define RM_HS300X_MASK_HUMIDITY_UPPER_0X3F (0x3F)
#define RM_HS300X_MASK_TEMPERATURE_LOWER_0XFC (0xFC)
#define RM_HS300X_MASK_STATUS_0XC0 (0xC0)
#define RM_HS300X_DATA_STATUS_VALID (0x00)
#define RM_HS300X_CALC_STATIC_VALUE (16383.0F)
#define RM_HS300X_CALC_HUMD_VALUE_100 (100.0F)
#define RM_HS300X_CALC_TEMP_C_VALUE_165 (165.0F)
#define RM_HS300X_CALC_TEMP_C_VALUE_40 (40.0F)
#define RM_HS300X_CALC_DECIMAL_VALUE_100 (100.0F)
void g_comms_i2c_bus0_quick_setup(void);
void g_hs300x_sensor0_quick_setup(void);
void start_demo(void);
static void demo_err(void);
static volatile hs3003_callback_status_t hs3003_callback_status;
void g_comms_i2c_bus0_quick_setup(void)
{
fsp_err_t err;
err = R_SCI_I2C_Open(&hs3003_i2c_ctrl, &hs3003_i2c_cfg);
assert(FSP_SUCCESS == err);
}
void hs3003_i2c_callback(i2c_master_callback_args_t * p_args)
{
if (HS3003_CALLBACK_STATUS_SUCCESS == p_args->event)
{
hs3003_callback_status = HS3003_CALLBACK_STATUS_SUCCESS;
}
else
{
hs3003_callback_status = HS3003_CALLBACK_STATUS_REPEAT;
}
}
void start_hs3003(void)
{
fsp_err_t err;
uint8_t r_buf[4] = {0};
uint16_t humi, temp;
float tmp_f = 0.0;
uint8_t data[2] = {0x00,0x00};
demo_sequence_t sequence = DEMO_SEQUENCE_1;
g_comms_i2c_bus0_quick_setup();
while(1)
{
err = R_SCI_I2C_Write(&hs3003_i2c_ctrl,data,1,0);
R_BSP_SoftwareDelay(40000,BSP_DELAY_UNITS_MICROSECONDS);
err = R_SCI_I2C_Read(&hs3003_i2c_ctrl,&r_buf,4,1);
if(err == FSP_SUCCESS)
{
if ((r_buf[0] & RM_HS300X_MASK_STATUS_0XC0) != RM_HS300X_DATA_STATUS_VALID)
{
printf("转换时间不哆");
}
humi = (r_buf[0] & RM_HS300X_MASK_HUMIDITY_UPPER_0X3F) << 8 | r_buf[1];
temp = (r_buf[2] << 8 | (r_buf[3] & RM_HS300X_MASK_TEMPERATURE_LOWER_0XFC)) >> 2;
tmp_f = (float)humi;
tmp_f = (tmp_f * RM_HS300X_CALC_HUMD_VALUE_100) / RM_HS300X_CALC_STATIC_VALUE;
printf("湿度: %.2f\n",tmp_f );
tmp_f = (float)temp;
tmp_f = ((tmp_f * RM_HS300X_CALC_TEMP_C_VALUE_165) / RM_HS300X_CALC_STATIC_VALUE) - RM_HS300X_CALC_TEMP_C_VALUE_40;
printf("温度: %.2f\n", tmp_f);
}
else {
printf("读取传感器出错!\n");
}
R_BSP_SoftwareDelay(1000000,BSP_DELAY_UNITS_MICROSECONDS);
}
}
hs3003.h
#ifndef __HS3003_H
#define __HS3003_H
#include "hal_data.h"
#define HS300X_ADR 0x44
#define HS300X_TEMP_MULTY 0.010071415
#define HS300X_TEMP_MIN 40
#define HS300X_HUMD_MULTY 0.006163516
#define HS300X_MAX_ITERATION 100
#define HS300X_DELAY_MEASUREMENT 35
#define HS300X_DELAY_ITERATION 1
#define HS300X_STALE_DATA 2
#define HS300X_OK 1
#define HS300X_ERROR_SENSOR_BUSY 0
#define HS300X_ERROR_COLLISION_I2C -1
void start_hs3003(void);
#endif
uart.c
#include "uart.h"
void bsp_uart_init(void)
{
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_UART_Open (&esp8266_uart_ctrl, &esp8266_uart_cfg);
assert(FSP_SUCCESS == err);
}
volatile bool uart_send_complete_flag = false;/
void esp8266_uart_callback (uart_callback_args_t * p_args)
{
switch (p_args->event)
{
case UART_EVENT_RX_CHAR:
{
R_SCI_UART_Write(&esp8266_uart_ctrl, (uint8_t *)&(p_args->data), 1);
break;
}
case UART_EVENT_TX_COMPLETE:
{
uart_send_complete_flag = true;
break;
}
default:
break;
}
}
#if defined __GNUC__ && !defined __clang__
int _write(int fd, char *pBuffer, int size);
int _write(int fd, char *pBuffer, int size)
{
(void)fd;
R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)pBuffer, (uint32_t)size);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
return size;
}
#else
int fputc(int ch, FILE *f)
{
(void)f;
R_SCI_UART_Write(&esp8266_uart_ctrl, (uint8_t *)&ch, 1);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
return ch;
}
#endif
uart.h
#ifndef __BSP_UART_H__
#define __BSP_UART_H__
#include "hal_data.h"
#include "stdio.h"
void bsp_uart_init(void);
#endif
0
|
|
|
|