【学习笔记】-Linux系统连接阿里云,并上传温湿度

一、进入官网配置

1、选择SDK定制

2、配置SDK

更具自己的要求配置,配置好后点击生成。

 3、生成后就会得到一个压缩包,然后解压,得到LinkSDK文件。

4、将解压后的文件复制到Linux系统中,先在终端在LinkSDK目录下,输入make,打开demos/mqtt_basic_demo.c,修改相应信息

 

 5、如果需要订阅和发布,修改以下函数:

6、然后在make编译一下,完成后,运行

显示连接成功!

二、读取温湿度,并将温湿度通过蓝牙发送

stm32读取到温湿度,蓝牙读取数据,并发送给另一个蓝牙,另一个蓝牙连接到Linux系统。

在RT-Thread系统中,读取温湿度:

1、使用UART2:

2、添加DHT11软件包:

 3、主函数main.c,主要是把数据组包,通过AT指令发送到另一个蓝牙

/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-10-19     RT-Thread    first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include <board.h>
#include <stdio.h>
#include <string.h>
#include "dhtxx.h"
#define SAMPLE_UART_NAME       "uart2"    /* 串口设备名称 */
static rt_device_t serial;                /* 串口设备句柄 */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;  /* 初始化配置参数 */
char str[]={0xAA,0xFB,0xFF,0xFF,0x48,0x45,0x4C,0x4C,0x4F,0x0D};
char str1[]={0xAA,0xFB,0xFF,0xFF,0x0D};
char buffer[50];
rt_int32_t temp;
rt_int32_t humi;
int main(void)
{

//    dht_device_t sensor = dht_create(23);
//
//    if(dht_read(sensor)) {
//        temp = dht_get_temperature(sensor);
//        humi = dht_get_humidity(sensor);
//
//        rt_kprintf("Temp: %d, Humi: %d\n", temp/10, humi/10);
//    }
//    else {
//        rt_kprintf("Read dht sensor failed.\n");
//    }
//    rt_thread_mdelay(3000);
//    dht_delete(sensor);
    /* step1:查找串口设备 */
    serial = rt_device_find(SAMPLE_UART_NAME);

    /* step2:修改串口配置参数 */
    config.baud_rate = BAUD_RATE_115200;        //修改波特率为 115200
    config.data_bits = DATA_BITS_8;           //数据位 8
    config.stop_bits = STOP_BITS_1;           //停止位 1
    config.bufsz     = 128;                   //修改缓冲区 buff size 为 128
    config.parity    = PARITY_NONE;           //无奇偶校验位

    /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);

    /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
    rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);

    /* 发送字符串 */
    while(1)
    {
        dht_device_t sensor = dht_create(23);

        if(dht_read(sensor)) {
            temp = dht_get_temperature(sensor);
            humi = dht_get_humidity(sensor);

            //rt_kprintf("Temp: %d, Humi: %d\n", temp/10, humi/10);
        }
        else {
            rt_kprintf("Read dht sensor failed.\n");
        }
        //rt_thread_mdelay(3000);
        dht_delete(sensor);
        //snprintf(buffer, sizeof(buffer),"0x%02X",(unsigned int)temp/10);
        char buff[10];
        snprintf(buff,sizeof(buff),"%d%d",temp/10,humi/10);
        char buf[]={0xAA,0xFB,0xFF,0xFF,'\0'};
        char buf1[]={0x0D,'\0'};
        char result[sizeof(buf) + strlen(buff) + sizeof(buf1)+1];
        //strcpy(result, buf); // 复制 buf 到结果缓冲区
        //strcat(result, buff); // 连接 buff 到结果缓冲区
        //strcat(result, buf1); // 连接 buf1 到结果缓冲区
        snprintf(result, sizeof(result), "%s%s%s", buf, buff, buf1);

        rt_kprintf("%s",result);
        //rt_device_write(serial, 0, str, strlen(str));
        rt_device_write(serial, 0, result, sizeof(result));
        rt_thread_mdelay(2000);
    }



    return RT_EOK;
}

4.连线

这里是23,对应的口是PB7

 5.修改上云程序,增加蓝牙接受数据,解包,读取温湿度

蓝牙接受函数:

