This project is read-only.

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.

Prerequisites

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:

  • \Hub\Drivers\Arduino.MicrosoftResearch.Dummy
  • \Hub\Scouts\Arduino
  • \Hub\Apps\Dummy

Follow these steps to add the required projects to the Core.sln project.

  1. Open Visual Studio and open Core.sln.
    \Hub\Core.sln
  2. In Solution Explorer, right-click the Drivers folder, select Add, then select Existing Project….
  3. Navigate to \Hub\Drivers\Arduino.MicrosoftResearch.Dummy.
  4. Select Arduino.MicrosoftResearch.Dummy.csproj, then click Open.
  5. In Solution Explorer under the Drivers folder, right-click Arduino.MicrosoftResearch.Dummy, then click Build.
  6. Right-click the Scout folder, select Add, then select Existing Project….
  7. Navigate to \Hub\Scouts\Arduino.
  8. Select Arduino.csproj, then click Open.
  9. In Solution Explorer under the Scout folder, right-click Arduino, then click Build.
  10. In Solution Explorer, open Apps/Dummy, right-click Dummy, then click Build.
  11. Build the entire solution (click Build Solution or press F6).

Load the Example Sketch

  1. Plug in the Arduino board using the USB cable.
  2. 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.
  3. Open the Arduino IDE.
  4. Click Tools, then Serial Port. Select the port to which the Arduino is connected.
  5. Click File, Open, and load the sketch:
    \Arduino\HomeOSArduinoUnoExample\HomeOSArduinoUnoExample.ino
  6. Click the Upload button to upload the sketch to the Arduino board.

For more details, see Getting Started with Arduino on Windows.

Update ArduinoScout.cs

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.

  1. In Solution Explorer, open \Scout\Arduino\ArduinoScout.cs.
  2. 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
  3. In Solution Explorer under the Scout folder, right-click Arduino, then click Build.
  4. Build the entire solution (click Build Solution or press F6).<!--EndFragment-->

Run the Platform

  1. Open the Command Prompt as Administrator.
  2. Navigate to \Hub\output\.
  3. Run startplatform.bat.
  4. Navigate to https://localhost:51430/GuiWeb.
  5. Click Add Devices, then click Scouts.
  6. Under Check which scouts to run, check HomeOS.Hub.Scouts.Arduino.
  7. Click OK.
  8. On the Add Devices page, select HomeOSArduinoDevice_Dummy_MicrosoftResearch_1234 (the device name is defined in \Arduino\HomeOSArduinoUnoExample\HomeOSArduinoUnoExample.ino ).
  9. Enter a name for your device.
  10. Under Install these applications, check AppDummy.
  11. Click Done.
  12. On the main Dashboard page, click AppDummy.
  13. 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 Serial.print.

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:

  1. The scout scans available serial ports looking for the port that was specified in ArduinoScout.cs.
  2. 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).
  3. If the returned string contains “HomeOSArduinoDevice”, the scout adds it to the device list.
  4. 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:

  1. Opens and registers the serial port to the device.
  2. Opens and registers a dummy port. This is a virtual port used to store information about the device’s functionality.
  3. Sends periodic requests to Arduino device and receives back values  (in Work()).
  4. 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.

Last edited Aug 25, 2014 at 9:57 PM by willum070, version 3

Comments

No comments yet.