2017年6月12日 星期一

mqtt on rpi


Ref : https://learn.adafruit.com/diy-esp8266-home-security-with-lua-and-mqtt/configuring-mqtt-on-the-raspberry-pi

Ref : http://www.instructables.com/id/Installing-MQTT-BrokerMosquitto-on-Raspberry-Pi/


用 stretch 的 iso

This is what I did as per your instructions:
To use the new repository you should first import the repository package signing key:

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
Then make the repository available to apt:

cd /etc/apt/sources.list.d/
Then one of the following, depending on which version of debian you are using:

sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list


Then update apt information:

sudo apt-get update


 sudo apt-get install python-pip

sudo pip install paho-mqtt

  1. sudo apt-get install mosquitto mosquitto-clients python-mosquitto

sudo systemctl restart mosquitto


--------------------------------------------------------------


import os
import string
import paho.mqtt.client as mqtt
import time


localBroker = "192.168.1.120"           # Local MQTT broker
localPort = 1883                        # Local MQTT port
localUser = "mosquitto"                 # Local MQTT user
localPass = "mosquitto"                 # Local MQTT password
localTopic = "test"             # Local MQTT topic to monitor
localTimeOut = 120                      # Local MQTT session timeout


if "__main__" == __name__:

   # The callback for when the client receives a CONNACK response from the server.
   def on_connect(client, userdata, flags, rc):
                print("Connected with result code "+str(rc))

                # Subscribing in on_connect() means that if we lose the connection and
                # reconnect then subscriptions will be renewed.
                client.subscribe(localTopic)

   # The callback for when a PUBLISH message is received from the server.
   def on_message(client, userdata, msg):
                #(sensorID, sensorVoltage) = string.split(msg.payload)
                 print(msg.payload)

   client = mqtt.Client()
   client.on_connect = on_connect
   client.on_message = on_message
   client.connect(localBroker, localPort, localTimeOut)

   client.loop_forever()
   quit()


----------------------------------------------------------------------------------------------------------

mosquitto_pub -h 192.168.1.120 -t test -m "hello world"




沒有留言:

張貼留言