Community
OpenEnergyMonitor

Community

Problems with GPIO push button on self build emonPi

(tonertiffi) #23

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

0 Likes

(tonertiffi) #24

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

Thank you again

0 Likes

(tonertiffi) #25

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


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)  ```
0 Likes

(tonertiffi) #26

0 Likes

(tonertiffi) #27

0 Likes

(tonertiffi) #28

to

the following happens

0 Likes

(tonertiffi) #29

0 Likes

(tonertiffi) #30
    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,) ```
0 Likes

(tonertiffi) #31

0 Likes

(Paul) #32

Ok, so try this for size

#!/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 (1.1 KB)]

0 Likes

(tonertiffi) #33
[email protected]:/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
[email protected]:/usr/local/bin $ ```
0 Likes

(Brian Orpin) #34

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

0 Likes

(Paul) #35

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.

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.

1 Like

(tonertiffi) #36

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

0 Likes

(tonertiffi) #37

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 :slight_smile: A big thank you to pb66 Paul you managed to get the last part working.

to others my setup

0 Likes

(tonertiffi) #38

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.

0 Likes

(tonertiffi) #39

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?

#!/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()  ```
0 Likes

(tonertiffi) #40

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


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) ```
0 Likes

(Paul) #41

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.

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.

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?

0 Likes

(tonertiffi) #42

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

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.

0 Likes