Giter VIP home page Giter VIP logo

xively_arduino's Introduction

#Xively Arduino library

A library for Arduino to make it easier to talk to Xively.

This library requires HTTP Client.

Build Status

##Features

  1. Generic functions for: - Uploading datapoints - Downloading datapoints
  2. Compatible with:
    • Arduino Ethernet shield
    • Arduino Ethernet board
    • Arduino Wifi shield
    • WiFly shield

##For a Quickstart Example
Look no further! If you want a quick example, connect your Arduino board to your computer and an ethernet cable and try out one of the examples included with this library.

In Arduino, go to Files > Examples and choose DatastreamUpload or DatastreamDownload from the xively_arduino library folder

##Setup Your Sketch

1. Specify your API key and Feed ID

char xivelyKey[] = "YOUR_XIVELY_API_KEY";
// Should be something like "HsNiCoe_Es2YYWltKeRFPZL2xhqSAKxIV21aV3lTL2h5OD0g"
#define FEED_ID XXXXXX 
// The 3 to 6-digit number (like 504 or 104097), that identifies the Xively Feed you're using

2. Create IDs for your datastreams as char arrays (or String objects for a String datastream)

In Xively, the name of a datastream is known as the Stream ID. In the example below, we'll give the datastreams names by setting their Stream IDs as "humidity", "temperature", "my_thoughts_on_the_temperature" and "more_thoughts".

// For datastreams of floats:
char myFloatStream[] = "humidity";
// For datastreams of ints:
char myIntStream[] = "temperature";
// For datastreams of Strings:
String myStringStream("my_thoughts_on_the_temperature");
// For datastreams of char buffers:
char myCharBufferStream[] = "more_thoughts";  // ID of the array
const int bufferSize = 140;                   // size of the array
char bufferValue[bufferSize];                 // the array of chars itself

String datastreams and char buffer datastreams are similar: both will be able to send strings to Xively datastreams. For beginners, using String datastreams will be fine much of the time.

Using char buffers reduces the memory footprint of your sketch by not requiring the String library. Also, using char buffers allows you to specify exactly how much memory is used for a datapoint, so you don't accidentally overflow the Arduino's mem capacity with a huge string datapoint. It's a little bit harder to understand for beginners -- consult XivelyDatastream.cpp for info.

3. Create an array of XivelyDatastream objects

XivelyDatastream datastreams[] = {
  // Float datastreams are set up like this:
  XivelyDatastream(myFloatStream, strlen(myFloatStream), DATASTREAM_FLOAT),
  // Int datastreams are set up like this:
  XivelyDatastream(myIntStream, strlen(myIntStream), DATASTREAM_INT),
  // String datastreams are set up like this:
  XivelyDatastream(myStringStream, DATASTREAM_STRING),
  // Char buffer datastreams are set up like this:
  XivelyDatastream(myCharBufferStream, strlen(myCharBufferStream), DATASTREAM_BUFFER, bufferValue, bufferSize),
};

XivelyDatastream objects can contains some or all of the following variables, depending on what type of datapoints are in the datastream (see above example for which are required):

Variable Type Description
1 aIdBuffer char* char array containing the ID of the datastream
2 aIdBufferLength int for int or float datastreams only: the number of char in the datastream's ID
3 aType int 0 or DATASTREAM_STRING for a String; 1 or DATASTREAM_BUFFER for a char buffer; 2 or DATASTREAM_INT for an int; 3 or DATASTREAM_FLOAT for a float
4 aValueBuffer char* A char array, aValueBufferLength elements long
5 aValueBufferLength int The number of elements in the char array

4. Last, wrap this array of XivelyDatastream objects into a XivelyFeed

Unlike the Stream ID, which is what a Datastream's name is stored as, the ID of a Feed is a number which is used to uniquely identify which Xively Feed you are addressing. For example, a Feed ID of 504 would mean that you were using the feed at xively.com/feeds/504.

XivelyFeed feed(FEED_ID, datastreams, 4);
Variable Type Description
1 aID unsigned long The Feed's ID, as defined at the top of your sketch
2 aDatastreams XivelyDatastream* Your XivelyDatastream array
3 aDatastreamsCount int How many datastreams are in the array

5. Instantiate the library's Xively client

Connecting by ethernet:

If you're using the Ethernet library:

EthernetClient client;
XivelyClient xivelyclient(client);

If you're on wireless, be sure to enter your SSID and password as the library requires, and then:

If you're using the built-in WiFi library:

WiFiClient client;
XivelyClient xivelyclient(client);

If you're using the [Sparkfun WiFly] 1 library:

WiFlyClient client;
XivelyClient xivelyclient(client);	

##Sending and Retrieving Xively Datapoints

###Read a Datapoint

Serial.print("My return is: ");

