wtyok 发表于 2020-6-14 21:31

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

报名的时候没有申请板子,自己手里还有好多板子不会用在吃灰{:smile:}。
今年寒假回来开始学的单片机,所以用的自己的stm32f767igt6开发版。
rt-thread的教程还没看明白,所以先硬着头皮上了。
做的是一个sht30采集温湿度数据,上传到自己的emq服务器,然后在nodered平台进行显示,用的报名申请的ucloud服务器。
https://bbs.21ic.com/forum.php?mod=image&aid=1468206&size=300x300&key=d69303d050059984&nocache=yes&type=fixnone
就是简单的连线,哈哈
现在来看程序:
创建的一个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_tsht3x_device;

    sht3x_device = sht3x_init("i2c1", 0x44);

    rt_thread_mdelay(100);

    char num;

    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服务器
https://bbs.21ic.com/forum.php?mod=image&aid=1468212&size=300x300&key=a00cde467eeccb8b&nocache=yes&type=fixnone
以下为nodered显示效果
https://bbs.21ic.com/forum.php?mod=image&aid=1468214&size=300x300&key=b7eff7090bcbc82f&nocache=yes&type=fixnone


zeshoufx 发表于 2020-6-16 08:42

谢谢分享,,,,,,,,,,,,,,
页: [1]
查看完整版本: 【RTOS】+ RT-Thread物联网环境监测平台