Go Down

Topic: Reading an email. (Read 3480 times) previous topic - next topic

HazardsMind

Ok enough about the strings.

This is a working code that uses arrays. If anyone wants to mod this, go ahead.

Code: [Select]
char StringIn[100];
int found;
char charData;
int i = 0;
int flag;
byte led = 13;


#include<string.h>
void setup()
{
  pinMode(led,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available())
  {
    charData = Serial.read(); //Hi, this is a test and my name is Andrew.

    if(charData =='.' || charData ==' ' || charData ==',' || charData =='\n') // if anyone of these is entered, it will compare it.
    {       
      found = strcmp("Test",StringIn); // compares the test string to the inputted string. NOTE: will return 0 if they are equal!

      if(found == 0) //are they equal?
       {
        Serial.println("got it");
        flag = 0;
       }

      else
       {
        if(!flag)
         {
          Serial.println("Scanning"); //not equal yet
          flag = 1;
         }
      }

      while(i!=0)
       {
        StringIn[i--] = 0;
       }
    }
    else{
      StringIn[i++] = charData;   
    }
    delay(20);
  }
}



NOTE: This code will not return anything if the test word is NOT found.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

strykeroz

Hi,

Here's another method I've used when searching for keywords in data streaming in via a serially-connected WiFly wifi bee.  A function that returns true if a searchstring (passed in searchString) is found in the serial buffer:
Code: [Select]
// --------------------------------------------------------------------------------------------
// read the WiFLy serial buffer until it's exhausted, looking for searchString
// return TRUE on finding searchString, leaving buffer at next character after
// the located searchString.  Return FALSE if searchString not found in buffer
// Does not retry until timeout, returns once buffer empty.
// Does NOT empty buffer after finding string.
// Parameters:
//   searchString:          the response we're searching for
//
boolean findInWiFlyBuffer(char* searchString) {
  char * ptr = searchString;                                   // iterator for matching
  char thisChar; 

  while(Serial.available() && *ptr) {                          // ends when found, or no more chars in buffer
    thisChar = Serial.read();                                  // IDE likes using a variable rather than direct comparison
    if(thisChar == *ptr) ptr++;                                // move to next searchSring character for comparison
    else ptr = searchString;                                   // back to start of searchString
  } // while
  return *ptr == 0 ? true : false;                             // returns true if comparison got to end of searchString (null char)
}
To test for the text "test" in the serial stream would simply be a call to:
Code: [Select]
if(findInWiFlyBuffer("test")) {
  // do stuff
}
If you need it to instead return the number of characters read before the string match, that is also easy done with a slight modification.  Note that in this method, everything up to the matched string is discarded from the serial buffer but a benefit is there is no need for a static array to be defined.

Cheers ! Geoff
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

zoomkat

To be of good practical use, the code needs to check the data stream for something special, at which point the data is captured into a string (or what ever) for further evaluation of the string content. 
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

strykeroz


To be of good practical use, the code needs to check the data stream for something special, at which point the data is captured into a string (or what ever) for further evaluation of the string content.  
In which case my function will be perfect - once it's found the keyword you're searching for, the next call to Serial.read() will be at the data you're wanting to interrogate.  All without creating any additional data structure to store it in.

That's precisely how I use it.

Geoff
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

zoomkat

I'm curious as to how the streaming data stream would be searched for more than one data string without being very cumbersome. With a saved data string the typical string search functions would seem to be easier to use (maybe why they were developed).
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

strykeroz

Hi

If you'd be wanting to create a int searchSerialBuffer(char * searchTerm1, char * searchTerm2) which would search for the first occurrence of either term, and return a value for which it found, that's entirely possible.  The only caveat that springs to mind is that the terms passed shouldn't be substrings of each other.

Can you give an example of what you'd be looking to do?

Geoff
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

Arrch


The issue as I understand it, in the two often referenced discussions, is said to possibly occurr in sub routines using Strings where used memory is normally expected to be freed when the sub routine is exited is not released.


That may be part of the issue, but memory fragmentation is another huge issue.

[/quote]I have yet to see any issues in simple code using Strings actually be corrected by changing to strings.[/quote]

Neither have I. I've also yet to see issues with using delay() to blink an LED but have seen plenty of issues with people using delay() in larger projects. Why not take a little bit of time to learn the alternative that isn't going to cause issues down the road?

Quote
Read the discussions and form your opinion.


I have and have, it's just different than yours.

PeterH


I'm curious as to how the streaming data stream would be searched for more than one data string without being very cumbersome.


I'd approach this by implementing a statefull filter class which was configured with the string/pattern of interest and had the incoming character stream fed through it. The internal state of each instance would record how far through the pattern had been matched so far, for example with a simple string match it would record the number of characters that had been matched. You could apply as many of these to the incoming stream as you wanted, and have control over the precedence between them (i.e. if one filter is matched, do you continue to apply other filters).
I only provide help via the forum - please do not contact me for private consultancy.

zoomkat

Quote
Why not take a little bit of time to learn the alternative that isn't going to cause issues down the road?


My issue is that 1) most of the time the "don't use Strings" statements come from very knowledgable persons who never correct the code at hand to show the newbie the alternative to using Strings, and 2) adding confusion to code troubleshooting by telling the newbie to not use Strings when Strings has nothing to do with the code issue at hand. These people never seem to walk their talk. Sad...
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

PeterH

I don't think that somebody recommending the use of c-strings has any obligation to provide a tutorial on how to use them, or to do the work themselves to make that change. C-strings and the runtime functions to manipulate them are ubiquitous and widely documented - unlike the Arduino String class which is unique to Arduino. As for knowing whether the use of Strings is contributing to the problem - given the way that subtle changes in the use of dynamic memory can dramatically influence the way the problem manifests, I don't see that it is feasible to say with any certainty whether String is or is not causing a given problem. Removing the use of String is a sensible step anyway if the sketch is behaving unexpectedly. It might cure the problem. If it doesn't, at least it rules that out as a potential cause and also eliminates the risk that future changes to the sketch will expose String problems.
I only provide help via the forum - please do not contact me for private consultancy.

PaulS

Quote
Read the discussions and form your opinion.

I have. It is my opinion that you are wrong. Not just wrong, but stubbornly wrong.

tacticalemu

So I finally had a chance to sit down and read all your replies and as much as I enjoyed the strings vs array battle that ensued, I think I should clarify my original post. Parsing the email for the line and determining the contents wasn't so much my issue, but I'm glad you all had plenty of opinions on the best way to go about that. I more need a nudge for how to actually get the email stored into the Arduino to be able to read it. I am not very familiar with how Arduino accesses the mailbox and how it can get retrieve the actual email. Once I can get the block of text into the Arduino, I should be good, but I have never tried accessing email from anything but a web browser and I'm not really sure where to start. (also pasting this into the original post)
If it ain't broke, fix it 'til it is.

strykeroz

Hi,

What are you using to connect the Arduino to the interwebs?  Do you have the Ethernet shield yet, or is that something open for options also?

Geoff
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

HazardsMind

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

tacticalemu


What are you using to connect the Arduino to the interwebs?  Do you have the Ethernet shield yet, or is that something open for options also?


I am currently using the Ethernet shield on loan from a friend as I wait for mine to come in. (free shipping is great, but slow as molasses.) I have also gotten the suggestion to use a Raspberry Pi as the connection and parser, and use the Arduino for the peripheral control, but I am always open to new suggestions if you know of an alternative way.
If it ain't broke, fix it 'til it is.

Go Up