-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test_emotive.py
128 lines (107 loc) · 3.48 KB
/
main_test_emotive.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
123
124
125
126
127
128
import random
import time
from cortex2 import EmotivCortex2Client
from paho.mqtt import client as mqtt_client
broker = 'broker.emqx.io'
port = 1883
topic = "BCI"
client_id = f'python-mqtt-{random.randint(0, 1000)}'
# ============================================================================================
url = "wss://localhost:6868"
emotive = None
profile_name = "FakeBCI"
CLIENT_ID = "bhOr0ZmsLWZVDeFl0QHE08e05lDI36blefUxfbLX"
CLIENT_SECRET = "BL3OlfhwzzPMXcxgFYT7uxaB6LSEAlwdLfqU4OG2oRJkRbQg63h4KMpvNp3144pOsybUpNLDrPQXuDXRC18oyIQUxh6Jum9ktR3nTNwChLW1ZdCcpzcuaavycLD000wK"
# ===========================================
txt_rl1 = "1"
txt_rl2 = "2"
txt_rl3 = "3"
txt_rl4 = "4"
# =====================Arduino===============================
def connect_cortexAPI():
global emotive
emotive = EmotivCortex2Client(url,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
check_response=True,
authenticate=True,
debug=False)
def get_com_emotiv():
global emotive
emotive.request_access()
emotive.authenticate()
emotive.query_headsets()
emotive.connect_headset(0)
emotive.create_session(0)
emotive.load_profile(profile_name)
emotive.subscribe(streams=["com"])
emotive.mental_command_action_sensitivity("get", profile_name)
# print(emotive.create_record("FakeRecord"))
def connect_mqtt():
connect_cortexAPI()
get_com_emotiv()
# record()
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
# client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client
def processing_relay(data, client):
global txt_rl1
global txt_rl2
global txt_rl3
global txt_rl4
print(data)
data_com = data[0]
data_num = data[1]
if(data_num >= 0.85 and data_com == 'left'):
msg = f"[{txt_rl1}]"
print("SEND :", msg)
result = client.publish(topic, msg)
time.sleep(5)
elif(data_num >= 0.85 and data_com == 'right'):
msg = f"[{txt_rl2}]"
print("SEND :", msg)
result = client.publish(topic, msg)
time.sleep(5)
elif(data_num >= 0.85 and data_com == 'pull'):
msg = f"[{txt_rl3}]"
print("SEND :", msg)
result = client.publish(topic, msg)
time.sleep(5)
elif(data_num >= 0.85 and data_com == 'push'):
msg = f"[{txt_rl4}]"
print("SEND :", msg)
result = client.publish(topic, msg)
time.sleep(5)
def publish(client):
global emotive
msg_count = 0
result = ""
while True:
emotive.mental_command_action_sensitivity("get", profile_name)
data = emotive.receive_data()["com"]
processing_relay(data, client)
# msg = f"[{txt}]"
# status = result[0]
# if status == 0:
# print(f"Send : {msg} => ({topic})")
# else:
# print(f"Failed to send message to topic {topic}")
# msg_count += 1
def run():
client = connect_mqtt()
client.loop_start()
publish(client)
def catch_event(msg):
if(msg == "[user]"):
print(CortexAPI.get_user_login())
# elif(msg == "[]"):
# print()
if __name__ == '__main__':
run()