Temperature comparison with triggers

Hi

I am using a raspberry pi 3 with Cayenne. I have two temperature DS18B20 sensors connected to GPIO4 on the pi. The sensors picked up automatically on Cayenne. I also have a relay connected to another GPIO which turns off and on a light. What I would like to do is set a trigger as follows:

If temperature sensor 1 is higher than temperature sensor 2 then turn the light on, and if the other way around, the light must be off.

Is this possible and if so, how do I set the trigger. For now I can only set the temperature trigger on a slide at a certain temperature for both sensors and can not do a comparison between them for the trigger.

Kind regards

Welcome to cayenne community.
what you are trying with trigger is currently not possible but the same thing can be achieved by some coding and bit long.
do a compare in your code of the two temperature using if loop and depending make a two state widget on or off.
then trigger this two state widget to turn on the relay on.

Thank you for the information @shramik_salgaonkar. Do you perhaps have any documentation and/or examples of the programming to use. I have no programming experience however willing to learn. I am a newbie to this whole IoT and have no idea where to begin.

Have a look at this to get started with Mqtt cayenne.

next have a look at this awesome tutorial which should solve all your problem

1 Like

Hi
I am also new to Raspberry Pi and just started with Cayenne. I have a similar project in that I want to control my swimming pool solar heating from the Cayenne dashboard. I was impressed with the user interface and the ease of connecting the DS18b20 temperature sensors and a control relay to activate the pool solar pump. However, I need to calculate the temperature difference between the 2 sensors to then determine if the pump should start in order to pump water through the roof collector. I understand your project has a very similar requirement (temperature difference between 2 sensors) and would be interested in how you achieved this measurement. I thought when I started with Cayenne it would be a simple trigger statement based on T_roof - T_pool to then switch on the pump if I had say a 5 degree difference. I have since realised it is not that simple with Cayenne. Any help would be gratefully appreciated. Thanks

welcome to cayenne community @petehelm. you can try reading the value from ds18b20 using python and then send the value to cayenne. Also, do the calculation in the code and compare it with the value from the slider you want. then add a two state widget if the cconditon is true and add trigger to this widget.GitHub - myDevicesIoT/Cayenne-MQTT-Python: Python Library for Cayenne MQTT API

Hi Petehelm

I have done this using code. The way I have set up my system is to compare the temperature difference and through the code manipulate which relay needs to switch. I used GPIO 12 to switch the relay but I suppose you can use any of the other GPIO’s. What you will have to do is add the cayenne connection information and change the device ID to match the temp sensors you are using. The code I am using is as follows:

#!/usr/bin/env python
import os
import glob
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
GPIO.setwarnings(False)


os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + 'ADD YOUR DEVICE ID HERE for sensor 1')[0]
device_file = device_folder + '/w1_slave'


device_folder2 = glob.glob(base_dir + 'ADD YOUR DEVICE ID HERE for sensor 2')[0]
device_file2 = device_folder2 + '/w1_slave'


# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.



def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

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

#second sensor

def read_temp2_raw():
    f = open(device_file2, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp2():
    lines = read_temp2_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp2_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp2_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 temp2_c #,temp_f                               this line






i=0
timestamp = 0

while True:

    if (read_temp() > read_temp2()):
        GPIO.output(12, 0)

    else:
        GPIO.output(12,1)

Just add your cayenne MQTT info as described in the previous posts and change the code to use your device ID and GPIO number you will use for the relay signal.
Good luck but this should be exactly what you are looking for.

1 Like

Hi Ridhwaan Singh,
Thanks very much for your prompt reply, I really appreciate your help.
I think if I can get this up and running using your code example it will meet my requirements.
Before I emailed last night, I progressed as far as loading MQTT Python Library from the repository which failed on first attempt using “python setup.py install” but worked when I ran as Sudo.
I now have MQTT running after some initial failures and have a new device on my dashboard ready to add code.
Thanks again for taking the time to help me out

@petehelm if need any help, let us know.