Arduino + Twitter + Thermistor = Frustration

First off let me say I have tried several different variations of code to get this to work with no success.

Let me say this is just a test for a more elaborate project that my students are working on. They are going to attempt to pass data to twitter.

I can get the simple version to work using the twitter library but I'm at a loss figuring out how to send posts at 5 minute intervals. I have another sketch that is built specifically for posting temperature but I can not get it to work . The connection is fine but I get a Authentication error. I have tried every combination of user name password. Base64 encoding included. I will post all there sketches. I modified the first one to use the EthernetDHCP and DNS libraries. The second is the old school way and the third I was attempting to use the twitter library. Any help would be great.

The thing that I need to most is how to post data (sensor data) on intervals. Since it will be unmanned this is how we will monitor its progress.

I need to add that I can post to twitter using the simplepost sketch in the examples.

Complicated with modified library

/*
 * temperature_twitter.pde
 *
 * Sends twitters every RATE milliseconds (30 minutes in this example)
 * to the Twitter API.
 *
 * Hardware needed:
 *   Arduino Duemillinove
 *   Wiznet Ethernet Shield
 *   TMP-36 Temp Sensor on ADC port 0
 *   USB cable for serial monitoring (optional)
 *
 * Variables to change:
 *   RATE  number of milliseconds between tweets
 *   byte mac[] = { 0x00, 0x1E, 0x4C, 0x29, 0xB7, 0xFE };  // make this your own
 *   byte ip[] = { 192, 168, 2, 51 };  // set to your particular network config
 *   byte gateway[] = { 192, 168, 2, 1 };  // set to your router
 *   byte subnet[] = { 255, 255, 255, 0 };  // should work as-is but should reflect your class
 *   TWITTERUSERNAMEPW "USERNAME:PASSWORD"  // Base64 encoded 
 *   sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
 *
 *   Probably not necessary, but if they ever DO change
 *     byte server[] = { 128, 121, 146, 100 };   // Twitter IP (default: 128.121.146.100)
 *     #define TWITTERURL "/statuses/update.xml"  // Twitter Update Script URL
 *
 * Usage:
 *   Simply connect and reset.
 *   (Note: a tweet will be sent immediately upon reboot)
 */ 

#include <Ethernet.h>
#include <PString.h>
#include <EthernetDHCP.h>
#include <EthernetDNS.h>

/* 
Some credits
Twitter Code:
  shardy : http://aculei.net/~shardy/hacklabtoilet/
  samuel muller : http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234028200/1
  mats vaneslow : http://www.mats-vanselow.de/2009/02/08/arduino-lernt-twittern/
Pstring Library
  Mikal Hart : http://arduiniana.org/libraries/PString/
Temperature Stuff
  Limor Freid : http://www.ladyada.net/learn/sensors/tmp36.html
*/

// some constants for compile time
#define RATE 1800000  // rate of tweets in milliseconds (30 min)

// Ethernet Settings //
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  // make this your own
//byte ip[] = { 192, 168, 2, 51 };  // set to your particular network config
//byte gateway[] = { 192, 168, 2, 1 };  // set to your router
//byte subnet[] = { 255, 255, 255, 0 };  // should work as-is but should reflect your class
 
// Twitter Settings //
byte server[] = { 128, 121, 146, 100 };   // Twitter IP (default: 128.121.146.100)
#define TWITTERURL "/statuses/update.xml"  // Twitter Update Script URL
#define TWITTERUSERNAMEPW "User:Password"  // Base64 encoded 

Client client(server, 80);
 
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
                   //the resolution is 10 mV / degree centigrade with a
                   //500 mV offset to allow for negative temperatures

long time = 0;     // scratch pad for rate timer

float temperatureC;  // pretty self explanatory
float temperatureF;  // pretty self explanatory

char buffer[141];    // char buffer to build tweet
PString mystring(buffer, sizeof(buffer));  // constructor for Pstring renderer

void setup()
{
  EthernetDHCP.begin(mac);
  Serial.begin(9600);    // opens serial port for status/monitoring
  //Ethernet.begin(subnet);  // sets up ethernet port
  delay(1000);  // give it time to do its thing
  int reading = analogRead(sensorPin);  // grab a reading from the temp sensor
  float voltage = reading * 5.0 / 1024;  // convert it into a voltage
  temperatureC = (voltage - 0.5) * 100;  // convert it into degrees C
  temperatureF = (temperatureC * 9 / 5) + 32;  // and degrees F
  mystring.print("Temperature Degrees C = ");  // start building tweet
  mystring.print(temperatureC);
  mystring.print("  Temperature Degrees F = ");  
  mystring.println(temperatureF);
  sendTwitterUpdate(buffer);  // and send it! (1 time on reboot)
  time = millis();  // take note of the time
}

