forked from rockangator/WheelGear
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
225 lines (187 loc) · 9.53 KB
/
Program.cs
File metadata and controls
225 lines (187 loc) · 9.53 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System.IO;
using System.Net.Sockets;
using Jayrock.Json.Conversion;
using System;
using System.Text;
using System.Collections;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace ConsoleApp1
{
class Program
{
//EEG data
public static StreamWriter eegdata = new StreamWriter(@"EEGData_" + string.Format("{0:yyyy-MM-dd_hh-mm-ss-fff}", DateTime.Now) + ".csv", true);
static void Main(string[] args)
{
eegdata.WriteLine("attention,meditation,delta,theta,lowAplha,highAlpha,lowBeta,highBeta,lowGamma,highGamma");
IDictionary eegPower;
IDictionary eSense;
TcpClient client;
Stream stream;
byte[] buffer = new byte[4096];
int bytesRead; // Building command to enable JSON output from ThinkGear Connector (TGC)
var com = @"{""enableRawOutput"": false, ""format"": ""Json""}";
byte[] myWriteBuffer = Encoding.ASCII.GetBytes(com);
try
{
Console.WriteLine("Starting connection to Mindwave Mobile Headset.");
client = new TcpClient("127.0.0.1", 13854);
stream = client.GetStream();
System.Threading.Thread.Sleep(500);
client.Close();
Console.WriteLine("Step 1 completed!!!");
}
catch (SocketException se)
{
Console.WriteLine("Error connecting to device."+se);
Console.ReadKey();
}
try
{
client = new TcpClient("127.0.0.1", 13854);
stream = client.GetStream();
Console.WriteLine("Sending configuration packet to device.");
if (stream.CanWrite)
stream.Write(myWriteBuffer, 0, myWriteBuffer.Length);
System.Threading.Thread.Sleep(500);
client.Close();
Console.WriteLine("Step 2 completed!!!");
}
catch (SocketException se)
{
Console.WriteLine("Error sending configuration packet to TGC."+se);
Console.ReadKey();
}
try
{
Console.WriteLine("Connecting to MQTT broker...");
//creating an instance with the ip of the broker. Default port is 1883.
MqttClient clientmq = new MqttClient("192.168.43.217");
//specifying mqtt version default is 3.1.1
clientmq.ProtocolVersion = MqttProtocolVersion.Version_3_1;
//connecting using username and password
byte code = clientmq.Connect(Guid.NewGuid().ToString(), "username", "qwertyuiop");
Console.WriteLine("Connected to MQTT broker!");
}
catch (Exception me)
{
Console.WriteLine("Error connecting with MQTT Broker" + me);
}
try
{
Console.WriteLine("Starting data collection.");
client = new TcpClient("127.0.0.1", 13854);
stream = client.GetStream();
// Sending configuration packet to TGC
if (stream.CanWrite)
stream.Write(myWriteBuffer, 0, myWriteBuffer.Length);
if (stream.CanRead)
{
//to check if device is ready
var ready = false;
var startRead = false;
//to note keyboard key press and note key press
Console.WriteLine("Enter any key to start.");
ConsoleKeyInfo key = Console.ReadKey(false);
Console.WriteLine("Reading bytes");
// This should really be in it's own thread
Console.CancelKeyPress += new ConsoleCancelEventHandler(saveData);
while (true)
{
bytesRead = stream.Read(buffer, 0, 4096);
string[] packets = Encoding.UTF8.GetString(buffer, 0, bytesRead).Split('\r');
foreach (string s in packets)
{
try
{
IDictionary data = (IDictionary)JsonConvert.Import(typeof(IDictionary), s);
//Check if device is ON/OFF
if (data.Contains("status"))
{
Console.WriteLine("Device is Off.");
ready = false;
break;
}
//Check fitting (device on head or not)
if (data.Contains("eSense"))
if (data["eSense"].ToString() == "{\"attention\":0,\"meditation\":0}")
{
Console.WriteLine("Check fitting.");
ready = false;
break;
}
//check if device is ready
if ((data.Contains("eSense")) && (ready == false))
{
IDictionary d = (IDictionary)data["eSense"];
if ((d["attention"].ToString() != "0") && (d["meditation"].ToString() != "0"))
{
ready = true;
Console.WriteLine("Device is ready. Press any key.");
// Console.WriteLine("Enter F for FORWARD, B for BACKWARD, L for LEFT, R for RIGHT, S for STOP and CTRL + C to close readings");
}
}
//start data reading only when device is ready.
if (ready)
{
if (Console.KeyAvailable == true)
{
startRead = true;
key = Console.ReadKey(true);
break;
}
//read data only when key press has been noted
if (startRead)
{
MqttClient clientmq = new MqttClient("192.168.43.217");
clientmq.ProtocolVersion = MqttProtocolVersion.Version_3_1;
byte code = clientmq.Connect(Guid.NewGuid().ToString(), "username", "qwertyuiop");
Console.WriteLine("EEG readings:");
Console.WriteLine(data);
eSense = (IDictionary)data["eSense"];
eegPower = (IDictionary)data["eegPower"];
string message = (eSense["attention"].ToString() + "," + eSense["meditation"].ToString() + "," + eegPower["" +
"delta"].ToString() + "," + eegPower["theta"].ToString() + "," + eegPower["lowAlpha"].ToString() + "," + eegPower["highAlpha"].ToString() + ","
+ eegPower["lowBeta"].ToString() + "," + eegPower["highBeta"].ToString() + "," + eegPower["lowGamma"].ToString() + "," +
eegPower["highGamma"].ToString());
eegdata.WriteLine(message);
ushort msgId = clientmq.Publish("wheelgear", Encoding.UTF8.GetBytes(message), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
break;
}
}
}
catch (Exception e)
{
}
}
}
}
System.Threading.Thread.Sleep(500);
client.Close();
}
catch (SocketException se)
{
Console.WriteLine("Error in data collection."+se);
Console.ReadKey();
}
}
public static void saveData(object sender, ConsoleCancelEventArgs args)
{
Console.WriteLine("Step 3 completed!!!");
try
{
Console.WriteLine("Saving data to csv file.");
eegdata.Flush();
eegdata.Close();
eegdata.Dispose();
Console.WriteLine("Step 4 completed!!! Enjoy!!!");
}
catch
{
Console.WriteLine("Error in data saving.");
}
}
}
}
//hk