arduino pachube.com / cosm.com multiple data sources howto

CSV Parser Error: CSV is invalid. Incorrect number of fields.

I am playing with my arduino at the moment, datalogging to cosm.com (formerly pachube.com).

I had issues with the csv line, it should be of the format:
label1,value1\n
label2,value2\n
label3,value3

Mine was in the format
label1,value1,label2,value2,label3,value3

The below code works for me, but that is because I have an LCD screen connected via a 74hc595 as a 3-wire interface. The cosm code works as intended.

My feed is available at https://cosm.com/feeds/69526

#include <dht11.h>
#include "Ultrasonic.h"
#include <ShiftLCD.h>
#include <SPI.h>
#include <Ethernet.h>

#define APIKEY "" // your cosm api key
#define FEEDID 00000 // your feed ID
#define USERAGENT "Cosm Arduino Example (00000)" // user agent is the project name

// assign a MAC address for the ethernet controller.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192,168,0,123);

// initialize the library instance:
EthernetClient client;

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
char server[] = "api.cosm.com"; // name address for cosm API

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to cosm.com

// initialize the library with the numbers of the interface pins
ShiftLCD lcd(2, 4, 3);
Ultrasonic ultrasonic(6,7);
dht11 DHT11;
#define DHT11PIN 5

double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}

void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("testing...");
Serial.println("testing...");
lcd.setCursor(0,1);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
lcd.print("Failed DHCP");
Serial.println("Failed DHCP");
// DHCP failed, so use a fixed IP address:
Ethernet.begin(mac, ip);
}
}

unsigned int c = 0;
int chk;
void loop()
{
int a = ultrasonic.Ranging(CM);
int b;
if (c == 0)
chk = DHT11.read(DHT11PIN);
c++;
if(c >= 10)
c = 0;
lcd.setCursor(0,1);
lcd.print("C");
lcd.print((int)DHT11.temperature);
lcd.print(" ");
lcd.setCursor(14,0);
lcd.print(c);
lcd.print(" ");
lcd.setCursor(4,1);
lcd.print("H");
lcd.print((int)DHT11.humidity);
lcd.print(" ");
lcd.setCursor(8,1);
lcd.print("D");
lcd.print(b = (int)dewPoint(DHT11.temperature, DHT11.humidity));
lcd.print(" ");
lcd.setCursor(12,1);
lcd.print("F");
lcd.print((int)dewPointFast(DHT11.temperature, DHT11.humidity));
lcd.print(" ");

lcd.setCursor(0, 0);
lcd.print("Dist:");
lcd.print(a);
lcd.print("cm ");

// if there's incoming data from the net connection.
// send it out the Serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}

// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
client.stop();
}

// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
char str[64];
sprintf(str, "tempC,%d\nhumidity,%d\ndewPoint,%d",(int)DHT11.temperature,(int)DHT11.humidity,b);
//sprintf(str, "%d,%d,%d",(int)DHT11.temperature,(int)DHT11.humidity,b);
Serial.println(str);
sendData(str);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();

//delay(100);
}

// this method makes a HTTP connection to the server:
void sendData(char str[64]) {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.println("Host: api.cosm.com");
client.print("X-ApiKey: ");
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);
client.print("Content-Length: ");

// calculate the length of the sensor reading in bytes:
// 8 bytes for "sensor1," + number of digits of the data:
int thisLength = strlen(str);
client.println(thisLength);

// last pieces of the HTTP PUT request:
client.println("Content-Type: text/csv");
client.println("Connection: close");
client.println();

// here's the actual content of the PUT request:
//client.print("sensor1,");
Serial.println("printing str");
client.println(str);
Serial.println("finished update");
}
else {
// if you couldn't make a connection:
client.stop();
}
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}

// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:

int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}

3 thoughts on “arduino pachube.com / cosm.com multiple data sources howto”

  1. Nice setup of COSM. I was wondering if you could help. I can’t seem to get my DHT11 working with COSM. I don’t have the LCD like you and tried to delete that part but it won’t work. Any help or ideas would be great.
    Thanks..

    Andrew

    Safari 534.57.2 Mac OS
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
    1. Can you post your code and I will see where the problem is :)

      Google Chrome 22.0.1229.79 GNU/Linux 64 bits
      Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
  2. Can you tell me where you obtained all the libraries you #include in this project.
    I do not have “Ultrasonic.h” or I am using the LCD HD44780. So will this require making changes in your program? If so what whould those changes look like.
    Thank You

    Firefox 19.0 Windows 8
    Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge