I have been working on this and I think I have cracked two thirds of the problem.
on another forum post I found a method of clearing the c strings when they got too big.
void clearStr (char* str) {
int len = strlen(str);
for (int c = 0; c < len; c++) {
str[c] = 0;
}
}
I use this in every cycle after they are no longer needed.
void Ethercheck(){
//Ethernet Client
if (client.connected()) {
if (client.available()) {
finder.getString("<text>","<",tweet,141);
Serial.print(F("Incoming Tweet:"));
Serial.println(tweet);
if (strcmp(tweet,previousLine)!=0){
clearStr(previousLine);
Serial.print(F("New Tweet:"));
Serial.println(tweet);
Serial.print(F("Previous Tweet:"));
Serial.println(previousLine);
strcpy(previousLine, tweet);
if (firstTweetRead != false){
Serial.println ("first tweet Read");
if (strncasecmp(tweet,makeCoffee,14)==0){
Serial.println ("Making coffee");
tweetString.begin();
tweetString = " Making coffee now, random number: ";
randNumber = random(1000);
tweetString.print (randNumber);
make();
}
}
else{
firstTweetRead = true;
}
}
else {
Serial.println(F("no new tweets"));
}
client.stop();
clearStr(tweet);
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and fifteen seconds have passed since
// your last connection, then attempt to connect again:
client.flush();
totalCount++;
Serial.print (F("Total count:"));
Serial.println(totalCount);
Ethernet.maintain();
delay(100);
connectToServer();
}
}
Before I had three Strings and now I only have one
finder.get[glow=yellow,2,300]String[/glow]("<text>","<",tweet,141);
As the code takes roughly three times as long to crash now I suspect this getString function from textFinder is responsible. Although if the information is stored in char tweet[141]; and tweet gets cleared by void clearStr(); is getString the problem at all?
I understand PaulS suggested a way in which I use an index like this..
currentLine[currentIndex++] = inChar;
currentLine[currentIndex] = '\0';
I do not really know how to approach this method. An example would be welcome.
//setup for all libs
#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>
#include <EthernetUdp.h>
#include <PString.h>
#include <TextFinder.h>
int ledPin = 3;
byte mac[] = { 0x--, 0x--, 0x--, 0x--, 0x--, 0x-- };
byte ip[] = {--,---,---,--- };
Twitter twitter("-------------------------------------");
int len;
//Ethernet Client
int totalCount = 0;
EthernetClient client;
const unsigned long requestInterval = 30000; // delay between requests
char serverName[] = "api.twitter.com"; // twitter URL
boolean requested; // whether you've made a request since connecting
unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
char previousLine [141]; //String to hold the previous text from server for comparison
char tweet[141]; // string to hold the tweet
char makeCoffee[15] = "make me coffee";
boolean readingTweet = false; // if you're currently reading the tweet
TextFinder finder( client );
//TweetString
char buffer[140] = {0};
PString tweetString(buffer, sizeof(buffer));
long randNumber;
boolean firstTweetRead = false;
void setup(){
pinMode (ledPin, OUTPUT);
randomSeed(analogRead(5));
//Serial Reader
Serial.begin(9600); // Initialize serial port
//General
delay(1000);
Ethernet.begin(mac);
Serial.begin(9600);
//Ethernet Client
// attempt a DHCP connection:
Serial.println("setup");
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Serial.println("failed, trying manually");
Ethernet.begin(mac, ip);
}
// connect to Twitter:
connectToServer();
}
void loop(){
Ethercheck();
}
void Ethercheck(){
//Ethernet Client
if (client.connected()) {
if (client.available()) {
finder.getString("<text>","<",tweet,141);
Serial.print(F("Incoming Tweet:"));
Serial.println(tweet);
if (strcmp(tweet,previousLine)!=0){
clearStr(previousLine);
Serial.print(F("New Tweet:"));
Serial.println(tweet);
Serial.print(F("Previous Tweet:"));
Serial.println(previousLine);
strcpy(previousLine, tweet);
if (firstTweetRead != false){
Serial.println ("first tweet Read");
if (strncasecmp(tweet,makeCoffee,14)==0){
Serial.println ("Making coffee");
tweetString.begin();
tweetString = " Making coffee now, random number: ";
randNumber = random(1000);
tweetString.print (randNumber);
make();
}
}
else{
firstTweetRead = true;
}
}
else {
Serial.println(F("no new tweets"));
}
client.stop();
clearStr(tweet);
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and fifteen seconds have passed since
// your last connection, then attempt to connect again:
client.flush();
totalCount++;
Serial.print (F("Total count:"));
Serial.println(totalCount);
Ethernet.maintain();
delay(100);
connectToServer();
}
}
void clearStr (char* str) {
int len = strlen(str);
for (int c = 0; c < len; c++) {
str[c] = 0;
}
}
void connectToServer() {
// attempt to connect, and wait a millisecond:
Serial.print(F("connecting to server..."));
if (client.connect(serverName, 80)) {
Serial.println(F("connected"));
Serial.println(F("making HTTP request..."));
// make HTTP GET request to twitter:
client.println(F("GET /1/statuses/user_timeline.xml?screen_name=/*Twitter account name*/&count=1 HTTP/1.1"));
client.println(F("HOST: api.twitter.com"));
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}
void make(){
Serial.println(F("Tweeting ..."));
if (twitter.post(tweetString)) {
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);
if (status == 200) {
digitalWrite(ledPin, HIGH); //turn coffee maker on
Serial.println(F(", Reply was tweeted. Making coffee now."));
//Start the coffee making
//when water level is low
//Stop the coffee making and run the finished function
}
else {
Serial.print(F("failed : code "));
Serial.println(status);
Serial.println(tweetString);
Serial.print(F("new Tweet is this: "));
tweetString.begin();
tweetString = " Making coffee now, random number: ";
randNumber = random(1000);
tweetString.print (randNumber);
Serial.println(tweetString);
delay(10000);
return make();
}
}
else {
Serial.println(F("connection failed. Cannot Tweet"));
}
}
Just a pointer to an example of someone using the char [byte array] method will hopefully be enough.