Support for Raspberry Pi and Orange Pi GPIOs in Home Assistant

So, I've been off in the GPIO library salt mines for a while, but am now ready to circle back and document how to get GPIO inputs and outputs working in Home Assistant. This now works on both Raspberry Pi and OrangePi, assuming that my patch gets merged. First off, let's talk about GPIO outputs. This is something which has been working for a while on both platforms (a while being a week or so, assuming you've patched Home Assistant with my pull request, but you're all doing that right?). To configure an output in Home Assistant, you would add the following to configuration.yaml: rpi_gpio: board_family: orange_pi switch: - platform: rpi_gpio ports: PA7: LED Where board_family can be either "raspberry_pi" or "orange_pi". Note that for Raspberry Pis, the pin numbers are always numbers whereas for OrangePi we are using "SUNXI" numbering, which is of the form "PA7". The circuit for this LED is really simple: Now we have a switch we can control in Home Assistant: GPIO inputs are similar. The configuration looks like this: rpi_gpio: board_family: orange_pi binary_sensor: - platform: rpi_gpio invert_logic: true ports: PA7: PUSHYBUTTON With a circuit like this: invert_logic set to true is required because our…

Continue ReadingSupport for Raspberry Pi and Orange Pi GPIOs in Home Assistant

Updated examples for OrangePi GPIOs

As part of working through adding OrangePi support to Home Assistant, Alastair and I decided to change to a different GPIO library for OrangePi to avoid the requirement for Home Assistant to have access to /dev/mem. I just realised that I hadn't posted updated examples of how to do GPIO output with the new library. So here's a quick post about that. Assuming that we have an LED on GPIO PA7, which is pin 29, then the code to blink the LED would look like this with the new library: import OPi.GPIO as GPIO import time # Note that we use SUNXI mappings here because its way less confusing than # board mappsings. For example, these are all the same pin: # sunxi: PA7 (the label on the board) # board: 29 # gpio: 7 GPIO.setmode(GPIO.SUNXI) GPIO.setwarnings(False) GPIO.setup('PA7', GPIO.OUT) while True: GPIO.output('PA7', GPIO.HIGH) time.sleep(1) GPIO.output('PA7', GPIO.LOW) time.sleep(1) The most important thing there is the note about SUNXI pin mappings. I find the whole mapping scheme hugely confusing, unless you use SUNXI and then its all fine. So learn from my fail people! What about input? Well, that's not too bad either. Let's assume that you have a button in a…

Continue ReadingUpdated examples for OrangePi GPIOs

GPIO inputs on Raspberry Pi

Now that I have GPIO outputs working nicely for Home Assistant using either a Raspberry Pi or an Orange Pi, I want to get GPIO inputs working as well. Naively, that's pretty easy to do in python on the Raspberry Pi: import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) while True: print('Reading...') if GPIO.input(17) == GPIO.HIGH: print('Pressed') else: print('Released') time.sleep(1) That code is of course horrid. Its horrid because its polling the state of the button, and its quite likely that I can sneak a button press in during one of those sleeps and it will never be noticed. Instead we can use edge detection callbacks to be informed of button presses as they happen: import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) def event_callback(channel): print('Event detected: %s' % GPIO.input(17)) GPIO.add_event_detect(17, GPIO.BOTH, callback=event_callback, bouncetime=50) while True: time.sleep(1) This second program provides helpful output like this: pi@raspberrypi:~ $ python gpio_button_edge_detect.py Event detected: 1 Event detected: 0 Which is me pressing the button once (it go high when pressed, and then low again when released). This is of course with a button wired to GPIO17 with a current limiting resistor between the button and the 3.3v…

Continue ReadingGPIO inputs on Raspberry Pi

Pull Requests for the LCA2019 Home Automation tutorial

A quick list of things I did for the LCA2019 Home Automation tutorial. Of course Alistair did a lot more, but I still want to track these. Add OrangePi GPIO support to Home Assistant (declined) Document OrangePi GPIO support in Home Assistant (declined) Add OrangePi Prime GPIO pinouts to OPi.GPIO (merged, and released) Expose pullup resistor constants in OPi.PGIO (merged, and released) A simple burn in script for the workshop boards (merged) Automated generation of dhcpd.conf for the workshop network (merged) Clarifications for the workshop prerequisites (merged) Not all pins on Orange Pi support edge detection Clarify where the GPIO header is on the LCA2019 shield

