-
-
Save tsileo/2759444 to your computer and use it in GitHub Desktop.
Arduino and Python Temperature Logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
// pin setups | |
int latchPin = 8; | |
int clockPin = 12; | |
int dataPin = 11; | |
int tempPin = 7; | |
char recMsg = '0'; | |
// librraries for connecting to sensor | |
OneWire oneWire(tempPin); | |
DallasTemperature tempSens(&oneWire); | |
// characters for displaying on 7-seg display 0-9 | |
byte numberSet[10] = { | |
B01111011, B01000001, B00110111, B01100111, // 0,1,2,3 | |
B01001101, B01101110, B01111110, B01000011, // 4,5,6,7 | |
B01111111, B01101111 // 8,9 | |
}; | |
void setup() { | |
// init serial | |
Serial.begin(9600); | |
// init temp sensor | |
tempSens.begin(); | |
pinMode(latchPin, OUTPUT); | |
// set display to 00 | |
displayNumb(0, 0); | |
} | |
void loop() { | |
while(Serial.available() > 0) { | |
recMsg = Serial.read(); | |
} | |
if (recMsg!='0') { | |
// request temp from sensor | |
tempSens.requestTemperatures(); | |
float t = tempSens.getTempCByIndex(0); | |
// send temp to serial | |
Serial.print(t); | |
// cast temp to float (only have 2 digits to use on display) | |
int rT = (int)t; | |
// get units of temp | |
int units = rT % 10; | |
// get tens value of temp | |
rT = rT/10; | |
int tens = rT % 10; | |
// display temp | |
displayNumb(units, tens); | |
recMsg = '0'; | |
} | |
} | |
void displayNumb(int a, int b) { | |
// get the code for the numbers | |
byte bitsA = numberSet[a]; | |
byte bitsB = numberSet[b]; | |
// set ready to shift out | |
digitalWrite(latchPin, LOW); | |
// shift units | |
shiftOut(dataPin, clockPin, bitsA); | |
// shift tens | |
shiftOut(dataPin, clockPin, bitsB); | |
// shift out data | |
digitalWrite(latchPin, HIGH); | |
} | |
// shift the data out to the shift registors | |
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) { | |
int i=0; | |
int pinState; | |
// set pinmodes for shift | |
pinMode(myClockPin, OUTPUT); | |
pinMode(myDataPin, OUTPUT); | |
digitalWrite(myDataPin, 0); | |
digitalWrite(myClockPin, 0); | |
// iterate over each bit in the myDataOut byte | |
for (i=7; i>=0; i--) { | |
digitalWrite(myClockPin, 0); | |
if ( myDataOut & (1<<i) ) { | |
pinState= 1; | |
} | |
else { | |
pinState= 0; | |
} | |
digitalWrite(myDataPin, pinState); | |
digitalWrite(myClockPin, 1); | |
digitalWrite(myDataPin, 0); | |
} | |
digitalWrite(myClockPin, 0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/python | |
import serial | |
import threading | |
import time | |
from datetime import datetime | |
def temp_logger(ard): | |
# send msg to arduino | |
ard.write('T') | |
# wait a second for reply | |
time.sleep(1) | |
# read 5 bytes from arduino (string sent) | |
temp = ard.read(5) | |
print temp | |
# open file for writing data | |
f = open('pythontemp_logger.txt', 'a') | |
now_time = datetime.now() | |
str_now_time = now_time.strftime("%H:%M:%S") | |
f.write(str_now_time + ',' + temp + '\n') | |
f.close() | |
# start the timer again | |
global t | |
t = threading.Timer(20.0, temp_logger, [ard]) | |
t.start() | |
# entry point | |
if __name__ == '__main__': | |
print 'INIT: starting task' | |
# attempt to make serial connection to arduino | |
try: | |
arduino = serial.Serial('/dev/tty.usbserial-A600agDn', 9600) | |
except: | |
print 'ERROR: whilst opening serial port' | |
exit(0) | |
# wait 2 seconds for arduino to reset | |
time.sleep(3) | |
# start the initial timer | |
t = threading.Timer(20.0, temp_logger, [arduino]) | |
t.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment