Community
OpenEnergyMonitor

Community

Problems with GPIO push button on self build emonPi


(tonertiffi) #55

Hi Paul,

Get the following error

[email protected]:/usr/local/bin $ python mqtt-temp

File “mqtt-temp”, line 50

if temp >= sensor[‘on’]:

^

IndentationError: unexpected indent

[email protected]:/usr/local/bin $


(Paul) #56

That’s most likely a copy and paste error. Python is very fussy about indentation, looking at the script there are 4 levels of indentation (plus no indentation). the if temp >= sensor['on']: line is on the 3rd level of indentation which means it probably must have 3 tabs or 12 spaces depending on what style you are using.

Beware of stray white space and or line endings altering things from what you see to what is seen by Python.

If you cannot resolve the problem you need to post the code you have leading up to and after the point it says is at fault as the required indention is defined by the code around it.


(tonertiffi) #57

Hi @pb66

  File "mqtt-temp", line 55
    publish_data('emon/Fans/' + sensor['name'] + '/values', sensor['status'])
                                                                            ^
IndentationError: unindent does not match any outer indentation level
[email protected]:/usr/local/bin $

(tonertiffi) #58
    for sensor in Sensors:
        temp = read_temp(sensor['id'])
        if temp >= sensor['on']:
                gpio.output(sensor['pin'], gpio.LOW)
                sensor['status'] = 1
        elif temp <= sensor['off']:
                gpio.output(sensor['pin'], gpio.HIGH)
                sensor['status'] = 0
            publish_data('emon/Fans/' + sensor['name'] + '/values', sensor['status'])
            #publish_data('emon/' + sensor['name'] + '/Status/values', sensor['status'])
            publish_data('emon/Temperatures/' + sensor['name'] + '/values', temp)
            #publish_data('emon/' + sensor['name'] + 'Temperature/values', temp)
            if temp >= sensor['on']:
                gpio.output(sensor['pin'], gpio.LOW)
                publish_data('emon/Fans/' + sensor['name'] + '/values', 1)
                #publish_data('emon/' + sensor['name'] + '/Status/values', 1)
            elif temp <= sensor['off']:
                gpio.output(sensor['pin'], gpio.HIGH)
                publish_data('emon/Fans/' + sensor['name'] + '/values', 0)
                #publish_data('emon/' + sensor['name'] + '/Status/values', 0)

    time.sleep(6)

(Paul) #59

Well yes there are some indentation errors there, for some reason you have duplicated some code one level in

#!/usr/bin/python3

import glob
import time
import paho.mqtt.publish as publish
import RPi.GPIO as gpio

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

Sensors = {{'name':'SENSOR_A','id':'28-800000abc123','pin':1,'on':20,'off':15},
           {'name':'SENSOR_B','id':'28-800000def456','pin':2,'on':20,'off':15},
           {'name':'SENSOR_C','id':'28-800000abc456','pin':3,'on':20,'off':15}}

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

def publish_data(topic, value):
    publish.single(topic, str(value),
                    hostname=Broker, port=1883,
                    auth=auth,)
    return

gpio.setmode(gpio.BOARD)

for sensor in Sensors:
    gpio.setup(sensor['pin'], gpio.OUT)
    gpio.output(sensor['pin'], gpio.HIGH)
    sensor['status'] = 0

while True:
    for sensor in Sensors:
        temp = read_temp(sensor['id'])
        if temp is not None:
            publish_data('emon/Temperatures/' + sensor['name'] + '/values', temp)
            #publish_data('emon/' + sensor['name'] + 'Temperature/values', temp)
            if temp >= sensor['on']:
                gpio.output(sensor['pin'], gpio.LOW)
                sensor['status'] = 1
            elif temp <= sensor['off']:
                gpio.output(sensor['pin'], gpio.HIGH)
                sensor['status'] = 0

        publish_data('emon/Fans/' + sensor['name'] + '/values', sensor['status'])
        #publish_data('emon/' + sensor['name'] + '/Status/values', sensor['status'])

    time.sleep(6)

(tonertiffi) #60

Convinced you a genius :smiley:


(tonertiffi) #61

In my effort i managed to break this, it does not update the values, if i hit refresh it works , go out, go back in values updated,never turns red


(tonertiffi) #62

Check pc time bottom left:smiley:


(Paul) #63

Sorry I don’t follow, are you saying emoncms is no longer refreshing?

Try using ctrl-f5 to see if that fixes it OR has your log in session timed out?


(tonertiffi) #64

Correct

it works everywhere else except for the feeds page.


(Paul) #65

I’m still not sure what you mean, you are showing us the graph page which is data derived from the feeds, it cannot update unless the feeds update. Which is why I suggested a clear cache to the browser.


(Paul) #66

What is that “paused” button?

image


(tonertiffi) #67

Gmail is paused :slight_smile:


(Paul) #68

ahh ok! :slight_smile:


(tonertiffi) #69

Ok this is what i do, and the results.
I go to the inputs and select a feed add it (log to feed)
I go to the feed page its not updating.
I delete that feed i just created (log to feed), the rest start updating.
i go out of the feed page, i go back in no feeds updating


(tonertiffi) #70

I changed on the My account,My profile, Beta Features:

Device Module Beta

was On, to OFF
started working 100%


(tonertiffi) #71

Its now 24:30pm here, will run it till the morning and let you know, If its still updating ok.


(Paul) #72

Please do. If the issue persists it sounds like something @TrystanLea should be aware of.

Just as another test, can you try using input and feed names without any special characters, it should be able to handle a couple of underscores, but you never know.


(tonertiffi) #73

Hi @pb66 ran it through the night, till now no problem, feeds r there, everything working 100%.As long as i do not switch to


(tonertiffi) #74

Hi @pb66, wondered on something, some off the gpio pins,on the RPI seem to have a low voltage not activated. I noticed that when i plug in the relay, eg IN1 signal to a random gpio pin on the RPI, the led on the pba has a very small glow, without doing anything.(this relay pba has 2x status leds) can i assume the following, that the pin gets the correct state, when being told what to do so from the script (else a small voltage on the pin if unused.)

, any idea? Not using it that way now RPI supplies the ground and power .
but that is my plan to use it as the final build.dJ5wKNh