Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
wh committed Jun 5, 2012
0 parents commit b74d563
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 0 deletions.
180 changes: 180 additions & 0 deletions Exosite.cpp
@@ -0,0 +1,180 @@
//*****************************************************************************
//
// exosite.cpp - Prototypes for the Exosite Cloud API
//
// Copyright (c) 2012 Exosite LLC. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:

// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Exosite LLC nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
//*****************************************************************************

#include <SPI.h>
#include "Exosite.h"
#include <Ethernet.h>
#include <EthernetClient.h>

#define serverName "m2.exosite.com"

/*
* Constructor
*/
Exosite::Exosite(EthernetClass *eth, byte* _mac, String _cik)
{
ethernet = eth;
mac = _mac;
cik = _cik;
}

void Exosite::init(void)
{
ethernet->begin(mac);
delay(500);
this->client = new EthernetClient();
}

/*
* Send data to Cloud
*/
int Exosite::sendToCloud(String res, int value)
{

ret = 0;
stringPos = 0;
DataRx = false;
RxLoop = true;
timeout_time = 0;
time_now = 0;
timeout = 3000; // 3 seconds
myDataString = ""; //allocate for actual data sent

if (client->connect(serverName,80)) {
if (client->connected()) {
myDataString += res;
myDataString += "="; //put into resource
myDataString += value; //just send the value
// Send request using Exosite basic HTTP API
client->println("POST /api:v1/stack/alias HTTP/1.1");
client->println("Host: m2.exosite.com");
client->print("X-Exosite-CIK: ");
client->println(cik);
client->println("Content-Type: application/x-www-form-urlencoded; charset=utf-8");
client->println("Accept: application/xhtml+xml");
client->print("Content-Length: ");
client->println(myDataString.length()); //calculate length
client->println();
client->println(myDataString);

// Read from the nic
//
timeout_time = millis()+ timeout;
while ((timeout_time > time_now) && RxLoop) {
if (client->available()) {
if (!DataRx)
DataRx= true;
c = client->read();
rxdata[stringPos] = c;
stringPos += 1;
} else {
rxdata[stringPos] = 0;

if (DataRx) {
DataRx= false;
RxLoop = false;

ret=1;
}
}//else
time_now = millis();
}// while ((timeout_time > time_now) && RxLoop) {

client->stop();
}
}// if (client->connect(serverName,80)) {
return ret;
}

/*
* read data from cloud
*/
int Exosite::readFromCloud(String res ,String* pResult)
{

ret = 0;
stringPos = 0;
DataRx= false;
RxLoop = true;
timeout_time = 0;
time_now = 0;
timeout = 3000; // 3 seconds
myDataString = ""; //allocate for actual data sent

if (client->connect(serverName,80)) {
if (client->connected()) {
// Send request using Exosite basic HTTP API
client->print("GET /api:v1/stack/alias?");
client->print(res);
client->println(" HTTP/1.1");
client->println("Host: m2.exosite.com");
client->print("X-Exosite-CIK: ");
client->println(cik);
client->println("Accept: application/x-www-form-urlencoded; charset=utf-8");
client->println();
// Read from the nic or the IC buffer overflows with no warning and goes out to lunch
timeout_time = millis()+ timeout;

while ((timeout_time > time_now) && RxLoop) {
if (client->available()) {
if (!DataRx)
DataRx= true;

c = client->read();
rxdata[stringPos] = c;

stringPos += 1;
} else {
rxdata[stringPos] = 0;

if (DataRx) {
DataRx = false;
RxLoop = false;
String rxstrg = String(rxdata);
int length = 0;
int rxresultpos = 0;
int subStringLength = 0;
if (rxstrg.startsWith("HTTP/1.1 200 OK")) {
length = rxstrg.length();
rxresultpos=rxstrg.indexOf('=');
subStringLength = length - rxresultpos;
*pResult= String(rxstrg.substring(rxresultpos+1));
} else {
rxresultpos=rxstrg.indexOf('\n');
subStringLength = rxresultpos;
*pResult= String(rxstrg.substring(0,rxresultpos));
}
ret=1;
}
}
time_now = millis();
}
client->stop();
}
}
return ret;
}
62 changes: 62 additions & 0 deletions Exosite.h
@@ -0,0 +1,62 @@
//*****************************************************************************
//
// exosite.h - Prototypes for the Exosite Cloud API
//
// Copyright (c) 2012 Exosite LLC. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:

// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Exosite LLC nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
//*****************************************************************************

#ifndef Exosite_h
#define Exosite_h

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>

class Exosite
{
private:
class EthernetClient* client; // Port 80 is default for HTTP
class EthernetClass* ethernet;
byte *mac;
String cik;
char rxdata[150];
int ret;
int stringPos;
boolean DataRx;
boolean RxLoop;
char c;
unsigned long timeout_time;
unsigned long time_now;
unsigned long timeout;
String myDataString;


public:
Exosite(EthernetClass *eth, byte* _mac, String _cik); //constructor
void init(void);
int sendToCloud(String res, int value);
int readFromCloud(String res ,String* pResult);

};

#endif
26 changes: 26 additions & 0 deletions LICENSE
@@ -0,0 +1,26 @@
Copyright (c) 2012, Exosite LLC
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Exosite LLC nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
56 changes: 56 additions & 0 deletions Readme.md
@@ -0,0 +1,56 @@

========================================
About Exosite Arduino Library
========================================
This project is an simple example of using an Andruino board equip with Ethernet Shield to send and receive data to/from the cloud by using Exosite's Cloud Data Platform. This example cloud_read_write read a data from the cloud, add 100 to it and send back to the cloud.

License is BSD, Copyright 2012, Exosite LLC (see LICENSE file)

Tested with Arduino 1.01

========================================
Quick Start
========================================
1) Download the Arduino toolchain and development environment

* http://www.arduino.cc/en/Main/software

2) Go to "Sketch->Show Sketch Folder", your Sketch folder will then be displayed. Create a directory name "libraries" if it is not exists".

3) Copy the Exosite library folder to the directory sketchbook-location\"libraries". Then you should see "File->Examples->Exosite"

4) Open the "File->Examples->Exosite->cloud_read_write"

5) Edit the "PUTYOURCIKHERE" value to match your CIK value

* HINT: Obtain a CIK from https://portals.exosite.com by clicking +Add Device

6) Edit the mac address values if you have valid set of mac addres, or you can just use the default value for testing.

7) In Portals (https://portals.exosite.com), add two DataSources to match the data resources (aliases) the code is using.

* HINT: Goto https://portals.exosite.com/manage/data and click +Add Data Source

* HINT: Ensure the "Resource:" values are set to "1" and "onoff" respectively to match the code

* HINT: Add an "on off switch" Widget to your dashboard to control data source "onff"

8) Go to "Tools->Board" to select the corresponding Arduino board type

9) In the Arduino software, compile and verify there are no errors

10) Go to "Tools->Serial" to select the serial port your Arduino board is connected to

11) Go to File->Upload to I/O Board to upload the program

12) When "Done uploading" is displayed, go to https://portals.exosite.com to see your data in the cloud!

* HINT: Your Arduino board must be connected to the Internet via the RJ-45 ethernet jack

For more information on this project and other examples, checkout our Exosite Garage github page at http://exosite-garage.github.com

========================================
Release Info
========================================
**Release 2011-06-05**
- initial version
57 changes: 57 additions & 0 deletions examples/cloud_read_write/cloud_read_write.ino
@@ -0,0 +1,57 @@
//*****************************************************************************
//
// cloud_read_write.ino - Simple read/write sample for the Exosite Cloud API
//
// Copyright (c) 2012 Exosite LLC. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:

// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Exosite LLC nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
//*****************************************************************************

#include <SPI.h>
#include <Ethernet.h>
#include <Exosite.h>

byte macData[] = { "PUTYOURMACHERE" }; // <-- Fill in your MAC here! (e.g. {0x90, 0xA2, 0xDA, 0x00, 0x22, 0x33})

String cikData = "PUTYOURCIKHERE"; // <-- Fill in your CIK here! (https://portals.exosite.com -> Add Device)

Exosite exosite(&Ethernet, macData, cikData);

void setup()
{
exosite.init();
}

void loop()
{

String retVal;


//Read the alias (resource name) "onoff"
if ( exosite.readFromCloud("onoff", &retVal))
{
//Send a value + 100 to Exosite, use the alias (resource name) "1"
exosite.sendToCloud("1", retVal.toInt() + 100);
}

delay(3000);
}

0 comments on commit b74d563

Please sign in to comment.