Continue ReadingPull Requests for the LCA2019 Home Automation tutorial

Further adventures in Home Assistant OrangePi GPIO

Its funny how a single sentence can change your course. In the last post about this work, I said: We also need to run hass as root,  because OrangePi GPIO support requires access to /dev/mem for reasons I haven’t dug into just yet. That's turned out to be (reasonably) a pretty big sticking point upstream. Access to /dev/mem gives you a whole bunch of access to the machine that Home Assistant probably shouldn't have. Alastair went off spelunking because he's more patient than me and found yet another OrangePi GPIO library. I think we're up to three or four of these at the moment, but this is the first one we've found which supports the sysfs interface to GPIO pins. That's exciting because it removes our run-as-root requirement. Its unexciting in that the sysfs interface has been deprecated by the kernel, but will remain supported for a while. I think people would be within their rights to conclude that the state of GPIO libraries for OrangePi is a bit of a dumpster fire right now. Anyways, the point of this post is mostly to write down how to use the sysfs interface to GPIO pins so that I can remember it later,…

Continue ReadingFurther adventures in Home Assistant OrangePi GPIO

Adventures in Home Assistant Raspberry Pi GPIO

Alastair D'Silva is running what looks to be a very well prepared home automation tutorial at LCA2019 based on Home Assistant. I offered to have a hack on the support for GPIO pins on OrangePi boards in Home Assistant because it sounded interesting for a vacation week. The only catch being that I'd never done anything with GPIO pins at all on either Raspberry Pi or Orange Pi. The first step seemed to be to get GPIO working at all on a Raspberry Pi (which is currently supported out of the box with Home Assistant). This online tutorial has a simple example of a circuit and the associated python code to blink a LED on a Raspberry Pi, so off I went to build that circuit. The circuit has a LED with a 330 ohm pull up resistor on GPIO pin 18 on the board. The sample python code on the page above just blinks that LED, which I used to make sure that the circuit as working as intended. To configure the GPIO pin as a switch in Home Assistant, I added the following to configuration.yaml (noting that the empty rpi_gpio entry isn't strictly required, but will be later): rpi_gpio: switch: -…

Continue ReadingAdventures in Home Assistant Raspberry Pi GPIO

The Consuming Fire

  • Post author:
  • Post category:Book

Another fast run read from Mr Scalzi, this book is the sequel to The Collapsing Empire. I think this book is actually better than the first, which I guess is fair given the first had to set the universe up. I particularly like the twist about two thirds of the way through this one, and I think the universe has a lot of potential to be really interesting in future books. Mr Scalzi remains on my I-buy-everything-he-does list. I wish he'd write another book in the Old Man's War universe.

Continue ReadingThe Consuming Fire

HomeAssistant configuration

I've recently been playing with HomeAssistant, which is quite cool. Its not perfect -- for example it broke recently for me without any debug logs indicating problems because it didn't want to terminate SSL any more, but its better than anything else I've seen so far. Along the way its been super handy to be able to refer to other people's HomeAssistant configurations to see how they got things working. So in that spirit, here's my current configuration with all of the secrets pulled out. Its not the most complicated config, but it does do some things which took me a while to get working. Some examples: The Roomba runs when no one is home, and let's me know when its bin is full. A custom component to track when events last occurred so that I can rate limit things like how often the Roomba runs when no one is home. I detect when my wired doorbell goes off, and play a "ding dong" MP3 in the office yurt out the back so I know when someone is visiting. ...and probably other things. I intend to write up interesting things as I think of them, but we'll see how we…

Continue ReadingHomeAssistant configuration

Chaos Monkeys

  • Post author:
  • Post category:Book

A very well written tale of a Wall Street quant who left during the GFC to adventure in startup land and ended up at Facebook attempting to solve their monetization problems for an indifferent employer. Martinez must have been stomping around Mountain View because his description of the environment and what its like to work inside a Silicon Valley company ring very true to me. A good read.

Continue ReadingChaos Monkeys

Artemis

  • Post author:
  • Post category:Book

Its been ages since I've read a book in a couple of days, let alone stayed up late when I really shouldn't in order to finish a book. Artemis is the book which broke me out of that rut -- this is a fun, clever, light read. Its quite different when compared to The Martian, but I think that's good. Weir has attempted to do something new instead of just playing on his previous successes. An excellent book, and Mr Weir is solidly landing on my buy-everything-he-writes list.

Continue ReadingArtemis

End of content

No more pages to load