0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內(nèi)不再提示

基于PSoC62開發(fā)板的智能室內(nèi)環(huán)境監(jiān)測系統(tǒng)

冬至子 ? 來源:kiyomiZ ? 作者:kiyomiZ ? 2023-07-31 15:47 ? 次閱讀

一、系統(tǒng)總體設計

1.項目簡介

基于psoc6的智能室內(nèi)環(huán)境監(jiān)測系統(tǒng)以PSoC62 with CAPSENSE evaluation kit為控制核心,通過AHT10溫濕度傳感器檢測模塊采集室內(nèi)環(huán)境數(shù)據(jù),并將數(shù)據(jù)通過無線網(wǎng)絡傳送到主控模塊,根據(jù)設定的閾值來控制電源、風扇等設備,從而達到自動調(diào)節(jié)室內(nèi)環(huán)境參數(shù)的目的。同時通過RW007 wifi模塊實現(xiàn)遠程的數(shù)據(jù)實時監(jiān)測與傳送,上傳到物聯(lián)網(wǎng)平臺onenet進行可視化。

2.硬件設計

開發(fā)板:PSoC62 with CAPSENSE evaluation kit開發(fā)板
涉及外設:
1)AHT10溫濕度傳感器
2)rw007 wifi模塊

3.軟件實現(xiàn)
軟件層基于RT-Thread,為了實現(xiàn)智能環(huán)境監(jiān)測的功能,使用了以下軟件包:
1)aht10:提供了使用溫度與濕度傳感器aht10基本功能,并且提供了軟件平均數(shù)濾波器可選功能。
2)Onenet:針對 OneNET 平臺連接做的的適配,可以讓設備完成數(shù)據(jù)的發(fā)送、接收、設備的注冊和控制等功能。
3)RW007-WiFi:RW007 模塊的 SPI 驅(qū)動。