void loop()




{
  fetchTwitterUpdate();  // grab responses from ethernet

  if ((millis() - time > RATE))  // has RATE milliseconds elapsed?
  {
    mystring.begin();  // clear out the buffer
    int reading = analogRead(sensorPin);  // grab a reading from the sensor
    float voltage = reading * 5.0 / 1024;  // convert it to a voltage
    temperatureC = (voltage - 0.5) * 100;  // convert it to degrees C
    temperatureF = (temperatureC * 9 / 5) + 32;  // convert it to degrees F
    mystring.print("Temperature Degrees C = ");  // start rendering the tweet
    mystring.print(temperatureC);
    mystring.print("  Temperature Degrees F = ");
    mystring.println(temperatureF);
    sendTwitterUpdate(buffer);  // and send it
    time = millis();  // make note of millis()
  }
   EthernetDHCP.maintain();
}
 
/// Functions //// 
void sendTwitterUpdate(char* tweet)        // Send message to Twitter
{
  Serial.print(tweet);  // send what we're doing out serial port
  Serial.print(" - ");
  Serial.print("connecting... ");
 
  if (client.connect()) 
  {
    client.print("POST ");
    client.print(TWITTERURL);
    client.println(" HTTP/1.1 ");
    client.println("Host: twitter.com");
    client.print("Authorization: Basic ");
    client.println(TWITTERUSERNAMEPW);
    client.print("Content-Length: ");
    client.println(9+strlen(tweet));
    client.println("");
    client.println("status=");
    client.println(tweet);
    Serial.println("twitter message send");
  } 
  else 
  {
    Serial.println("connection failed.");
  } 
}

void fetchTwitterUpdate()     // get responses from Twitter
{
  if (client.available()) 
  {
    char c = client.read();  // grab a character
    Serial.print(c);  // shoot it out the serial port for debug
  }
 
  while(!client.connected()) 
  {
    client.stop();
  }
}

This is the original code.

/*
 * temperature_twitter.pde
 *
 * Sends twitters every RATE milliseconds (30 minutes in this example)
 * to the Twitter API.
 *
 * Hardware needed:
 *   Arduino Duemillinove
 *   Wiznet Ethernet Shield
 *   TMP-36 Temp Sensor on ADC port 0
 *   USB cable for serial monitoring (optional)
 *
 * Variables to change:
 *   RATE  number of milliseconds between tweets
 *   byte mac[] = { 0x00, 0x1E, 0x4C, 0x29, 0xB7, 0xFE };  // make this your own
 *   byte ip[] = { 192, 168, 2, 51 };  // set to your particular network config
 *   byte gateway[] = { 192, 168, 2, 1 };  // set to your router
 *   byte subnet[] = { 255, 255, 255, 0 };  // should work as-is but should reflect your class
 *   TWITTERUSERNAMEPW "USERNAME:PASSWORD"  // Base64 encoded 
 *   sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
 *
 *   Probably not necessary, but if they ever DO change
 *     byte server[] = { 128, 121, 146, 100 };   // Twitter IP (default: 128.121.146.100)
 *     #define TWITTERURL "/statuses/update.xml"  // Twitter Update Script URL
 *
 * Usage:
 *   Simply connect and reset.
 *   (Note: a tweet will be sent immediately upon reboot)
 */ 

#include <Ethernet.h>
#include <PString.h>

/* 
Some credits
Twitter Code:
  shardy : http://aculei.net/~shardy/hacklabtoilet/
  samuel muller : http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234028200/1
  mats vaneslow : http://www.mats-vanselow.de/2009/02/08/arduino-lernt-twittern/
Pstring Library
  Mikal Hart : http://arduiniana.org/libraries/PString/
Temperature Stuff
  Limor Freid : http://www.ladyada.net/learn/sensors/tmp36.html
*/

// some constants for compile time
#define RATE 1800000  // rate of tweets in milliseconds (30 min)

// Ethernet Settings //
byte mac[] = { 0x00, 0x1E, 0x4C, 0x29, 0xB7, 0xFE };  // make this your own
byte ip[] = { 192, 168, 2, 51 };  // set to your particular network config
byte gateway[] = { 192, 168, 2, 1 };  // set to your router
byte subnet[] = { 255, 255, 255, 0 };  // should work as-is but should reflect your class
 
// Twitter Settings //
byte server[] = { 128, 121, 146, 100 };   // Twitter IP (default: 128.121.146.100)
#define TWITTERURL "/statuses/update.xml"  // Twitter Update Script URL
#define TWITTERUSERNAMEPW "USERNAME:PASSWORD"  // Base64 encoded 

