打印
[uCOS/RTOS]

【RTOS】+ RT-Thread物联网环境监测平台

[复制链接]
3400|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wtyok|  楼主 | 2020-6-14 21:31 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
报名的时候没有申请板子,自己手里还有好多板子不会用在吃灰
今年寒假回来开始学的单片机,所以用的自己的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显示效果



图片3.png (37.5 KB )

图片3.png

图片2.png (290.6 KB )

图片2.png

批注 2020-06-14 211940.png (693.58 KB )

批注 2020-06-14 211940.png

物联网环境监测平台.rar

260.44 KB

使用特权

评论回复

相关帖子

沙发
zeshoufx| | 2020-6-16 08:42 | 只看该作者
谢谢分享,,,,,,,,,,,,,,

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

6

帖子

0

粉丝