mqtt安装与基本测试
#安装mosquitto mqtt
#引入仓库
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
#更新源
sudo apt-get update
#安装mosquitto
sudo apt install mosquitto
#安装客户端
sudo apt-get install mosquitto-clients
######测试
#一个终端开启sub mqtt
mosquitto_sub -h localhost -t "test_local" -v
#另一个终端发布信息,看能否收到
mosquitto_pub -h localhost -t "test_local" -m "HELLO MQTT"
若sub的中端收到,那么mqtt安装成功。
python部分
#api参考网站
https://pypi.org/project/paho-mqtt/#usage-and-api
#安装pip
sudo apt install python-pip
#安装python 的mqtt包
pip install paho-mqtt
publish.single("MQTT Examples", 2); 第一个参数为mqtt的topic第二个是内容。
import paho.mqtt.publish as publish
publish.single("MQTT Examples", 2)
c++部分
#paho.mqtt.c 库
https://github.com/eclipse/paho.mqtt.c
#下载oaho c库
git clone https://github.com/eclipse/paho.mqtt.c.git
#编译安装
make
sudo make install
#编译sub程序
g++ MQTTClient_subscribe.c -o sub -lpaho-mqtt3c
/*******************************************************************************
* Copyright (c) 2012, 2020 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* https://www.eclipse.org/legal/epl-2.0/
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial contribution
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>