- Ubuntu Server 擔任 MQTT Server
- Ubuntu 向執行MQTT Server註冊接收來自hello/world 的訊息
- Raspberry 安裝 DHT11 溫溼度感測器及軟體 可參考 葉難:Raspberry Pi 溫溼度感測器DHT11
- 讀取Pi上溫溼度感測結果,DHT11感測器data pin接在Pi上GPIO 2
- Raspberry Pi 安裝 MQTT Client
- Pi 向 Ubuntu MQTT Server (IP:192.168.0.xx) 註冊接收來自 hello/world 的訊息
- Pi執行讀取DHT11感測器資料,並將結果作為訊息內容發佈到 hello/world
- Ubuntu或Pi上向MQTT Server註冊接收來自hello/world 訊息的視窗 應該都可以看到類似的執行結果:
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa sudo apt-get update sudo apt-get install mosquitto sudo apt-get install mosquitto-clients
mosquitto_sub -d -t hello/world視窗暫時不要關閉
sudo apt-get update sudo apt-get install build-essential python-dev git clone https://github.com/adafruit/Adafruit_Python_DHT.git cd Adafruit_Python_DHT sudo python setup.py install
cd examples sudo ./AdafruitDHT.py 11 2執行結果:
Temp=25.0* Humidity=48.0%
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key sudo apt-key add mosquitto-repo.gpg.key cd /etc/apt/sources.list.d/接著視 Pi 作業系統版本而定
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list或
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo apt-get update sudo apt-get install mosquitto sudo apt-get install mosquitto-clients
mosquitto_sub -h 192.168.0.xx -d -t hello/world
mosquitto_pub -h 192.168.0.xx -d -t hello/world -m "`/root/AdafruitDHT.py 11 2`"
Client mosqsub/30610-myUbuntu received PINGRESP Client mosqsub/30610-myUbuntu received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (26 bytes)) Temp=25.0* Humidity=48.0%
延伸閱讀:
- Install and Test Mosquitto (MQTT) on Ubuntu and OSX
- 葉難:Raspberry Pi 溫溼度感測器DHT11
- GPIO:Raspberry Pi Models A and B
#
1 則留言:
感謝,學習了
張貼留言