1、参考链接 :http://www.nongnu.org/lwip/2_0_x/group__mqtt.html

2、首先移植好lwip,然后添加lwip-2.0.2\src\apps\mqtt  文件下 的 mqtt.c 文件,如果有头文件问题,清解决头文件问题!

3、根据参考链接,做一下修改

下面代码中的MQTT 服务器 是我自己搭建的,你也可以找一台Linux 主机搭建一个。。2017年4月24日09:10:19

/*
* MQTT client for lwIP * Author: Erik Andersson * Details of the MQTT protocol can be found at:
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
*
*/ #include "oneNet_MQTT.h" #include "string.h" ip_addr_t mqttServerIpAddr; //1. Initial steps, reserve memory and make connection to server: //1.1: Provide storage //Static allocation: //下面这两句转移到 MAIN.c 中
// mqtt_client_t static_client; // example_do_connect(&static_client); //Dynamic allocation:
// mqtt_client_t *client = mqtt_client_new();
// if(client != NULL) {
// example_do_connect(&client);
// } //1.2: Establish Connection with server void example_do_connect(mqtt_client_t *client)
{
struct mqtt_connect_client_info_t ci; err_t err; /* Setup an empty client info structure */
memset(&ci, , sizeof(ci)); /* Minimal amount of information required is client identifier, so set it here */
ci.client_id = "lwip_test"; // MQTT服务器地址为:59.110.142.105端口号为:1883
IP4_ADDR(&mqttServerIpAddr, , , , );//配置 MQTT 服务器地址 /* Initiate client and connect to server, if this fails immediately an error code is returned
otherwise mqtt_connection_cb will be called with connection result after attempting
to establish a connection with the server.
For now MQTT version 3.1.1 is always used */ //MQTT 服务器进行连接 ,并注册 “连接结果处理”的回调函数,2017年4月12日09:00:12 err = mqtt_client_connect(client, &mqttServerIpAddr, , mqtt_connection_cb, , &ci); /* For now just print the result code if something goes wrong */
if(err != ERR_OK)
{
printf("mqtt_connect return %d\n", err);
}
} //Connection to server can also be probed by calling mqtt_client_is_connected(client) //-----------------------------------------------------------------
//2. Implementing the connection status callback
//执行连接状态 的 回调函数
static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
{
err_t err;
if(status == MQTT_CONNECT_ACCEPTED)
{
printf("mqtt_connection_cb: Successfully connected\n"); /* Setup callback for incoming publish requests */
//注册 消息推送 回调函数 以及消息数据到来的处理函数
mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); /* Subscribe to a topic named "subtopic" with QoS level 2, call mqtt_sub_request_cb with result */
err = mqtt_subscribe(client, "subtopic", , mqtt_sub_request_cb, arg); if(err != ERR_OK)
{
printf("mqtt_subscribe return: %d\n", err);
}
}
else
{
printf("mqtt_connection_cb: Disconnected, reason: %d\n", status); /* Its more nice to be connected, so try to reconnect */
example_do_connect(client);
}
} void mqtt_sub_request_cb(void *arg, err_t result)
{
/* Just print the result code here for simplicity,
normal behaviour would be to take some action if subscribe fails like
notifying user, retry subscribe or disconnect from server */
printf("Subscribe result: %d\n", result);
} //-----------------------------------------------------------------
//3. Implementing callbacks for incoming publish and data /* The idea is to demultiplex topic and create some reference to be used in data callbacks
Example here uses a global variable, better would be to use a member in arg
If RAM and CPU budget allows it, the easiest implementation might be to just take a copy of
the topic string and use it in mqtt_incoming_data_cb
*/ //这两个函数 一个 处理 消息头 一个 处理 具体的 数据 static int inpub_id;
static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len)
{ //消息来了之后,先进入这里用于判断 是什么主题 的,2017年4月20日09:25:51 printf("Incoming publish at topic \" %s \" with total length %u\r\n", topic, (unsigned int)tot_len); /* Decode topic string into a user defined reference */
if(strcmp(topic, "subtopic") == )
{
inpub_id = ;
}
else if(topic[] == 'A')
{
/* All topics starting with 'A' might be handled at the same way */
inpub_id = ;
}
else
{
/* For all other topics */
inpub_id = ;
}
} static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags)
{
//消息来了之后先判断主题 上面那个函数,然后在这里是根据不同的主题,进行不同的处理 uint8_t tempBuff[]={}; uint8_t i; //数据复制到到缓冲区,并加 字符‘0’结尾,因为 发送工具只能发送 字符 不能发送字符串,2017年4月20日09:24:43
for(i=;i<len;i++)
{
tempBuff[i]=data[i];
}
tempBuff[i]=; printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags); if(flags & MQTT_DATA_FLAG_LAST)
{
/* Last fragment of payload received (or whole part if payload fits receive buffer
See MQTT_VAR_HEADER_BUFFER_LEN) */ /* Call function or do action depending on reference, in this case inpub_id */
if(inpub_id == )
{
/* Don't trust the publisher, check zero termination */
if(tempBuff[len] == )
{
printf("mqtt_incoming_data_cb: %s\n", (const char *)data);
}
}
else if(inpub_id == )
{
/* Call an 'A' function... */
}
else
{
printf("mqtt_incoming_data_cb: Ignoring payload...\n");
}
}
else
{
/* Handle fragmented payload, store in buffer, write to file or whatever */
}
} //-----------------------------------------------------------------
//4. Using outgoing publish //使用 向外推送消息
//MQTT传输的消息分为:主题(Topic)和负载(payload)两部分 void example_publish(mqtt_client_t *client, void *arg)
{
const char *pub_payload= "Hello,MQTT!\r\n";//推送的有效数据(负载)
err_t err;
/*
* 最多一次,这一级别会发生消息丢失或重复,消息发布依赖于底层TCP/IP网络。即:<=1
* 至多一次,这一级别会确保消息到达,但消息可能会重复。即:>=1
* 只有一次,确保消息只有一次到达。即:=1。在一些要求比较严格的计费系统中,可以使用此级别
*/
u8_t qos = ; /* 0 1 or 2, see MQTT specification */ //=0 的意思应该是不保留有效负载(有效数据)
u8_t retain = ; /* No don't retain such crappy payload... */ err = mqtt_publish(client, "pub_topic", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if(err != ERR_OK)
{
printf("Publish err: %d.\r\n", err);
}
else
{
printf("Publish Success.\r\n");
}
} /* Called when publish is complete either with sucess or failure */
//推送消息完成后 的 回调函数
void mqtt_pub_request_cb(void *arg, err_t result)
{
if(result != ERR_OK)
{
printf("Publish result: %d\n", result);
}
} //-----------------------------------------------------------------
//5. Disconnecting //Simply call mqtt_disconnect(client)
    static_client.conn_state=;

    /* Initialize MQTT client */
example_do_connect(&static_client); while()
{ example_publish(&static_client,(void *)mqtt_pub_request_cb); /* vTaskDelayUntil是绝对延迟,vTaskDelay是相对延迟。*/
vTaskDelay();
}

上面 的服务器 地址 今天 已经 到期了 ,阿里 的 太贵了 没钱 续费了  哎 2017年6月26日17:15:40

LWIP2.0.2 & FreeRTOS & MQTT 客户端的 使用的更多相关文章

  1. MQTT客户端库-Paho GO

    为了加深理解,本文是翻译文章.原文地址 Paho GO Client   语言 GO 协议 EPL AND EDL 官网地址 http://www.eclipse.org/paho/ API类型 As ...

  2. 物联网架构成长之路(32)-SpringBoot集成MQTT客户端

    一.前言 这里虽然是说MQTT客户端.其实对于服务器来说,这里的一个具有超级权限的MQTT客户端,就可以做很多事情.比如手机APP或者网页或者第三方服务需要发送数据到设备,但是这些又不是设备,又不能让 ...

  3. MQTT Client library for C (MQTT客户端C语言库-paho)

    原文:http://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/index.html 来自我的CSDN博客   最近在使用Paho的MQTT客 ...

  4. oauth2.0服务端与客户端搭建

    oauth2.0服务端与客户端搭建 - 推酷 今天搭建了oauth2.0服务端与客户端.把搭建的过程记录一下.具体实现的功能是:client.ruanwenwu.cn的用户能够通过 server.ru ...

  5. elasticsearch 5.0 获取 TransportClient 操作客户端java API

    本文转载自:http://blog.csdn.net/likui1314159/article/details/53233881 elasticsearch 5.0 获取 TransportClien ...

  6. linux c MQTT客户端实现

    linux c MQTT客户端实现 摘自:https://www.jianshu.com/p/d309de966379 一.前言:mqtt协议是轻量级的消息订阅和发布(publish/subscrib ...

  7. MQTT客户端

    MQTT客户端 最近公司项目中使用到了一个MQTT的协议,用这个通讯协议将嵌入式端收集到的数据接入到物联网中,很是方便的解决了,嵌入式端存储空间小,也解决了用户需要自定义使用这些记录数据的需求.而且相 ...

  8. java在线聊天项目0.6版 解决客户端关闭后异常问题 dis.readUTF()循环读取已关闭的socket

    服务端对try catch finally重新进行了定义,当发生异常,主动提示,或关闭出现异常的socket 服务器端代码修改如下: package com.swift; import java.io ...

  9. (二)MQTT客户端模拟连接阿里云并上传数据

    本文主要讲述使用MQTT.fx接入物联网平台 一.下载MQTT.fx客户端 官网链接 二.设置相关参数 打开MQTT单片机编程工具,将三元组复制进去,生成所需要的信息 单片机工具下载地址 三元组还记得 ...

随机推荐

  1. SSH注解方式与XML配置方式对照表

    一.Hibernate 1.一对多注解 2.单表注解 二.Struts2 Struts2注解 三.Spring 1.IOC注解 2.AOP注解

  2. Algorithm——整数反转

    一.问题 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 12 ...

  3. 【SPOJ】MGLAR10 - Growing Strings

    Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is us ...

  4. 使用js在HTML中自定义字符串格式化方法

    Python中支持字符串格式化,其基本形式如下: str = "I'm {name},{age} years old" print(str.format(name="te ...

  5. IhyerDB modBus采集器配置.

    近期查了一下ihyerDB-modbus采集器的相关配置,由于没有相关的modbus设备,于是今天上午根据网上的线索下载了Modbus Slave(modbus从站仿真器).笔记本也没有串口,于是下载 ...

  6. Linux时间同步+国内常用的NTP服务器地址

    当Linux服务需要时间戳的时候,时间同步就显得十分重要.这里介绍下,最近我使用的一个同步命令. # ntpdate s1a.time.edu.cn 国内常用的NTP地址 210.72.145.44 ...

  7. MUI框架-13-使用百度地图 API(图文教程)

    MUI框架-13-使用百度地图 API(图文教程) 后面有实例,转载请注明出处 一.申请百度地图权限 1.打开 百度地图开放平台:http://lbsyun.baidu.com/apiconsole/ ...

  8. 安卓app开发-03-项目的基本开发步骤

    android项目的基本开发步骤 这里分享一下开发 安卓 app 的流程,当然有些感觉不必要,其实不然,前期工作也是极为重要的额,就像开发的时候如果目标不对的话,到后期后很迷的,所以一定要提前做好规划 ...

  9. 实用爬虫-02-爬虫真正使用代理 ip

    实用爬虫-02-爬虫真正使用代理 ip 因为这里呢,是实用爬虫,想要仔细学习一些基础的,可以去查看: Python 爬虫教程:https://www.cnblogs.com/xpwi/category ...

  10. Box(视图组件)如何在多个页面不同视觉规范下的复用

    本文来自 网易云社区 . 问题描述 Android App中的页面元素,都是由一个个Box(可以理解成一个个自定义View组件和Widget同级)组成,这些Box可以在不同的页面.不同的模块达到复用的 ...