Updating ThingWorx Using an Arduino Uno and a Serial Connection

This tutorial is a follow-up to my previous tutorial, “Getting Started with the Arduino Uno and ThingWorx.” Connecting to ThingWorx with the Arduino is easy if you possess an Ethernet or WiFi Shield but you can still connect without one! Using the Processing language you can send data from your Arduino to a computer via a serial connection and push it to ThingWorx.

Requirements:

  • ThingWorx hosted instance
  • Arduino Uno
  • Processing Environment (found at Processing.org)
  • Arduino Environment

In this post:


Introduction to Processing:

Processing is the language that the Arduino language is built on. This means that the syntax is very similar. I would recommend visiting the tutorials and documentation that is provided on the Processing.org site as it is very informative and helpful.

Tutorial:

In order to push data to your ThingWorx thing refer to my previous tutorial, “Getting Started with the Arduino Uno and ThingWorx,” to get a Thing and Service set up. Next, copy and paste the example code that is included with this post. That’s really all you need to do!

More specifically, enter all of your pertinent server and thing information into your processing code. You should now be ready to start pushing data to ThingWorx. Fire up your Arduino and upload the example code to it. Next, push the “play” button at the top of your Processing Environment.

You may run into a few issues. One, if your Processing code throws an error about your serial port (it will say something about a “null exception”) you’ll need to determine which serial port your Arduino is at. The Processing code will print a list of available ports and default to the first one on the list. If your port is different then you can change it in the code:

You will also have an issue if you try and open your Serial Monitor in your Arduino Environment. This will cause your port to become unavailable.

You can either change the highlighted number to match the position that the port is listed or just change the highlighted variable portName to the actual name of the port:

The Processing code will print a list of available ports and default to the first one on the list. If your port is different then you can change it in the code:

You will also have an issue if you try and open your Serial Monitor in your Arduino Environment. This will cause your port to become unavailable.

Advantages and Disadvantages: Now that you know how to connect your Arduino to ThingWorx using three different methods, let’s discuss some possible advantages and disadvantages of each. In the case of the Ethernet Shield, you are able to move from one network to another without much issue. No new coding is needed. With the WiFi shield, you must recode your Arduino any time that you switch networks so you can enter the SSID and Security Code if needed. However, once the Arduino is configured correctly, you merely turn it on and it runs as a standalone device.

The serial connection allows you connect to the internet without the use of a shield. The cost of an Ethernet or WiFi shield is typically between $30 and 80$ depending on the manufacturer. A disadvantage is that you must be tethered to the Arduino with a computer if you want to send data.

The Processing language is very easy to learn and there are tutorials on the web that will instruct you on how to build user interfaces that you can control your Arduino with. This makes the serial connection an attractive option if you want some control over pins or variables on your Arduino!

And now for some code…


/***************************************************************************************************
ThingWorx Serial Example for Arduino Uno

This Arduino and Processing code allows you to connect to ThingWorx without the use of an Ethernet
or WiFi Shield.  The data is sent from the Arduino to the computer via the serial (USB) connection
to your computer.  All server and “Thing” data goes into the Processing code that is commented out
at the end of the Arduino Code. When you are running this code do NOT use the Serial Monitor in the
Arduino environment as it will interrupt the Serial connection to Processing.  The Processing language
can be found at Processing.org.

Created by Nick Milleson
Design Engineer
EAC Product Development Solutions
nmilleson@eacpds.com
***************************************************************************************************/

//Make sure that SENSORCOUNT matches SENSORCOUNT in the Processing code
#define SENSORCOUNT 4

double sensorValues[SENSORCOUNT];

//Denote the pins you will be using
int sensorPins[] = {A0, A1, A2, A3};

void setup() {
//begin Serial connection
Serial.begin(9600);

for (int idx = 0; idx < SENSORCOUNT; idx++)
{
pinMode(sensorPins[idx], INPUT);
}

}

void loop() {

//Read info from the pins
for (int idx = 0; idx < SENSORCOUNT; idx++)
{
sensorValues[idx] = analogRead(sensorPins[idx]);
}

//send out a serial message that tells your Processing code that you are beginning a transmission
Serial.println(“begin”);

for (int idx = 0; idx < SENSORCOUNT; idx++)
{
//Send data over the serial port
Serial.println(sensorValues[idx]);
}

delay(1000);
}

/***************************************************************************************************
ThingWorx Serial Example for Arduino Uno

This Arduino and Processing code allows you to connect to ThingWorx without the use of an Ethernet
or WiFi Shield.  The data is sent from the Arduino to the computer via the serial (USB) connection
to your computer.

Enter your Server, appKey, Thing, Service, and sensorNames to match up with the data being sent by
the Arduino.  Make sure that you do NOT use the Serial monitor in the Arduino Environment. It will
disrupt the connection.

For a tutorial on creating your Thing and Service, visit:
Getting Started with the Arduino Uno and ThingWorx

Getting Started with the Arduino Uno and ThingWorx

Created by Nick Milleson
Design Engineer
EAC Product Development Solutions
nmilleson@eacpds.com
***************************************************************************************************/
/*
import processing.net.*;
import processing.serial.*;

Client myClient;                     // Client object

Serial myPort;                       // The serial port

final int SENSORCOUNT = 4;           // This value must match SENSORCOUNT in your Arduino Code

String sensorValues[] = new String[SENSORCOUNT];
String junk;
String beginString = “begin”;
String myServer = “enter server here”;
String appKey = “enter appKey here”;
String thingName = “enter Thing here”;
String serviceName = “Enter Service here”;
String myURI = “POST /Thingworx/Things/” + thingName + “/Services/” + serviceName + “?appKey=” + appKey + “&method=post&x-thingworx-session=true<“;
String myHost = “Host: ” + myServer;
String myContent = “Content-type: text/html\n”;
String sensorNames[] = {
“valueOne”, “valueTwo”, “valueThree”, “valueFour”
};  //Enter your variable names (these must match the inputs in your Service)

void setup() {

// Print a list of the serial ports, for debugging purposes:
println(Serial.list());

String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);

myClient = new Client(this, myServer, 80);
}

int idx = SENSORCOUNT + 2;

void draw() {

if (myPort.available() > 0)
{
junk = null;
junk = myPort.readStringUntil(‘\n’);

// look for the initial “begin” string that Arduino sends
if (junk != null)
{
if (beginString.equals(trim(junk)))
{
junk = null;
idx=0;
}
}

//Read each sensor value
if ((junk != null) && (idx < SENSORCOUNT))
{
sensorValues[idx] = junk;
junk = null;
idx++;
}

//When all sensor values have been read, send the info to ThingWorx
if (idx == SENSORCOUNT)
{

myClient.write(myURI);

for (int index = 0; index < SENSORCOUNT; index++)
{
myClient.write(“&” + sensorNames[index] + “=” + trim(sensorValues[index]));
}
myClient.write(“> HTTP/1.1\n”);
myClient.write(myHost + ‘\n’);
myClient.write(myContent + ‘\n’);

println(“Sending this REST call:”);
print(myURI);
for (int index = 0; index < SENSORCOUNT; index++)
{
print(“&” + sensorNames[index] + “=” + trim(sensorValues[index]));
}
print(“> HTTP/1.1\n”);
print(myHost + ‘\n’);
print(myContent + ‘\n’);
print(‘\n’);

idx = SENSORCOUNT + 2;
}
}
}
*/