float float_value = datastreams[0].getFloat();        // Retrieve the latest datapoint in a float datastream
int int_value = datastreams[1].getInt();              // Retrieve the latest datapoint in an int datastream
String string_value = datastreams[2].getString();     // Retrieve the latest datapoint in a String datastream
char[140] char_buffer = datastreams[3].getBuffer();   // Retrieve the latest datapoint in a char buffer datastream

###Update a Datapoint

The library makes it easy to upload data as strings or numbers.

datastreams[0].setFloat(1.5);                           // Push a float datapoint
datastreams[1].setInt(23);                              // Push an int datapoint
datastreams[2].setString("Pretty comfy temperature");   // Push a String datapoint
datastreams[3].setBuffer("But quite dry");              // Push a char buffer datapoint

##Error codes

This library uses amcewen/HttpClient which has following error codes:

  • HTTP_SUCCESS = 0 - no error
  • HTTP_ERROR_CONNECTION_FAILED = -1 - connection to api.xively.com has failed
  • HTTP_ERROR_API = -2 - a method of HttpClient class was called incorrectly
  • HTTP_ERROR_TIMED_OUT = -3 - connection with api.xively.com has timed-out
  • HTTP_ERROR_INVALID_RESPONSE = -4 - invalid or unexpected response from the server

Apart from the above, the library will convert any non-2xx status code to a nagative value like so:

ret = http.responseStatusCode();
if ((ret < 200) || (ret > 299))
{

  if (ret > 0)
  {
    ret = ret * -1;
  }
}

Therefore:

  • if we got a 2xx, we will return that as is
  • if we got a negative value (e.g. HTTP_ERROR_CONNECTION_FAILED, HTTP_ERROR_TIMED_OUT or other), we will return that as is
  • any non-2xx status code is returned multiplied by -1, i.e. a 403 (Authentication error) will be returned as -403

githalytics.com alpha

xively_arduino's People

Contributors

amcewen avatar baz44 avatar bjpirt avatar errordeveloper avatar frasermac avatar levent avatar paulbellamy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xively_arduino's Issues

.GetFloat always returns 0.00?

Is it a known issue that currently the Arduino Library is returning 0.00 on .GetFloat() - no values appear to be coming with a 200 response.

Query captured from support ticket sent to main XIvely support

Possible compatibility issue with Wire library

_Originally reported as cosm/cosm-arduino#9 by @mcdonaldajr._

I have downloaded the MultipleDatastreamsUpload.ino example.
I change the mac address, the cosmKey, streamId and run it. It works beautifully.
I added this after all other headers:

#include <Wire.h>

Now it doesn't work. I get error code -400 a few times, then -411.

I have a standard Arduino Uno with standard Ethernet Shield, so this should be easily reproducible. I have re-downloaded stuff to ensure I'm running the latest versions of everything, including the libraries.

If I comment out the line:

// datastreams[2].setString(stringValue);

it starts working again. COSM returns 200 OK.
I'm no expert, and can't debug the COSM or the Wire library, but I suspect we have a problem with something incorrectly addressing memory somewhere.

Help gratefully received, as I (and I suspect many others) need the Wire library to access my sensors.

Thanks, Anthony

Feed with multiple float values returns HTTP 400

Hi,

I have found an issue with the Arduino Xively library.

I compiled the MultipleDatastreamUpload example - and that works as expected with a float, buffer and string stream.

If I change the buffer stream to a float stream as well, then the example fails with an HTTP 400 error, with the response body having:

{"title":"JSON Parser Error","errors":"JSON doesn't appear to be a hash"}

The only changes to the example are:

XivelyDatastream datastreams[] = {
XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
XivelyDatastream(bufferId, strlen(bufferId), DATASTREAM_FLOAT),
XivelyDatastream(stringId, DATASTREAM_STRING)
};

datastreams[1].setFloat(millis()/10.0f);
Serial.print("Setting buffer value to:\n ");
Serial.println(datastreams[1].getFloat());

I am having this same issue in another application I wrote - where sending one float stream in a feed works, but as soon as I add a 2nd float stream then it fails with this same error.

getString returns date too

String returnString = feed[0].getString(); where feed is a XivelyFeed should return the string value of the referenced datastream. Instead, it returns a string that contains both the date and string parts of the CSV datapoint.

e.g.: 2013-04-23T00:40:34.032979Z,value

inappropriate function call

When trying to compile to connect through a WiFly shield, I have the code as given for the WiFly shield in the README, namely

WiFlyClient client;
XivelyClient xivelyclient(client);

then I get a number of errors. The first is because the WiFlyClient declaration from the Sparkfun library expects a destination and a port

If I replace this with

WiFlyClient client("api.xively.com",80);
XivelyClient xivelyclient(client);

then I get the error

Xively_sketch_aug20a:57: error: no matching function for call to 'XivelyClient::XivelyClient(WiFlyClient&)'
/Users/paultravers/Documents/Arduino/libraries/xively/XivelyClient.h:11: note: candidates are: XivelyClient::XivelyClient(Client&)
/Users/paultravers/Documents/Arduino/libraries/xively/XivelyClient.h:9: note:                 XivelyClient::XivelyClient(const XivelyClient&)

