Jump to content

Python Temperature Sensor using io16 and temp bricklet


Atak

Recommended Posts

Temperature Sensor

 

Using the io16 and temperature bricklet along with with a master brick from Tinkerforge, I have created a simple temperature sensor project which outputs its' readings to LEDs on a breadboard. The red LED lights up during warmer temperatures and the yellow LED lights up during cooler temperatures.

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-  
# Temperature Sensor using IO16 and Temperature bricklets

import os

HOST = "localhost"
PORT = 4223
UID = "xXx" # Change to your UID for IO16 bricklet
UID2 = "xXx" # Change to your UID for Temperature bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_io16 import IO16
from tinkerforge.bricklet_temperature import Temperature

ipcon = IPConnection(HOST, PORT) # Create IP connection to brickd
io = IO16(UID) # Create device object
ipcon.add_device(io)

def cb_temperature(temperature):
    running = True
    while running:
        temp = str(temperature/100.0)
        if str(2650) < temp:
            io.set_port_configuration('b', 1 << 0, 'o', False) # Set pin 0 on port b to output low
            io.set_port_configuration('b', 1 << 7, 'o', True) # Set pin 7 on port b to output high
            os.system('clear') # use 'clr' with Windows
            print 'Hot'  # display status
            print (str(temperature/100.0) + ' °C') # get reading
            return
        elif str(2650) > temp:
            io.set_port_configuration('b', 1 << 0, 'o', True) # Set pin 0 on port b to output high
            io.set_port_configuration('b', 1 << 7, 'o', False) # Set pin 7 on port b to output low
            os.system('clear')
            print 'Cold' # display status
            print (str(temperature/100.0) + ' °C') # get reading
            return
        else:
		print 'Reading'
		return

if __name__ == "__main__":

    t = Temperature(UID2) # Create device object
    ipcon.add_device(t) # Add device to IP connection
    # Don't use device before it is added to a connection

    # Set Period for temperature callback to 1s (1000ms)
    # Note: The callback is only called every second if the 
    #       temperature has changed since the last call!
    t.set_temperature_callback_period(1000)

    # Register temperature callback to function cb_temperature
    t.register_callback(t.CALLBACK_TEMPERATURE, cb_temperature)

    raw_input('Press key to exit\n') # Use input() in Python 3
    ipcon.destroy()

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...