Balloon Collaboration
December 10, 2019, 09:42:44 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Hosting for barntek.net has been extended for another 3 years
 
   Home   Help Search gallery Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Temperature Sensors  (Read 1599 times)
Larry
Administrator
Hero Member
*****
Posts: 1167



View Profile
« on: March 21, 2014, 09:46:24 PM »

I have been playing with my temperature sensors.  I have one soldered to the expansion board mounted on the Pi.  I, also, have a second, external sensor connected which will measure outside temperature.  These are mounted on what will become the Master Pi, I hope.  This program is generating 2 files: temp.dat and ctemp.  Temp.dat is the data log of all the temperatures collected every 10 seconds.  Ctemp is a file which only stores the current temperatures from sensor 1 and sensor2, comma delimited and ended with a CRLF.  I thought that Danny could just read this file to obtain the current temperature reading when he is compiling the telemetry statement.  If you want me to formatted differently, let me know.

I am, also, using a 2x20 char LCD display to show the temps instead of printing to the screen.

FYI, the sensors measure in C to a degree, and I have to convert it into F, hence the odd steps in the readings.

Quote
#!/usr/bin/python

###  DS1621 Digital Thermometer and Thermostat
###  This program access the device via I2C
###        by Larry Barnes


import time
import smbus
import serial

port=serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)

bus=smbus.SMBus(1)

addr1=0x48
addr2=0x4A

crlf=chr(13)+chr(10)

SCP=chr(0xFE)+chr(0x45)  # Set Cursor Position   +chr(position value)
HOM=chr(0xFE)+chr(0x46)  # Home Cursor
CLS=chr(0xFE)+chr(0x51)  # Clear Display
    
def StartConvertT(address):
    bus.write_byte(address,0xEE)

def ReadTemperature(address):
    half=0,.5
    x=bus.read_word_data(address,0xAA)
    x1=x>>8 # Get the left Byte
    x2=x-(x1<<8) # Get the right Byte
    x3=bool(x1&128) # All I need is bit 7 for the half degree indicator
    x4=x2+half[x3]  # Temperature in Celcius
    return x4

def convertC2F(x):
    x1=x*9/5+32
    return x1

port.write(CLS)
port.write(SCP+chr(2)+"Temp1")
port.write(SCP+chr(12)+"Temp2")

# Start the Temperature Calculations
StartConvertT(addr1)
StartConvertT(addr2)

try:
    while True:
        temp1=ReadTemperature(addr1)
   temp2=ReadTemperature(addr2)
        f1=convertC2F(temp1)
   f2=convertC2F(temp2)
        temp1C='%.1f' % (temp1)
        temp1F='%.1f' % (f1)
   temp2C='%.1f' % (temp2)
   temp2F='%.1f' % (f2)        

        port.write(SCP+chr(42)+temp1F+" F ")
   port.write(SCP+chr(52)+temp2F+" F ")

   dt=time.asctime()
   with open('temp.dat','a') as d:
      d.write(dt+","+str(temp1F)+","+str(temp2F)+crlf)
   with open('ctemp','w') as d:   
      d.write(str(temp1F)+","+str(temp2F)+crlf)
   
   time.sleep(10)

except KeyboardInterrupt:
    pass
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!