/
sensor-arduino.py
executable file
·122 lines (90 loc) · 3.45 KB
/
sensor-arduino.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
119
120
121
122
#!/usr/bin/python
# -*- coding: utf-8 -*-
# read pressure sensors connected to arduino; senossor used - smartec SPD102DAhyb
# kui /dev/ttyACM0 ei kao lsof | grep /dev/ttyACM ja kill vastavaed protsessid
# vt. ka ls /var/lock
from PyQt5.Qt import *
import PyQt5.QtCore
import sys
from pyfirmata import ArduinoMega, util
import threading, time, socket
from websocket import create_connection # you need websocket-client module installed
class FirmataThread(threading.Thread):
def __init__(self,widget):
super(FirmataThread, self).__init__()
self.board = ArduinoMega('/dev/ttyACM0')
self.it = util.Iterator(self.board)
self.it.start()
self.board.analog[0].enable_reporting()
self.board.analog[1].enable_reporting()
self.board.analog[2].enable_reporting()
self.board.analog[3].enable_reporting()
self.board.analog[4].enable_reporting()
self.board.analog[5].enable_reporting()
self.stopNow = False
self.ws = 0
self.widget = widget
print "ArduinoMEGA ready to operate"
#self.connectWS()
#self.max1 = 0.2
#self.min1 = 0.1
#self.max2 = 0.2
#self.min2 = 0.1
def connectWS(self):
try:
self.ws = create_connection("ws://192.168.1.220:33033/ws")
print "Websocket created"
except:
print "Could not create connection"
self.ws = 0
def run(self):
oldValue = [-10,-10,-10,-10,-10,-10]
while ( not self.stopNow) :
for sensor in range(6):
value = self.board.analog[sensor].read()
if (value==None):
print "Coulde not read value form sensor ",sensor
return
value = (float(value) - 0.5) * 2 # - 0.5)*2 # to -1..1; for sensor 0.5 is neutral neutral now inhaling positive, exhaling negative
value = int(value *100) / 100.0 # leave just 2 decimals
#TODO: ümarda nii, et suurus oleks 1 komakoht (-10..10)? , saada ws ainult, kui on muutunud
#print value, float(value), v1
if (value!=oldValue[sensor] ):
oldValue[sensor] = value
valueLabel[sensor].setText(str(value))
if (self.ws!=0):
self.ws.send("blower,"+str(sensor+1)+ "," + str(value) )
time.sleep(0.25)
def stop(self):
self.stopNow = True
self.board.exit()
if (self.ws!=0):
self.ws.close
#main
app = QApplication(sys.argv)
window = QWidget() # window as main widget
layout = QGridLayout(window) # use gridLayout - the most flexible one - to place the widgets in a table-like structure
window.setLayout(layout)
window.setWindowTitle("Firmata from arduino")
arduino = FirmataThread(window)
arduino.connectWS()
layout.addWidget(QLabel("Sensor 1: "),0,0) # first row, first column
layout.addWidget(QLabel("Sensor 2: "),1,0) # second row, first column
layout.addWidget(QLabel("Sensor 3: "),2,0) # first row, first column
layout.addWidget(QLabel("Sensor 4: "),3,0) # second row, first column
layout.addWidget(QLabel("Sensor 5: "),4,0) # first row, first column
layout.addWidget(QLabel("Sensor 6: "),5,0) # second row, first column
valueLabel = [None] * 6
for i in range(6):
valueLabel[i] = QLabel("?")
layout.addWidget(valueLabel[i],i,1)
stopButton = QPushButton("Stop")
stopButton.clicked.connect(arduino.stop)
layout.addWidget(stopButton,6,1)
connectButton = QPushButton("Connect")
connectButton.clicked.connect(arduino.connectWS)
layout.addWidget(connectButton,6,0)
window.show()
arduino.start()
#arduino.join()
sys.exit(app.exec_())