There’s a lot of buzz these days about leveraging the internet and the ‘cloud’ for business — especially in the realms of product development. Phrases like Internet of Things (IoT), Machine 2 Machine, Product as a Service, Internet of Everything, Cloud Things, Connected Products and others get thrown around daily. Regardless of the term you use, the real intent of any of these IoT projects is to generate some additional value. That could be from more capability, deeper customer interaction, predictive maintenance, new revenue streams, market share, knowledge of how the product is being used or many other scenarios. I thought it would be fun to leverage it for gardening!

This time of year, there are a lot of northern gardeners are doing a dance with garden plants they’ve started from seed while winter was still rolling along. Now the seedlings want to be outside in the sun, but its too cold at night. The plants are getting crowded and too big to park in a window in the living room. Build a plastic tent outside as a make-shift greenhouse and it’s too hot during the day. So you’re left with shuffling the plants inside and outside to follow the spring sun and avoid a late frost. But how do you balance this while keeping your day job?

Our Makeshift IoT Greenhouse

Enter the IoT and a little project in our parking lot… With a little creativity, some parts we had around and a few extra parts from the thrift store, we built an ‘almost’ free greenhouse we can monitor through any web-browser. Our interest at first is simply to monitor the temperature, relative humidity and light level both inside and outside the greenhouse. It’s fun to see the values ebb and flow with the day and with the weather. This was achieved through an Ethernet connected Arduino wired to a photoresistor and a pair of DHT11 Digital Humidity and Temperature sensors. Currently the system feeds into ThingSpeak for graphical representation with plans to port it over to ThingWorx in upcoming days.

GreenhouseDashboard-150417

Since the sun is hot this time of year but the cold weather isn’t over, we’ll likely experience some high temperature spikes and some cold nights that could damage the tomatoes. This brings up phase 2 — ‘control.’ Our near term plans are to have powered ventilation and heating to kick in either by reaching through the internet or with the internal controller. Eventually we’ll get to a fully autonomous greenhouse that manage temperature, have sensors in the soil monitoring moisture and turning on/off soaker hoses and misting fans all the while keeping us informed on a smart phone. Those are going to be happy tomatoes with their own twitter account…

Maybe all this tech and automation will take some of the fun out of gardening and fiddling with the plants to get it ‘just right’. What’s more likely is that it will just encourage the acquisition of more plants and the need for a bigger garden and a bigger greenhouse with more automation — but that would be fun too…

EAC Product Development Solutions is a Minnesota based company providing engineering and product development software, service and consulting to the discrete manufacturing industry. With 20 years of product development experience, EAC is seeking to help companies that desire to get more from their products through the IoT. We engage in all aspects of product strategy, product design and enabling technology to achieve those goals. Please contact Rob at rmiller@eacpds.com to find out more.

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
https://www.eacpds.com/product-development-system-blog/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/htmln”;
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.1n”);
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.1n”);
print(myHost + ‘n’);
print(myContent + ‘n’);
print(‘n’);

idx = SENSORCOUNT + 2;
}
}
}
*/

This is a brief tutorial that will show you how to connect your Arduino Uno to ThingWorx and upload values to a ThingWorx “Thing.” First you will learn how to set up a ThingShape, a Thing, a Service within your ThingShape, and an Application Key. Next you will learn how to program your Arduino Uno to start sending data to your ThingWorx Things! Two simple ThingWorx libraries along with example codes are included with this tutorial. They will allow you to start pushing data in minutes!

Requirements:

  • ThingWorx hosted instance
  • Arduino Uno
  • Ethernet or WiFi Shield with corresponding libraries
  • Arduino Environment

In this post:

Tutorial 1: Setting up your ThingWorx Thing

In order to start pushing data to ThingWorx, we must first define our ThingShape, service, Thing, and Application Key.


1: Create an Application Key

An application key is the authentication that you use to send or receive data from your Thing. It is associated with a ‘tag’ that is common to all of the components of your thing. To create the key first get to the home page of your ThingWorx Composer.

1.1:   Under the “Security” heading on the left click on “Application Keys.”

1: Create an Application Key

1.2:   Next, click on the “New” button towards the top of the screen. Fill in the required fields

Next, click on the “New” button towards the top of the screen and fill in the required fields

1.3:   Now, create a tag by clicking on the wand next to tags. Either choose an existing tag or create a new one. The tag ties your application key to all the components of your Thing.

