Go Down

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

tacticalemu

Dec 19, 2012, 05:15 am Last Edit: Dec 22, 2012, 11:01 pm by tacticalemu Reason: 1
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.
If it ain't broke, fix it 'til it is.

HazardsMind

#1
Dec 19, 2012, 02:44 pm Last Edit: Dec 19, 2012, 03:28 pm by HazardsMind Reason: 1
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: [Select]

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
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PeterH

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.
I only provide help via the forum - please do not contact me for private consultancy.

HazardsMind

#3
Dec 19, 2012, 03:46 pm Last Edit: Dec 19, 2012, 05:29 pm by HazardsMind Reason: 1
Even simpler:

Code: [Select]
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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Arrch

#4
Dec 19, 2012, 05:04 pm Last Edit: Dec 19, 2012, 05:06 pm by Arrch Reason: 1
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: [Select]

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
}

HazardsMind

#5
Dec 19, 2012, 05:25 pm Last Edit: Dec 19, 2012, 05:37 pm by HazardsMind Reason: 1
@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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

HazardsMind

Update! mine didn't work properly before, but it does now, and Arrch code does not work at the moment.

Code: [Select]
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;
    }
  }
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

zoomkat

You may want to look into using TextFinder.h library. Seems to be made to do what you want to do.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

tacticalemu

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.
If it ain't broke, fix it 'til it is.

Arrch


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.

HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Arrch

#11
Dec 20, 2012, 10:38 pm Last Edit: Dec 20, 2012, 10:40 pm by Arrch Reason: 1

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.

zoomkat

#12
Dec 21, 2012, 12:21 am Last Edit: Dec 21, 2012, 01:38 am by zoomkat Reason: 1
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!  ;)
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Arrch


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


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?

zoomkat

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.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up