简体   繁体   中英

Take temperature readings every 10 minutes from a Raspberry Pi

I have a Raspberry Pi with temperature sensors. I wrote Python code that measure the temperature and sends this into my database. I want to send measurements every 10 minutes.

My code is currently:

#!/usr/bin/python

import Adafruit_CharLCD as LCD
import math
import os
import RPi.GPIO as GPIO
import spidev
import string
import time
import urllib2,urllib3,urllib

# ....

timestamp = int(time.time())
print timestamp     

# <Temperature measurement here>

Do I have to do something like this?

if ($timestamp < (time() - 600)): # if test ok, send measures.
    url = 'database address'
    user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
    param = {'Timestamp' : timestamp,
              'Te1' : temperatures[1],
               Te2
               Te3...

How can I make this test procedure?

You can do the wait loop as follows:

import time

...
timestamp = int(time.time())
while True: 
   time.sleep(10) # sleep 10 sec
   if int(time.time()-timestamp) > 10*60*1000:
      saveToDatabase()
      timestamp =  int(time.time())

The infinite loop checks every 10 seconds if 10 minutes (10*60*1000 milliseconds) is exceeded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM