Introduction: Cayenne, Python and MQTT Tutorials-4 - Analog Input

Get temperature sensor value with DS18B20 from anywhere with Cayenne IOT Dashboard.

This tutorial will show you how to get temperature sensor value from Cayenne IOT Dashboard.

Step 1: Wiring

You will need the following parts for this tutorial:

1x Raspberry Pi

3x male-female jumper wires

1x 4.7k ohm resistor

1x DS18B20

Connect everything up as shown in Fritzing diagram.

To be sure your DS18B20 sensor working properly on RPi click this link and follow instructions.

Step 2: Sign Up!

To use Cayenne IOT Dashboard, you need to sign up Cayenne from the website.

Step 3: Selecting Device

After log in, Select "Bring Your Own Thing".

A window will pop up with MQTT and your Client details. You will use these informations in Python script. When you connect Cayenne MQTT Broker by running Python script, you will direct to Dashboard.

Step 4: Adding Widget

To add Value and Gauge widget, click on:

  • Add new
  • Custom widgets
  • Value widget
  • Then fill widget infos as shown(do same for Gauge widget)

Note: Channnel number must be same as in Python script. I choose Channel 5 for two widgets.

Step 5: Python Code

You need to install this library for Python script.

It is a MQTT library to communicate RPİ and Cayenne IOT Broker.

cayenne-mqttThis library can be installed using pip and pip3:

  • pip install cayenne-mqtt (for Python2)
  • pip3 install cayenne-mqtt (for Python3) >>>for this tutorial.

Note: In the script, you must modify your MQTT credentials and widget channel.

<p>import os<br>import glob
import time
import cayenne.client</p><p>os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')</p><p>base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'</p><p># Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = ""
MQTT_PASSWORD  = ""
MQTT_CLIENT_ID = ""</p><p> #The callback for when a message is received from Cayenne.
def on_message(message):
    print("message received: " + str(message))
  # If there is an error processing the message return an error string, otherwise return nothing.</p><p>def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines</p><p>def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        #temp_f = temp_c * 9.0 / 5.0 + 32.0     if you want use Fahrenheit, modify these lines  
        return temp_c#,temp_f                               this line</p><p>client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)</p><p>while True:
  client.loop()
  value=read_temp()
  client.celsiusWrite(5, value)                             #this line
  #client.fahrenheitWrite(5, value)                         this line
  time.sleep(3)

</p>

Step 6: Final Stage

This video shows final stage.

Step 7: