What's important with the Internet of Things is that the data gathered from the 'thing' (device or sensor) is collected so that it can be used for other applications. In this tutorial we will create an application that sends data through a Unity bridge to a web server so that the data can be stored and then retrieved. Our focus is on getting the data to a web server. We won't cover retrieval of data ... yet.
When sending data through to a web server, sometimes it's important to control how often that data is sent through. A lot depends on the server. For our server, given the number of people who might be sending data through at one time, we need to limit you to only send data through at intervals of 30 seconds or more. So that means adding delay(30000); in our code somewhere so the void loop() function will only run once every 30 seconds.
Here's the code for the arduino. There's only a few lines that need changing - we've coloured them red so you can see them more easily.
#define PIN 6
#define NUMPIXELS 3
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int sensorPin = A0;
int sensorValue = 0;
// we use this just for effect
int delayval = 100;
int litPixels = 0;
// turn off pixels
for (int i=NUMPIXELS-1;i>=0;i--)
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// determine how many pixels to turn on
if (sensorValue < 300)
litPixels = 1;
else if (sensorValue < 600)
litPixels = 2;
litPixels = 3;
// turn on pixels with different colours
for (int i=0;i<litPixels;i++)
else if (i==1)
else if (i==2)
// send reading to serial after converting to a string
String valuestr = String(sensorValue);
All the changes do is change the sensorValue to a string as this is what we need to send to our webserver.
To get our data into our webserver we need some way of connecting the arduino to the internet. We could use a wifi enabled arduino but sometimes it's not possible to connect them using EQ wifi. So we are trying an alternative method - using Unity code to manage the connection for us via a laptop that is already logged into the EQ wifi or network connection.
You can download the bridge code here:
After you have the project open in unity, you need to add in the details of your device. For the purposes of this workshop, we will give you these details. To do this:
This needs to be done in the unity script itself.
Inside the script find the line:
public static SerialPort sp = new SerialPort("/dev/cu.usbmodem44", 9600, Parity.None, 8, StopBits.One);
The bit that needs to change is "/dev/cu.usbmodem44" and this will depend on the USB port that your computer is using. The best way of finding this is at the bottom of your arduino IDE. It will say something like:
Arduino/Genuino Uno on /dev/cu.usbmodem1421
On windows it will be something like:
Arduino/Genuino Uno on COM6
This is what you need to put into the code. Be careful as this identifier may change each time you unplug your arduino, especially with some apple computers using Sierra.
Here's how to get it going:
Due to a quirk in unity you will need to keep unity in focus (i.e. your cursor in unity) for the bridge to keep functioning
Hopefully everything is now running. Jump into the viewing page to see your data.