Next, create a tag by clicking on the wand next to tags

1.4:   After selecting a tag click Save at the top and you will see that an Application Key has populated the “keyId” field.

Save your tag


2: Create a ThingShape and Service

2.1:   From the ThingWorx Composer home page select Thing Shapes on the left side of the page under the “Modeling” heading.

From the ThingWorx Composer home page select Thing Shapes on the left side of the page under the “Modeling” heading.

2.2:   Next select “New” at the top of the screen and fill out the required fields.

Next select “New” at the top of the screen and fill out the required fields.

2.3:   Make sure your tag matches the tag you used when creating your Application Key. On the left side of the screen select “Properties.”

Make sure your tag matches the tag you used when creating your Application Key. On the left side of the screen select “Properties.”

2.4:   Select “Add My Property” to start adding Properties to your ThingShape. These are the values that you will be pushing to the ThingWorx site via your Arduino’s ethernet or wifi shield. Select “Number” as the type. You should have a Properties page that looks something like this:

Select “Add My Property” to start adding Properties to your ThingShape. These are the values that you will be pushing to the ThingWorx site via your Arduino's ethernet or wifi shield.

2.5:   Now we’ll create a service that reads values from your Arduino and populates your Properties with them. First, while still in the ThingShape edit mode, click on Services on the left hand side and select the “Add My Service” button at the top.

While still in the ThingShape edit mode, click on Services on the left hand side and select the “Add My Service” button at the top.

2.6:   Fill in the Name and Description, then click on Inputs/Outputs.

Add inputs that correspond to the data values that you are sending from the Arduino. These input names MUST match the names that you select in your Arduino code (discussed later). Also make sure that the type is set to STRING. Next, we need to write a short JavaScript that will convert the string values to type float and then populate the properties. Note below that properties will have a “me.” in front of them.

Add inputs that correspond to the data values that you are sending from the Arduino. These input names MUST match the names that you select in your Arduino code

2.7:   Click the Done button at the bottom and then the Save button at the top to complete your ThingShape and Service.


3: Create a Thing

3.1:   From the Composer home page click “Thing” and then click “New”. Populate the required fields. Make sure your tag is the same one you used for your ThingShape and Application Key. Choose the “GenericThing” as your Thing Template. Choose the ThingShape that you just created as your implemented ThingShape.

From the Composer home page click “Thing” and then click “New”. Populate the required fields.

3.2:   Now click Save and you’re done with your Thing!


4: Test your Service

4.1:   While you are still in your Thing you can now test the service you made by clicking on Services on the left side of the screen. If the Service doesn’t show up then click save again on your Thing and ThingShape and close and reopen your Thing tab. The screen should look something like the one below when you click on Services.

Test the service you made by clicking on Services on the left side of the screen.

4.2:   Click the “Test” button on the Service that you just created and populate the fields. Click Execute Service.

Click the “Test” button on the Service that you just created and populate the fields.

4.3:   Now exit out of that window and go to your Properties tab on the left and refresh the properties. The values should have populated into your properties!

Go to your Properties tab on the left and refresh the properties. The values should have populated into your properties!

If the properties didn’t update or you got a JavaScript error, be sure to check back through your Property names, types, and JavaScript code and ensure that it matches the formats from above.


Tutorial 2: Programming your Arduino

Included in this tutorial are two Arduino libraries, called ThingWorxWiFi (5.2 KB .zip) and ThingWorxEthernet (4.9 KB .zip). They also come with example programs. Download these libraries and place them in the /Arduino/libraries folder. Open up your Arduino Environment and under File/Examples, find the Example code for the particular hardware you are using (either WiFi or Ethernet shield). This tutorial will walk you through the Ethernet code. The WiFi code is almost identical save for the required WiFi network information. When you open the code it should look like this:

The WiFi code is almost identical save for the required WiFi network information.

Make sure you denote how many values you will be pushing to ThingWorx (next to the #define sensorCount). Now fill in the required info: a unique MAC address, server, app key, thing name, service name, the names of your values or sensor values (these must match your Service variables exactly!), the time between connections, and the corresponding pins that your sensors are on. This is all you need to start pushing values! The code as written will send your raw (0 to 1028) analog to digital converted values to ThingWorx. You may choose to process your data into usable values before you send it off. If so, either modify the values in this loop:

You may choose to process your data into usable values before you send it off. If so, either modify the values in this loop:

Or however you see fit.