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 servervoid 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, 0, sizeof(ci));/* Minimal amount of information required is client identifier, so set it here */ ci.client_id = "lwip_test";// MQTT服务器地址为:59.110.142.105端口号为:1883IP4_ADDR(&mqttServerIpAddr, 59, 110, 142, 105);//配置 MQTT 服务器地址/* Initiate client and connect to server, if this fails immediately an error code is returnedotherwise 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, 1883, mqtt_connection_cb, 0, &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", 2, 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 callbacksExample here uses a global variable, better would be to use a member in argIf RAM and CPU budget allows it, the easiest implementation might be to just take a copy ofthe 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") == 0) {inpub_id = 0;} else if(topic[0] == 'A')        {/* All topics starting with 'A' might be handled at the same way */inpub_id = 1;}else{/* For all other topics */inpub_id = 2;}
}static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags)
{//消息来了之后先判断主题 上面那个函数,然后在这里是根据不同的主题,进行不同的处理
    uint8_t tempBuff[20]={0};uint8_t i;//数据复制到到缓冲区,并加  字符‘0’结尾,因为 发送工具只能发送 字符 不能发送字符串,2017年4月20日09:24:43for(i=0;i<len;i++){tempBuff[i]=data[i];}tempBuff[i]=0;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 bufferSee MQTT_VAR_HEADER_BUFFER_LEN)  *//* Call function or do action depending on reference, in this case inpub_id */if(inpub_id == 0) {/* Don't trust the publisher, check zero termination */if(tempBuff[len] == 0) {printf("mqtt_incoming_data_cb: %s\n", (const char *)data);}}else if(inpub_id == 1) {/* 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 = 2; /* 0 1 or 2, see MQTT specification *///=0 的意思应该是不保留有效负载(有效数据)u8_t retain = 0; /* 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=0;/* Initialize MQTT client */example_do_connect(&static_client);while(1){example_publish(&static_client,(void *)mqtt_pub_request_cb);/* vTaskDelayUntil是绝对延迟,vTaskDelay是相对延迟。*/vTaskDelay(2000);}

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

转载于:https://www.cnblogs.com/suozhang/p/6755206.html

LWIP2.0.2 FreeRTOS MQTT 客户端的 使用相关推荐

  1. ESP8266的MQTT客户端搭建教程(基于NONS_SDK_v2.0)

    前言 MQTT是IBM开发的一个即时通讯协议,面向M2M和物联网的连接,采用轻量级发布和订阅消息传输机制,并且有可能成为物联网的重要组成部分. ESP8266是一款物美价廉的Wi-Fi芯片,集成Ten ...

  2. 【ESP8266】ESP8266的MQTT客户端搭建教程(基于NONS_SDK_v2.0)

    前言 MQTT是IBM开发的一个即时通讯协议,面向M2M和物联网的连接,采用轻量级发布和订阅消息传输机制,并且有可能成为物联网的重要组成部分. ESP8266是一款物美价廉的Wi-Fi芯片,集成Ten ...

  3. C#创建MQTT客户端接收服务器信息

    服务端下载地址:https://download.csdn.net/download/horseroll/11012231 MQTT是什么? MQTT (Message Queue Telemetry ...

  4. MQTT客户端连接服务器协议,mqtt客户端和服务器长连接

    mqtt客户端和服务器长连接 内容精选 换一换 介绍设置客户端和服务器的安全认证方式的相关参数.参数说明:表明与服务器建立链接后,不进行任何操作的最长时间.参数类型:USERSET取值范围:整型,0- ...

  5. MQTT再学习 -- 安装MQTT客户端及测试

    上一篇文章我们已经讲了 MQTT 服务器的搭建,参看:MQTT再学习 -- 搭建MQTT服务器及测试 接下来我们看一下 MQTT 客户端. 一.客户端下载 首先,客户端也有多种,我们需要面临选择了. ...

  6. MQTT客户端库-Paho GO

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

  7. mqtt客户端工具_如何在 Rust 中使用 MQTT

    Rust 是由 Mozilla 主导开发的通用.编译型编程语言.该语言的设计准则为:安全.并发.实用,支持 函数式.并发式.过程式以及面向对象的编程风格.Rust 速度惊人且内存利用率极高.由于没有运 ...

  8. 【程序】Marvell 88W8686 WiFi模块(WM-G-MR-09)创建或连接热点,并使用lwip2.0.3建立http服务器(20180312版)

    该程序是旧版本!最新版本为20180706版: https://blog.csdn.net/ZLK1214/article/details/80941657 本程序所用的单片机型号为:STM32F10 ...

  9. 基于Domoticz智能家居系统(十四)用ESP8266做MQTT客户端实验

    基于Domoticz智能家居系统(十四)用ESP8266做MQTT客户端实验 用ESP8266做MQTT客户端 一些前期的准备 第一步 设置ESP8266开发板的BSP的搜索引擎链接 第二步 下载安装 ...

最新文章

  1. 软件工程实践2017 个人技术博客
  2. c#属性的相关学习总结。
  3. rtems线程管理与调度(一)
  4. java sqlhelper_java版sqlhelper(转)
  5. QLibrary Class Reference(qt加载外部库)
  6. 检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)...
  7. java 类型转换方法_Java中的实用类型转换的方法
  8. 连接MySql出现Client does not support authentication protocol requested by server错误
  9. 《决战大数据大数据的关键思考 升级版》PDF电子书分享
  10. linux下查看进程与线程
  11. 汉诺塔c 语言程序代码,汉诺塔 (C语言代码)
  12. network location awareness 错误
  13. echarts地图外边缘添加阴影投影或外发光
  14. 2021年100道最新软件测试面试题,常见面试题及答案汇总
  15. sinr是什么意思_信噪比有负的吗?表示什么意思?
  16. 急如闪电快如风,彩虹女神跃长空,Go语言高性能Web框架Iris项目实战-初始化项目ep00
  17. 自律的程序员生活是什么样的?
  18. Batch Normalization原理与实战
  19. ajax提交时页面转圈,jquery的ajax提交时loading提示的处理方法
  20. 投资理财-投资难的秘密

热门文章

  1. FFmpeg解码音频代码
  2. ios 构建版本一直在处理中_app已审核通过,ios构建版本失败,提示此构建版本...
  3. cmake中添加引用动态链接_C# 添加、编辑、删除PPT中的超链接
  4. Javascript模块化编程系列三: CommonJS AMD 模块化规范描述
  5. Teamcenter2007 安装步骤
  6. Java Applet 授权命令
  7. linux卸载tar安装的erlang包,linux - 从tar安装erlang导致错误,想知道如何指定文件夹 - 堆栈内存溢出...
  8. Spring Cloud Gateway (六) 自定义 Global Filter
  9. android uri parcel,Android ParcelFileDescriptor实现进程间通信
  10. mysql8.0依赖_分享MySql8.0.19 安装采坑记录