GPIO Reading through Python

HASS is known to have inconsistent reading of GPIO of RasPi

My original issue: RasPi 3 GPIO input not stable [Hassbian]
Problem: Pi GPIO Binary Sesnsor

I was looking for a workaround. A script that reads the GPIO status and then relays it as input to HASS. Any help or point me to the right direction. I am also looking at command_line as a platform too. Any help would be appreciated

I did suggest this to someone else a while ago

1 Like

I am sorry I could not quite follow. I need HASS because my RasPi is acting as a bell. There are 7 different entrances; with two speakers (who is sensitive to each entrance) with its own tone and 7 Segment Display indicating the entrance and LCD Display indicating the same. It provides notification in LG TV WebOS. Planning to get iOS also done. Everything works but HASS sometimes do not reflect the true value of the input pin. After some digging around I understood its a common problem. I am looking for a work around. I don’t even mind going to the actual python script (source of hass) and modifying. So its not just controlling IO pins but a lot more. If I directly check the status through a multimeter or through WiringPI or even just GPIO by exporting and reading - I can see the expected value but somehow the same is not passed down to HASS. Kindly help.

Not sure if this help but you can put a php script on a raspberry pi and start apache. It is now a web based switch than HA or anything can react with. I would change the output below to 1 or 0 so HA can determine its state.

<?php
//set pins mode to output
    system ( "gpio -g mode 21 out" );
//activate and print
     system ( "gpio -g write 21 1" );
	     echo ( "fireplace off " );
     system ( "gpio -g read 21" );

and in HA

  fireplace:
    command_on: 'curl -k "http://192.168.2.186/action/fireplaceon.php"'
    command_off: 'curl -k "http://192.168.2.186/action/fireplaceoff.php"'
1 Like

I was suggesting you use the code from the link in that thread to read the GPIO and pass the data on to HA via MQTT. Isn’t this what you wanted?

Why am I having output. I do not wish to manipulate the pins but reflect the true input.

But I have HA installed in my RPi. I was looking at the component python_scripts or commandline. I will however look at sending it as MQTT to HA. Do you think it will be a big overhead to Pi? I will be running it 24x7 non stop.

To read the value of gpio pin 21 on a pi, type

gpio -g read 21

to get a webpage that tells you that value of that pin, make a php script with that command in it

<?php
     system ( "gpio -g read 21" );

then point HA to read that webpage for a 0 or 1 to tell HA the pi’s gpio 21 is on or off.

1 Like

I have been running mosquitto on my Pi B+ continuously for about 18 months. Mosquitto uses much less cpu than HA, appdaemon or influxdb :slightly_smiling_face:

1 Like

Thanks for that confirmation. What I meant was running HA and MQTT server together in the same Rpi.

I managed to use the command_line component to get what I wanted:

binary_sensor:
  - platform: command_line
    name: Gate 2
    command: "cat /sys/class/gpio/gpio26/value"
    payload_on: "1"
    payload_off: "0"
    scan_interval: 1

However, in order for this to work I need to export and set the pin as input at once. I tried many methods including crontab, rc.local, .bashrc. I had no success. Any idea on how to get a script running just once at the boot and once during shutdown?

Thanks.

@gpbenton - Is there a way we can chat. I have another project incoming that would excessive use of MQTT and looking at a soln to have a client-server architecture. To have 1 HA and then communicate to others. Is there DM in this? More info at Targeted UI/ or HA to HA communication

I reckon I need to install WiringPi for this to work?

yes, and php / apache if you don’t. I did it many years ago when I first got my pi. google it and you’ll find instructions