which appears to suggest that the declaration of the client type in the first statement is incompatible with the datatype required in the second statement. I am sure there is a solution for this but can't find it myself.

get(feed, xivelyKey) timeout

This takes very long time, and in the end it returns with -3. On an Arduino with ethernet shield.
I have been able to get through maybe 1 in 100 tries.

Web front for Xively + Arduino

Hi all, I am a noob here so thanks in advance for the support. I have now made Arduino and Xively talk to each other just fine. Now, I would like to have a web app where I can display the information read from sensors on the Arduino but don't know what solution is the best out there. I don't necessarily wish to have to learn a new language so I can program this. Ideally, this could be done with a simple app/solution that I can send Xively API commands back and forth with. Can someone point me in the right direction or provide help?

Extracting a timestamp

There should be an example showing how to extract timestamp value for a datapoint.

Original post:

Hi,
I'd like to request a way to pull the date/time out of the Xively data I pull from a feed. Right now, from what I can tell in the library, the parser just skips right over the date. Not really a bug, just a request.
thanks!
Eli

Error messages when uploading multiple datastreams

When I try to upload multiple data streams to Xively, I frequently get errors. About 50-60% of the time I get return values of 200, the rest of the time I get values of 3 and 411. Occasionally this causes the Arduino to freeze. Any idea what's going on? Is there any way to see the server response from unsuccessful HTTP requests?

I'm using an Arduino Mega 2560 with WiFi shield. I have updated the WiFi shield firmware to the latest version. I'm using the Arduino IDE 1.0.5-r2.

Here's the code I'm using.

/*
Internet OD reader (IODR) program
Version 8
Dan Olson
9-8-2014

For use with v2 of the IODR, using an Arduino Mega 2560, Arduino WiFi shield and IODR v2 shield circuit board (designed by me)
Detect growth of bacterial cultures by measuring the absorbtion of a light

Pin assignments:
0: used by ethernet shield
1: used by ethernet shield
2: yellow LED
3: RX for lcd display (unused)
5: for entering calibration mode
7: handshake communication between wifi shield and arduino (can't be used for other things)
9: transistor that controls all 8 LEDs (note, this was previously pin 7, but moved to pin 9 since the wifi shield needs to use pin 7)
8: temperature sensor
10: ethernet shield enable
18: TX for lcd display
50, 51, 52: SPI bus for WiFi shield communication with arduino
53: necessary to set this pin as output to allow ethernet shield to work
20-34 (even numbered pins): blank reset buttons
A8: pin 89, light sensor for tube 8
A9: pin 88, light sensor for tube 7
A10: pin 87, light sensor for tube 6
A11: pin 86, light sensor for tube 5
A12: pin 85, light sensor for tube 4
A13: pin 84, light sensor for tube 3
A14: pin 83, light sensor for tube 2
A15: pin 82, light sensor for tube 1
*/

include <OneWire.h>

include <SPI.h>

//#include <Ethernet.h> //not needed since we're using wifi

include <HttpClient.h>

//#include <Cosm.h> // not needed since we're using new xively library

include <Math.h>

include <SoftwareSerial.h>

include <WiFi.h>

include <Xively.h>

define VERSION 8

define API_KEY "ykoxrQb5UKnebVGoBH5Vl1XZQQITCV7nAFnXy5dEAuxkStef" // my Xively API key

char xivelyKey[] = "ykoxrQb5UKnebVGoBH5Vl1XZQQITCV7nAFnXy5dEAuxkStef";

define FEED_ID 641819840 // my Cosm feed ID

// initialize variables

// no longer needed // MAC address for your Ethernet shield
// no longer needed //byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xA7, 0x42 }; // for ethernet shield attached to mega

// define login details for wifi shield
// currently set up for use with home network, need to change for Dartmouth network
// IODR #2 wifi shield mac address is 78:C4:0E:02:10:F9. Note that the sticker on the back says 90:A2:DA:0F:0C:64 but this is not correct. Run the WiFi "ConnectNoEncryption" file to see the correct MAC address.

char ssid[] = "Dartmouth Public"; // your network SSID (name)
//char pass[] = "celebration!"; // your network password
int status = WL_IDLE_STATUS; // status of wireless connection

//define strings for datastream IDs
char tube1Stream[] = "tube1";
char tube2Stream[] = "tube2";
char tube3Stream[] = "tube3";
char tube4Stream[] = "tube4";
char tube5Stream[] = "tube5";
char tube6Stream[] = "tube6";
char tube7Stream[] = "tube7";
char tube8Stream[] = "tube8";
char temperatureStream[] = "temperature";

