sending arduino data to the IoT

Arduino Code

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.

Step 1: Some important principles

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.

Step 2: Code

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.

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__

#include <avr/power.h>


#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;

void setup()





void loop()


int litPixels = 0;

// turn off pixels

for (int i=NUMPIXELS-1;i>=0;i--)


pixels.setPixelColor(i, pixels.Color(0,0,0));




// 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++)


if (i==0)


pixels.setPixelColor(i, pixels.Color(0,0,150));


else if (i==1)


pixels.setPixelColor(i, pixels.Color(0,150,0));


else if (i==2)


pixels.setPixelColor(i, pixels.Color(150,0,0));





// 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.

The Unity Bridge

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.

Step 1: Installing the unity code

You can download the bridge code here:

After downloading

  • Unzip the file
  • Open unity
  • Open the IoT_API_Bridge project in unity

Step 2: Setting up the bridge

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:

  • In the hierarchy tab click on script_control
  • Then go to the Inspector tab
  • expand the section Webformpost and you will see five fields to complete
  • Add your details into the first three
  • Complete the fourth as Reading
  • Complete the fifth as Light Level


Step 3: Selecting the USB (Serial) Port

This needs to be done in the unity script itself.

  • click on the projects tab
  • click on assets
  • for Unity 5.3 find the script script_control and for 5.5 find the script webformpost and double click it to open it

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.

Step 4: Running

Here's how to get it going:

  • Make sure your arduino is plugged in and running
  • Check using the serial window in the Arduino IDE that you can see the output
  • Close the serial window in the Arduino IDE - this is important
  • Click on run (the play button at the top) in unity

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.