CharlyDelta
Posts: 164
Joined: Thu Jul 18, 2013 4:04 am
Location: Montreal

time.sleep

Fri Mar 06, 2020 10:02 pm

Hello;

The goal is to display the ambient and object temperature with an infra-red probe every 4 seconds... but the script displays only once. However, I have other different probes that work very well in a loop with time.sleep
Thanks for pointing me in the right direction.

Code: Select all

import smbus
import time
class MLX90614():

    MLX90614_RAWIR1=0x04
    MLX90614_RAWIR2=0x05
    MLX90614_TA=0x06
    MLX90614_TOBJ1=0x07
    MLX90614_TOBJ2=0x08

    MLX90614_TOMAX=0x20
    MLX90614_TOMIN=0x21
    MLX90614_PWMCTRL=0x22
    MLX90614_TARANGE=0x23
    MLX90614_EMISS=0x24
    MLX90614_CONFIG=0x25
    MLX90614_ADDR=0x0E
    MLX90614_ID1=0x3C
    MLX90614_ID2=0x3D
    MLX90614_ID3=0x3E
    MLX90614_ID4=0x3F

    def __init__(self, address=0x5a, bus_num=1):
        self.bus_num = bus_num
        self.address = address
        self.bus = smbus.SMBus(bus=bus_num)

    def read_reg(self, reg_addr):
        return self.bus.read_word_data(self.address, reg_addr)

    def data_to_temp(self, data):
        temp = (data*0.02) - 273.15
        return temp

    def get_amb_temp(self):
        data = self.read_reg(self.MLX90614_TA)
        return self.data_to_temp(data)

    def get_obj_temp(self):
        data = self.read_reg(self.MLX90614_TOBJ1)
        return self.data_to_temp(data)

if __name__ == "__main__":
    sensor = MLX90614()
    print(sensor.get_amb_temp())
    print(sensor.get_obj_temp())

    annotate = open("/dev/shm/mjpeg/user_annotate.txt", 'w')
   # annotate.write ('temp = {}'.format((data*0.02) - 273.15))
    annotate.write ('Obj = {}'.format (sensor.get_obj_temp()))
    annotate.close()
    annotate = open("/dev/shm/mjpeg/user_annotate.txt", 'w')
    annotate.write ('Amb = {}'.format (sensor.get_amb_temp()))
    annotate.close()
    time.sleep(4)

User avatar
B.Goode
Posts: 10191
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: time.sleep

Fri Mar 06, 2020 10:09 pm

This script doesn't have a loop.

It runs once, waits as requested, then exits.

Garvan
Posts: 41
Joined: Sun Jan 05, 2020 9:59 am

Re: time.sleep

Mon Mar 09, 2020 2:53 pm

In your code you open a file, write to it, close it and then open it again. This will not work. Everything you wrote to the file the first time is deleted when you open it for writing a second time. And as mentioned above, you have no loop.

Study this simple program. It opens a file, writes two lines to it each loop, and then exits after ten loops. Finally it closes the file. Look up each command in google - for example https://wiki.python.org/moin/WhileLoop to see what it does.

Code: Select all

import time

annotate = open("user_annotate.txt", 'w')
i=0
while True:
	i+=1
	annotate.write ('Obj = {}'+ str(i)+'\n')
	annotate.write ('Amb = {}'+ str(i)+'\n')
	time.sleep(1)
	if i == 10:
		break
   
annotate.close()
Ask questions if there is something you don't understand.

User avatar
scruss
Posts: 3155
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: time.sleep

Mon Mar 09, 2020 4:15 pm

There is a library that would make your sensor reading much tidier: PyMLX90614
That way, you could focus on getting your code right rather than sensor configuration
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

CharlyDelta
Posts: 164
Joined: Thu Jul 18, 2013 4:04 am
Location: Montreal

Re: time.sleep

Tue Mar 10, 2020 9:00 pm

Thanks for your help. I'll work on it.

Return to “Python”