// create datastreams
XivelyDatastream datastreams[] = {
XivelyDatastream(tube1Stream, strlen(tube1Stream), DATASTREAM_FLOAT),
XivelyDatastream(tube2Stream, strlen(tube2Stream), DATASTREAM_FLOAT),
XivelyDatastream(tube3Stream, strlen(tube3Stream), DATASTREAM_FLOAT),
XivelyDatastream(tube4Stream, strlen(tube4Stream), DATASTREAM_FLOAT),
XivelyDatastream(tube5Stream, strlen(tube5Stream), DATASTREAM_FLOAT),
XivelyDatastream(tube6Stream, strlen(tube6Stream), DATASTREAM_FLOAT),
XivelyDatastream(tube7Stream, strlen(tube7Stream), DATASTREAM_FLOAT),
XivelyDatastream(tube8Stream, strlen(tube8Stream), DATASTREAM_FLOAT),
XivelyDatastream(temperatureStream, strlen(temperatureStream), DATASTREAM_FLOAT),
};

// Wrap all 9 datastream into a feed
XivelyFeed feed(FEED_ID, datastreams, 9);
unsigned long lastConnectionTime = 0; // last time we connected to Xively
const unsigned long connectionInterval = 60000; // delay between connecting to Xively in milliseconds

// network setup
WiFiClient client;
XivelyClient xivelyclient(client);
boolean connectedToInternet = false;

// digital temperature sensor
int DS18S20_Pin = 8; //DS18S20 temp sensor signal pin on digital 8
OneWire ds(DS18S20_Pin); // on digital pin 8
float temperature;

// LEDs and light sensors
int ledPin = 9;
int yellowLED = 2;
int numTubes = 8;
int calibrationPin = 5;
int lightInPin[] = {A15, A14, A13, A12, A11, A10, A9, A8}; //pins for analog inputs for light sensor
float lightIn[] = {0,0,0,0,0,0,0,0}; //value of light sensor
float ODvalue[] = {0,0,0,0,0,0,0,0};
int LEDoffReading[] = {0,0,0,0,0,0,0,0};
int LEDonReading[] = {0,0,0,0,0,0,0,0};
int pointsToAverage = 30; //number of light readings to average in each data point sent to Cosm
int blankButtonState[] = {0,0,0,0,0,0,0,0}; // status of buttons for blanking individual tubes
//int newBlankButtonState[] = {0,0,0,0,0,0,0,0};
int blankButtonPin[] = {20, 22, 24, 26, 28, 30, 32, 34};
int lastButtonPressed = 0; // keep track of which reset button was pressed most recently

// calibration data from 3-17-2013
// float blankValue[] = {598.8,980.7,564.0,847.7,988.9,505.6,638.2,622.6}; //from 3/16/2013 calibration
float blankValue[] = {1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0}; //from 3/25/2013 calibration
//float slopeValue[] = {1.13805, 1.10954, 1.10806, 1.11424, 1.08111, 1.18568, 1.15645, 1.05881};
//float iceptValue[] = {0.07818, 0.0152, 0.00991, 0.01595, -0.02349, -0.03377, -0.02151, -0.05815};
int calibrationMode = 1; // 1 is for no calibration, 0 is for calibration mode. in calibration mode, one reading is taken every time the button is pressed
int calibrationCount = 0; // keeps track of how many times the calibration button was pressed

// for lcd serial display
SoftwareSerial mySerial (3,18);

void setup() {
mySerial.begin(9600); //start lcd display
delay(500); // wait for lcd display to boot up

Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(yellowLED, OUTPUT);
analogReference(EXTERNAL);
pinMode(calibrationPin, INPUT);
pinMode(53, OUTPUT); //necessary for WiFi shield function

// calibration mode is for calibrating the analog channels
// see description in main loop
// needs to be rewritten as its own subroutine to make program flow clearer
calibrationMode = digitalRead(calibrationPin); // decide whether or not to enter calibration mode
if (calibrationMode == 0){ // enter calibration mode
Serial.println();
Serial.println("Entering calibration mode...");
calibrationCount = 0;
}

// connect to internet with wifi adapter
Serial.print("Internet OD reader v");
Serial.println(VERSION);
Serial.println("==========================");

Serial.println("Initializing network");
// send message to LCD display
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write("Trying to connect to internet ");

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid); //use for an SSID that doesn't require a password
//status = WiFi.begin(ssid, pass); //use for an SSID that requires a password
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
connectedToInternet = true;
printWifiStatus();

// temporarily disabled for testing
/*
// make 4 attempts to connect to internet
for (int i=1; i<4; i++){
status = WiFi.begin(ssid, pass); //try to connect to internet
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
Serial.print("using password: ");
Serial.println(pass);
Serial.print("status: ");
Serial.println(status);

if (status != WL_CONNECTED) {
  Serial.print("Error connecting to network, trying again...");
  Serial.print("attempt ");
  Serial.print(i);
  Serial.println(" of 3");
  delay(10000);
}
else {
  i=4; //jump out of for loop
  connectedToInternet = true;
  Serial.println("Network initialized");
  Serial.println();
  printWifiStatus();
  // send message to LCD display
  //mySerial.write(254); // move cursor to beginning of first line
  //mySerial.write(128);
  //mySerial.write("Connected to internet          ");
  delay(1000);
}

}
*/

}

