(1) As an intro, I learned how to read serial data into Unity and how to adjust a game object in response. I also created a button to send an output.
(2) I learned to use Javascript, specifically Chart.js to read data from a serial port and live plot it using chart.js
First I followed Diego's tutorials on interfacing with Unity.
First I followed his instructtions for setting up the Serial Communication C# script. This script is what allows Unity to receive data incoming from a serial port. The first step for this was a little "hello world" showing that we get this script talking to the unity console.
This hellow world then appears in the unity project console! This is what will happen with the serial data.
This is what the overall project looks like, you can create folders for your images, other files, and your script.
Next I made a game object out of a transparent UFO button that I found via google images. I added text to this button with the though that this button could go in my eventual GUI that controls my final project's sensor readings.
This button is contrlled via the serial com script that Diego's tutorial introduced. In his code this script changes the size of a sphere in responce to a sensor's inputs.
After setting up this interface I felt comfortable setting up a serial com port to an interface, but I wanted to explore another avenue. As first I though I would use Unity and I found some exisiting projects to play around with:
Unity was alright, but I realized it was definitiely overkill for creating a data visulation interface for my final project.
All of the three.js examples in class were really beautiful, so I set about learned to make an interface with javascript.
I found a really helpful tutorial that really helped me set up Node.js and Chart.js. Here is the tutorial
Setting up Node.js and Chart.js:
1. Create new folder for project
2. Go to a directory where you want to create this app and create a new folder and navigate to that folder in terminal / command prompt / powershell. now type npm init to create a new Node.js app and fill up all the details.
3. Run “npm install express socket.io serialport --save” to install express, socket.IO and serialport libraries
4. Create index.js files
A. I pasted the following code from the example site. This creates an Express server, then connects us to the serial port.
var express = require('express');
var app = express();
var server = app.listen(4000, () => { //Start the server, listening on port 4000.
console.log("Listening to requests on port 4000...");
})
var io = require('socket.io')(server); //Bind socket.io to our express server.
app.use(express.static('public')); //Send index.html page on GET /
const SerialPort = require('serialport');
const Readline = SerialPort.parsers.Readline;
const port = new SerialPort('COM3'); //Connect serial port to port COM3. Because my Arduino Board is connected on port COM3. See yours on Arduino IDE -> Tools -> Port
const parser = port.pipe(new Readline({delimiter: '\r\n'})); //Read the line only when new line comes.
parser.on('data', (temp) => { //Read data
console.log(temp);
var today = new Date();
io.sockets.emit('temp', {date: today.getDate()+"-"+today.getMonth()+1+"-"+today.getFullYear(), time: (today.getHours())+":"+(today.getMinutes()), temp:temp}); //emit the datd i.e. {date, time, temp} to all the connected clients.
});
io.on('connection', (socket) => {
console.log("Someone connected."); //show a log as a new client connects.
})
B. My instance of serialport is COM18 for the USB connection to my board.
C. Then I set up a parser to read the data from the serial port. The delimiter is ‘/n’.
5. Create the files that will plot the data in a new public folder, I call mine index.html
A. Now I can take advantage of the wide range of available javascript libraries
B. We will use the Chart.js library, install it will the git bash command: “npm install chart.js --save”
For my interface, I start with an example that appeared like this:
Temperature Plot
Data
Date:
This is the cooresponding code:
Temperature Plot
Data
Date:
For my project I will be displaying multiple datasets at once, so I need to modify this to accomodate that. I added code to parse data that is seperated by commas:
In order to display multiple graphs I need a new class division:
I also need to define a new chart and a new ctx. After defining those I need to update the socket.on('temp', function(data) {} to update all the charts with new data.
After making all these changes, this was my final code:
Buoy Data
Buoy Data
Date:
This looks like this:
Buoy Data
Buoy Data
Date:
To run my code I need to simply open git bash in the project folder and run node index and then open index.html.