Using Arduino with Lab of Things
Arduino is an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board. Arduino can be used to develop interactive objects, taking inputs from a variety of switches
or sensors, and controlling a variety of lights, motors, and other physical outputs.
You can use Arduino to develop custom devices to use with the Lab of Things. This guide walks you through the process of setting up an Arduino Uno, and introduces the driver code, scout code, and the Arduino sketch code (a sketch is the custom software you
create for Arduino). To keep things simple, HomeOS will communicate with the Arduino via a USB connection (your Hub will connect to the Arduino via a COM port). Instructions for using a Bluetooth serial connection are included at the end of the tutorial.
If you are new to Arduino, we recommend reading
Getting Started with Arduino to set up the Arduino board for the first time. Likewise, if you are new to Lab of Things, you should read
Getting Started with Lab of Things to learn how to set up the environment and run your hub.
Add the App, Scout and Driver to Core.sln
Like all other HomeOS devices, the Arduino requires a scout, driver, and app in order to function. These projects are included with the source code, but are not added to Core.sln by default. We will be working with the following projects:
Follow these steps to add the required projects to the Core.sln project.
- Open Visual Studio and open Core.sln.
- In Solution Explorer, right-click the Drivers folder, select
Add, then select Existing Project….
- Navigate to \Hub\Drivers\Arduino.MicrosoftResearch.Dummy.
- Select Arduino.MicrosoftResearch.Dummy.csproj, then click
- In Solution Explorer under the Drivers folder, right-click
Arduino.MicrosoftResearch.Dummy, then click Build.
- Right-click the Scout folder, select Add, then select
- Navigate to \Hub\Scouts\Arduino.
- Select Arduino.csproj, then click Open.
- In Solution Explorer under the Scout folder, right-click
Arduino, then click Build.
- In Solution Explorer, open Apps/Dummy, right-click
Dummy, then click Build.
- Build the entire solution (click Build Solution or press F6).
Load the Example Sketch
- Plug in the Arduino board using the USB cable.
- Open Device Manager and scroll down to Ports (COM & LPT). You should see an open port named "Arduino UNO (COMxx)". Make a note of the port name.
- Open the Arduino IDE.
- Click Tools, then Serial Port. Select the port to which the Arduino is connected.
- Click File, Open, and load the sketch:
- Click the Upload button to upload the sketch to the Arduino board.
For more details, see
Getting Started with Arduino on Windows.
Before running the platform to discover the Arduino board, you will need to update the scout with the name of the serial port in use by the Arduino.
- In Solution Explorer, open \Scout\Arduino\ArduinoScout.cs.
- Locate the following line and add the serial port name (in this example it is “COM6”):
Dictionary<string, SerialPort> serialPorts = new Dictionary<string, SerialPort>();
string serialPortofArduino = "COM6"; //need to tell driver which serial port this one is on
- In Solution Explorer under the Scout folder, right-click
Arduino, then click Build.
- Build the entire solution (click Build Solution or press F6).<!--EndFragment-->
Run the Platform
- Open the Command Prompt as Administrator.
- Navigate to \Hub\output\.
- Run startplatform.bat.
- Navigate to https://localhost:51430/GuiWeb.
- Click Add Devices, then click Scouts.
- Under Check which scouts to run, check
- Click OK.
- On the Add Devices page, select HomeOSArduinoDevice_Dummy_MicrosoftResearch_1234
(the device name is defined in
- Enter a name for your device.
- Under Install these applications, check AppDummy.
- Click Done.
- On the main Dashboard page, click AppDummy.
- On the Dummy Application page, click Update. You should see a series of messages sent from the Arduino in response to queries from the driver.
What is Happening Here?
The Arduino is following the same pattern that is used by all other HomeOS connected devices. First the scout is activated, and discovers the Arduino on its serial port (COM6 in this example). It then loads the driver, which has the job of communicating
between the device and the selected applications, in this case DummyApp. Let’s take a closer look at the code to see what is going on, and get a better understanding of the interaction between the Arduino sketch, the Driver, and DummyApp.
How the Sketch Works
The sketch HomeOSArduinoUnoExample.ino is intentionally very simple. It leverages the “blink” example which you have most likely already run, but it also adds some new functionality which enables it to communicate with the Hub via a serial port. The Blink
code is only there to let you know that the sketch is loaded and running properly. Besides, who doesn’t love a blinking LED?
Every Arduino sketch always has two special functions:
setup() and loop(). The
setup() function is called once when the sketch starts, and is commonly used for tasks such as setting up pin modes or initializing libraries. The
loop() function is the heart of most sketches, and is called repeatedly while the sketch is running. Both must always be present even if they are not used.
The job of the processCommandsFromLoT() function is to parse incoming commands. It is called from within
loop(). Whenever incoming data is detected on the serial port,
Serial.readBytesUntil() is called, and the bytes are stored in a buffer called
incomingData. The processCommandsFromLoT() function contains a switch statement that gets the first character of
incomingData to read the command and issue the appropriate response using
How the Scout Works
The scout’s job is to locate connected devices and make them available to the platform. When you run the platform, the scout does the following things:
- The scout scans available serial ports looking for the port that was specified in ArduinoScout.cs.
- If the port is found, the scout opens the port and issues the “[?]” command in an attempt to retrieve the device identification string (as defined in the Arduino sketch).
- If the returned string contains “HomeOSArduinoDevice”, the scout adds it to the device list.
- The scout hands over the device to the driver. You can now configure the device in the Home Hub.
How the Driver Works
The job of the driver module is to facilitate communication between the HomeOS Arduino device and the Home Hub. It performs the following tasks:
- Opens and registers the serial port to the device.
- Opens and registers a dummy port. This is a virtual port used to store information about the device’s functionality.
- Sends periodic requests to Arduino device and receives back values (in
- Sends values to the Dummy example application (in OnOperationInvoke()).
How the App Works
The Dummy app calls the driver which in turn pings the device and returns a response containing the device ID string and timestamp information.
Note: If you change anything in the Arduino sketch, you will need to reset the platform and re-add the Arduino device in order for the changes to be recognized.
Go Wireless with Bluetooth
Since Bluetooth is essentially serial over RF, you should be able to connect your Arduino using a Bluetooth module such as the
Sparkfun BlueSMiRF. If you are new to BlueSMiRF, follow the
tutorial to get set up. Once the device is paired with your Home Hub, it will appear as just another COM port. To get things up and running, you will need to do the following:
- Update the COM port that is specified in ArduinoScout.cs.
- Connect the BlueSMiRF TX pin to Arduino D0, and the BlueSmirf RX pin to Arduino D1.
- Using the Arduino IDE Serial Monitor, enter
command mode and set the BlueSMiRF to 9600 baud (the command to do this is SU,96). Reset the Arduino immediately. If you try to keep the default value of 57.6 you’ll get timeouts when the platform attempts to read from the port.
Notes: The tutorial advises connecting BlueSMiRF to Arduino D2 and D3 pins, which is fine for the tutorial but won’t work with Lab of Things. This is because using pins other than D0 and D1 requires the SoftwareSerial library. SoftwareSerial
lacks the ReadBytesUntil() function, which means that the demo sketch would need to be rewritten. Don’t worry about port contention, just be sure that the COM port for your wired connection is selected when you upload the sketch to the Arduino.