
初级技术员

- 积分
- 28
- 在线时间
- 12 小时
- 精华
- 0
- 注册时间
- 2020-4-1
- 最后登录
- 2021-1-8
|
报名的时候没有申请板子,自己手里还有好多板子不会用在吃灰 。
今年寒假回来开始学的单片机,所以用的自己的stm32f767igt6开发版。
rt-thread的教程还没看明白,所以先硬着头皮上了。
做的是一个sht30采集温湿度数据,上传到自己的emq服务器,然后在nodered平台进行显示,用的报名申请的ucloud服务器。

就是简单的连线,哈哈
现在来看程序:
创建的一个SHT30获取温湿度数据的线程,使用rtthread studio 自带的sht3x软件包和模拟i2c。
- /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2020-06-14 wangy the first version
- */
- #include <rtthread.h>
- #include <board.h>
- #include <sht3x.h>
- #include <string.h>
- #define THREAD_PRIORITY 10
- #define THREAD_STACK_SIZE 1028
- #define THREAD_TIMESLICE 20
- static rt_thread_t tid1 = RT_NULL;
- /* 入口函数 */
- static void sht30_init_thread_entry(void *parameter)
- {
- sht3x_device_t sht3x_device;
- sht3x_device = sht3x_init("i2c1", 0x44);
- rt_thread_mdelay(100);
- char num[100];
- while (1)
- {
- if(RT_EOK == sht3x_read_singleshot(sht3x_device))
- {
- memset(num,0,100);
- sprintf(num,"{\"humidity\": %d,\"temperature\": %d}",(int)sht3x_device->humidity,(int)sht3x_device->temperature);
- rt_device_write(rt_device_find("uart2" ), 0, num, (strlen(num)));
- rt_kprintf("sht30 humidity : %d.%d ", (int)sht3x_device->humidity, (int)(sht3x_device->humidity * 10) % 10);
- rt_kprintf("temperature: %d.%d\n", (int)sht3x_device->temperature, (int)(sht3x_device->temperature * 10) % 10);
- }
- else
- {
- rt_kprintf("read sht3x fail.\r\n");
- break;
- }
- rt_thread_mdelay(2000);
- }
- }
- /* 创建线程 */
- int sht30_init(void)
- {
- /* 创建线程*/
- tid1 = rt_thread_create("sht_30_init",
- sht30_init_thread_entry, RT_NULL,
- THREAD_STACK_SIZE,
- THREAD_PRIORITY, THREAD_TIMESLICE);
- /* 如果获得线程控制块,启动这个线程 */
- if (tid1 != RT_NULL)
- rt_thread_startup(tid1);
- return 0;
- }
复制代码 主函数包括了串口2 和 sht30的初始化。
- /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2020-05-15 RT-Thread first version
- */
- #include <rtthread.h>
- #include <string.h>
- #define DBG_TAG "main"
- #define DBG_LVL DBG_LOG
- #include <rtdbg.h>
- #include <sht3x.h>
- extern int sht30_init(void);
- //static char str[] = "hello wty!\r\n"; /* 需要发送的数据 */
- static int uart2_init(void)
- {
- rt_device_open(rt_device_find("uart2"), RT_DEVICE_FLAG_INT_RX);
- return 0;
- }
- int main(void)
- {
- int count = 1;
- sht30_init();
- uart2_init();
- while (count++)
- {
- // LOG_D("Hello RT-Thread!");
- // rt_kprintf("hello");
- rt_thread_mdelay(1000);
- }
- return RT_EOK;
- }
- MSH_CMD_EXPORT(uart2_init, uart device sample);
复制代码 mqtt模块用的透传模块,设置好topic那些信息,开机进入透传所以没有程序了。
下面是自己搭建的emq服务器

以下为nodered显示效果

|
 共1人点赞
|