Jump to content

Python Temperature Sensor using io16 and temp bricklet


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 zu diesem Kommentar
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.

Gast
Reply to this topic...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

×
×
  • Neu erstellen...