Pages: [1]   Go Down
Author Topic: Interfacing with Pachube (One Wire sensor)  (Read 1680 times)
0 Members and 1 Guest are viewing this topic.
Madrid, Spain
Offline Offline
Newbie
*
Karma: 0
Posts: 41
void guarantee{};
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm having some troubles interfacing with Pachube. I'm running an Arduino with an ethernet shield and a One Wire Dallas thermometer.
Each component works correctly and the sensor outputs a valide temperature.
The sketch performs well (as you can see from the start of the  serial log) but than fails at connecting to the server. What is going wrong?
I'm pretty sure my ethernet connection is valide since it worked with my previous project involving Telnet.

Here is my code followed by the serial log:

Code:
#include <SPI.h>

#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>

char PACHUBE_API_STRING[] = "a6714b6a217827edadfd003843c03c259a08add554eda3871b844612eddc6819";  // Your API key
int PACHUBE_FEED_ID = 25024; // Your feed ID
// Digital IO port used for one wire interface
int ONE_WIRE_BUS = 8 ;
// Ethernet mac address - this needs to be unique
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// IP addres of www.pachube.com
byte server[] = { 209,40,205,190 };
// Arduino address
byte ip[] = { 192,168,1, 226 };
byte gateway[] = { 192,168,1, 1 };
byte subnet[] = { 255, 255, 0, 0 };
char version[] = "PachubeClient Ver 0.01c";
#define CRLF "rn"
// simple web client to connect to Pachube.com
Client client (server, 80);
// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// 1wire device address
DeviceAddress thermometer;
void setup()
{
   // Note: Ethernet shield uses digitial IO pins 10,11,12, and 13
   Serial.begin(9600);
   Serial.println(version);
   Serial.println();
   // locate devices on the 1Wire bus
   Serial.print("Locating devices on 1Wire bus...");
   sensors.begin();
   int count = sensors.getDeviceCount();
   Serial.print("Found ");
   Serial.print( count );
   Serial.println(" devices on 1wire bus");
   // select the first sensor
   for ( int i=0; i<count; i++ )
   {
      if ( sensors.getAddress(thermometer, i) )
      {
         Serial.print("1wire device ");
         Serial.print(i);
         Serial.print(" has address: ");
         printAddress(thermometer);
         Serial.println();
      }
      else
      {
         Serial.print("Unable to find address for 1wire device ");
         Serial.println( i );
      }
   }
   // show the addresses we found on the bus
   Serial.print("Using 1wire device: ");
   printAddress(thermometer);
   Serial.println();
   // set the resolution to 9 bit
   sensors.setResolution(thermometer, 9);
   Serial.print("Initializing ethernet...");
   delay(5000);
   Ethernet.begin(mac, ip, gateway);
   delay(5000);
   Serial.println(" done.");
}
void sendData()
{
   float temp = sensors.getTempC(thermometer);
   //float temp = sensors.getTempF(thermometer);
   Serial.print("Temp=");
   Serial.println(temp);
   Serial.println("connecting...");
   if (client.connect())
   {
      Serial.println("connected");
      client.print(
         "PUT /api/feeds/" );
      client.print(PACHUBE_FEED_ID);
      client.print(".csv HTTP/1.1" CRLF
                   "User-Agent: Fluffy Arduino Ver 0.01" CRLF
                   "Host: www.pachube.com" CRLF
                   "Accept: */" "*" CRLF  // need to fix this
                   "X-PachubeApiKey: " );
      client.print(PACHUBE_API_STRING);
      client.print( CRLF
                    "Content-Length: 5" CRLF
                    "Content-Type: application/x-www-form-urlencoded" CRLF
                    CRLF );
      client.println(temp);
      unsigned long reqTime = millis();
      // wait for a response and disconnect
      while ( millis() < reqTime + 10000) // wait 10 seconds for response
      {
         if (client.available())
         {
            char c = client.read();
            Serial.print(c);
         }
         if (!client.connected())
         {
            Serial.println();
            Serial.println("server disconnected");
            break;
         }
      }
      Serial.println("client disconnecting");
      Serial.println("");
      client.stop();
   }
   else
   {
      Serial.println("connection failed");
   }
}
void printAddress(DeviceAddress deviceAddress)
{
   for (uint8_t i = 0; i < 8; i++)
   {
      if (deviceAddress[i] < 16) Serial.print("0");
      Serial.print(deviceAddress[i], HEX);
   }
}
void loop()
{
   sensors.requestTemperatures(); // Send the command to get temperatures
   sendData();
   delay( ( 5l * 60l * 1000l) - 11000l  ); // wait 5 minutes
}

Serial log:

Code:
PachubeClient Ver 0.01c

Locating devices on 1Wire bus...Found 1 devices on 1wire bus
1wire device 0 has address: 28830526030000E2
Using 1wire device: 28830526030000E2
Initializing ethernet... done.
Temp=33.00
connecting...
connection failed
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


When you look at the code of client.connect()  - yes its diving in the deep time - there are several places where the code returns 0 (failure).

by adding a digitalWrite(LEDPIN, HIGH); - LEDPIN is a free I/o pin with a led - you can check where (and why) the connection failed.

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Madrid, Spain
Offline Offline
Newbie
*
Karma: 0
Posts: 41
void guarantee{};
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
yes its diving in the deep time - there are several places where the code returns 0 (failure).
I don't understand what you mean?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

This can also be a cause as it is maybe the most reused mac address. Try to use an unique one (mac address of an unused PC).

 
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry I had to be more explicit,

I meaned in - C:\Program Files (x86)\arduino-0022\libraries\Ethernet\Client.cpp - is the code of the client.connect() function.

Rob
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Madrid, Spain
Offline Offline
Newbie
*
Karma: 0
Posts: 41
void guarantee{};
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sorry but I still don't understand.  The MAC address is fine (tried with another sketch right now).
How will looking at the code of the client connect function help me.
I'm a noob, sorry again.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The client.connected() call in your code fails. The library code shows there are several places where it can fail due to different reasons. You need to dive into that to understand why the connection failed - which if statement made it fail, -  network debugging is not easy.

It would have been better if Client.connect() returned a negative value if it failed, indicating the reason, but it isn't. (although noone stops you to recode it for debugging purpose).

Rob
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Atlanta, GA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think your pachube ip address is wrong - try 173,203,98,29

see: http://community.pachube.com/IP_address
« Last Edit: June 26, 2011, 12:07:44 pm by pantonvich » Logged

Atlanta, GA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you should also not post your api key - you should re-create it or someone will hack your account
Logged

Pages: [1]   Go Up
Jump to: