Go Down

Topic: Interfacing with Pachube (One Wire sensor) (Read 1 time) previous topic - next topic

asselinpaul

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: [Select]
#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: [Select]
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

robtillaart


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.

Rob Tillaart

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

asselinpaul

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?

robtillaart

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).

Rob Tillaart

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

robtillaart

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
Rob Tillaart

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

asselinpaul

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.

robtillaart


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
Rob Tillaart

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

pantonvich

#7
Jun 26, 2011, 06:46 pm Last Edit: Jun 26, 2011, 07:07 pm by pantonvich Reason: 1
I think your pachube ip address is wrong - try 173,203,98,29

see: http://community.pachube.com/IP_address

pantonvich

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

Go Up