# Introducing intervals into sensor readings

#1

Hi, Im looking to alter or add to the Temperature and Humidity(from the Home Weather Display) script so that it gives values every so often rather than every second as it does now - it reads continuously. How can that be done?
My aim is to set up a remote weather station that senses and saves values every 1/2 hr…can that be done?

#2

Its just a loop printing readings. The loop could be modified to run every 30 minutes easily.

What you need is a simple if else inserted into the the loop. pseudo code:
`while True: If time == 30min: Take readings Else: pass`
I’m not on my laptop now, so I cannot run python and cannot figure out how to check time. However python is nicely documented.
https://docs.python.org/3.5/library/datetime.html#datetime.time.minute

https://docs.python.org/3.5/library/time.html

Let me know how it works! Have fun!!

#4

Hi @aplsaws,

As @graykevinb has said you can use that algorithm. I am pasting the code to make it a little easy. This code will write the Temperature and humidity values to a file called Temperature.txt on Desktop for every 30 Minutes and also keep displaying the Temperature value on the LCD.

``````from grovepi import *
from grove_rgb_lcd import *
import time
start=time.time()
start=start//60

dht_sensor_port = 7             # Connect the DHt sensor to port 7
dht_sensor_type = 0             # change this depending on your sensor type - see header comment

while True:
try:
[ temp,hum ] = dht(dht_sensor_port,dht_sensor_type)             #Get the temperature and Humidity from the DHT sensor
#print("temp =", temp, "C\thumidity =", hum,"%")
t = str(temp)
h = str(hum)
# To check for 30 mins
if ((time.time()//60)-start) == 30:
#To write to a file
f=open("Temperature.txt","a+")
f.write("temp =%fC\thumidity =%f\n"%(temp,hum))
f.close
start=time.time()
start=start//60

setRGB(0,128,64)
setRGB(0,255,0)
setText("Temp:" + t + "C      " + "Humidity :" + h + "%")
except (IOError,TypeError) as e:
print("Error")
``````

Please let us know if this helps,
-Shoban

#5

Shoban(not to exclude graykevinb) I copied the different code into the my existing file and tried to run it…i get a invalid syntax error and the cursor flashes on a quote mark in this line: f.write(“temp =%fC\thumidity =%f\n”%(temp,hum)), actually the second quote mark in the string…
I have no idea what to look for!..

here is the entire file contents and bear in mind I am using the DHT22 sensor:

from grovepi import *

from grove_rgb_lcd import *

import time

start=time.time()

start=start//60

dht_sensor_port = 7 # Connect the DHt sensor to port 7
dht_sensor_type = 1 # change this depending on your sensor type - see header comment

while True:

try:
[ temp,hum ] = dht(dht_sensor_port,dht_sensor_type) #Get the temperature and Humidity from the DHT sensor

print(“temp =”, temp, “C\thumidity =”, hum,"%")

t = str(temp)

h = str(hum)

#To check for 30 mins

if ((time.time()//60)-start) ==30:

#To write to a file

f=open(“Temperature.txt”,“a+”)

f.write_“temp =%fC\thumidity =%f\n”%(temp,hum))

f.close

start=time.time()

start=start//60

setRGB(0,128,64)

setRGB(0,255,0)

setText(“Temp:” + t + "C " + “Humidity :” + h + “%”)

except (IOError,TypeError) as e:

print(“Error”)

#6

Also its not formatted as it is above…Im moving files from a windows machine to a raspberry pi and back -the formatting seems to get lost somewhere along the way!

#7

That’s probably it. I’m no python syntax expert, but I didn’t see anything wrong. Correct me if I’m wrong @Shoban.
Are you just copying and pasting from the forums? That messes stuff up. I hate to say this but manually typing it in may be the only option.

#8

Hi @aplsaws,

What you should try doing is,to copy this example and add the lines that are missing from the code that I have pasted. As @graykevinb said it might mess up if you directly try copy pasting from the post here.

Let us know if that helps,
-Shoban

#9

Ok, sorry for the misunderstanding…the misformat came from me copying
from the python file on the pi TO the windows laptop…i made sure to add
your code to the existing file in the format that existed there…if that
makes any sense?
Aside from snapping a pic and posting that, i dont know how to transfer the
file i have to the forum without it getting screwed up…

#10

Hi @aplsaws,

Its okay, its definitely not a problem, Yes make sure to copy the existing code by typing something like this

`sudo cp Home_Weather_Display.py Home_Weather_Display1.py` and make the changes inside Home_Weather_Display1.py. Let us know how it goes. If you have get any error , post a pic of how the code looks on your Pi and post the error. We will help you out.

-Shoban

#11

Im sure if you will be able to make this out, but here are some photos of
the file:

#12

Error number one: f.write_"temp =%fc\thumdity you get the poi t which line!
Is missing parentheses. So it should start like this: f.write("temp …
The underscore is not needed.

I don’t “think” there’s any more bugs, but can’t hurt trying it again.

#13

Hi @aplsaws,

Yes the error is because of the typo that @graykevinb has pointed. Sorry for the delay.

-Shoban

#14

Hi again and thanks for the help.
I did change the lack of parenthesis and now get errors regarding
indentation…i cant post them because of formatting loss in transferring
the data as it is on the pi to a windows machine - it gets buggered up in
the transfer.
I tried posting pics to the forum and was denied due to the posts being too
small in text and then too large!..
Im stymied at this point because i cant show you the python file as it is
with the errors.
Any tips…simple cut and paste to a pastebin?..
Pete

#15

Hi @aplsaws.

I have posted the python code here, make sure your Pi is connected to internet and do the following.

• Move to /home/pi/Desktop/GrovePi/Projects/Home_Weather_Display
`cd /home/pi/Desktop/GrovePi/Projects/Home_Weather_Display/`
`wget https://raw.githubusercontent.com/Shoban94/GrovePi/patch-1/Projects/Home_Weather_Display/Temp_Hum_intr.py`
• Run the code by typing
`sudo python Temp_Hum_intr.py`

Please let us know if this helps,
-Shoban

#16

Hi , just want to say thanks for your help.

Yes this works! After an initial glitch of not realizing the sensor type
was incorrect, i straightened that out and am running it now. I’ll let it
go for a few hrs and let you know the end results.
Im a bit puzzled about what was wrong with the first attempt and what
caused the errors - indentation and other, so im still going to get that
code posted.
Thanks again!
Pete

#17

Hi @aplsaws.

Glad that it works for you now, let us know how it goes.

-Shoban