- Platform information:
- Hardware: Raspberry Pi Zero
- OS: Raspbian Stretch
- Java Runtime Environment: 8
- openHAB version: openHAB 2.5.0.M1 Milestone Build
- Issue of the topic:
I’d like to create a PID controller that uses a web GUI to show the temp and allow for adjustments to a given setpoint. I have a few extra DHT11 sensors laying around and would like to use one of those to read both temperature and humidity. Currently I"m using a relay that is controlled by the GPIO pin of the RPi.
I can currently read the temp from the DHT11 using a python script I found on this forum. It reads that out just fine. But when I try and use the exec binding it gives me an error code.
I used the following tutorial to create what I have so far. I will post the items, sitemap and rules files below.
Also any help with creating the buttons to adjust the setpoint would be very helpful. I’d like it to look something like this. In the example provided, the center number (68) is the current temp and he Heat To is the setpoint. When you click on the up and down arrows, it adjusts the setpoint. I"m looking to do something similar to this.
-
Please post configurations (if applicable):
- PID.Items file –
//-----Proxy items used for logic
Switch Heat "Heat"
//-----Number used for home.sitemap Setpoint
Number:Temperature Setpoint_Temp "PID Setpoint [%.1f °F]" <temperature>
//-----PID_Temp and PID_Hum numbers converted from the strings below (Note: See ../things/PID.things)
Number PID_Hum_Numb "Humidity [%.1f %%]" <humidity>
Number:Temperature PID_Temp_Numb "Temperature [%.1f °F]" <temperature>
//-----Strings read from DHT11 temp/humidity sensor
String PID_Hum {channel="exec:command:hum:output"}
String PID_Temp {channel="exec:command:temp:output"}
//-----Relay Items used to turn on/off Heat
Switch Heat_Relay "Heat Relay" {gpio="pin:18 activelow:yes initialValue:high"}
- pid.sitemap – Sitemap configuration related to the issue
sitemap home label="Gill's Guide Control Center"
{
Frame label="PID UI"
{
Text item=Setpoint_Temp
Text item=PID_Hum_Numb
Text item=PID_Temp_Numb
Switch item=Heat_Relay
}
}
- Rules code related to the issue
//-----Used to turn off Heat if Setpoint changed but temp hasn't changed-----//
rule "Update PID Temp Number every minute"
when
Time cron "0 0/1 * 1/1 * ? *"
then
sendCommand(PID_Temp_Numb.name,PID_Temp_Numb.state.toString)
end
//-----Rule to turn off/on Heat (Auto)-----//
rule "Turn Heat on if Heat switch is ON and PID Temp is greater than Setpoint"
when
Item PID_Temp_Numb received command or
Item PID_Temp_Numb changed
then
if (Heat.state==ON && PID_Temp_Numb.state > Setpoint_Temp.state)
{
Heat_Relay.sendCommand(ON)
//-----Used to turn off Heat if Setpoint changed but temp hasn't changed-----//
rule "Update PID Temp Number every minute"
when
Time cron "0 0/1 * 1/1 * ? *"
then
sendCommand(PID_Temp_Numb.name,PID_Temp_Numb.state.toString)
end
//-----Rule to turn off/on Heat (Auto)-----//
rule "Turn Heat on if Heat switch is ON and PID Temp is greater than Setpoint"
when
Item PID_Temp_Numb received command or
Item PID_Temp_Numb changed
then
if (Heat.state==ON && PID_Temp_Numb.state > Setpoint_Temp.state)
{
Heat_Relay.sendCommand(ON)
Thread::sleep(5000)
}
else
{
Heat_Relay.sendCommand(OFF)
Thread::sleep(5000)
}
end
rule "Turn Heat off if Heat switch changes from ON to OFF"
when
Item Heat changed from ON to OFF
then
Thread::sleep(2000)
Heat_Relay.sendCommand(OFF)
Thread::sleep(5000)
end
- Scripts related to the issue
hum.py
#!/usr/bin/env python2.7
import sys
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print('{0:0.2f}'.format(humidity))
else:
print('Failed to get reading. Try again!')
sys.exit(1)
temp.py
#!/usr/bin/env python2.7
import sys
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
temperature = temperature * 9/5.0 + 32
if humidity is not None and temperature is not None:
print('{0:0.2f}'.format(temperature))
else:
print('Failed to get reading. Try again!')
sys.exit(1)
- If logs where generated please post these here using code fences:
pi@spid-control:/var/log/openhab2 $ tail -10 events.log
Traceback (most recent call last):
File "/etc/openhab2/scripts/hum.py", line 10, in <module>
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
File "/usr/local/lib/python2.7/dist-packages/Adafruit_DHT/common.py", line 94, in read_retry
humidity, temperature = read(sensor, pin, platform)
File "/usr/local/lib/python2.7/dist-packages/Adafruit_DHT/common.py", line 81, in read
return platform.read(sensor, pin)
File "/usr/local/lib/python2.7/dist-packages/Adafruit_DHT/Raspberry_Pi_2.py", line 34, in read
raise RuntimeError('Error accessing GPIO.')
RuntimeError: Error accessing GPIO.