Skip to content

Instantly share code, notes, and snippets.

@bphermansson
Created April 16, 2017 10:50
Show Gist options
  • Save bphermansson/54cb18ecdd30b571ece48472b94dba9d to your computer and use it in GitHub Desktop.
Save bphermansson/54cb18ecdd30b571ece48472b94dba9d to your computer and use it in GitHub Desktop.
Read power from a power meter and control a analog gauge and a RGB-led.
#!/usr/bin/env python
import urllib
import json
from time import sleep
import os
# Used to control Gpios
import pigpio
pi = pigpio.pi()
pi.set_mode(13, pigpio.OUTPUT) # GPIO as output Green led
pi.set_mode(19, pigpio.OUTPUT) # GPIO as output Blue led
pi.set_mode(26, pigpio.OUTPUT) # GPIO as output Red led
pi.write(13, 0)
pi.write(19, 0)
pi.write(26, 0)
# On Broadcom Gpio12 (physical pin 32) we have a analog gauge
dial = 12
# Test
#pi.write(13, 1)
#pi.hardware_PWM(dial, 2000, 250000)
#sleep(1)
#pi.write(13, 0)
#pi.hardware_PWM(dial, 2000, 0)
# Create temp file if it doesnt exist
if os.path.exists("/tmp/washerRunning"):
target = file("/tmp/washerRunning", "r+")
else:
target = file("/tmp/washerRunning", "w")
target.close
# Get data from server
url = "http://192.168.1.117/"
f = urllib.urlopen(url)
data = f.read()
parsed_json = json.loads(data)
curP = int((parsed_json['power']))
if curP > 100:
# Washer draws power, it's on
print "Running"
# Check if the washer already are marked as running
target = open("/tmp/washerRunning", 'r')
washerRuns = target.read
if washerRuns != "True":
# No it aint
# Light led
pi.write(19, 1)
pi.hardware_PWM(dial, 2000, 900000)
# Set it as running
print "Opening the file..."
target = open("/tmp/washerRunning", 'w')
target.write("True")
target.close
else:
# Power is low...
print "P is low"
target = open("/tmp/washerRunning", 'r')
washerRuns = target.read()
print "WR: " + washerRuns
if washerRuns == "True":
# But washer has status of running
# Check power again after 180s
print "Washer is running but power is low"
sleep(180)
f = urllib.urlopen(url)
data = f.read()
parsed_json = json.loads(data)
curP = int((parsed_json['power']))
if curP < 100:
# Power is still low, the machine is done
print "P is still low"
target = open("/tmp/washerRunning", 'w')
target.write("False")
target.close
pi.write(19, 0)
pi.hardware_PWM(dial, 2000, 0)
print "Washer is idle"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment