本项目是基于RA4M2完成了环境网关设备,传感器HS3003采集温湿度,温湿度通过wifi模块周期性上传到TCP服务器平台;
windows10;
MDK5;
RASC4.0.0;
DAP调试器;
上电之后,做系统初始化,初始化完成之后WIFI自动连接TCP服务端,并将温湿度信息发送给TCP服务端。实现环境网关。
hs3003.c
#include "hs3003.h"
#include "stdio.h"
/* Sequence */
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;
/* Callback status */
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)
/* Definitions of Mask Data for A/D data */
#define RM_HS300X_MASK_HUMIDITY_UPPER_0X3F (0x3F)
#define RM_HS300X_MASK_TEMPERATURE_LOWER_0XFC (0xFC)
#define RM_HS300X_MASK_STATUS_0XC0 (0xC0)
/* Definitions for Status Bits of A/D Data */
#define RM_HS300X_DATA_STATUS_VALID (0x00) // Status-bit: Valid data
/* Definitions for Calculation */
#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)
/* See Developer Assistance in the project */
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)
{
// printf("0x%X,0x%X,0x%X,0x%X\n", r_buf[0], r_buf[1], r_buf[2], r_buf[3]);
// printf("state:%x\n", r_buf[0] & RM_HS300X_MASK_STATUS_0XC0);
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 // look at datasheet
#define HS300X_TEMP_MULTY 0.010071415 // look at datasheet
#define HS300X_TEMP_MIN 40 // look at datasheet
#define HS300X_HUMD_MULTY 0.006163516 // look at datasheet
#define HS300X_MAX_ITERATION 100
#define HS300X_DELAY_MEASUREMENT 35 // typical on datasheet 16.90 ms, rounded up a little (35ms = 1 try)
#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;
}
}
/* 重定向 printf 输出 */
#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
更多回帖