Introduction: Cayenne, Python and MQTT Tutorials-3 - Analog Output

Picture of Cayenne, Python and MQTT Tutorials-3 - Analog Output

Dim a LED or change PWM output of Raspberry Pi pin from anywhere with Cayenne IOT Dashboard.

This tutorial will show you how to control LED brigthness from Cayenne IOT Dashboard.

Step 1: Wiring

Picture of Wiring

You will need the following parts for this tutorial:

  • 1x Raspberry Pi
  • 2x male-female jumper wires
  • 1x 560 ohm resistor
  • 1x 10mm LED

You will need to connect the following pins to the LED and resistor:

  • Rasp. Pi GND to LED cathode (short leg)
  • Rasp. Pi Pin 17 to one leg of the 560 ohm resistor
  • LED anode (long leg) to the second leg of the 560 ohm resistor

Step 2: Sign Up!

Picture of Sign Up!

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

Step 3: Selecting Device

Picture of 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

Picture of Adding Widget

To add Slider Controller widget, click on:

  • Add new
  • Custom widgets
  • Slider Controller widget

Then fill widget infos:

  • Select Data type Analog Actuator
  • Set Slider min value to 0
  • Set Slider min value to 100

and click on Add Widget.

You can change widget infos later on widget.

Note: Channnel number must be same as "message.channel" in Python script.

Step 5: Python Code

Picture of Python Code

You need to install this library for Python script. It is MQTT library to communicate RPİ and Cayenne IOT Broker.

  • cayenne-mqtt

This 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.

messsage.channel is channel of slider widget and it is a integer. message.value is value of received message from Cayenne and it is a string, we converted it to 2 digits float with round function and then divide by 100.

In gpiozero library, PWMLED.value changes between 0 and 1. 0 means Led is totally closed. 1 means Led is totally open.

import cayenne.client #Cayenne MQTT Client
from gpiozero import PWMLED led=PWMLED(17) #Declaring button pin 17

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard. MQTT_USERNAME = "YOUR MQTT USERNAME" MQTT_PASSWORD = "YOUR MQTT PASSWORD " MQTT_CLIENT_ID = "YOUR CLİENT ID"

# The callback for when a message is received from Cayenne. def on_message(message): print("message received: " + str(message)) if message.channel==2: #Dashboard Slider widget channel. They must be same. led.value=float(message.value)/100 #Setting led value to recevied slider value

client = cayenne.client.CayenneMQTTClient() client.on_message = on_message #When message recieved from Cayenne run on_message function client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

while True: client.loop()

Step 6: Final Stage

This video shows final stage.

Comments

About This Instructable

316views

11favorites

License:

Add instructable to: