-
Notifications
You must be signed in to change notification settings - Fork 2
/
HC05_bluetooth_read.py
86 lines (74 loc) · 2.72 KB
/
HC05_bluetooth_read.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import serial
import numpy as np
import time
ser = serial.Serial('/dev/rfcomm0', 9600)
def saveReading(temperature):
newReading = time.strftime("%Y-%m-%d %H:%M:%S") + \
',' + str(temperature) + '\n'
print('Saving new reading: ' + newReading)
with open('/home/pi/Desktop/temperatureReadings.csv', 'ab') as file:
file.write(newReading.encode('utf-8'))
'''
def testSerial(someString):
start = False
temp = []
for i in range(0, len(someString)):
character = someString[i]
asciiOrd = ord(character)
#If it is a start sequence and we have already started,
#start over.
if (asciiOrd == 60 and start == True):
temp = []
#If it is a start sequence and we have not started,
#start now
elif (asciiOrd == 60 and start == False):
start = True
#If it is not a start or a stop, and we have started,
#simply append.
elif (asciiOrd != 60 and asciiOrd !=62 and start == True):
temp.append(character)
#If it is an end character, and we have started then we are done.
elif (asciiOrd == 62 and start == True):
#If there is something there, and it is a proper float
if len(temp) > 0:
try:
converted = float(''.join(temp))
saveReading(converted)
except Exception as e:
print(e)
start = False
temp = []
'''
print("Waiting for data...")
temp = []
start = False
while (True):
#Read one byte at a time
if (ser.inWaiting() > 0):
character = ser.read()
asciiOrd = ord(character)
#If it is a start sequence and we have already started,
#start over.
if (asciiOrd == 60 and start == True):
temp = []
#If it is a start sequence and we have not started,
#start now
elif (asciiOrd == 60 and start == False):
start = True
#If it is not a start or a stop, and we have started,
#simply append.
elif (asciiOrd != 60 and asciiOrd !=62 and start == True):
temp.append(character.decode('ascii'))
#If it is an end character, and we have started then we are done.
elif (asciiOrd == 62 and start == True):
#If there is something there, and it is a proper float
if len(temp) > 0:
try:
converted = float(''.join(temp))
saveReading(converted)
#Acknowledge receipt of data
ser.write('<5>'.encode('utf-8'))
except Exception as e:
print(e)
start = False
temp = []