2016年4月5日 星期二

Raspberry Pi DHT11溫溼度感測器 + MQTT 傳送感測訊息

  1. Ubuntu Server 擔任 MQTT Server
  2. sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa 
    sudo apt-get update 
    sudo apt-get install mosquitto
    sudo apt-get install mosquitto-clients
    

  3. Ubuntu 向執行MQTT Server註冊接收來自hello/world 的訊息
  4. mosquitto_sub -d -t hello/world
    
    視窗暫時不要關閉
  5. Raspberry 安裝 DHT11 溫溼度感測器及軟體
  6. 可參考 葉難:Raspberry Pi 溫溼度感測器DHT11
    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
    

  7. 讀取Pi上溫溼度感測結果,DHT11感測器data pin接在Pi上GPIO 2
  8. cd examples
    sudo ./AdafruitDHT.py 11 2
    
    執行結果:
    Temp=25.0*  Humidity=48.0%
    

  9. Raspberry Pi 安裝 MQTT Client
  10. 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
    

  11. Pi 向 Ubuntu MQTT Server (IP:192.168.0.xx) 註冊接收來自 hello/world 的訊息
  12. mosquitto_sub -h 192.168.0.xx -d -t hello/world
    

  13. Pi執行讀取DHT11感測器資料,並將結果作為訊息內容發佈到 hello/world
  14. mosquitto_pub -h 192.168.0.xx -d -t hello/world -m "`/root/AdafruitDHT.py 11 2`"
    

  15. Ubuntu或Pi上向MQTT Server註冊接收來自hello/world 訊息的視窗
  16. 應該都可以看到類似的執行結果:
    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%
    


延伸閱讀:


 #

1 則留言:

urreed 提到...

感謝,學習了