// main program loop
void loop() {

// infinite loop in calibration mode
// calibration mode can only be entered during setup, so controller needs to be reset
// it allows you to step through the analog readings and adjust the trim potentiometers
// once you've entered the calibration mode, each press of the CAL-MODE button takes one reading
while (calibrationMode == 0){
Serial.println("_CALIBRATION MODE_");
readLightSensors();
calibrationDisplay();
//checkBlankButtons();
//blankButtonStatusDisplay();
while(digitalRead(calibrationPin) == 1){ //button is pressed
}
delay(100); // debounce
while(digitalRead(calibrationPin) == 0){ //button is released
}
delay(100); // debounce
}

// normal mode (i.e. not calibration mode)
temperature = getTemp(); //read temperature sensor (deg. C)
//Serial.println(" ***** readLightSensors() ****");
readLightSensors(); // read light sensor values
//Serial.println(" ***
* serialPrintState() ****");
//serialPrintState(); // display the raw data from the sensors
//Serial.println(" ***
* checkBlankButtons() ****");
checkBlankButtons();
//Serial.println(" ***
* displayTubeStatus() *****");
displayTubeStatus(); // display the status of the current tube on the lcd display
delay(300);

// send data to Xively
if (connectedToInternet){
if (millis() - lastConnectionTime > connectionInterval) {

  digitalWrite(yellowLED, HIGH); // turn on yellow LED when sending data to Xively
  sendData();
  // update connection time so we wait before connecting again
  lastConnectionTime = millis();
  //serialPrintHeaders();
  digitalWrite(yellowLED, LOW); // turn off yellow LED
}

}

}
// end of main program loop

//display a message on the LCD saying that one tube blank value was reset
void displayTubeReset(int tubeNum){
char resetTopLine[] = "Tube: ";
char resetBotLine[] = "Was reset ";
// set tube number
resetTopLine[5] = tubeNum + 49; //convert integer to ascii by adding 48, add one more to shift index by 1

mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write(resetTopLine);

mySerial.write(254); // move cursor to beginning of second line
mySerial.write(192);
mySerial.write(resetBotLine);

Serial.print("displayTubeReset:");
Serial.println(resetTopLine);
delay(3000); //show the message for 3 seconds
}

// display current OD, raw light sensor value and blank value on serial LCD
void displayTubeStatus(){
char bval[60];
char odval[60];
char ltin[60];
char topLine[] = "Tube: Blk= ";
char botLine[] = "OD= Raw= ";

dtostrf(blankValue[lastButtonPressed], 4, 0, bval);
dtostrf(LEDonReading[lastButtonPressed], 4, 0, ltin);
dtostrf(ODvalue[lastButtonPressed], 3, 2, odval);

//if the OD value gave a "NaN" or "inf" value, this will add in blank spaces to make things display properly
// if (sizeof(odval)<4){
// odval += " ";
// }

//set tube number
topLine[5] = lastButtonPressed + 49; //convert integer to ascii by adding 48, add one more to shift index by 1

//set blank, OD and raw numbers
for (int i=0; i < 4; i++){
topLine[12+i] = bval[i]; //set blank value
botLine[3+i] = odval[i]; //set OD value
botLine[12+i] = ltin[i]; //set raw value
}

/*
Serial.print("___topLine->");
Serial.print(topLine);
Serial.println("
");
Serial.print("___botLine->");
Serial.print(botLine);
Serial.println("
");
*/

mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write(topLine);

mySerial.write(254); // move cursor to beginning of second line
mySerial.write(192);
mySerial.write(botLine);

}

// check the state of the reset buttons. keep track of which button was most recently pressed.
// if a button is pressed and held for a certain amount of time (>2 seconds), then reset the blank value for that well
void checkBlankButtons(){
for (int i=0; i<numTubes; i++){
//in v2 of the IODR shield, I accidentally wired the reset buttons to give digital 0 when pressed instead of
//digital 1 (as in previous version). So I added a "!" before the all 3 digitalRead commands to fix this
blankButtonState[i] += !digitalRead(blankButtonPin[i]);

  // if a button was pressed, keep track of which one
  // if multiple buttons were pressed, remember the highest numbered one
  if (!digitalRead(blankButtonPin[i]) == 1){
    lastButtonPressed = i; 
  }

  // if a button was not held down during this cycle, reset it's counter
  // i.e. reset button 2 was held down for 2 cycles, then it was released
  // now it's held down for 2 cycles again.  it shouldn't reset the blank value
  if (!digitalRead(blankButtonPin[i]) == 0){
    blankButtonState[i] = 0;
  }

  // if a button has been held down for 4 cycles, reset that tube
  if (blankButtonState[i] > 4) {
    // reset blankValue for tube i
    blankValue[i] = LEDonReading[i];
    displayTubeReset(i); // write a message to the serial LCD saying the tube was reset
    // reset the blank button state
    blankButtonState[i] = 0;
  }
}
//blankButtonStatusDisplay();

}

// display the state of each pushbutton used to blank the tube reader
void blankButtonStatusDisplay(){
Serial.print("Blank Button State: ");
for (int i=0; i<numTubes; i++){
Serial.print(blankButtonState[i]);
Serial.print(" ");
}
Serial.print(" lastButtonPressed=");
Serial.print(lastButtonPressed);
Serial.println();
}

void calibrationDisplay(){
// write raw values of light sensor to serial port
for (int i=0; i<numTubes; i++){
Serial.print(lightIn[i]);
Serial.print(" ");
}
Serial.println();
}

void serialPrintHeaders(){
// write column names
Serial.println("(lightIn, ODvalue)");
for (int i=0; i<numTubes; i++){
Serial.print("T");
Serial.print(i+1);
Serial.print(" ");

}
Serial.println();

}

void serialPrintState(){
// write values
//Serial.print(" ");
for (int i=0; i<numTubes; i++){
//Serial.print(LEDoffReading[i]);
//Serial.print(" ");
//Serial.print(LEDonReading[i]);
//Serial.print(" ");
Serial.print(lightIn[i]); //raw value of light sensor
Serial.print(" ");
//Serial.print(ODvalue[i]); //calculated OD value
//Serial.print(" ");
}

// display temperature
Serial.print("Temp: ");
Serial.print(temperature);
Serial.println();

}

void readLightSensors(){
// clear the lightIn value
for (int i = 0; i<numTubes; i++){
lightIn[i] = 0;
ODvalue[i] = 0;
}

// start accumulating lightIn values
for (int j = 0; j<pointsToAverage; j++){

// read light sensor values with the LED off
// this measures ambient light levels, which will later get subtracted from the reading
digitalWrite(ledPin, LOW); //turn off LEDs
delay(10); //it takes ~1ms for the light senor reading to stabilize after the LED has been turned off
for (int i = 0; i < numTubes; i++){
  LEDoffReading[i] = analogRead(lightInPin[i]);      
}

// read light sensor values with the LED on
digitalWrite(ledPin, HIGH); //turn on LEDs
delay(10); //it takes ~1ms for the light senor reading to stabilize after the LED has been turned on
for (int i = 0; i < numTubes; i++){
  LEDonReading[i] = analogRead(lightInPin[i]);      
}  


// calculate the difference and add it to lightIn
for (int i = 0; i < numTubes; i++){
  lightIn[i] += (LEDonReading[i] - LEDoffReading[i]);
}

}

// divide lightIn by pointsToAverage to get the average value
for (int i = 0; i < numTubes; i++){
lightIn[i] = lightIn[i]/pointsToAverage;
//ODvalue[i] = -(log10(lightIn[i]/blankValue[i])-iceptValue[i])/slopeValue[i];
ODvalue[i] = -(log10(lightIn[i]/blankValue[i]));
}

// turn the lights off when you're done
digitalWrite(ledPin, LOW); //turn off LEDs
}

float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}

if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}

ds.reset_search();

byte MSB = data[1];
byte LSB = data[0];

float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;

return TemperatureSum;

}

// send the supplied value to Xively, printing some debug information as we go
void sendData() {
Serial.println ("Data being sent to Xively");
for (int i=0; i<numTubes; i++){
datastreams[i].setFloat(ODvalue[i]);
Serial.print("Tube ");
Serial.print(i);
Serial.print(" = ");
Serial.println(datastreams[i].getFloat());
}
datastreams[numTubes].setFloat(temperature);

Serial.println("Uploading to Xively");
int ret = xivelyclient.put(feed, xivelyKey);
Serial.print("xivelyclient.put returned ");
Serial.println(ret);

Serial.println();
}

// print status of the wifi adapter to the serial port
// need to have the serial port enabled
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}

documentation for xively arduino functions

Can anyone tell me where I can find documentation that tells me what the return value for the xivelyclient.get function can be found? For that matter, can anyone tell me where to find documentation for the xively arduino library?

To slow to perform action on an Arduino after I change a value on Xively

When I manually log in to Xively and make a change in one of my tags, it is supposed to turn on an LED in my Arduino, I am using the method below to read the value of the tag, but sometimes it can take up to 4 seconds for the LED to reflect the new value.

I am new with Xively, can anyone point me in the right direction and tell me if there is another method that I can use to update the LED in real time?

'''
void loop()
{
ret = xivelyclient.get(feed, xivelyKey);
Serial.println("THE ABOVE PRODUCES A LATENCY OF ABOUT 2 SECONDS");
if (ret > 0)
{
outputtoled = feed[0].getFloat();
analogWrite(ledpin, outputtoled);
}

}

'''

Getting various error codes from WiFiMultipleDatastreamsUpload example

_Originally reported as cosm/cosm-arduino#11 by @mcdonaldajr._

