MQTT运行
- 阿里云创建设备
- MQTT源码下载及编译
- MQTT移植到ARM Linux平台
- MQTT点灯和关灯
阿里云创建设备
注意:我这里用的是连接协议是WIFI协议,如果用网线连接的话,创建的时候选择:“以太网”
MQTT源码下载及编译
- https://code.aliyun.com/edward.yangx/public-docs/wikis/user-guide/Linkkit_User_Manual
- 在该网页下有“快速体验”连接,点击后按照下述连接描述在Ubuntu上进行验证
- https://code.aliyun.com/edward.yangx/public-docs/wikis/user-guide/linkkit/Quick_Start
- 可能是因为我装的18.04的版本,虽然已经按照指示安装gawk了,但是还是提示相关的信息如下:
*在设备上创建Topic,和mqtt_example.c中的对应起来
MQTT移植到ARM Linux平台
- https://code.aliyun.com/edward.yangx/public-docs/wikis/user-guide/linkkit/Port_Guide/Build_Example
- make reconfig
- make 生成ARM在的mqtt_example可执行程序
MQTT点灯和关灯
创建一个LED设备后,以mqtt_example.c为模板,这里需要用到cjson
#include "cJSON.h"
...
void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
int fd;
const char *leds = "/sys/class/leds/led-run/brightness";
cJSON *json = NULL;
cJSON *json_params = NULL;
cJSON *json_id = NULL;
cJSON *json_led = NULL;
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");
json = cJSON_Parse(topic_info->payload);
if (!json)
{
HAL_Printf("Error before: [%s]\r\n",cJSON_GetErrorPtr());
}
else
{
json_id = cJSON_GetObjectItem(json , "id");
if(json_id->type == cJSON_String)
{
HAL_Printf("id:%s\r\n", json_id->valuestring);
}
json_params = cJSON_GetObjectItem(json , "params");
if(json_params)
{
if(cJSON_GetObjectItem(json_params, "led"))
{
json_led = cJSON_GetObjectItem(json_params, "led");//get json data
if(json_led->type == cJSON_Number)
{
HAL_Printf("LED:%d\r\n", json_led->valueint);
if((fd = open(leds, O_RDWR|O_NOCTTY|O_NDELAY))<0)
{
HAL_Printf("open %s failed\n",leds);
perror("open leds");
exit(1);
}
else
{
if(json_led->valueint==1)
{
write(fd,"1",1);
}else{
write(fd,"0",1);
}
HAL_Printf("ioctl %s success, led %d\n",leds,json_led->valueint);
}
close(fd);
}
}
}
}
cJSON_Delete(json);
break;
default:
break;
}
}
//消息订阅
int example_subscribe(void *handle)
{
int res = 0;
const char *fmt = "/sys/%s/%s/thing/service/property/set";
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);
//IOT_MQTT_Subscribe向云端订阅指定的Topic
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;
}
- MQTT 相关API:https://code.aliyun.com/edward.yangx/public-docs/wikis/user-guide/linkkit/Prog_Guide/API/MQTT_Provides
完成以上内容参考了阿里提供的官方指导和橙子MAX发布的视频教程:https://www.bilibili.com/video/av55613036?from=search&seid=4167138359701025330