Twitter & Arduino

Hi,

I'm trying to build a arduino that twitters if my tomatoes need watering, just like botanicalls. So far I was able to send a single twitter, but after that one twitter has been send I keep getting "connection failed"errors. If I want to send another twitter I need to reset the board.

Any hints/ideas are very appreciated :slight_smile:

This is the Code

#include <Ethernet.h>
#include <PString.h>
int moistureSensor1 = 0;
int moisture_val1;
int Relais1 =  8;

//////////// EINSTELUNGEN ///////////////////
 
// Etherneteinstellungen für das Arduino Board //
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 178, 5 };
byte gateway[] = { 192, 168, 178, 1 };
byte subnet[] = { 255, 255, 255, 0 };
 
// Twitter Settings //
byte server[] = { 168, 143, 171, 180 };   // IP von Twitter (default: 128.121.146.100)
#define TWITTERURL "/statuses/update.xml"  // URL zum Update Script
#define TWITTERUSERNAMEPW "dG9tYXRlbnBxxxxxxxxxxxxxxxxxxxxx"  // Base64 enkodet USERNAME:PASSWORT
 
Client client(server, 80);
char buffer[141];
PString mystring(buffer, sizeof(buffer));

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);  // Ethernet initalisieren
  pinMode(Relais1, OUTPUT);
  Serial.begin(9600);   // Serielle Verbindung für Statusmeldungen
  delay(1000);  // Kurz warten, um serielle Verbindung zu starten
}
 
void loop()
{ 
  
  
   moisture_val1=analogRead(moistureSensor1);
   Serial.println(moisture_val1);
   delay(1000);
   
   mystring.print("Actual resistance = ");  // start building tweet
   mystring.print(moisture_val1);
   Serial.println(buffer);
   sendTwitterUpdate(buffer);
    
fetchTwitterUpdate();
}
 
/// Functions ////
 
void sendTwitterUpdate(char* tweet)        // Nachricht an Twitter übermitteln
{
   Serial.println("connecting...");
 
  if (client.connect()) {
    Serial.println("connected");
 
    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()     // Rückmeldung von Twitter auslesen
{
    if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
 
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
}

Twitter doesn't allow you to post the same message over and over. The moisture level is not going to change significantly in one second, so you are trying to post the same message over and over.

Either don't post unless the moisture level has changed, or time-stamp each message so that it IS unique.

thanks for your hints,

I added a random number to the tweets just to make sure they differ enough but the problem is still the same. Even if I pull the plug of the moisture sensor and the value goes down to 0 I keep getting the "connection failed" errors.

#include <PString.h>

Are you using the (older) version that has a memory leak?