I am going to show you the code I use to make my pumps work to get the ph + and ph- solution into my mixing bucket. you can add more buttons to control more relays ( Timed for pumps )
I know cayanne can control relays and such but I wanted to be able to hit a button and have my pump turn on for X amount of seconds. I can easily time with my measuring glass how many seconds = how much ML of fluid pushes out. Also I’m pretty sure if you have pumps that push a lot that you could probably use a potentiometer ( search DC motor controller $2 ) to slow them down since trying to get the pwm chip to work with mqtt was more of a PIA than I felt like messing with.
This was quick and easy a few hours of messing around and here is the how to.
First setup the Cayanne Button Code.
Go to your dashboard then click add device and the
This will being you to a screen showing your username, password and client ID.
Replace the XXX in the code with your information.
This is the code I am using
import cayenne.client
import time
MQTT_USERNAME = “XXX”
MQTT_PASSWORD = “XXX”
MQTT_CLIENT_ID = “XXX”
def on_message(message):
print("message received: " + str(message))
if (message.channel == 7) and (message.value == “0”):
execfile(“gpio12.py”)
if (message.channel == 8) and (message.value == “0”):
execfile(“gpio20.py”)
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
i=0
timestamp = 0
while True:
client.loop()
if (time.time() > timestamp + 10):
timestamp = time.time()
i = i+1
In the main tile button Script you see 2 .py/2 buttons\actions.
you can delete one if you only need 1 button.
I am doing 2 buttons so I can ph+ and ph- remotely.
Below is the scripts for the buttons with timers built in for 5 seconds. ( GPIO20.py)
these 2 scripts can be named whatever you want. just make sure to change the code in the first script to point to the button timer script\file.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(20, GPIO.OUT)
GPIO.output(20, 1)
time.sleep(0.5)
GPIO.setmode(GPIO.BCM)
GPIO.setup(20, GPIO.OUT)
GPIO.output(20, 0)
GPIO.cleanup()
Now run the first script once this is set up.
If you get any errors then you probbaly need to update paho-mqtt
run “sudo pip install paho-mqtt==1.3.0` to install 1.3.0”
it will stay running and scrolling your username/password and some other mqtt stuff thats what its supposed to do.
go back yo your dashboard and click on add - Devices Now scroll to the bottom and where it shows custom widget and click on button.
This is what it should look like.
If the add button is greyed out and not letting you add then the script isnt currently running and connected to cayenne. Cayenne will not let you add a button to that mqtt string/connection unless it see’s it is working currently.
Pay attention to where the “9” is on this picture.
In the first script/main mqtt script you see the line " i[ (message.channel == 9) and (message.value == “1”):] the message.channel is pretty much like a virtual button # so make all of these different as you are adding them.
Here is what mine looked like when I was done.
The problem\bug I see is that this is a toggle button so you have to make a trigger that tells it when I turn this on turn this virtual button back off ( this happens after the gpioxx.py script has ran )
Now they work as buttons that turn on and show the on icon\status until they have run the course then turn to off status with the grey/off icon.
HELP!
1st problem: is when I tell it to run other scripts say tell it to run a script that stays running it will kill all of the other buttons. the script stays running so this script does not let any other commands work.
1.5 problem: running this on startup I’ve tried crontab and it isn’t reliable I’ve went through about 3 pages of google and still haven’t gotten a reliable way to start the button script on startup. services etc… nothing is reliable so if you have a way to start the script on startup that works for you please share the code with me.
2nd problem:
Is there a way to make a virtual button that when pressed can set a schedule for a button/relay?
I would love a button that says VEG and one that says Bloom to change the lighting schedule ( based on time of day not seconds in case of a power outage etc… ). Is there a script any of the super familar cayenne people know about or could help me with to make this happen? I could always buy $10 timers but I already have a 10amp 8 channel relay hooked to this pi project.
Rough picture I have a waterproof enclosure for this but its still in desk-testing phase