Skip to content

Instantly share code, notes, and snippets.

@dubkov
Created January 13, 2017 07:15
Show Gist options
  • Save dubkov/067474d78ca736fac3c9d94174fc6c5f to your computer and use it in GitHub Desktop.
Save dubkov/067474d78ca736fac3c9d94174fc6c5f to your computer and use it in GitHub Desktop.
rest uno
#include <HTU21D.h>
//#include <HTU21D.h>
#include <Wire.h>
/****************************************************************************
Arduino to Thingworx Ethernet Web Client using Ethernet Shield
This sketch reads sensor values and sends the values to a Thing on ThingWorx.
The sketch uses the Ethernet Repeating Web client (info below) and a custom ThingWorx library
as a starting point. The user must choose a unique MAC address (the sketch right now
is set up as DHCP but you can add Static IP and DNS if you'd like. Just make sure you
relay those changes in the Ethernet.begin() call). On the Thingworx side make sure your
nameArray[] values, your thingName[], your serviceName[] and your server[] matches exactly.
Use your provided appKey that you recieve when you set up your Thing.
Create a service in your Thing or ThingShape and create inputs that will match the variable names you use
in your Arduino code. In the JavaScript window type something similar to the following:
me.PropertyOne = parseFloat(InputOne);
me.PropertyTwo = parseFloat(InputTwo);
Where Property one is the name of your first Thing or ThingShape property and InputOne is the name of
your first input. Everything is case sensitive.
Update sensorCount to reflect how many sensors (or whatever data variables) you want to
send.
created 3/5/2015
by Nick Milleson
Design Engineer
EAC Product Development Solutions
www.eacpds.com
nmilleson@eacpds.com
/****************************************************************************
Repeating Web client
This sketch connects to a a web server and makes a request
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.
This example uses DNS, by assigning the Ethernet client with a MAC address,
IP address, and DNS address.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 19 Apr 2012
by Tom Igoe
http://arduino.cc/en/Tutorial/WebClientRepeating
This code is in the public domain.
*****************************************************************************/
#include <SPI.h>
#include <Ethernet.h>
//How many values you will be pushing to ThingWorx
#define propertyCount 2
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
char server[] = "thingworx-academic-staff.ptcmscloud.com";
EthernetClient client;
//ThingWorx App key which replaces login credentials)
char appKey[] = "eaae5cc6-5331-41a0-9e73-211df11d9c97";
// ThingWorx Thing name for which you want to set properties values
char thingName[] = "HTU21DThing";
//Interval of time at which you want the properties values to be sent to TWX server
int timeBetweenRefresh = 5000;
// ThingWorx service that will set values for the properties you need
// See the documentation for this tutorial for more information
char serviceName[] = "setTempAndHumid";
//Initialize Properties Names and Values Arrays
char* propertyNames[] = {"Temp", "Humid"};
double propertyValues[propertyCount];
// last time you connected to the server, in milliseconds
unsigned long lastConnectionTime = 0;
// state of the connection last time through the main loop
boolean lastConnected = false;
//Initialize an HTU21D library object to read
// temperature and humidity data from your connected sensor
HTU21D myHumidity;
void setup() {
//shut down the SD Card pins
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
// start serial port:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
//initialize HTU21D object to read values from sensors
myHumidity.begin();
// start the Ethernet connection:
Serial.println("Trying to get an IP address using DHCP");
Ethernet.begin(mac);
Serial.print("My IP address: ");
Serial.print(Ethernet.localIP());
Serial.println();
}
void loop() {
// Aquire sensor values
propertyValues[0] = myHumidity.readTemperature();
propertyValues[1] = myHumidity.readHumidity();
// wait the established interval of time before
// reading values from the sensor
// and sending them to the TWX server again
// delay(timeBetweenRefresh);
if (millis() - lastConnectionTime > timeBetweenRefresh) {
updateValues(propertyValues, client, server, appKey, thingName, serviceName, propertyNames);
}
}
void updateValues(double values[] , EthernetClient &client, char server[], char appKey[], char thingName[], char serviceName[], char* sensorNames[])
{
//build the String with the data that you will send
//through REST calls to your TWX server
char data[80];
strcpy(data, "?appKey=");
strcat(data, appKey);
strcat(data, "&method=post&x-thingworx-session=true");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// send the HTTP POST request:
client.print("POST /Thingworx/Things/");
client.print(thingName);
client.print("/Services/");
client.print(serviceName);
client.print(data);
client.print("<");
for (int idx = 0; idx < propertyCount; idx++)
{
client.print("&");
client.print(propertyNames[idx]);
client.print("=");
client.print(propertyValues[idx]);
}
client.print(">");
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Content-Type: text/html");
client.println();
client.stop();
lastConnectionTime = millis();
// print the request out
Serial.print("POST /Thingworx/Things/");
Serial.print(thingName);
Serial.print("/Services/");
Serial.print(serviceName);
Serial.print(data);
Serial.print("<");
for (int idx = 0; idx < propertyCount; idx++)
{
Serial.print("&");
Serial.print(propertyNames[idx]);
Serial.print("=");
Serial.print(propertyValues[idx]);
}
Serial.print(">");
Serial.println(" HTTP/1.1");
Serial.print("Host: ");
Serial.println(server);
Serial.println("Content-Type: text/html");
Serial.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("the connection could not be established");
client.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment