MQTT笔记

物联网协议 MQTT CoAP RESTful/HTTP XMPP对比
http://www.open-open.com/lib/view/open1421196046843.html

MQTT单片机实现
http://blog.csdn.net/zhou6343178/article/details/51182623

MQTT代理服务器
http://mosquitto.org/download/
喜欢NodeJS的可用Mosca

MQTT的GO语言客户端
http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.golang.git/
安装Broker
wget http://download.opensuse.org/repositories/home:/oojah:/mqtt/CentOS_CentOS-7/home:oojah:mqtt.repo -O mqtt.repo
mv mqtt.repo /etc/yum.repos.d
yum list | grep mosquitto
# 安装mosquitto
yum install mosquitto
# 启动mosquitto 后台运行
mosquitto -d

安装Go Client
go get git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git
//测试源代码
package main

import (
        //"time"
        "fmt"
        "os"
        "os/signal"
        //"github.com/influxdata/influxdb/client/v2"
        //"reflect"
        //"github.com/surge/glog"
        MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
)

var f MQTT.MessageHandler = func(client *MQTT.Client, msg MQTT.Message) {
        fmt.Printf("TOPIC: %sn", msg.Topic())
        fmt.Printf("MSG: %sn", msg.Payload())
}

func main() {

        sigc := make(chan os.Signal, 1)
        signal.Notify(sigc, os.Interrupt, os.Kill)


        opts := MQTT.NewClientOptions().AddBroker("tcp://m2m.eclipse.org:1883").SetClientID("cid")
        opts.SetDefaultPublishHandler(f)

c := MQTT.NewClient(opts)
        if token := c.Connect(); token.Wait() && token.Error() != nil {
                panic(token.Error())
        }

        if token := c.Subscribe("microic", 0, nil); token.Wait() && token.Error() != nil {

                fmt.Println(token.Error())
                os.Exit(1)
        }



        //token := c.Publish("microic", 0, false, "pub 123")
        //token.Wait()


        <-sigc
        c.Disconnect(250)
        fmt.Println("quit")
}


发表于:2016-09-23 09:38:01

原文链接(转载请保留): http://www.multisilicon.com/blog/a35254648.html

友情链接: MICROIC
首页