For context, I have a ratio-controller Pi HAT, which is used to operate a remote-power-socket. In addition, I have a temp/humidity sensor wired into the Pi. The idea being, when temp is greater than threshold, the fan (plugged into remote-power-socket) kicks on. When temp drops, it turns off.
I have sort of mashed up the following two scripts, to try get where I am trying to go.
EDIT (15/MAR) - move to bottom page.
This first one is meant to do what I want, in its entirety. But, only the temp/humidity reading + forward readings to web-server parts of it work. This because the the part that operates the Pi HAT that talks to the remote-power-board, is for a different Pi HAT to the one I have):
Code: Select all
#!/usr/bin/python3
# monitor.py - For Terrarium Controllers using Adafruit
# DHT sensors, Energenie Pimote sockets, and ThingSpeak.
# MIT license.
# https://www.carnivorousplants.co.uk/resources/raspberry-pi-terrarium-controller/
# Imports
from gpiozero import Energenie
import Adafruit_DHT
import requests
# Attempt to get a sensor reading. The read_retry method will
# retry up to 15 times, waiting 2 seconds between attempts
sensormodel = Adafruit_DHT.AM2302
sensorpin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensormodel, sensorpin)
# If either reading has failed after repeated retries,
# abort and log message to ThingSpeak
thingspeak_key = 'XXXXXXXXXXXXXXXX'
if humidity is None or temperature is None:
f = requests.post('https://api.thingspeak.com/update.json', data = {'api_key':thingspeak_key, 'status':'failed to get reading'})
# Otherwise, check if temperature is above threshold,
# and if so, activate Energenie socket for cooling fan
else:
fansocket = 1
tempthreshold = 28
if temperature > tempthreshold:
# Activate cooling fans
f = Energenie(fansocket, initial_value=True)
else:
# Deactivate cooling fans
f = Energenie(fansocket, initial_value=False)
# Send the data to Thingspeak
r = requests.post('https://api.thingspeak.com/update.json', data = {'api_key':thingspeak_key, 'field1':temperature, 'field2':humidity})
This second one operates both types of Pi HATs (the one I do and and the one I don't). You'll see in there it's split into two sections. Purple On and Green On. So running through this script just cycles through both types, but only the Green On works for my gear (and it does work.)
Code: Select all
# combined.py 15/05/2016 D.J.Whale
#
# A simple demo of combining both FSK (MiHome) and OOK (green button legacy)
#
# NOTE: This is only a test harness.
# If you really want a nice way to control these devices, wait for the 'device classes'
# issues to be implemented and tested on top of the raw radio interface, as these
# will be much nicer to use.
import time
from energenie import Messages, OpenThings, radio, encoder, Devices
# build FSK messages for MiHome purple
OpenThings.init(Devices.CRYPT_PID)
PURPLE_ID = 0x68B # captured from a real device using Monitor.py
m = OpenThings.alterMessage(
Messages.SWITCH,
header_sensorid=PURPLE_ID,
recs_0_value=1)
purple_on = OpenThings.encode(m)
m = OpenThings.alterMessage(
Messages.SWITCH,
header_sensorid=PURPLE_ID,
recs_0_value=0)
purple_off = OpenThings.encode(m)
# build OOK messages for legacy green button
GREEN_ON = encoder.build_switch_msg(True, device_address=1)
GREEN_OFF = encoder.build_switch_msg(False, device_address=1)
def switch_loop():
print("Turning green ON")
radio.modulation(ook=True)
radio.transmit(GREEN_ON)
time.sleep(0.5)
print("Turning purple ON")
radio.modulation(fsk=True)
radio.transmit(purple_on, inner_times=2)
time.sleep(2)
print("Turning green OFF")
radio.modulation(ook=True)
radio.transmit(GREEN_OFF)
time.sleep(0.5)
print("Turning purple OFF")
radio.modulation(fsk=True)
radio.transmit(purple_off, inner_times=2)
time.sleep(2)
if __name__ == "__main__":
print("starting combined switch tester")
print("radio init")
radio.init()
try:
while True:
switch_loop()
finally:
radio.finished()
# END
So if all that makes sense, hopefully you can see what I've tried to do in the third one below - get (1) the the temp/humidity reading + push reading to web-server from the first scrip, and (2) getting the 'Green On' parts from the second script, to operate the hardware (based on some if/else decisions).
Code: Select all
from gpiozero import Energenie
from energenie import radio, encoder
import requests
import Adafruit_DHT
fansocket = 1
tempthreshold = 20
sensormodel = Adafruit_DHT.AM2302
sensorpin = 26
humidity, temperature = Adafruit_DHT.read_retry(sensormodel, sensorpin)
fansocket_on = encoder.build_switch_msg(True, device_address=1)
fansocket_off = encoder.build_switch_msg(False, device_address=1)
thingspeak_key = 'SUPERSECRETAPIKEY'
if humidity is None or temperature is None:
f = requests.post('https://api.thingspeak.com/update.json', data = {'api_key':thingspeak_key, 'status':'failed to get reading'})
else:
if temperature > tempthreshold:
radio.modulation(ook=True)
radio.transmit(fansocket_on)
else:
radio.modulation(ook=True)
radio.transmit(fansocket_off)
r = requests.post('https://api.thingspeak.com/update.json', data = {'api_key':thingspeak_key, 'field1':temperature, 'field2':humidity})EDIT
OK, so I've got a little further. I've cut down the Pi HAT/power-board switcher to the below. All it does is switch the first socket on and off in an infinite loop (But the important bit it, it works). Now all I need to do is work the conditions in from the temp/humidity/push to server script, so that it switches on when above X temp, and off when it's below X temp. I have no idea how to mash the two - they're different languages by the looks?
Code: Select all
from energenie import radio, encoder
GREEN_ON = encoder.build_switch_msg(True, device_address=1)
GREEN_OFF = encoder.build_switch_msg(False, device_address=1)
def switch_loop():
radio.modulation(ook=True)
radio.transmit(GREEN_ON)
radio.modulation(ook=True)
radio.transmit(GREEN_OFF)
if __name__ == "__main__":
print("starting combined switch tester")
print("radio init")
radio.init()
try:
while True:
switch_loop()
finally:
radio.finished()