Pages: [1] 2   Go Down
Author Topic: Reading an email.  (Read 3009 times)
0 Members and 1 Guest are viewing this topic.
New Jersey
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all. I am trying to see if there is a way to have arduino read an email, parse it for a specific line, and then based on that line perform an action.

example:

blahblahblahblah
blahblahblahblah
lineofinterest in the format of (trigger:action)
blahblahblahblah


effectively, there would be 4 or 5 different actions that may or may not appear in a list (action1, action2, action3) but the trigger at the beginning of the line is always going to be the same.

I have seen many examples of getting the Ethernet shield to check an account and see if there are emails existing, but i have not seen one yet about getting it to actually read the content of an email. any nudge in the right direction would be greatly appreciated

EDIT: 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.
« Last Edit: December 22, 2012, 05:01:25 pm by tacticalemu » Logged

If it ain't broke, fix it 'til it is.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3569
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check out this page.
http://msdn.microsoft.com/en-us/library/efzty53k(v=vs.71).aspx

this one may be easier to understand, the example can be found in the Arduino examples
http://arduino.cc/en/Tutorial/StringIndexOf

Im trying to simplify this for you myself, but maybe someone else already has a code you can try.

A very poor way of doing it would be to read in a character, store it in a string or array, and using an IF statement, compare it to the word you want.

Code:
pseudo:

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

if(charData == ' ' || charData == '\n') // if a space " " is found or a new line is made
{
   if(StringIn == "Andrew"){ //does the string match? :: made an edit from ' ' to " "
      Serial.println("Andrew was found!"); //string does match
      }
   else {
       Serial.println("Andrew was NOT found!"); //string does NOT match
       StringIn = ""; //clear string for new data
       }
     }
 else StringIn += charData; //if string does not equal " ", then store data into string
  }
}

EDIT: code is now compiled, but not tested. I dont have my stuff with me today, sorry.

Something along the lines of this. I do NOT reccommend this though
« Last Edit: December 19, 2012, 09:28:02 am by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Given that you have an Ethernet shield, what you're asking for is possible.

I assume the email would come from a mailbox on the public internet. In that case you would have to set up your network so that the Arduino was able to connect to the public internet.

You would need to find what access protocols the mail server supported, the network ports used (if non-standard) and what authentications standards it supported.

You'd need to write a sketch that used the supported protocols to access and authenticate with the mail server. You may be able to find an existing library for the protocols you use, or you may not. If not, you'd need to create your own implementation. It wouldn't be especially difficult to implement your own, but it wouldn't be trivial either.

You'd need to configure the Arduino with details of the mail server, mailbox address and credentials.

Once you have all that working, you would be able to retrieve emails from the mailbox. The mails could easily be too big for the Arduino to hold in memory so you would probably need to buffer them one line at a time. Your next problem will be to interpret the mail encoding to extract the text. If you only need to support plain text email, no work is needed. If you need to deal with MIME encoded emails then this could be very complicated to implement.

Once you have obtained the text, parsing it to recognise specific key words or patterns would be relatively straight forward, and very similar to examples you'll already find of recognising commands received over the serial port.

What you're asking for would be very much easier to do on a PC since you can use existing software to deal with the networking and protocol handling issues and you could easily implement some pattern matching in JScript, VBScript, or whatever other language you like working in. It's possible on the Arduino, but would require you to find or create quite a bit of functionality.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3569
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Even simpler:

Code:
void loop()
{
  if(Serial.available())
  {
     charData = Serial.read(); //Hi, my name is Andrew

       if(charData == ' ' || charData == '\n') // if a space " " is found or a new line is made
       {
           boolean tagged = StringIn.indexOf("Andrew"); //scan string for "Andrew", true if found
           tagged ? Serial.println("Andrew was found!"): Serial.println("Andrew was NOT found!"); //string does it match, or NOT
           StringIn = ""; //clear string for new data
       }
   }
   else StringIn += charData; //if string does not equal " ", then store data into string
}

again, code is compiled, but not tested.
« Last Edit: December 19, 2012, 11:29:28 am by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the string equivalent just as a demonstration of how easy it actually is and less prone to errors down the road (I avoided fixing the piss-poor indenting as part of the demonstration):

Code:
char StringIn[80];
index i=0;

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

if(charData == ' ' || charData == '\n') // if a space " " is found or a new line is made
{
    char *tagged = strstr(StringIn, "Andrew"); //scan string for "Andrew", true if found
     tagged!=0 ? Serial.println("Andrew was found!"): Serial.println("Andrew was NOT found!"); //string does it match, or NOT
     StringIn[0] = '\0'; //clear string for new data
     i=0;
       }
     }
 else StringIn[i++] = charData; //if string does not equal " ", then store data into string
 }
« Last Edit: December 19, 2012, 11:06:47 am by Arrch » Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3569
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@tacticalemu

So there ya go, my code uses the String Method and Arrch uses array method.
Now you said you were going to be reading from an email, so you will need to tweak this line a bit "char StringIn[80];" , Im not sure what the maximum size of an array is, but that should not matter if your getting the data and then clear the array.

But both should work.
« Last Edit: December 19, 2012, 11:37:50 am by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3569
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Update! mine didn't work properly before, but it does now, and Arrch code does not work at the moment.
 
Code:
String stringOne;
char charData, bodyTag;
byte led = 13;

void setup()
{
  pinMode(led,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available())
  {
    charData = Serial.read(); //Hi, my name is Andrew
    if(charData == '.' || charData == ' ' || charData == '\n')
    {
      bodyTag = stringOne.indexOf("on");
      bodyTag >= 0 ? digitalWrite(led, HIGH): digitalWrite(led, LOW);
      stringOne = '\0';
    }

    else {
      stringOne += charData;
    }
  }
}
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You may want to look into using TextFinder.h library. Seems to be made to do what you want to do.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

New Jersey
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys, I'll take a more in depth look at all your suggestions over the weekend, and go from there. I'm still kind of new to Arduino so I'm glad you all were able to at least give me a helpful nudge.
Logged

If it ain't broke, fix it 'til it is.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Update! mine didn't work properly before, but it does now, and Arrch code does not work at the moment.
I didn't make mine to work; I made it as a demonstration of how easy it is to use a string, rather than a String.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3569
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So you demonstrated something he couldn't even test, just to show him how the string works? If it doesn't work then how will he know that the Idea even works to begin with.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So you demonstrated something he couldn't even test, just to show him how the string works? If it doesn't work then how will he know that the Idea even works to begin with.

Forums are public; the OP isn't the only one who reads this thread. The point was that anybody who looked at your code, then mine, would realize that c style strings aren't these scary things that are severely limited in what they can do. I didn't post code that worked because you didn't. I simply took your code and converted it into a char array to demonstrate that using Strings aren't that much easier.

I'll make sure that the next time you post code, to assume it doesn't work.
« Last Edit: December 20, 2012, 04:40:37 pm by Arrch » Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I didn't make mine to work; I made it as a demonstration of how easy it is to use a string, rather than a String.

Guess it just goes to show that String wasn't actually the problem. Who would have thought!  smiley-wink
« Last Edit: December 20, 2012, 07:38:58 pm by zoomkat » Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Guess it just goes to show that String wasn't actually the problem. Who would have thought!  smiley-wink

Never said it was, but why use it when there is (as demonstrated) an easy alternative when it's known to be a habitual issue causer?
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Never said it was, but why use it when there is (as demonstrated) an easy alternative when it's known to be a habitual issue causer?

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. I have yet to see any issues in simple code using Strings actually be corrected by changing to strings. Read the discussions and form your opinion.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1] 2   Go Up
Jump to: