Show Posts
Pages: 1 2 3 [4] 5 6 ... 9
46  Using Arduino / Programming Questions / Re: The HATED for String objects - "To String, or not to String" on: September 25, 2012, 12:55:15 pm
Quote
Problem is that many people don't know there's a reason to take care until it bites them, and then they post about a problem with crashes. You don't see many posts from people who managed to stay within limits, like the 2K RAM limit.

This is what I have found. I've been trying to 'teach myself' how to code, I have no background in Electronics or coding. I see something like a string that seems to make sense if used in the way I want to use it. Two weeks later... no, start again. It breaks and I had no way of knowing why, or what to use instead.
47  Using Arduino / Programming Questions / Re: Ethernet sheild stops connecting after an extended period on: September 25, 2012, 08:39:54 am
I'm lost, This is printing jibberish.
Code:
void Ethercheck(){
 //Ethernet Client
   if (client.connected()) {
    if (client.available()) {
      // read incoming bytes:
      char inChar = client.read();
      //Serial.print (inChar);

     // add incoming byte to end of line:
      currentLine[currentIndex++] = inChar;
      currentLine[currentIndex] = '\0';
      Serial.print (currentLine);
       
      // if you get a newline, clear the line:
      if (inChar == '\n') {
        previousTweet == tweet;

      }
      // if the current line contains <text>, it will
      // be followed by the tweet:
      if (strncmp(currentLine,"<text>",6)==0) {
        // tweet is beginning. Clear the tweet string:
        readingTweet = true;
      }
      // if you're currently reading the bytes of a tweet,
      // add them to the tweet String:
      if (readingTweet) {
        if (inChar != '<') {
          tweet[tweetIndex++] = inChar;
          tweet[tweetIndex] = '\0';
         
        }
        else {
          // if you got a "<" character,
          // you've reached the end of the tweet:
          readingTweet = false;
         
           if (tweet != previousTweet){
            Serial.println(F("TWEET"));
            Serial.println(tweet);   
            // close the connection to the server:
            Serial.println(F("disconnecting"));
            client.stop();
               
                if (firstTweetRead != false){
                if (strncmp(tweet,makeCoffee, 16)==0) {
                 
                  //if( waterLevel is higher than said amount){
                  Serial.println (F("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"));
           Serial.println(F("disconnecting"));
           client.stop();
         
         }
        }
      }
    }
  }
  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);
    connectToServer();
   
  }
 
}



48  Using Arduino / Programming Questions / Re: Ethernet sheild stops connecting after an extended period on: September 25, 2012, 07:32:54 am
Loe and behold, I've fucked up again.

Code:
void Ethercheck(){
 //Ethernet Client
   if (client.connected()) {
    if (client.available()) {
      // read incoming bytes:
      char inChar = client.read();
     // Serial.print (inChar);

     // add incoming byte to end of line:
      currentLine[index++] = inChar;
      currentLine[index] = '\0';

      // if you get a newline, clear the line:
      if (inChar == '\n') {
        previousTweet == tweet;
      }
      // if the current line ends with <text>, it will
      // be followed by the tweet:
      if (strncmp(currentLine,"<text>")==0) {
        // tweet is beginning. Clear the tweet string:
        readingTweet = true;
      }
      // if you're currently reading the bytes of a tweet,
      // add them to the tweet String:
      if (readingTweet) {
        if (inChar != '<') {
          tweet[index++] = inChar;
        //  currentLine[index] = '\0';
         
        }
        else {
          // if you got a "<" character,
          // you've reached the end of the tweet:
          readingTweet = false;
           if (tweet != previousTweet){
          Serial.println(tweet);   
          // close the connection to the server:
          Serial.println(F("disconnecting"));
          client.stop();
               
                if (firstTweetRead != false){
                if (strncmp(tweet,"<text>")==0) {
                 
                  //if( waterLevel is higher than said amount){
                  Serial.println (F("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"));
           Serial.println(F("disconnecting"));
           client.stop();
         
         }
        }
      }
    }
  }
  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);
    connectToServer();
   
  }
 
}




