Computer Shopper - UK (2020-10)

(Antfer) #1

ISSUE392|COMPUTER SHOPPER|OCTOBER 2020 93


init.rinit.restypestype=ce=ctypestypes.c_in.c_intt
init.argtypes =[ctypes.c_void_p]init.argtypes =[ctypes.c_void_p]
init(None)init(None)

def SHTC3_Read_Temperature(self):def SHTC3_Read_Temperature(self):
temperature =self.dll.SHTC3_Read_THtemperature =self.dll.SHTC3_Read_TH
temperature.restype =ctypes.c_floattemperature.restype =ctypes.c_float
temperature.argtypes =[ctypes.c_temperature.argtypes =[ctypes.c_
void_p]void_p]
return temperature(None)return temperature(None)

def SHTC3_Read_Humidity(self):def SHTC3_Read_Humidity(self):
humidity =self.dll.SHTC3_Read_RHhumidity =self.dll.SHTC3_Read_RH
humidity.restype =ctypes.c_floathumidity.restype =ctypes.c_float
humidity.argtypes=[ctypes.c_void_p]humidity.argtypes=[ctypes.c_void_p]
return humidity(None)return humidity(None)
offset =10offset =10


if name == "main":if name == "main":
shtc3 =SHTC3()shtc3 =SHTC3()
while True:while True:
time.sleep(5)time.sleep(5)
temp =shtc3.SHTC3_Read_Temperature()temp =shtc3.SHTC3_Read_Temperature()
-offset-offset
humidity =shtc3.SHTC3_Read_Humidity()humidity =shtc3.SHTC3_Read_Humidity()
if shtc3.SHTC3_Read_Humidity() >50:if shtc3.SHTC3_Read_Humidity() >50:
print('It is humid:Humdity =print('It is humid: Humdity =
%6.2f%%' %humidity)%6.2f%%' %humidity)
else:else:
print('It is not humid: Humdity=print('It is nothumid: Humdity=
%6.2f%%' %humidity)%6.2f%%' %humidity)
if shtc3.SHTC3_Read_Temperature() >if shtc3.SHTC3_Read_Temperature() >
30:30:
print('It is hot: Temperature=print('It is hot: Temperature =
%6.2f°C' %temp)%6.2f°C' %temp)
else:else:
print('It is cool: Temperature =print('It is cool: Temperature =
%6.2f°C' %temp)%6.2f°C' %temp)


To explain whatthis code does, the first
import commands add thelibraries that
we’ll need later on. Next, the class section


creates functions thatwill read the
temperatureand humidity when called.
We then create avariable called offset,
which has the difference in the actual
temperaturecompared to the temperature
read fromthe Pi.Weused asmartthermostat
to get the initial difference. To set your own
offset, change thescript so thatthe offset is 0,
then run the script for 20 minutes or so,then
you can compare the temperaturerecorded
by the Pi against the temperatureonan
external thermometer,smartorotherwise.
Ta ke the difference and set this as your offset.
Now,weget into the script. We’ve got a
While loop here,which keeps on running
(you need to press Ctrl-C to cancel it).
We’ve got asleep statement to makethe
script pause for five seconds on each
interaction, although you can makethis
longer if you prefer.
Next, we set avariableastemp,takingthe
current temperatureand subtracting our
offset, and we read the current humidity and

storethis in avariable called humidity.Our
set of If statementsunderneaththen say it’s
humid if the humidity is above 50%, or not
humid if under; and it says if it’s hot if the
temperatureisabove 30oCorcool if it’s
below this. Both statements print thecurrent
temperatureand humidity.
To see the script in action, download our
file and put it on your computer,and then
at the command line typepython tandh.py.
Thescript will run through.

TAKING IT FURTHER


Now thatyou’ve got abasic script working,
you can starttododifferent things with the
settings. Forexample, you can change the If
statements, to alsolook for low temperatures
and low humidity.For example, if you wanted
your Pi to monitor when it was cold, and
thereforeindanger of pipes freezing, you
could look for temperatures below 5oC.
However,wedorecommend upgrading to
the external temperatureprobe for this to
increase accuracy.Likewise, you can make
the script tell you when humidity is low and
it’s time to turn on ahumidifier.
Yo ucan also makethe script interact with
any devices connected to the Pi.For instance,
if you follow our last Raspberry Pi project
guide (available atwww.shopperdownload.
co.uk/pi/piguide.pdf), youcan find outhow
to control LEDs connected to the GPIO.
By customising this script,you canmake
an LED turn on whenthere’shighhumidity as
avisualwarning. Yo ucan even use the
WebHooksservice to connect your Pi to
IFTTT, say turning on asmartplug that
controls ahumidifier automatically or
sending awarning message to your phone.
Theimportant thing is thatwiththe
addition of extrasensors,the Pi can become
awareofits surroundings, letting it react to
real-life changes.

ABOVE:Our script gives you the current temperatureand humidity

LEFT:Youneed to enable the I2Cinterface to
get going with the SenseHAT

PI PROJECTS

Free download pdf