# Problems with GPIO push button on self build emonPi

**URL:** <https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347>\
**Category:** Hardware\
**Created:** [5 March 2019 20:40 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347 "2019-03-05T20:40:21Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [12 March 2019 19:03 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/23 "2019-03-12T19:03:09Z")

</div>

You go fill yourself up, really do appreciate your help, thank you so much, you a star

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [12 March 2019 19:05 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/24 "2019-03-12T19:05:26Z")

</div>

> [@pb66](#):
>
> IMO it would probably be better to hardcode a list of sensors, that is the main reason I prefer to use Python to read temp sensors over any arduino based device, it’s easier to maintain a list of id’s and that way you are in control rather than leaving the script to just report what it finds, but either is possible.

I am all for hard coding the sensors, just do not have the understanding to do so.

Thank you again

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [12 March 2019 20:11 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/25 "2019-03-12T20:11:43Z")

</div>

Don’t know if this helps changed the code slightly, slowed it down but it still creates multiple sensors ever 60 seconds " time.sleep(60) " it now reads the 2 sensors

````auto

import glob
import time
import paho.mqtt.publish as publish

Broker = '127.0.0.1'
auth = {
    'username': 'emonpi',
    'password': 'emonpimqtt2016',
}

base_dir = '/sys/bus/w1/devices/'
device_folders = glob.glob(base_dir + '28-*')

def read_temp(device_folders):
    valid = False
    temp = 0
    with open(device_file, 'r') as f:
        for line in f:
            if line.strip()[-3:] == 'YES':
                valid = True
            temp_pos = line.find(' t=')
            if temp_pos != -1:
                temp = float(line[temp_pos + 3:]) / 1000.0

    if valid:
        return temp
    else:
        return None

while True:
    for device_folder in device_folders:
        device_file = device_folder + '/w1_slave'
        temp = read_temp(device_file)
        if temp is not None:
            pub_topic = 'emon/' + device_file + 'values/'
            publish.single(pub_topic, str(temp),
                    hostname=Broker, port=1883,
                    auth=auth,)
    time.sleep(60) ```
````

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [12 March 2019 20:12 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/26 "2019-03-12T20:12:11Z")

</div>

![test3](https://community.openenergymonitor.org/uploads/default/original/2X/9/91ed545b939d6e37064fc30c457d5e2947ce9299.png)

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [12 March 2019 20:13 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/27 "2019-03-12T20:13:35Z")

</div>

![test4](https://community.openenergymonitor.org/uploads/default/original/2X/1/1b57d76cef580fa43982b46bfa164f5250e102b3.png)

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [12 March 2019 20:18 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/28 "2019-03-12T20:18:39Z")

</div>

> [@tonertiffi](#):
>
> pub\_topic = ‘emon/’ + device\_file + ‘values/’

to

> [@tonertiffi](#):
>
> pub\_topic = ‘emon/temp1’ + device\_file + ‘values/’

the following happens

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [12 March 2019 20:21 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/29 "2019-03-12T20:21:24Z")

</div>

![test%205](https://community.openenergymonitor.org/uploads/default/original/2X/e/e6489a755060d2d63e33fdc899049ec139ecb94e.png)

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [12 March 2019 20:36 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/30 "2019-03-12T20:36:24Z")

</div>

````auto
    for device_folder in device_folders:
        device_file = device_folder + '/w1_slave'
        temp = read_temp(device_file)
        if temp is not None:
            pub_topic = 'emon/temp/temp' + device_file + '/values'
            publish.single(pub_topic, str(temp),
                    hostname=Broker, port=1883,
                    auth=auth,) ```
````

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [12 March 2019 20:37 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/31 "2019-03-12T20:37:52Z")

</div>

![test%206](https://community.openenergymonitor.org/uploads/default/original/2X/b/b9f03680e849b5826e80d6883628151202dfbc4c.png)

---

<div class="post-metadata">

**Author:** ![pb66](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/pb66/32/27_2.png) [@pb66](https://community.openenergymonitor.org/u/pb66)\
**Post date:** [12 March 2019 23:58 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/32 "2019-03-12T23:58:32Z")

</div>

> [@tonertiffi](#):
>
> I am all for hard coding the sensors, just do not have the understanding to do so.

Ok, so try this for size

```auto
#!/usr/bin/python3

import glob
import time
import paho.mqtt.publish as publish

Broker = '127.0.0.1'
auth = {
    'username': 'emonpi',
    'password': 'emonpimqtt2016',
}

Sensors = {'SENSOR_A':'28-800000abc123',
           'SENSOR_B':'28-800000def456',
           'SENSOR_C':'28-800000abc456'}

def read_temp(sensorid):
    valid = False
    temp = 0
    device_file = '/sys/bus/w1/devices/' + sensorid + '/w1_slave'
    with open(device_file, 'r') as f:
        for line in f:
            if line.strip()[-3:] == 'YES':
                valid = True
            temp_pos = line.find(' t=')
            if temp_pos != -1:
                temp = float(line[temp_pos + 3:]) / 1000.0

    if valid:
        return temp
    else:
        return None

while True:
    for name, id in Sensors:
        temp = read_temp(id)
        if temp is not None:
            pub_topic = 'emon/Temperatures/' + name + '/values'
            publish.single(pub_topic, str(temp),
                    hostname=Broker, port=1883,
                    auth=auth,)
    time.sleep(6)

```

You will need to replace the “Sensors” details with your own sensor ids and chosen names. You can get the sensor ids by running `ls -l /sys/bus/w1/devices` from the command line.

If it works (still untested at my end) it should give you a single node called “Temperatures” and each sensor should appear by name under that node in emoncms.

I haven’t altered any of the temperature value conditioning etc, just added the list of sensors and edited what we had to loop through that list, read the temp by id and to publish to a topic by name. I assume the topic will work only because you had it working previously. Fingers crossed.

[edit - attached file - [mqtt-temp.py.txt](https://community.openenergymonitor.org/uploads/default/original/2X/f/f2c25f6308e97bb8668e036430c86fb0ca045103.txt) (1.1 KB)]

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [13 March 2019 04:20 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/33 "2019-03-13T04:20:37Z")

</div>

````auto
pi@emonpi:/usr/local/bin $ python mqtt-temp
Traceback (most recent call last):
  File "mqtt-temp", line 35, in <module>
    for name, id in Sensors:
ValueError: too many values to unpack
pi@emonpi:/usr/local/bin $ ```
````

---

<div class="post-metadata">

**Author:** ![borpin](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/borpin/32/193_2.png) [@borpin](https://community.openenergymonitor.org/u/borpin)\
**Post date:** [13 March 2019 07:56 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/34 "2019-03-13T07:56:50Z")

</div>

> [@tonertiffi](#):
>
> still no change keeps on creating sys every 5s

I think I know what the issue is - I think the `device_file` string might contain a character that emoncms does not accept for a topic name. I had an issue with ‘:’ (issue [#1105](https://github.com/emoncms/emoncms/issues/1105)) and the symptoms were a new input generated every time. A colon should not affect it as the quick fix was merged. But this was never investigated for other characters.

To debug, you also need to monitor the MQTT broker from a different machine. You can get Windows MQTT clients to do that or use the ‘mosquitto\_sub’ command from another Linux machine (or even a second SSH connection).

---

<div class="post-metadata">

**Author:** ![pb66](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/pb66/32/27_2.png) [@pb66](https://community.openenergymonitor.org/u/pb66)\
**Post date:** [13 March 2019 08:25 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/35 "2019-03-13T08:25:11Z")

</div>

My bad, I was out later than expected last night so it was a bit rushed when I posted. Change line 35 to use `Sensors.iteritems()` rather than `Sensors` should fix that.

> [@borpin](#):
>
> I think I know what the issue is - I think the `device_file` string might contain a character that emoncms does not accept for a topic name.

Yeah, I just converted what was there to run once, to loop through multiple times and edited the topic change each time rather than publishing all the values to the same topic without noting that the device\_folder was a full path, not just the folder name (aka sensor id). That’s (hopefully) fixed in last nights effort.

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [13 March 2019 15:49 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/36 "2019-03-13T15:49:26Z")

</div>

I knew you were the man to talk to, thank you so much you solved it working like a dream.

 ![test7](https://community.openenergymonitor.org/uploads/default/original/2X/6/63301ac3d660a9b8966e3f26b2816d963e26ecd3.png)

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [13 March 2019 16:09 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/37 "2019-03-13T16:09:08Z")

</div>

Just as a matter of interests, was not able to get the emonpi in South Africa,managed to get all the bits from hobbie shops and it works 🙂 A big thank you to pb66 Paul you managed to get the last part working.

 ![RPI-GPIO-N-final](https://community.openenergymonitor.org/uploads/default/original/2X/7/78cb54eed4a7ba7d1d885d891ea6fbdbff9bc0d1.jpeg) to others my setup

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [13 March 2019 16:28 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/38 "2019-03-13T16:28:45Z")

</div>

One idea i have being toying with is to use a 8 port relay kit, so that when a temp reaches a certain deg it would turn on the relay on the board and keep it on for a predefined time.

 ![195256-1_740x](https://community.openenergymonitor.org/uploads/default/original/2X/0/02a11074fefe5f90735d2795c5756e1a62e9c569.jpeg)

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [14 March 2019 18:02 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/39 "2019-03-14T18:02:30Z")

</div>

Was wondering if you had a second, to have a quick look again, when i run the script, relay turns on, and stays on, even if i stop, the script stays on, temp is way below the value. Also would it be possible, to Select a certain temp sensor, to monitor , if that value goes high, then turn on the relay pin?

````auto
#!/usr/bin/env python3

import os
import glob
import time
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(13, GPIO.OUT)
GPIO.output(13, GPIO.LOW)

base_dir='/sys/bus/w1/devices/'
device_folder=glob.glob(base_dir + '28-*')[0]
device_file=device_folder + '/w1_slave'

set_temp=39.222, 72

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

def read_temp():
    lines=temp_raw()
    while lines[0].strip() [-3:] != 'YES':
        time.sleep(0.2)
        lines=temp_raw()
    temp_output=lines[1].find('t=')
    if temp_output != -1:
        temp_string=lines[1].strip() [temp_output+2:]
        temp_c=float(temp_string) / 1000
        temp_f=temp_c * 9.0 / 5.0 + 32.0
        return temp_c, temp_f
while True:
    if read_temp()>= set_temp:
        sleep(30)
        GPIO.setup(13, GPIO.IN)
        exit() ```
````

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [14 March 2019 19:58 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/40 "2019-03-14T19:58:56Z")

</div>

I have this so far ,no errors, but relay does not turn on ☹

````auto

import os
import glob
import time
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(13, GPIO.OUT)
GPIO.output(13, GPIO.LOW)

Sensors = {'28-020e9177ec79',}

set_temp=22.22

def read_temp(sensorid):
    valid = False
    temp = 0
    device_file = '/sys/bus/w1/devices/' + sensorid + '/w1_slave'
    with open(device_file, 'r') as f:
        for line in f:
            if line.strip()[-3:] == 'YES':
                valid = True
            temp_pos = line.find(' t=')
            if temp_pos != -1:
                temp = float(line[temp_pos + 3:]) / 1000.0

    if valid:
        return temp
    else:
        return None

while True:
    if Sensors.iteritems()>= set_temp:
        GPIO.setup(13, GPIO.IN)
        sleep(30) ```
````

---

<div class="post-metadata">

**Author:** ![pb66](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/pb66/32/27_2.png) [@pb66](https://community.openenergymonitor.org/u/pb66)\
**Post date:** [15 March 2019 00:04 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/41 "2019-03-15T00:04:22Z")

</div>

> [@tonertiffi](#):
>
> Was wondering if you had a second, to have a quick look again,

Can you tell us more about what the end game is? What do you want this all to do, not just this step, but the whole temperature and relay implementation, you seem to have ditched the work we did previously and reverted to the original again and then something inbetween.

> [@tonertiffi](#):
>
> when i run the script, relay turns on, and stays on, even if i stop, the script stays on,

That’s because all you have coded is turning the relay on when it reaches temp, you have no code to return it to off, it’s not “auto-return” you need to tell it exactly what to do eg for a heater you might use

if under 18°C turn heater on  
else if over 22°C turn heater off

The state of the GPIO output will stick at what ever it is when the script exits unless you add a “turn heater off” line before exiting, note if the script stops ungracefully (crashes or is killed) the current state would still stick as the end code isn’t run.

> [@tonertiffi](#):
>
> Also would it be possible, to Select a certain temp sensor, to monitor , if that value goes high, then turn on the relay pin?

Do you want different sensors to control different pins (relays) ? or multiple sensors reporting and just one specific sensor doing the relay? or do you just want one sensor and one relay?

---

<div class="post-metadata">

**Author:** ![tonertiffi](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/32/9427_2.png) [@tonertiffi](https://community.openenergymonitor.org/u/tonertiffi)\
**Post date:** [15 March 2019 04:23 UTC](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347/42 "2019-03-15T04:23:05Z")

</div>

> [@pb66](#):
>
> Can you tell us more about what the end game is? What do you want this all to do, not just this step, but the whole temperature and relay implementation, you seem to have ditched the work we did previously and reverted to the original again and then something inbetween.

Hi no that code you wrote form me i am using as is not changed anything thank you again.

> [@pb66](#):
>
> Can you tell us more about what the end game is? What do you want this all to do, not just this step, but the whole temperature and relay implementation, you seem to have ditched the work we did previously and reverted to the original again and then something inbetween.
> 
> ![](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/tonertiffi/40/9427_2.png) tonertiffi:

the end game, is to use the sensors, to monitor the equipment, if the battery box were to get to hot turn on a relay , to run a fan. Inverter to hot turn on a fan ect.i am looking at runing a separate script, relay-fan.py to monitor the scrit you wrote with the SENSORS-A ect, i would like to use a seperate pin on the relay per sensor.

[Previous page](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347.md?page=1)

[Next page](https://community.openenergymonitor.org/t/problems-with-gpio-push-button-on-self-build-emonpi/10347.md?page=3)
