How to do it...

  1. Create the following socketControl.py script:
#!/usr/bin/python3 # socketControl.py import time import RPi.GPIO as GPIO #HARDWARE SETUP # P1 # 2[V=G====XI====]26[=======]40 # 1[=====321=====]25[=======]39 #V=5V G=Gnd sw_num=[15,13,11]#Pins for Switch 1,2,3 sw_state=[16,18]#Pins for State X=Off,I=On MSGOFF=0; MSGON=1 SW_ACTIVE=0; SW_INACTIVE=1 class Switch(): def __init__(self): self.setup() def __enter__(self): return self def setup(self): print("Do init") #Setup the wiring GPIO.setmode(GPIO.BOARD) for pin in sw_num: GPIO.setup(pin,GPIO.OUT) for pin in sw_state: GPIO.setup(pin,GPIO.OUT) self.clear() def message(self,number,state): print ("SEND SW_CMD: %s %d" % (number,state)) if state==MSGON: self.on(number) else: self.off(number) ...

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.