Quote
setup
connecting to server...connected
making HTTP request...
Total count:1
connecting to server...

I'm not sure how to use the strncmp(). I thought this would be correct.
PaulS, Thanks for helping me with every stage of this project. I'd say I was teaching myself but that would be a lie.
49  Using Arduino / Programming Questions / Re: Ethernet sheild stops connecting after an extended period on: September 25, 2012, 06:58:07 am
When I strip the Ethercheck void back to just printing the client without the strings it works until my computer sleeps (at five hours). I think this means it is the strings causing the problem.

I currently use the .startsWith to find out if the tweet starts with the same words as makeCoffee in order to recognise a tweeted command. I am told by the console that I cannot use .startsWith with Char. I have researched a function called
Code:
memcmp();
Is this what I have to use to create a comparison between two chars?

Also if I cannot say
Code:
  currentLine += inChar;
When currentLine is a char, not a string.. how do I complete the function with currentLine as a char?

50  Using Arduino / Programming Questions / Re: Ethernet sheild stops connecting after an extended period on: September 24, 2012, 10:04:16 am
Quote
You should be able to use that with your code.

SurferTim,
Call me stupid, I don't know how to do that. I've been at it all day.

Quote
Just use plain old char arrays with a null terminator - classic 'C strings. The String class is an attempt to make these easier to use but in its current implementation introduces a memory leak (has this been fixed yet?) and also tends to lead to heap fragmentation. For an application where stability was important, I'd suggest you stay away from anything using dynamic heap memory allocation and that includes the String class.
PeterH,
I've also tried to turn the Strings into Chars but I keep getting problems with the comparison operators between things like tweet and inChar.


Here is the original code, tidied up a bit. I feel like I'm watching paint dry in the rain today.
lil help?

Code:
//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[] = { $$$$$$$$$$$$$$$ };
byte ip[] = {$$$$$$$$$$$$$4};
Twitter twitter("$$$$$$$$$$$$$$$$$$$$$$$$$$");



//Ethernet Client

EthernetClient client;
const unsigned long requestInterval = 15000;  // 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
String currentLine = "";            // string to hold the text from server
String previousLine = "";           //String to hold the previous text from server for comparison
String tweet = "";                  // string to hold the tweet
String makeCoffee = ">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
 
  //Ethernet Client
  currentLine.reserve(256);
  tweet.reserve(150);
 
  //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()) {
      // read incoming bytes:
      char inChar = client.read();

      // add incoming byte to end of line:
      currentLine += inChar;

      // if you get a newline, clear the line:
      if (inChar == '\n') {
        previousLine = "";
        previousLine = tweet;
        currentLine = "";
      }
      // if the current line ends with <text>, it will
      // be followed by the tweet:
      if ( currentLine.endsWith("<text>")) {
        // tweet is beginning. Clear the tweet string:
        readingTweet = true;
        tweet = "";
      }
      // if you're currently reading the bytes of a tweet,
      // add them to the tweet String:
      if (readingTweet) {
        if (inChar != '<') {
          tweet += inChar;
        }
        else {
          // if you got a "<" character,
          // you've reached the end of the tweet:
          readingTweet = false;
           if (tweet != previousLine){
          Serial.println(tweet);   
          // close the connection to the server:
          client.stop();
               
                if (firstTweetRead != false){
                if (tweet.startsWith (makeCoffee)){
                 
                  //if( waterLevel is higher than said amount){
                  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("no new tweets");
           client.stop();
         
         }
        }
      }
    }
  }
  else if (millis() - lastAttemptTime > requestInterval) {
    // if you're not connected, and fifteen seconds have passed since
    // your last connection, then attempt to connect again:
    connectToServer();
   
  }
 
}
 
 



void connectToServer() {
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    Serial.println(serverName);
    Serial.println("making HTTP request...");
    // make HTTP GET request to twitter:
    client.println("GET /1/statuses/user_timeline.xml?screen_name=$$$$$$$$$$$$$$&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
  }
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}   







