在GPRS模块(GA6)和STM32F103C8T6单片机上移植MQTT协议

在GPRS模块(GA6)和STM32F103C8T6单片机上移植MQTT协议

最近几天折腾了一下,在GPRS模块(GA6)和STM32F103C8T6单片机上移植MQTT协议,网上找了一些资料,不是很全面,下面结合自己的移植过程记录一下。

下载MQTT的库

我移植MQTT的库:Paho,这个库支持非常多的平台,当然也包括了嵌入式平台:GitHub – paho.mqtt.embedded-c
这里写图片描述
将该库中的MQTTPacket文件夹下载下来,MQTTPacket文件夹下面主要有三个文件夹,如上图所示,我们使用的文件主要在src文件夹和samples文件夹中。
src文件夹中存放着MQTT核心功能的代码,而samples中存放着五个例子:ping.c,ping_nb.c,pub0sub1.c,pub0sub1_nc.c,qos0pub.c和网络驱动文件(transport.c和transport.h),如下图所示:
这里写图片描述

移植步骤

1, 准备工作

因为我板子都是淘宝上买的,给过来很多配套的资料,熟悉下怎么通过STM32F103C8T6单片机串口连接GPRS模块(GA6)来发送TCP消息,当然也熟悉下keil 5这个工具,因为我不是做单片机开发的,第一次玩这个,着实也是折腾了一下,才勉强用Keil 5完成我要的任务,这里的STM32F103C8T6单片机串口连接GPRS模块(GA6)来发送TCP消息在Keil 5中建一个工程试试,一般资料中都会有,直接用就可以,后面的移植就基于这个工程来弄。

2,移植

直接把前面我们下载下来的MQTTPacket/src目录中的所有文件,如下图所示导入到上面的工程中。
这里写图片描述
我导入之后的结果如下图所示:
这里写图片描述
这里只是截取了一个片段,后面我会把整个完成的工程代码放出。当然上面用于cmake编译的CMakeLists.txt文件,我们不需要添加进来。导入之后重新编译是没有错误的,如果前面的工程本来就有错误的话,请先修改的没错误。

这样其实就已经把MQTT库移植完成了,是不是很简单,应该说这个的支持比较好,下面我们来把库samples目录文件夹中的例子移植进去,我是基于pub0sub1.c这个例子来做的,我先把这个文件的原内容贴出来,方便对比:

/*******************************************************************************
 * Copyright (c) 2014 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Ian Craggs - initial API and implementation and/or initial documentation
 *    Sergio R. Caprile - clarifications and/or documentation extension
 *******************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "MQTTPacket.h"
#include "transport.h"

/* This is in order to get an asynchronous signal to stop the sample,
as the code loops waiting for msgs on the subscribed topic.
Your actual code will depend on your hw and approach*/
#include <signal.h>

int toStop = 0;

void cfinish(int sig)
{
    signal(SIGINT, NULL);
    toStop = 1;
}

void stop_init(void)
{
    signal(SIGINT, cfinish);
    signal(SIGTERM, cfinish);
}
/* */

int main(int argc, char *argv[])
{
    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    int rc = 0;
    int mysock = 0;
    unsigned char buf[200];
    int buflen = sizeof(buf);
    int msgid = 1;
    MQTTString topicString = MQTTString_initializer;
    int req_qos = 0;
    char* payload = "mypayload";
    int payloadlen = strlen(payload);
    int len = 0;
    char *host = "m2m.eclipse.org";
    int port = 1883;

    stop_init();
    if (argc > 1)
        host = argv[1];

    if (argc > 2)
        port = atoi(argv[2]);

    mysock = transport_open(host, port);
    if(mysock < 0)
        return mysock;

    
  • 7
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 19
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值