Using standard Arduino with standard Wireless Shield.

Opened example and added SSID, password, API key and feed ID. Changed nothing else.
When run, COSM returns various error codes, plus sometimes 200, indicating success.
Error codes: -3, -4, -411. Estimate less than 50% return success (200).

Output log:

Starting multiple datastream upload to Cosm...

Attempting to connect to SSID: BTHomeHub2-7PT7
Connected to wifi
SSID: BTHomeHub2-7PT7
IP Address: 192.168.1.90
signal strength (RSSI):-31 dBm
Read sensor value 608.00
Setting buffer value to:
    a message to upload
Setting string value to:
    7 is a random number
Uploading it to Cosm
cosmclient.put returned 200

Read sensor value 504.00
Setting buffer value to:
    a message to upload
Setting string value to:
    49 is a random number
Uploading it to Cosm
cosmclient.put returned 200

Read sensor value 515.00
Setting buffer value to:
    a message to upload
Setting string value to:
    73 is a random number
Uploading it to Cosm
cosmclient.put returned -411

Read sensor value 519.00
Setting buffer value to:
    a message to upload
Setting string value to:
    58 is a random number
Uploading it to Cosm
cosmclient.put returned -3

Read sensor value 527.00
Setting buffer value to:
    a message to upload
Setting string value to:
    30 is a random number
Uploading it to Cosm
cosmclient.put returned -411

Read sensor value 533.00
Setting buffer value to:
    a message to upload
Setting string value to:
    72 is a random number
Uploading it to Cosm
cosmclient.put returned 200

Read sensor value 534.00
Setting buffer value to:
    a message to upload
Setting string value to:
    44 is a random number
Uploading it to Cosm
cosmclient.put returned 200

Read sensor value 535.00
Setting buffer value to:
    a message to upload
Setting string value to:
    78 is a random number
Uploading it to Cosm
cosmclient.put returned -4

Read sensor value 533.00
Setting buffer value to:
    a message to upload
Setting string value to:
    23 is a random number
Uploading it to Cosm
cosmclient.put returned -3

Read sensor value 525.00
Setting buffer value to:
    a message to upload
Setting string value to:
    9 is a random number
Uploading it to Cosm
cosmclient.put returned 200

Read sensor value 521.00
Setting buffer value to:
    a message to upload
Setting string value to:
    40 is a random number
Uploading it to Cosm
cosmclient.put returned -411

get(feed, xivelyKey); call takes 1 minute to return with the data.

I was running the DatastreamDownload.ino example with just my key and feed as the only modifications to the code. and it was taking 1 minutes to run this line of code:

int ret = get(feed, xivelyKey);

I did some debugging and found that the implementation of xivelyclient.get() in XivelyClient.cpp was hanging in the following while loop:

while ((next != '\r')  && (next != '\n') && (http.available() || http.connected()))
{
   next = http.read();
}

I imagine that the only reason it was ever coming out of this loop is because the connection is being closed by the server.

In order to make the function work for me I added the last two lines in the if statement just above the while loop and deleted the while loop.

if ((idBitfield & 1<<i) && (aFeed[i].idLength() == idIdx))
{
   // We've found a matching datastream
   // FIXME cope with any errors returned
  aFeed[i].updateValue(http);

  // When we get here we'll be at the end of the line, but if aFeed[i]
  // was a string or buffer type, we'll have consumed the '\n'
  next = '\n';
  http.stop();
  return ret;
}

I'm sure this is not an elegant solution but it works for me for now...

BTW thanks for writing this wrapper. It works for everything else I've tried.

Http library mismatch ?

Using the Arduino IDE 1.6.3 (latest) and added the Http dependency but now getting

error: no matching function for call to 'HttpClient::HttpClient(Client&)'
HttpClient http(_client);

It looks like version incompatibility ... this xively library development seems to have stalled, anyone still around ?

xively Library does not compile in Arduino 1.6.5

The library does not compile using Arduino 1.6.5. I count 9 warnings and 2 errors.
Here is output from the compiler.

In file included from C:\Users\username\Documents\Arduino\libraries\xively/Xively.h:2:0,
from XivelyTutorial.ino:8:
C:\Users\username\Documents\Arduino\libraries\xively/XivelyDatastream.h: In member function 'char XivelyDatastream::idChar(int)':
C:\Users\username\Documents\Arduino\libraries\xively/XivelyDatastream.h:43:90: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
return (_idType == DATASTREAM_STRING ? _idString[idx] : (idx > strlen(_idBuffer._buffer) ? '\0' : _idBuffer._buffer[idx]));
^

In file included from C:\Users\username\Documents\Arduino\libraries\xively\XivelyDatastream.cpp:1:0:
C:\Users\username\Documents\Arduino\libraries\xively/XivelyDatastream.h: In member function 'char XivelyDatastream::idChar(int)':
C:\Users\username\Documents\Arduino\libraries\xively/XivelyDatastream.h:43:90: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
return (_idType == DATASTREAM_STRING ? _idString[idx] : (idx > strlen(_idBuffer._buffer) ? '\0' : _idBuffer._buffer[idx]));
^