二、部分代碼實現(xiàn)
1.aht.c部分代碼
#include
#include
#include
#include
#define DBG_TAG "aht10"
#define DBG_LVL DBG_INFO
#include
#include "aht10.h"
#ifdef PKG_USING_AHT10
#define AHT10_ADDR 0x38 //connect GND
#define AHT10_CALIBRATION_CMD 0xE1 //calibration cmd for measuring
#define AHT10_NORMAL_CMD 0xA8 //normal cmd
#define AHT10_GET_DATA 0xAC //get data cmd
static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
{
rt_uint8_t buf[3];
buf[0] = reg; //cmd
buf[1] = data[0];
buf[2] = data[1];
if (rt_i2c_master_send(bus, AHT10_ADDR, 0, buf, 3) == 3)
return RT_EOK;
else
return -RT_ERROR;
}
static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf)
{
struct rt_i2c_msg msgs;
msgs.addr = AHT10_ADDR;
msgs.flags = RT_I2C_RD;
msgs.buf = buf;
msgs.len = len;
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
static rt_err_t sensor_init(aht10_device_t dev)
{
rt_uint8_t temp[2] = {0, 0};
write_reg(dev->i2c, AHT10_NORMAL_CMD, temp);
rt_thread_delay(rt_tick_from_millisecond(500)); //at least 300 ms
temp[0] = 0x08;
temp[1] = 0x00;
write_reg(dev->i2c, AHT10_CALIBRATION_CMD, temp); //go into calibration
rt_thread_delay(rt_tick_from_millisecond(450)); //at least 300 ms
return RT_EOK;
}
/*check calibration enable */
static rt_uint8_t calibration_enabled(aht10_device_t dev)
{
rt_uint8_t val = 0;
read_regs(dev->i2c, 1, &val);
if ((val & 0x68) == 0x08)
return RT_EOK;
else
return RT_ERROR;
}
static float read_hw_temperature(aht10_device_t dev)
{
rt_uint8_t temp[6];
float cur_temp = -50.0; //The data is error with missing measurement.
rt_err_t result;
RT_ASSERT(dev);
result = rt_mutex_take(dev->lock, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
rt_uint8_t cmd[2] = {0, 0};
write_reg(dev->i2c, AHT10_GET_DATA, cmd); // sample data cmd
result = calibration_enabled(dev);
if (result != RT_EOK)
{
rt_thread_mdelay(1500);
sensor_init(dev); // reset sensor
LOG_E("The aht10 is under an abnormal status. Please try again");
}
else
{
read_regs(dev->i2c, 6, temp); // get data
/*sensor temperature converse to reality */
cur_temp = ((temp[3] & 0xf) << 16 | temp[4] << 8 | temp[5]) * 200.0 / (1 << 20) - 50;
}
}
else
{
LOG_E("The aht10 could not respond temperature measurement at this time. Please try again");
}
rt_mutex_release(dev->lock);
return cur_temp;
}
static float read_hw_humidity(aht10_device_t dev)
{
rt_uint8_t temp[6];
float cur_humi = 0.0; //The data is error with missing measurement.
rt_err_t result;
RT_ASSERT(dev);
result = rt_mutex_take(dev->lock, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
rt_uint8_t cmd[2] = {0, 0};
write_reg(dev->i2c, AHT10_GET_DATA, cmd); // sample data cmd
result = calibration_enabled(dev);
if (result != RT_EOK)
{
rt_thread_mdelay(1500);
sensor_init(dev);
LOG_E("The aht10 is under an abnormal status. Please try again");
}
else
{
read_regs(dev->i2c, 6, temp); // get data
cur_humi = (temp[1] << 12 | temp[2] << 4 | (temp[3] & 0xf0) >> 4) * 100.0 / (1 << 20); //sensor humidity converse to reality
}
}
else
{
LOG_E("The aht10 could not respond temperature measurement at this time. Please try again");
}
rt_mutex_release(dev->lock);
return cur_humi;
}
2.spi_wifi_rw007.c部分代碼
#include
#include
#include
#ifndef RW007_LOG_LEVEL
#define RW007_LOG_LEVEL DBG_LOG
#endif
#define DBG_ENABLE
#define DBG_SECTION_NAME "[RW007]"
#define DBG_LEVEL RW007_LOG_LEVEL
#define DBG_COLOR
#include
#include "spi_wifi_rw007.h"
static struct rw007_spi rw007_spi;
static struct rw007_wifi wifi_sta, wifi_ap;
static struct rt_event spi_wifi_data_event;
static rt_bool_t inited = RT_FALSE;
static rw007_power_switch_t _powerswitch = rw007_power_switch_on;
rw007_ble_recv_data_func rw007_ble_recv_cb;
void rw007_ble_recv_data_func_reg(rw007_ble_recv_data_func recv_func_cb)
{
rw007_ble_recv_cb = recv_func_cb;
}
static void rw007_ble_recv_data(void *buff, int len)
{
if(rw007_ble_recv_cb)
{
rw007_ble_recv_cb(buff, len);
}
}
void rw007_ble_send_data(void *buff, int len)
{
struct spi_data_packet * data_packet = RT_NULL;
data_packet = rt_mp_alloc(&rw007_spi.spi_tx_mp, RT_WAITING_FOREVER);
if (data_packet)
{
data_packet->data_type = DATA_TYPE_BLE;
data_packet->data_len = len;
rt_memcpy(data_packet->buffer, (const char *)buff, len);
rt_mb_send(&rw007_spi.spi_tx_mb, (rt_ubase_t)data_packet);
rt_event_send(&spi_wifi_data_event, RW007_MASTER_DATA);
}
}
static int wifi_data_transfer(struct rw007_spi *dev, uint16_t seq, uint8_t rx_buffer, const void tx_buffer)
{
const struct spi_data_packet send_packet = (const struct spi_data_packet )tx_buffer;
struct spi_master_request cmd;
struct spi_slave_response resp;
struct rt_spi_message message;
struct rt_spi_device rt_spi_device = dev->spi_device;
rt_uint32_t max_data_len = 0;
/
Clear cmd /
rt_memset(&cmd, 0, sizeof(cmd));
cmd.type = master_cmd_phase;
cmd.seq = seq;
/
Set magic word /
cmd.magic1 = MASTER_MAGIC1;
cmd.magic2 = MASTER_MAGIC2;
/
If the buffer is not full, Set master ready flag bit /
if(rx_buffer != RT_NULL)
{
cmd.flag |= MASTER_FLAG_MRDY;
}
/
Set length for master to slave when data ready
/
if (send_packet != RT_NULL)
{
/
Invalid data packet /
if ((send_packet->data_len == 0) || (send_packet->data_len > SPI_MAX_DATA_LEN))
{
send_packet = RT_NULL;
}
else
{
cmd.M2S_len = send_packet->data_len + member_offset(struct spi_data_packet, buffer);
}
}
/
Stage 1: Send command to rw007 /
rt_memset(&resp, 0, sizeof(resp));
message.send_buf = &cmd;
message.recv_buf = &resp;
message.length = sizeof(resp);
message.cs_take = 1;
message.cs_release = 0;
message.next = RT_NULL;
/
Start a SPI transmit /
rt_spi_take_bus(rt_spi_device);
/
Receive response from rw007 /
rt_spi_device->bus->ops->xfer(rt_spi_device, &message);
/
Clear event /
rt_event_recv(&spi_wifi_data_event,
RW007_SLAVE_INT,
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
RT_WAITING_NO,
RT_NULL);
rt_spi_release(rt_spi_device);
/
End a SPI transmit /
rt_spi_release_bus(rt_spi_device);
/
checkout Stage 1 slave status /
if ((resp.magic1 != SLAVE_MAGIC1) || (resp.magic2 != SLAVE_MAGIC2) || (resp.type != slave_cmd_phase))
{
LOG_E("The wifi Stage 1 status %x %x %x %dr", resp.magic1, resp.magic2, resp.type, cmd.seq);
goto _cmderr;
}
/
receive first event /
if (rt_event_recv(&spi_wifi_data_event,
RW007_SLAVE_INT,
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
SLAVE_INT_TIMEOUT,
RT_NULL) != RT_EOK)
{
LOG_E("The wifi slave response timed outr");
}
/
Stage 2: Receive response from rw007 /
cmd.type = master_data_phase; //data Stage
rt_memset(&resp, 0, sizeof(resp));
message.send_buf = &cmd;
message.recv_buf = &resp;
message.length = sizeof(resp);
message.cs_take = 1;
message.cs_release = 0;
/
Start a SPI transmit /
rt_spi_take_bus(rt_spi_device);
/
Receive response from rw007 /
rt_spi_device->bus->ops->xfer(rt_spi_device, &message);
/
Check response's magic word and seq /
if ((resp.magic1 != SLAVE_MAGIC1) || (resp.magic2 != SLAVE_MAGIC2) || (resp.seq != seq) || (resp.type != slave_data_phase))
{
LOG_E("The wifi Stage 2 status %x %x %x %x %d %dr", resp.magic1, resp.magic2, resp.seq, resp.type, resp.S2M_len, cmd.seq);
goto _txerr;
}
/
Check rw007's data ready flag /
if (resp.flag & SLAVE_FLAG_SRDY)
{
max_data_len = cmd.M2S_len;
}
if (resp.S2M_len > MAX_SPI_PACKET_SIZE)
{
/
Drop error data /
resp.S2M_len = 0;
}
if (resp.S2M_len > max_data_len)
{
max_data_len = resp.S2M_len;
}
/
Setup message /
if((resp.S2M_len == 0) && (rx_buffer != RT_NULL))
{
rt_mp_free(rx_buffer);
rx_buffer = RT_NULL;
}
message.send_buf = send_packet;
message.recv_buf = rx_buffer;
message.length = RT_ALIGN(max_data_len, 4);/
align clk to word /
message.cs_take = 0;
message.cs_release = 1;
/
Transmit data /
rt_spi_device->bus->ops->xfer(rt_spi_device, &message);
/
End a SPI transmit /
rt_spi_release_bus(rt_spi_device);
/
Parse recevied data /
if(rx_buffer)
{
rt_mb_send(&dev->spi_rx_mb, (rt_ubase_t)rx_buffer);
}
/
receive data end event /
if (rt_event_recv(&spi_wifi_data_event,
RW007_SLAVE_INT,
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
SLAVE_INT_TIMEOUT,
RT_NULL) != RT_EOK)
{
LOG_E("The wifi slave data response timed outr");
}
/
The slave has data /
if (resp.slave_tx_buf > 0)
{
return TRANSFER_DATA_CONTINUE;
}
return TRANSFER_DATA_SUCCESS;
_txerr:
/
END SPI transfer */
message.send_buf = RT_NULL;
message.recv_buf = RT_NULL;
message.length = 0;
message.cs_take = 0;
message.cs_release = 1;
rt_spi_device->bus->ops->xfer(rt_spi_device, &message);
rt_spi_release_bus(rt_spi_device); // End a SPI transmit
_cmderr:
rt_thread_delay(1);
return TRANSFER_DATA_ERROR;
}
static int spi_wifi_transfer(struct rw007_spi *dev, const void tx_buffer)
{
static uint16_t cmd_seq = 0;
int result = TRANSFER_DATA_SUCCESS;
uint8_t * rx_buffer = rt_mp_alloc(&dev->spi_rx_mp, RT_WAITING_NO);
int32_t retry;
/
Generate the transmission sequence number /
cmd_seq++;
if (cmd_seq >= 65534)
{
cmd_seq = 1;
}
/
set retry count /
retry = 3;
while (retry > 0)
{
result = wifi_data_transfer(dev, cmd_seq, rx_buffer, tx_buffer);
if (result != TRANSFER_DATA_ERROR)
{
break;
}
retry--;
}
/
Receive response from rw007 error */
if (retry <= 0)
{
LOG_E("rw007 transfer failedr");
goto _err;
}
return result;
_err:
if(rx_buffer)
{
rt_mp_free((void *)rx_buffer);
}
return TRANSFER_DATA_ERROR;
}
static void _wifi_buffer_free(void)
{
struct spi_data_packet send_packet = RT_NULL;
/
Check to see if any data needs to be sent */
while (rt_mb_recv(&rw007_spi.spi_tx_mb, (rt_ubase_t )&send_packet, RT_WAITING_NO) == RT_EOK)
{
/
Free send data space */
if (send_packet != RT_NULL)
{
rt_mp_free((void )send_packet);
send_packet = RT_NULL;
}
}
/
Check to see if any data needs to be sent */
while (rt_mb_recv(&rw007_spi.spi_rx_mb, (rt_ubase_t )&send_packet, RT_WAITING_NO) == RT_EOK)
{
/
Free send data space */
if (send_packet != RT_NULL)
{
rt_mp_free((void *)send_packet);
send_packet = RT_NULL;
}
}
rt_kprintf("free wifi buffern");
}
static rw007_power_switch_cb_t _powerup_cb = 0;
static rw007_power_switch_cb_t _powerdown_cb = 0;
static void rw007_powerswitch(rw007_power_switch_t power_switch)
{
static rw007_power_switch_t current_switch = rw007_power_switch_on;
if (!(power_switch != current_switch))
{
return;
}
current_switch = power_switch;
if (power_switch)
{
if (_powerup_cb != 0)
{
_powerup_cb();
}
}
else
{
if (_powerdown_cb != 0)
{
_powerdown_cb();
}
}
}
rt_err_t rw007_register_powerswitch_cb(rw007_power_switch_cb_t powerdown_cb, rw007_power_switch_cb_t powerup_cb)
{
if (!powerdown_cb || !powerup_cb) return -RT_ERROR;
_powerup_cb = powerup_cb;
_powerdown_cb = powerdown_cb;
return RT_EOK;
}
rt_err_t rw007_powerswitch_request(rw007_power_switch_t power_switch)
{
rt_ubase_t level;
if (!_powerdown_cb || !_powerup_cb) return -RT_EEMPTY;
level = rt_hw_interrupt_disable();
if (power_switch != _powerswitch)
{
_powerswitch = power_switch;
rt_hw_interrupt_enable(level);
return rt_event_send(&spi_wifi_data_event, RW007_POWERSWITCH);
}
rt_hw_interrupt_enable(level);
return -RT_EBUSY;
}
static void wifi_data_process_thread_entry(void *parameter)
{
const struct spi_data_packet *data_packet = RT_NULL;
struct rw007_spi *dev = (struct rw007_spi )parameter;
while(1)
{
/
get the mempool memory for recv data package */
if(rt_mb_recv(&dev->spi_rx_mb, (rt_ubase_t )&data_packet, RT_WAITING_FOREVER) == RT_EOK)
{
if (data_packet->data_type == DATA_TYPE_STA_ETH_DATA)
{
#ifdef RW007_USING_SPI_TEST
pkg_count.wlan_rx_count++;
#endif
/
Ethernet package from station device */
rt_wlan_dev_report_data(wifi_sta.wlan, (void )data_packet->buffer, data_packet->data_len);
}
else if (data_packet->data_type == DATA_TYPE_AP_ETH_DATA)
{
#ifdef RW007_USING_SPI_TEST
pkg_count.wlan_rx_count++;
#endif
/
Ethernet package from ap device */
rt_wlan_dev_report_data(wifi_ap.wlan, (void )data_packet->buffer, data_packet->data_len);
}
else if (data_packet->data_type == DATA_TYPE_PROMISC_ETH_DATA)
{
/
air wifi package from promisc */
rt_wlan_dev_promisc_handler(wifi_sta.wlan, (void )data_packet->buffer, data_packet->data_len);
}
/
event callback */
else if(data_packet->data_type == DATA_TYPE_CB)
{
struct rw007_resp * resp = (struct rw007_resp )data_packet->buffer;
if(resp->cmd == RT_WLAN_DEV_EVT_SCAN_REPORT)
{
/
parse scan report event data */
struct rt_wlan_buff buff;
struct rt_wlan_info * wlan_info;
wlan_info = (struct rt_wlan_info )&resp->value;
buff.data = wlan_info;
buff.len = sizeof(struct rt_wlan_info);
/
indicate scan report event /
rt_wlan_dev_indicate_event_handle(wifi_sta.wlan, RT_WLAN_DEV_EVT_SCAN_REPORT, &buff);
}
else
{
if(resp->cmd == RT_WLAN_DEV_EVT_AP_START || resp->cmd == RT_WLAN_DEV_EVT_AP_STOP ||
resp->cmd == RT_WLAN_DEV_EVT_AP_ASSOCIATED || resp->cmd == RT_WLAN_DEV_EVT_AP_DISASSOCIATED)
{
/
indicate ap device event /
rt_wlan_dev_indicate_event_handle(wifi_ap.wlan, (rt_wlan_dev_event_t)resp->cmd, RT_NULL);
}
else
{
/
indicate sta device event /
rt_wlan_dev_indicate_event_handle(wifi_sta.wlan, (rt_wlan_dev_event_t)resp->cmd, RT_NULL);
}
}
}
else if (data_packet->data_type == DATA_TYPE_RESP)
{
/
parse cmd's response */
struct rw007_resp * resp = (struct rw007_resp )data_packet->buffer;
if(resp->cmd < RW00x_CMD_MAX_NUM)
{
if(dev->resp[resp->cmd])
{
rt_free(dev->resp[resp->cmd]);
}
/
stash response result /
dev->resp[resp->cmd] = rt_malloc(MAX_SPI_PACKET_SIZE);
if(dev->resp[resp->cmd])
{
rt_memcpy(dev->resp[resp->cmd], resp, MAX_SPI_PACKET_SIZE);
/
notify response arrived */
rt_event_send(dev->rw007_cmd_event, RW00x_CMD_RESP_EVENT(resp->cmd));
}
}
}
else if (data_packet->data_type == DATA_TYPE_BLE)
{
rw007_ble_recv_data((void )data_packet->buffer, data_packet->data_len);
}
/
free recv mempool memory */
rt_mp_free((void *)data_packet);
}
}
}
static void spi_wifi_data_thread_entry(void parameter)
{
rt_bool_t empty_read = RT_TRUE;
rt_uint32_t event;
rt_ubase_t send_packet = 0;
int state;
while (1)
{
/
receive first event */
if (rt_event_recv(&spi_wifi_data_event,
RW007_MASTER_DATA|
RW007_SLAVE_INT|
RW007_POWERSWITCH,
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER,
&event) != RT_EOK)
{
continue;
}
if (event & RW007_POWERSWITCH)
{
rw007_powerswitch(_powerswitch);
}
if (_powerswitch != rw007_power_switch_on)
{
_wifi_buffer_free();
if (send_packet)
{
rt_mp_free((void *)send_packet);
send_packet = 0;
}
continue;
}
if (!send_packet)
{
if (rt_mb_recv(&rw007_spi.spi_tx_mb, (rt_ubase_t )&send_packet, RT_WAITING_NO) != RT_EOK)
{
send_packet = 0;
}
}
/
transfer */
state = spi_wifi_transfer(&rw007_spi, (const void *)send_packet);
if (state != TRANSFER_DATA_ERROR)
{
if (send_packet)
{
rt_mp_free((void )send_packet);
send_packet = 0;
}
}
/
Try reading again */
if (state == TRANSFER_DATA_CONTINUE)
{
rt_event_send(&spi_wifi_data_event, RW007_SLAVE_INT);
empty_read = RT_TRUE;
}
else
{
if ((state == TRANSFER_DATA_SUCCESS) && (empty_read == RT_TRUE))
{
rt_event_send(&spi_wifi_data_event, RW007_SLAVE_INT);
empty_read = RT_FALSE;
}
else
{
empty_read = RT_TRUE;
}
}
}
}
rt_inline struct rw007_wifi *wifi_get_dev_by_wlan(struct rt_wlan_device *wlan)
{
if (wlan == wifi_sta.wlan)
{
return &wifi_sta;
}
if (wlan == wifi_ap.wlan)
{
return &wifi_ap;
}
return RT_NULL;
}
rt_inline void spi_send_cmd(struct rw007_spi * hspi, RW00x_CMD COMMAND, void * buffer, rt_uint32_t len)
{
struct spi_data_packet * data_packet;
struct rw007_cmd * cmd;
data_packet = rt_mp_alloc(&hspi->spi_tx_mp, RT_WAITING_FOREVER);
data_packet->data_type = DATA_TYPE_CMD;
cmd = (struct rw007_cmd *)data_packet->buffer;
cmd->cmd = COMMAND;
cmd->len = len;
if(cmd->len)
{
rt_memcpy(&cmd->value, buffer, cmd->len);
}
data_packet->data_len = member_offset(struct rw007_cmd, value) + cmd->len;
rt_mb_send(&hspi->spi_tx_mb, (rt_ubase_t)data_packet);
rt_event_send(&spi_wifi_data_event, RW007_MASTER_DATA);
}
rt_inline rt_err_t spi_set_data(struct rt_wlan_device *wlan, RW00x_CMD COMMAND, void * buffer, rt_uint32_t len)
{
struct rw007_spi * hspi = wifi_get_dev_by_wlan(wlan)->hspi;
rt_uint32_t result_event;
rt_err_t result = RT_EOK;
spi_send_cmd(hspi, COMMAND, buffer, len);
if(rt_event_recv(hspi->rw007_cmd_event,
RW00x_CMD_RESP_EVENT(COMMAND),
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
rt_tick_from_millisecond(10000),
&result_event) != RT_EOK)
{
return -RT_ETIMEOUT;
}
if(hspi->resp[COMMAND])
{
result = hspi->resp[COMMAND]->result;
rt_free(hspi->resp[COMMAND]);
hspi->resp[COMMAND] = RT_NULL;
return result;
}
return RT_EOK;
}
rt_inline rt_err_t spi_get_data(struct rt_wlan_device *wlan, RW00x_CMD COMMAND, void * buffer, rt_uint32_t *len)
{
struct rw007_spi * hspi = wifi_get_dev_by_wlan(wlan)->hspi;
rt_uint32_t result_event;
rt_err_t result = RT_EOK;
spi_send_cmd(hspi, COMMAND, RT_NULL, 0);
if(rt_event_recv(hspi->rw007_cmd_event,
RW00x_CMD_RESP_EVENT(COMMAND),
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
rt_tick_from_millisecond(10000),
&result_event) != RT_EOK)
{
return -RT_ETIMEOUT;
}
if(hspi->resp[COMMAND])
{
*len = hspi->resp[COMMAND]->len;
rt_memcpy(buffer, &hspi->resp[COMMAND]->value, hspi->resp[COMMAND]->len);
result = hspi->resp[COMMAND]->result;
rt_free(hspi->resp[COMMAND]);
hspi->resp[COMMAND] = RT_NULL;
return result;
}
return RT_EOK;
}
rt_err_t rw007_sn_get(char sn[24])
{
rt_uint32_t size_of_data;
return spi_get_data(wifi_sta.wlan, RW00x_CMD_GET_SN, sn, &size_of_data);
}
rt_err_t rw007_version_get(char version[16])
{
rt_uint32_t size_of_data;
return spi_get_data(wifi_sta.wlan, RW00x_CMD_GET_VSR, version, &size_of_data);
}
rt_err_t rw007_cfg_ota(enum rw007_ota_enable_mode enable, enum rw007_ota_upgrade_mode upgrade_mode)
{
struct rw007_ota_cfg ota_cfg;
ota_cfg.ota_enable = enable != rw007_ota_disable ? rw007_ota_enable : rw007_ota_disable;
ota_cfg.upgrade_mode = upgrade_mode != rw007_ota_upgrade_immediate
? rw007_ota_upgrade_manual : rw007_ota_upgrade_immediate;
// TODO: don't modify the following
ota_cfg.server_mode = rw007_ota_server_default;
ota_cfg.server_port = 5683;
ota_cfg.reserve = 0;
ota_cfg.server_url_len = 0;
return spi_set_data(wifi_sta.wlan, RW00x_CMD_CFG_OTA, &ota_cfg, sizeof(ota_cfg));
}
rt_err_t rw007_ble_cfgwifi(uint32_t duration_ms)
{
struct rw007_ble_cfgwifi cfgwifi;
cfgwifi.duration_ms = duration_ms;
return spi_set_data(wifi_sta.wlan, RW00x_CMD_BLE_CFGWIFI, &cfgwifi, sizeof(struct rw007_ble_cfgwifi));
}
static rt_err_t wlan_init(struct rt_wlan_device *wlan)
{
if(inited == RT_FALSE)
{
inited = RT_TRUE;
return spi_set_data(wlan, RW00x_CMD_INIT, RT_NULL, 0);
}
return RT_EOK;
}
static rt_err_t wlan_mode(struct rt_wlan_device *wlan, rt_wlan_mode_t mode)
{
return spi_set_data(wlan, RW00x_CMD_SET_MODE, &mode, sizeof(mode));
}
static rt_err_t wlan_scan(struct rt_wlan_device *wlan, struct rt_scan_info *scan_info)
{
return spi_set_data(wlan, RW00x_CMD_SCAN, RT_NULL, 0);
}
static rt_err_t wlan_join(struct rt_wlan_device *wlan, struct rt_sta_info *sta_info)
{
struct rw007_ap_info_value value;
value.info.security = sta_info->security;
value.info.band = RT_802_11_BAND_2_4GHZ;
value.info.datarate = 0;
value.info.channel = sta_info->channel;
value.info.hidden = 0;
value.info.rssi = 0;
value.info.ssid = sta_info->ssid;
rt_memcpy(value.info.bssid, sta_info->bssid, 6);
strncpy(value.passwd, (const char *)&sta_info->key.val[0], sta_info->key.len);
value.passwd[sta_info->key.len] = '?';
return spi_set_data(wlan, RW00x_CMD_JOIN, &value, sizeof(value));
}
static rt_err_t wlan_softap(struct rt_wlan_device *wlan, struct rt_ap_info *ap_info)
{
struct rw007_ap_info_value value;
value.info.security = ap_info->security;
value.info.band = RT_802_11_BAND_2_4GHZ;
value.info.datarate = 0;
value.info.channel = ap_info->channel;
value.info.hidden = ap_info->hidden;
value.info.rssi = 0;
value.info.ssid = ap_info->ssid;
strncpy(value.passwd, (const char *)&ap_info->key.val[0], ap_info->key.len);
value.passwd[ap_info->key.len] = '?';
return spi_set_data(wlan, RW00x_CMD_SOFTAP, &value, sizeof(value));
}
static rt_err_t wlan_disconnect(struct rt_wlan_device *wlan)
{
return spi_set_data(wlan, RW00x_CMD_DISCONNECT, RT_NULL, 0);
}
static rt_err_t wlan_ap_stop(struct rt_wlan_device *wlan)
{
return spi_set_data(wlan, RW00x_CMD_AP_STOP, RT_NULL, 0);
}
static rt_err_t wlan_ap_deauth(struct rt_wlan_device *wlan, rt_uint8_t mac[])
{
return spi_set_data(wlan, RW00x_CMD_AP_DEAUTH, mac, 6);
}
static rt_err_t wlan_scan_stop(struct rt_wlan_device *wlan)
{
return spi_set_data(wlan, RW00x_CMD_SCAN_STOP, RT_NULL, 0);
}
static int wlan_get_rssi(struct rt_wlan_device *wlan)
{
int rssi = -1;
rt_uint32_t size_of_data;
spi_get_data(wlan, RW00x_CMD_GET_RSSI, &rssi, &size_of_data);
return rssi;
}
static rt_err_t wlan_set_powersave(struct rt_wlan_device *wlan, int level)
{
return spi_set_data(wlan, RW00x_CMD_SET_PWR_SAVE, &level, sizeof(level));
}
static int wlan_get_powersave(struct rt_wlan_device *wlan)
{
int level = -1;
rt_uint32_t size_of_data;
spi_get_data(wlan, RW00x_CMD_GET_PWR_SAVE, &level, &size_of_data);
return level;
}
static rt_err_t wlan_cfg_promisc(struct rt_wlan_device *wlan, rt_bool_t start)
{
return spi_set_data(wlan, RW00x_CMD_CFG_PROMISC, &start, sizeof(start));
}
static rt_err_t wlan_cfg_filter(struct rt_wlan_device *wlan, struct rt_wlan_filter *filter)
{
return -RT_ENOSYS;
}
static rt_err_t wlan_set_channel(struct rt_wlan_device *wlan, int channel)
{
return spi_set_data(wlan, RW00x_CMD_SET_CHANNEL, &channel, sizeof(channel));
}
static int wlan_get_channel(struct rt_wlan_device *wlan)
{
int channel = -1;
rt_uint32_t size_of_data;
spi_get_data(wlan, RW00x_CMD_GET_CHANNEL, &channel, &size_of_data);
return channel;
}
static rt_err_t wlan_set_country(struct rt_wlan_device *wlan, rt_country_code_t country_code)
{
return spi_set_data(wlan, RW00x_CMD_SET_COUNTRY, &country_code, sizeof(country_code));
}
static rt_country_code_t wlan_get_country(struct rt_wlan_device *wlan)
{
rt_country_code_t code;
rt_uint32_t size_of_data;
spi_get_data(wlan, RW00x_CMD_GET_COUNTRY, &code, &size_of_data);
return code;
}
static rt_err_t wlan_set_mac(struct rt_wlan_device *wlan, rt_uint8_t mac[])
{
if(wlan == wifi_sta.wlan)
{
return spi_set_data(wlan, RW00x_CMD_MAC_SET, mac, 6);
}
return spi_set_data(wlan, RW00x_CMD_AP_MAC_SET, mac, 6);
}
static rt_err_t wlan_get_mac(struct rt_wlan_device *wlan, rt_uint8_t mac[])
{
rt_uint32_t size_of_data;
if(wlan == wifi_sta.wlan)
{
return spi_get_data(wlan, RW00x_CMD_MAC_GET, mac, &size_of_data);
}
return spi_get_data(wlan, RW00x_CMD_AP_MAC_GET, mac, &size_of_data);
}
static int wlan_send(struct rt_wlan_device *wlan, void *buff, int len)
{
struct rw007_spi * hspi = wifi_get_dev_by_wlan(wlan)->hspi;
struct spi_data_packet * data_packet;
if(wlan == RT_NULL)
{
return -1;
}
data_packet = rt_mp_alloc(&hspi->spi_tx_mp, RT_WAITING_FOREVER);
if (wlan == wifi_sta.wlan)
{
data_packet->data_type = DATA_TYPE_STA_ETH_DATA;
}
else
{
data_packet->data_type = DATA_TYPE_AP_ETH_DATA;
}
data_packet->data_len = len;
rt_memcpy(data_packet->buffer, buff, len);
rt_mb_send(&hspi->spi_tx_mb, (rt_ubase_t)data_packet);
rt_event_send(&spi_wifi_data_event, RW007_MASTER_DATA);
return len;
}
const static struct rt_wlan_dev_ops ops =
{
.wlan_init = wlan_init,
.wlan_mode = wlan_mode,
.wlan_scan = wlan_scan,
.wlan_join = wlan_join,
.wlan_softap = wlan_softap,
.wlan_disconnect = wlan_disconnect,
.wlan_ap_stop = wlan_ap_stop,
.wlan_ap_deauth = wlan_ap_deauth,
.wlan_scan_stop = wlan_scan_stop,
.wlan_get_rssi = wlan_get_rssi,
.wlan_set_powersave = wlan_set_powersave,
.wlan_get_powersave = wlan_get_powersave,
.wlan_cfg_promisc = wlan_cfg_promisc,
.wlan_cfg_filter = wlan_cfg_filter,
.wlan_set_channel = wlan_set_channel,
.wlan_get_channel = wlan_get_channel,
.wlan_set_country = wlan_set_country,
.wlan_get_country = wlan_get_country,
.wlan_set_mac = wlan_set_mac,
.wlan_get_mac = wlan_get_mac,
.wlan_recv = RT_NULL,
.wlan_send = wlan_send,
};
rt_err_t rt_hw_wifi_init(const char spi_device_name)
{
static struct rt_wlan_device wlan_sta, wlan_ap;
rt_err_t ret;
wifi_sta.wlan = &wlan_sta;
wifi_sta.hspi = &rw007_spi;
wifi_ap.wlan = &wlan_ap;
wifi_ap.hspi = &rw007_spi;
/
align and struct size check. */
RT_ASSERT((SPI_MAX_DATA_LEN & 0x03) == 0);
memset(&rw007_spi, 0, sizeof(struct rw007_spi));
rw007_spi.spi_device = (struct rt_spi_device )rt_device_find(spi_device_name);
if (rw007_spi.spi_device == RT_NULL)
{
LOG_E("spi device %s not found!r", spi_device_name);
return -RT_ENOSYS;
}
/
config spi /
{
struct rt_spi_configuration cfg;
cfg.data_width = 8;
cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /
SPI Compatible: Mode 0. /
cfg.max_hz = RW007_SPI_MAX_HZ; /
15M 007 max 30M /
rt_spi_configure(rw007_spi.spi_device, &cfg);
}
/
init spi send mempool /
rt_mp_init(&rw007_spi.spi_tx_mp,
"spi_tx",
&rw007_spi.spi_tx_mempool[0],
sizeof(rw007_spi.spi_tx_mempool),
sizeof(struct spi_data_packet));
/
init spi send mailbox /
rt_mb_init(&rw007_spi.spi_tx_mb,
"spi_tx",
&rw007_spi.spi_tx_mb_pool[0],
SPI_TX_POOL_SIZE,
RT_IPC_FLAG_PRIO);
/
init spi recv mempool /
rt_mp_init(&rw007_spi.spi_rx_mp,
"spi_rx",
&rw007_spi.spi_rx_mempool[0],
sizeof(rw007_spi.spi_rx_mempool),
sizeof(struct spi_data_packet));
/
init spi recv mailbox /
rt_mb_init(&rw007_spi.spi_rx_mb,
"spi_rx",
&rw007_spi.spi_rx_mb_pool[0],
SPI_RX_POOL_SIZE,
RT_IPC_FLAG_PRIO);
/
init spi data notify event /
rt_event_init(&spi_wifi_data_event, "wifi", RT_IPC_FLAG_FIFO);
rw007_spi.rw007_cmd_event = rt_event_create("wifi_cmd", RT_IPC_FLAG_FIFO);
/
register wlan device for ap /
ret = rt_wlan_dev_register(&wlan_ap, RT_WLAN_DEVICE_AP_NAME, &ops, 0, &wifi_ap);
if (ret != RT_EOK)
{
return ret;
}
/
register wlan device for sta /
ret = rt_wlan_dev_register(&wlan_sta, RT_WLAN_DEVICE_STA_NAME, &ops, 0, &wifi_sta);
if (ret != RT_EOK)
{
return ret;
}
{
rt_thread_t tid;
/
Create package parse thread /
tid = rt_thread_create("wifi_handle",
wifi_data_process_thread_entry,
&rw007_spi,
2048,
8,
20);
if(!tid)
{
return -RT_ERROR;
}
rt_thread_startup(tid);
/
Create wifi transfer thread /
tid = rt_thread_create("wifi_xfer",
spi_wifi_data_thread_entry,
RT_NULL,
2048,
9,
20);
if(!tid)
{
return -RT_ERROR;
}
rt_thread_startup(tid);
}
spi_wifi_hw_init();
return RT_EOK;
}
void spi_wifi_isr(int vector)
{
/
enter interrupt /
rt_interrupt_enter();
/
device has a package to ready transfer /
rt_event_send(&spi_wifi_data_event, RW007_SLAVE_INT);
/
leave interrupt */
rt_interrupt_leave();
}
void rw007_wifi_state_reset(void)
{
if (inited == RT_TRUE)
{
// inited
RT_FALSE;
rt_wlan_dev_indicate_event_handle(wifi_sta.wlan, RT_WLAN_DEV_EVT_DISCONNECT, RT_NULL);
wlan_init(wif寫文章的平臺電腦怎么寫文章(電子稿)i_sta.wlan);
wlan_mode(wifi_sta.wlan, RT_WLAN_STATION);
wlan_init(wifi_ap.wlan);
wlan_mode(wifi_ap.wlan, RT_WLAN_AP);
}
}

三、實驗測試
系統(tǒng)搭建完成后,進行了室內(nèi)環(huán)境監(jiān)測實驗。測試環(huán)境為某高校的實驗室,面積約為10m×10m。在實驗過程中,選取了學生經(jīng)常活動的區(qū)域作為測試區(qū)域。測試結果表明,系統(tǒng)對室內(nèi)溫濕度有很好的監(jiān)測效果,能夠準確反映室內(nèi)環(huán)境狀況。

四、總結
本文設計了一種基于psoc6的智能室內(nèi)環(huán)境監(jiān)測系統(tǒng),系統(tǒng)能夠根據(jù)預設閾值自動調(diào)節(jié)室內(nèi)環(huán)境參數(shù),保證室內(nèi)環(huán)境達到一個相對健康的水平。系統(tǒng)將物聯(lián)網(wǎng)技術應用于室內(nèi)環(huán)境監(jiān)測中,實現(xiàn)了對室內(nèi)溫濕度參數(shù)的實時檢測和控制,同時系統(tǒng)還具有設備可擴展性和可維護性

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 濾波器
    +關注

    關注

    161

    文章

    7816

    瀏覽量

    178111
  • SPI接口
    +關注

    關注

    0

    文章

    258

    瀏覽量

    34382
  • 溫濕度傳感器

    關注

    5

    文章

    579

    瀏覽量

    35721
  • wifi模塊
    +關注

    關注

    60

    文章

    380

    瀏覽量

    73495
  • OneNET
    +關注

    關注

    1

    文章

    51

    瀏覽量

    12803
收藏 人收藏

    評論

    相關推薦

    基于瑞薩RA6M5 MCU的室內(nèi)環(huán)境監(jiān)測系統(tǒng)設計

    空氣污染越來越被大家重視,人們80%的時間都是在室內(nèi)度過的,室內(nèi)的空氣環(huán)境質(zhì)量對人們的身體健康更是息息相關。本項目旨在開發(fā)一款室內(nèi)環(huán)境監(jiān)測
    的頭像 發(fā)表于 01-15 12:18 ?1667次閱讀
    基于瑞薩RA6M5 MCU的<b class='flag-5'>室內(nèi)環(huán)境監(jiān)測</b><b class='flag-5'>系統(tǒng)</b>設計

    【CANNON申請】室內(nèi)環(huán)境監(jiān)測

    和市場。項目描述:初步實現(xiàn)室內(nèi)基本環(huán)境參數(shù)如溫濕度的監(jiān)測連接顯示,后續(xù)逐步增加多種室內(nèi)參數(shù)監(jiān)測,并完善APP及網(wǎng)絡軟件,從而形成一個比較完善
    發(fā)表于 01-26 13:37

    【CANNON申請】家庭室內(nèi)環(huán)境監(jiān)測系統(tǒng)

    申請理由:想做一個家庭室內(nèi)環(huán)境監(jiān)測系統(tǒng),主要是測量室內(nèi)溫度、濕度、粉塵濃度、光照、噪聲等環(huán)境參數(shù),同時對室內(nèi)換氣設備/空氣凈化設備、燈光等進
    發(fā)表于 01-26 14:30

    求一個畢業(yè)設計:基于單片機的室內(nèi)環(huán)境監(jiān)測預警系統(tǒng)設計。

    基于單片機的室內(nèi)環(huán)境監(jiān)測預警系統(tǒng)設計。電路圖要用protues仿真
    發(fā)表于 04-08 15:42

    求助:基于單片機的室內(nèi)環(huán)境監(jiān)測預警系統(tǒng)的設計,protues仿真圖

    求助:基于單片機的室內(nèi)環(huán)境監(jiān)測預警系統(tǒng)的設計,protues仿真圖
    發(fā)表于 04-15 00:16

    【Intel Edison申請】基于intel edison的室內(nèi)環(huán)境監(jiān)測系統(tǒng)

    室內(nèi)環(huán)境監(jiān)測系統(tǒng),測試包括溫度,濕度、氣壓在內(nèi)的值,上位機計劃利用PC,edison與PC之間利用藍牙或者串口通信,開發(fā)具體步驟如下1、開箱,進行硬件解析,評估硬件性能2、搭建開發(fā)
    發(fā)表于 06-27 17:23

    關于室內(nèi)環(huán)境監(jiān)測系統(tǒng):co、火警的綜合檢測和報警

    本人幾乎是新手,因為工作需要,需要做這個關于室內(nèi)環(huán)境監(jiān)測系統(tǒng):co、火警的綜合檢測和報警。路過的大神們,給點指引的方向.............謝謝了,各位的援助之手?。。。。。?/div>
    發(fā)表于 01-03 17:55

    如何用樹莓派玩一把室內(nèi)環(huán)境監(jiān)測?

    如何用樹莓派玩一把室內(nèi)環(huán)境監(jiān)測?
    發(fā)表于 05-08 07:38

    基于單片機的室內(nèi)環(huán)境監(jiān)測系統(tǒng)設計

    基于單片機的室內(nèi)環(huán)境監(jiān)測系統(tǒng)設計,摘要:018【畢設課設】基于單片機居家室內(nèi)環(huán)境監(jiān)測系統(tǒng)仿真設。說明:濕度實時采集并顯示,溫度,CO和煙霧濃度需要按選擇鍵進行切換顯示數(shù)值。設置按鍵選擇
    發(fā)表于 07-15 07:19

    基于STM32單片機的室內(nèi)環(huán)境監(jiān)測系統(tǒng)的設計資料分享

    設計簡介:本設計是基于單片機的室內(nèi)環(huán)境監(jiān)測系統(tǒng),主要實現(xiàn)以下功能:可實現(xiàn)LCD1602顯示溫濕度、二氧化碳、甲醛值、煙霧值、甲烷值、一氧化碳值通過按鍵切換顯示內(nèi)容環(huán)境數(shù)據(jù)到達設置閾值報警(閾值無法
    發(fā)表于 02-21 07:14

    基于單片機的室內(nèi)環(huán)境監(jiān)測系統(tǒng)設計_王君巖

    基于單片機的室內(nèi)環(huán)境監(jiān)測系統(tǒng)設計,值得下載
    發(fā)表于 12-07 11:09 ?53次下載

    多傳感器室內(nèi)環(huán)境監(jiān)測系統(tǒng)_孫占鵬

    多傳感器室內(nèi)環(huán)境監(jiān)測系統(tǒng)_孫占鵬
    發(fā)表于 03-19 11:41 ?11次下載

    關于多功能室內(nèi)環(huán)境監(jiān)測系統(tǒng)的設計介紹

    2011 ATMEL AVR校園設計大賽--多功能室內(nèi)環(huán)境監(jiān)測系統(tǒng)
    的頭像 發(fā)表于 07-10 03:06 ?4910次閱讀

    室內(nèi)環(huán)境監(jiān)測仿真圖

    室內(nèi)環(huán)境監(jiān)測仿真圖
    發(fā)表于 03-14 15:17 ?26次下載

    溫濕度傳感器在室內(nèi)環(huán)境監(jiān)測中的應用

    為電信號,并通過無線或有線方式傳輸給監(jiān)測系統(tǒng),以供用戶實時了解室內(nèi)環(huán)境狀況。以下將詳細介紹溫濕度傳感器在室內(nèi)環(huán)境監(jiān)測中的應用。
    的頭像 發(fā)表于 06-18 11:14 ?1298次閱讀