Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. create a directory MQTT

    Code Block
    language bash
    sudo mkdir MQTT
    cd MQTT/
  2. Download the mosquitto from this site or simply use the following command. http://mosquitto.org/download/

    Code Block
    language bash
    sudo wget http://mosquitto.org/files/source/mosquitto-1.0.2.tar.gz
  3. I used this mosquitto-1.0.2.tar.gz version for this tutorial
  4. Follow these commands to install mosquitto

    Code Block
    language bash
    sudo apt-get install libwrap0-dev
    tar zxf mosquitto-1.0.2.tar.gz
    cd mosquitto-1.0.2/
    make
    sudo make install
    sudo ldconfig
    sudo make clean
    sudo iptables -A INPUT -p tcp -m tcp --dport 1883 -j ACCEPT
  5. If it fails the compile process try the following supportive commands on the terminal to install the dependency libraries. Then compile again

    Code Block
    language bash
    sudo apt-get install libssl-dev
    sudo apt-get install python-mosquitto
  6. Next step is to prepare the MQTT client script to communicate with gadgetkeeper MQTT server. Following Python script can be used to do it. First go to the gadgetkeeper directory we created on the Temperature monitoring with BeagleBoneBlack and DHT11/22 and AM2302 temperature and humidity sensor tutorial. Then create a file as follows

    Code Block
    language bash
    cd /root/gadgetkeeper/
    sudo nano mqtt_listner.py

    Then paste the following Python code on it.

    Code Block
    language py
    #!/usr/bin/python
    import time
    import mosquitto
    import random
    import commands
    ###############################################
     
    # change this thing ID as required
    THING_ID="ecf5ceb002d111e49e72fb53d9d5a540"
    BROKER_NAME="devapi.gadgetkeeper.com"
    READ_TEMP="python /root/gadgetkeeper/read_temperature.py 11 30"
    READ_HUMID="python /root/gadgetkeeper/read_humidity.py 11 30"
    ###############################################
     
    topic = 'thing.' + THING_ID
    broker = BROKER_NAME
    client_name = "device-1"
    last_id = ""
    def on_message(mosq, obj, msg):
        global last_id
        print 'Request: ' +  msg.payload
        id,jsn,cmd = msg.payload.split(',', 2 );
        #print id
        #print jsn
        #print cmd
        if last_id == id:
            print 'Update successful'
            return
        last_id = id
        #data_val = 20.00
        #data_val = random.uniform(1.5, 100.9) #genarate random value for testing
        #read temperature from the sensor
        data_value = commands.getoutput(READ_TEMP)
    #    data_value = commands.getoutput(READ_HUMID)
        if 'Failed' not in data_value:
            data_val = data_value[26:]
    #       data_val = data_value[23:] 
            print 'Sensor reading: ' + data_val
        else:
            time.sleep(2) #delay 2s and try again
            data_val = commands.getoutput(READ_TEMP)
    #       data_val = commands.getoutput(READ_HUMID)
        if 'Failed' in data_value:
            data_val = -1000.0 # If sensor reading fails assign this value
            print 'Sensor read error'
        reply_msg = id + ',' + jsn + ',' + "\"result\":" + str(data_val) + '}'
        print 'Response: ' + reply_msg
        client_rply = mosquitto.Mosquitto("device-1")
        client_rply.connect(broker)
        client_rply.publish(topic, reply_msg ,0)
    client = mosquitto.Mosquitto(client_name)
    client.connect(broker)
    client.subscribe(topic,0)
    client.on_message = on_message
    while True:
        client.loop(15)
        time.sleep(2)
    
    
  7. Now simply run this script as follows

    Code Block
    language bash
    sudo python mqtt_listner.py
  8. Make sure that port 1883 is not blocked by the firewall.

...