void make(){
  Serial.println("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(", 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("failed : code ");
      Serial.println(status);
      Serial.println(tweetString);
      Serial.print("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("connection failed. Cannot Tweet");
  }
}
51  Using Arduino / Programming Questions / Re: Ethernet sheild stops connecting after an extended period on: September 23, 2012, 04:52:26 pm
No, I don't think I need one for this project.
52  Community / Website and Forum / Forum android app? on: September 23, 2012, 04:43:11 pm
Are there any free apps that access these forums?
It's a pain to keep having to use the internet browser.
53  Using Arduino / Programming Questions / Re: Ethernet sheild stops connecting after an extended period on: September 23, 2012, 04:41:08 pm
Are there alternatives?
54  Using Arduino / Programming Questions / Re: Ethernet sheild stops connecting after an extended period on: September 23, 2012, 02:42:21 pm
Well that didn't work at all. Even having the void in the sketch without telling it to run reset it before it had a chance to do anything. I didn't know that was possible.
55  Using Arduino / Programming Questions / Re: Ethernet sheild stops connecting after an extended period on: September 23, 2012, 02:32:36 pm
If I was to use this function

Code:
void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
Serial.println ("Cannot connect, resetting");
asm volatile ("  jmp 0");


where in here should I place the software_Reset(); ?

Code:
void connectToServer() {
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    Serial.println("making HTTP request...");
    // make HTTP GET request to twitter:
    client.println("GET /1/statuses/user_timeline.xml?screen_name=accountnamehere&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
  }
  else
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}   


56  Using Arduino / Programming Questions / Ethernet sheild stops connecting after an extended period on: September 23, 2012, 01:44:08 pm
I have read many accounts of the same thing happening.

My Arduino Ethernet shield (attached to an uno) retrieves XML data from twitter for about an hour and then stops connecting to the server... I think.
Serial monitor looks like this. It's all good up until the continuous "connecting to server..." At this point it has stopped functioning properly.

Quote
connecting to server...
making HTTP request...
no new tweets
connecting to server...
making HTTP request...
no new tweets
connecting to server...
making HTTP request...
no new tweets
connecting to server...
making HTTP request...
connecting to server...
connecting to server...
connecting to server...
connecting to server...
connecting to server...


I understand there may be a way of resetting the Ethernet shield without a reset of the Uno if I insert some kind of 'watchdog' function into this section of code...

Code:
void connectToServer() {
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    Serial.println("making HTTP request...");
    // make HTTP GET request to twitter:
    client.println("GET /1/statuses/user_timeline.xml?screen_name=accountnamehere&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
  }
  else
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}   



Is this correct?

I think it's funny how I heard no mention of this problem until I experienced it. Then when searching I find a constant stream of similar troubles dating back four years, with no clear antidote.

If you know how to fix this I will be very grateful.

Best Regards

Stuart
 

57  Using Arduino / General Electronics / Re: Capacitor codes on: September 22, 2012, 02:58:01 pm
If you have a smart phone there are some great free reference apps for these sorts of things. They are mostly good when you need your PC screen for other things.
58  Using Arduino / General Electronics / Re: What type of Relay to use? on: September 22, 2012, 02:54:29 pm
That's true.
The basics escape me today. Got a cold coming or something.
Thanks for the help.
59  Using Arduino / General Electronics / Re: What type of Relay to use? on: September 22, 2012, 07:38:37 am
I'm controlling it with the arduino with a relay. Yes.

I found the following information about the device

Input power W 1000
Rated voltage/Frequency V~Hz 220-240/50-60

I have a sparkfun beefcake PCB coming in the post just in case. It seems useful to have one sat around.

I buy all of my stuff through proto-pic.co.uk. They are very helpful.
60  Using Arduino / General Electronics / Re: What type of Relay to use? on: September 22, 2012, 06:48:42 am
Yes, ill advised indeed. I have a 220/250v AC5A relay. do I need a 13A instead?
Pages: 1 2 3 [4] 5 6 ... 9