Client client(server, 80);
 
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
                   //the resolution is 10 mV / degree centigrade with a
                   //500 mV offset to allow for negative temperatures

long time = 0;     // scratch pad for rate timer

float temperatureC;  // pretty self explanatory
float temperatureF;  // pretty self explanatory

char buffer[141];    // char buffer to build tweet
PString mystring(buffer, sizeof(buffer));  // constructor for Pstring renderer

void setup()
{
  Serial.begin(9600);    // opens serial port for status/monitoring
  Ethernet.begin(mac, ip, gateway, subnet);  // sets up ethernet port
  delay(1000);  // give it time to do its thing
  int reading = analogRead(sensorPin);  // grab a reading from the temp sensor
  float voltage = reading * 5.0 / 1024;  // convert it into a voltage
  temperatureC = (voltage - 0.5) * 100;  // convert it into degrees C
  temperatureF = (temperatureC * 9 / 5) + 32;  // and degrees F
  mystring.print("Temperature Degrees C = ");  // start building tweet
  mystring.print(temperatureC);
  mystring.print("  Temperature Degrees F = ");  
  mystring.println(temperatureF);
  sendTwitterUpdate(buffer);  // and send it! (1 time on reboot)
  time = millis();  // take note of the time
}

void loop()
{
  fetchTwitterUpdate();  // grab responses from ethernet

  if ((millis() - time > RATE))  // has RATE milliseconds elapsed?
  {
    mystring.begin();  // clear out the buffer
    int reading = analogRead(sensorPin);  // grab a reading from the sensor
    float voltage = reading * 5.0 / 1024;  // convert it to a voltage
    temperatureC = (voltage - 0.5) * 100;  // convert it to degrees C
    temperatureF = (temperatureC * 9 / 5) + 32;  // convert it to degrees F
    mystring.print("Temperature Degrees C = ");  // start rendering the tweet
    mystring.print(temperatureC);
    mystring.print("  Temperature Degrees F = ");
    mystring.println(temperatureF);
    sendTwitterUpdate(buffer);  // and send it
    time = millis();  // make note of millis()
  }
}
 
/// Functions //// 
void sendTwitterUpdate(char* tweet)        // Send message to Twitter
{
  Serial.print(tweet);  // send what we're doing out serial port
  Serial.print(" - ");
  Serial.print("connecting... ");
 
  if (client.connect()) 
  {
    client.print("POST ");
    client.print(TWITTERURL);
    client.println(" HTTP/1.1");
    client.println("Host: twitter.com");
    client.print("Authorization: Basic ");
    client.println(TWITTERUSERNAMEPW);
    client.print("Content-Length: ");
    client.println(9+strlen(tweet));
    client.println("");
    client.println("status=");
    client.println(tweet);
    Serial.println("twitter message sent!");
  } 
  else 
  {
    Serial.println("connection failed.");
  } 
}

void fetchTwitterUpdate()     // get responses from Twitter
{
  if (client.available()) 
  {
    char c = client.read();  // grab a character
    Serial.print(c);  // shoot it out the serial port for debug
  }
 
  while(!client.connected()) 
  {
    client.stop();
  }
}

This is my attempt to use the Twitter Library.

#include <Ethernet.h>
#include <EthernetDHCP.h>
#include <EthernetDNS.h>
#include <Twitter.h>
#include <math.h>

double Thermister(int RawADC) {
 double Temp;
 Temp = log(((10240000/RawADC) - 10000));
 Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
 Temp = Temp - 273.15;            // Convert Kelvin to Celcius
 Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
 return Temp;
}

int Temp = 0;

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

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter(" this is the connection to twitter it works on simple txt");

// Message to post
char msg[] = "Temp";

void setup()
{
  delay(1000);
  EthernetDHCP.begin(mac);
  Serial.begin(9600);
  
  Serial.println("connecting ...");
  if (twitter.post(msg)) {
    int status = twitter.wait();
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }
}

void loop()
{
  Temp = (int(Thermister(analogRead(0))));
  EthernetDHCP.maintain();
  delay(1000);
}

One thing - you don't realize that the Basic Authentication parameter needs to be Base64 encoded, not plain text.

If you will notice in the very first post I noted that I tried base 64 encoding (not just plain txt), multiple sources multiple configurations.

I also tried plain text.

None of which worked.

J

Can anyone confirm that the basic authentication has not changed? I simply cannot find a problem. I tried a new account and a old account, all versions of encoding.

I also noticed that there are version with a space after the : in the password tried that to no avail.

Any help would be great.