-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.py
118 lines (101 loc) · 3.53 KB
/
sensor.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#serial monitor must be closed - application listens on one port
import serial
import time
import pandas as pd #for parsing .csv file
import os
import datetime
arduino = serial.Serial('COM10', 9600) # create Serial port object called arduino with correct port and baudrate
time.sleep(2) # wait 2 sec for communication to establish
sensor1 = "Temperature sensor"
sensor2 = "Humidity sensor"
filename = ""
def start(sensor1, sensor2):
"""
Start menu: choose sensor and later choose action.
Takes two sensor names as the parameters.
"""
while True:
chooseSensor(sensor1, sensor2)
printMenu()
def chooseSensor(snsr1, snsr2):
"""
Sending trigger to arduino to choose sensor.
Filename is assigned depending on sensor choice.
"""
global filename
menuSensor = """
Welcome!
Choose the sensor:
1 %s
2 %s
3 Exit
""" % (snsr1,snsr2)
print (menuSensor)
userInput = input()
if(userInput=="1"): # choose temperature sensor
filename = "data_temperature.csv"
sensor = snsr1
arduino.write("1".encode('utf-8')) # encode the b'\n'
elif (userInput=="2"): # choose humidity sensor
filename = "data_humidity.csv"
sensor = snsr2
arduino.write("2".encode('utf-8')) # encode the b'\n'
elif (userInput=="3"):
exit()
else:
print("\nWrong user input\n")
return chooseSensor(snsr1, snsr2)
print("You chose %s." % (sensor))
def printMenu():
"""
Choose action(start and stop logging, transmit data) and send trigger to arduino.
"""
menu = """
Press:
1 Start the logging process
2 Change sensor
3 Exit
"""
print (menu)
userInput = input()
if(userInput=="1"):
print ("Started logging...\nPress any key to stop logging")
startLogging()
os.system('pause')
stopLogging()
print ("Stopped logging...\nPress any key to transmit data and save it in a file")
os.system('pause')
transmitData()
writeToFile()
print ("Transmitted and saved data...")
elif (userInput=="2"):
return
elif (userInput=="3"):
exit()
else:
print("\nWrong user input\n")
return printMenu()
def startLogging():
"""Send trigger to arduino to start logging."""
arduino.write("3".encode('utf-8')) # encode the b'\n'
def stopLogging():
"""Send trigger to arduino to stop logging."""
arduino.write("4".encode('utf-8')) # encode the b'\n'
def transmitData():
"""Send trigger to arduino to send back data."""
arduino.write("5".encode('utf-8')) # encode the b'\n'
def writeToFile():
"""
Read data from arduino and save it in a chosen .csv file.
File name depends on chosen sensor.
"""
global filename
msg = arduino.readline()
decodedLine = msg.decode('utf-8')
print (decodedLine) # decode the b'\n'
timestamp = datetime.datetime.now()
file = open(filename, "a+") #open a file for writing and create if it doesn't exist
file.write(str(timestamp) + "," + decodedLine) #write data to the file
file.close() #close the file when done
print("\t*******************************\n\tHumidity and temperature sensor\n\t*******************************\n")
start(sensor1,sensor2)