#include <stdio.h>       // 引入标准输入输出库
#include <stdlib.h>      // 引入标准库(如:malloc和free等函数)
#include <string.h>      // 引入字符串操作函数库
#include <unistd.h>      // 引入UNIX标准函数库
#include <fcntl.h>       // 文件控制库
#include <termios.h>     // 终端I/O函数库

int main() {

    int fd;                          // 文件描述符用于串行通信
    struct termios options;          // 用于存储和配置串行端口设置的结构
    unsigned char buffer[256];       // 用于读取串口数据的缓冲区
    int bytes_read;                  // 从串口读取的字节数

    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);  // 尝试打开串行端口ttyUSB0

    if (fd == -1) {                  // 如果打开失败
        perror("Unable to open port"); // 打印错误信息
        return 1;                    // 退出程序
    } else {
        fcntl(fd, F_SETFL, 0);       // 设置文件描述符属性
        printf("Port is open.\n");  // 端口打开成功,打印消息
    }

    tcgetattr(fd, &options);         // 获取当前设备的配置

    cfsetispeed(&options, B115200);  // 设置输入波特率为115200
    cfsetospeed(&options, B115200);  // 设置输出波特率为115200

    options.c_cflag &= ~CSIZE;       // 清除数据位
    options.c_cflag |= CS8;          // 设置数据位为8
    options.c_cflag &= ~PARENB;      // 无奇偶校验
    options.c_cflag &= ~CSTOPB;      // 停止位为1
    options.c_iflag &= ~(IXON | IXOFF | IXANY);  // 不使用软件流控制
    options.c_cflag &= ~CRTSCTS;     // 不使用硬件流控制
    options.c_cflag |= (CLOCAL | CREAD);  // 忽略调制解调器的状态线,使能接收

    tcsetattr(fd, TCSANOW, &options);  // 将修改后的配置应用到设备

    printf("Port is configured.\n");  // 打印端口配置完成的消息

    while (1) {  // 无限循环
        tcflush(fd, TCIFLUSH);          // 清空输入缓冲区
        bytes_read = read(fd, buffer, sizeof(buffer) - 1);  // 从串行端口读取数据

        if (bytes_read > 0) {         // 如果读取到了数据
            printf("Received: ");
            //定义变量存储数据部分的十六进制字符串
            char data_hex[50];
            int data_hex_len=0;
            for (int i = 0; i < bytes_read; i++) {
		
                printf("%02X ",buffer[i]);  // 以16进制打印数据
                
          
            }
	
            printf("\n");
            
            char code[5];
	    char code1[5];
	    sprintf(code, "%c%c", buffer[3], buffer[4]);
	    sprintf(code1, "%c%c", buffer[5], buffer[6]);
	    printf("转换后的ASCII码: %s\n", code);
	    printf("转换后的ASCII码: %s\n", code1);

        } else if (bytes_read < 0) {  
            perror("Error reading from serial port");  // 如果读取时发生错误,则打印错误信息
            break;                     // 跳出无限循环
        }
    }

    close(fd);                        // 关闭串行端口

    return 0;                         // 退出程序
}

6.将接受函数和上云函数结合,上云发布函数修改 

 /* MQTT 发布消息功能示例, 请根据自己的业务需求进行使用 */
    {
	char pub_payload[100];
	char pub_payload1[100];
        char *pub_topic = "/sys/xxxx/xxx/thing/event/property/post";
        sprintf(pub_payload , "{\"id\":\"1\",\"version\":\"1.0\",\"params\":{\"wendu\":%s}}",code);
	sprintf(pub_payload1 , "{\"id\":\"1\",\"version\":\"1.0\",\"params\":{\"shidu\":%s}}",code1);
        res = aiot_mqtt_pub(mqtt_handle, pub_topic, (uint8_t *)pub_payload, (uint32_t)strlen(pub_payload), 0);
	res1 = aiot_mqtt_pub(mqtt_handle, pub_topic, (uint8_t *)pub_payload1, (uint32_t)strlen(pub_payload1), 0);
        if (res < 0||res1<0) {
            printf("aiot_mqtt_sub failed, res: -0x%04X\n", -res);
            return -1;
        }
    }

7.修改好后,make编译一下,运行,可以看到温湿度变化

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值