概述
RT-Thread Studio学习(八)利用ESP8266连接阿里云IOT
- 简介
- 新建RT-Thread项目并使用外部时钟
- 使能串口6
- 接入ESP8266
- 添加AT组件
- 添加阿里云IOT软件包
- 测试
- 上报/接收数据
简介
本文将基于STM32F407VET芯片介绍如何在RT-Thread Studio开发环境下使用阿里云IOT软件包。
新建RT-Thread项目并使用外部时钟
详细步骤参考文档《RT-Thread Studio学习之使用外部时钟系统》。
使能串口6
详细步骤参考文档《RT-Thread Studio学习之使用多串口》。
接入ESP8266
本文测试使用的是ESP8266-01S,与MCU的接线如下图所示:
图中的MCU_RXD对应UART6_RXD,MCU_TXD对应UART6_TXD。因为ESP8266-01S模块的CH_PD和RST引脚有上拉,所以可以悬空。
添加AT组件
硬件连接好之后需要在RT-Thread Studio设置中添加AT组件,在Studio的软件包中心搜索at_device
,点击添加。
进入软件包的详细配置页面,进行如下配置:
配置好WIFI SSID和密码后,保存项目并编译下载。上电后在控制台输入ifconfig,会有如下显示:
添加阿里云IOT软件包
在软件包中心搜索ali-iotkit
并添加,
右击组件选中详细配置,输入阿里云IOT平台设备的三元组信息,开启示例代码,其他参数默认:
测试
保存项目,编译下载。上电后在控制台输入ali_mqtt_sample
,就可以连接到阿里云IOT平台进行MQTT通信了。
在阿里云IOT平台,进入相关设备的日志服务
,可以查看到相关消息内容:
在这里,stm32不断地发送hello信息,阿里云平台设备同步接收。
上报/接收数据
新建mqtt_sample.c文件,内容如下:
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*
* Again edit by rt-thread group
* Change Logs:
* Date Author Notes
* 2019-07-21 MurphyZhao first edit
*/
#include "rtthread.h"
#include "dev_sign_api.h"
#include "mqtt_api.h"
char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char DEMO_DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0};
char DEMO_DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0};
uint8_t temp_f407=0; //采集到的温度数值,-50~100
uint8_t humi_f407=0; //采集到的湿度数值,0~100
void *HAL_Malloc(uint32_t size);
void HAL_Free(void *ptr);
void HAL_Printf(const char *fmt, ...);
int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1]);
int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1]);
int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN]);
uint64_t HAL_UptimeMs(void);
int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
#define EXAMPLE_TRACE(fmt, ...)
do {
HAL_Printf("%s|%03d :: ", __func__, __LINE__);
HAL_Printf(fmt, ##__VA_ARGS__);
HAL_Printf("%s", "rn");
} while(0)
static void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
iotx_mqtt_topic_info_t *topic_info = (iotx_mqtt_topic_info_pt) msg->msg;
switch (msg->event_type) {
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
/* print topic name and topic message */
EXAMPLE_TRACE("Message Arrived:");
EXAMPLE_TRACE("Topic : %.*s", topic_info->topic_len, topic_info->ptopic);
EXAMPLE_TRACE("Payload: %.*s", topic_info->payload_len, topic_info->payload);
EXAMPLE_TRACE("n");
break;
default:
break;
}
}
static int example_subscribe(void *handle)
{
int res = 0;
//const char *fmt = "/%s/%s/user/get";
const char *fmt = "/sys/%s/%s/thing/event/property/post";
char *topic = NULL;
int topic_len = 0;
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL) {
EXAMPLE_TRACE("memory not enough");
return -1;
}
memset(topic, 0, topic_len);
HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);
res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL);
if (res < 0) {
EXAMPLE_TRACE("subscribe failed");
HAL_Free(topic);
return -1;
}
HAL_Free(topic);
return 0;
}
static int example_mypublish(void *handle) //向Topic主题发布属性函数
{
int res = 0;
const char *fmt = "/sys/%s/%s/thing/event/property/post";
const char *fmt_payload = "{"params" : { "temperature":%d,"humidity":%d } }";
char *topic = NULL;
int topic_len = 0;
int payload_len = 0;
char *payload = NULL;
payload_len = strlen(fmt_payload)+4;
payload = HAL_Malloc(payload_len);
if (payload == NULL) {
EXAMPLE_TRACE("memory not enough");
return -1;
}
memset(payload, 0, payload_len);
HAL_Snprintf(payload,payload_len,fmt_payload,temp_f407,humi_f407);
temp_f407 = (temp_f407 + 10)%100;
humi_f407 = (humi_f407 + 7)%100;
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL) {
EXAMPLE_TRACE("memory not enough");
return -1;
}
memset(topic, 0, topic_len);
HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);
res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, payload, strlen(payload));
if (res < 0) {
EXAMPLE_TRACE("publish failed, res = %d", res);
HAL_Free(topic);
HAL_Free(payload);
return -1;
}
HAL_Free(topic);
HAL_Free(payload);
return 0;
}
static void example_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
EXAMPLE_TRACE("msg->event_type : %d", msg->event_type);
}
static int mqtt_example_mymain(int argc, char *argv[])
{
void *pclient = NULL;
int res = 0;
iotx_mqtt_param_t mqtt_params;
HAL_GetProductKey(DEMO_PRODUCT_KEY);
HAL_GetDeviceName(DEMO_DEVICE_NAME);
HAL_GetDeviceSecret(DEMO_DEVICE_SECRET);
EXAMPLE_TRACE("mqtt example");
memset(&mqtt_params, 0x0, sizeof(mqtt_params));
mqtt_params.handle_event.h_fp = example_event_handle;
pclient = IOT_MQTT_Construct(&mqtt_params);
if (NULL == pclient) {
EXAMPLE_TRACE("MQTT construct failed");
return -1;
}
res = example_subscribe(pclient);
if (res < 0) {
IOT_MQTT_Destroy(&pclient);
return -1;
}
while (1)
{
example_mypublish(pclient);
rt_thread_delay(60000);
IOT_MQTT_Yield(pclient, 200);
}
return 0;
}
static rt_thread_t tid1 = RT_NULL;
int mqtt_thread(void) //创建mqtt线程
{
tid1 = rt_thread_create("mqtt_thd", mqtt_example_mymain, RT_NULL, 4096, 14, 10);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
return 0;
}
#ifdef FINSH_USING_MSH
MSH_CMD_EXPORT(mqtt_thread, ali mqtt sample);
#endif
最后
以上就是殷勤哑铃为你收集整理的RT-Thread Studio学习(八)利用ESP8266连接阿里云IOT简介新建RT-Thread项目并使用外部时钟使能串口6接入ESP8266添加AT组件添加阿里云IOT软件包测试上报/接收数据的全部内容,希望文章能够帮你解决RT-Thread Studio学习(八)利用ESP8266连接阿里云IOT简介新建RT-Thread项目并使用外部时钟使能串口6接入ESP8266添加AT组件添加阿里云IOT软件包测试上报/接收数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复