C:\Users\username\Documents\Arduino\libraries\xively/XivelyDatastream.h: In constructor 'XivelyDatastream::XivelyDatastream(String&, int)':
C:\Users\username\Documents\Arduino\libraries\xively/XivelyDatastream.h:52:6: warning: 'XivelyDatastream::_valueType' will be initialized after [-Wreorder]
int _valueType;
^

C:\Users\username\Documents\Arduino\libraries\xively/XivelyDatastream.h:50:9: warning: 'String XivelyDatastream::_idString' [-Wreorder]
String _idString;
^

C:\Users\username\Documents\Arduino\libraries\xively\XivelyDatastream.cpp:5:1: warning: when initialized here [-Wreorder]
XivelyDatastream::XivelyDatastream(String& aId, int aType)
^

In file included from C:\Users\username\Documents\Arduino\libraries\xively\XivelyDatastream.cpp:1:0:
C:\Users\username\Documents\Arduino\libraries\xively/XivelyDatastream.h: In constructor 'XivelyDatastream::XivelyDatastream(char*, int, int)':
C:\Users\username\Documents\Arduino\libraries\xively/XivelyDatastream.h:52:6: warning: 'XivelyDatastream::_valueType' will be initialized after [-Wreorder]
int _valueType;
^

C:\Users\username\Documents\Arduino\libraries\xively/XivelyDatastream.h:50:9: warning: 'String XivelyDatastream::_idString' [-Wreorder]
String _idString;
^

C:\Users\username\Documents\Arduino\libraries\xively\XivelyDatastream.cpp:10:1: warning: when initialized here [-Wreorder]
XivelyDatastream::XivelyDatastream(char* aIdBuffer, int aIdBufferSize, int aType)
^

In file included from C:\Users\username\Documents\Arduino\libraries\xively\XivelyDatastream.cpp:1:0:
C:\Users\username\Documents\Arduino\libraries\xively\XivelyDatastream.cpp: In member function 'int XivelyDatastream::updateValue(Stream&)':
C:\Users\username\Documents\Arduino\libraries\xively/XivelyDatastream.h:7:27: error: jump to case label [-fpermissive]

define DATASTREAM_STRING 0

^

C:\Users\username\Documents\Arduino\libraries\xively\XivelyDatastream.cpp:42:7: note: in expansion of macro 'DATASTREAM_STRING'
case DATASTREAM_STRING:
^

C:\Users\username\Documents\Arduino\libraries\xively\XivelyDatastream.cpp:38:7: error: crosses initialization of 'int len'
int len = aStream.readBytesUntil('\n', _value._valueBuffer._buffer, _value._valueBuffer._bufferSize);
^

C:\Users\username\Documents\Arduino\libraries\xively\XivelyDatastream.cpp:54:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^

Error compiling.

Make library easier to install in Arduino IDE

The library can't simply be downloaded and installed to the Arduino IDE because the filename isn't accepted. So, a user must download, unzip, and then change the name of the folder that contains the library files. This isn't a huge burden, but it'd be easy to fix. Just change the name of the folder to "Xively" instead of "xively_arduino-master" so that when someone downloads it they can install it straight away without any error messages.

screen shot 2014-06-05 at 11 40 10 am
screen shot 2014-06-05 at 11 39 49 am

Compiling Errors with Xively_DatatreamUpload

I'm totally new to Xively, trying out your sample code. When I compile Xively_DatastreamUpload, I get this sequence of errors, all of which point to XivelyClient.cpp.

Thanks, in advance for your help!

Users/admin/Documents/Arduino/libraries/Xively/XivelyClient.cpp: In member function 'int XivelyClient::put(XivelyFeed&, const char*)':
/Users/admin/Documents/Arduino/libraries/Xively/XivelyClient.cpp:12: error: 'HttpClient' was not declared in this scope
/Users/admin/Documents/Arduino/libraries/Xively/XivelyClient.cpp:12: error: expected `;' before 'http'
/Users/admin/Documents/Arduino/libraries/Xively/XivelyClient.cpp:15: error: 'http' was not declared in this scope
/Users/admin/Documents/Arduino/libraries/Xively/XivelyClient.cpp: In member function 'int XivelyClient::get(XivelyFeed&, const char*)':
/Users/admin/Documents/Arduino/libraries/Xively/XivelyClient.cpp:74: error: 'HttpClient' was not declared in this scope
/Users/admin/Documents/Arduino/libraries/Xively/XivelyClient.cpp:74: error: expected `;' before 'http'
/Users/admin/Documents/Arduino/libraries/Xively/XivelyClient.cpp:77: error: 'http' was not declared in this scope
/Users/admin/Documents/Arduino/libraries/Xively/XivelyClient.cpp:165: error: 'delay' was not declared in this scope

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.