New Jersey
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« on: December 18, 2012, 11:15:14 pm » |
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
Edison Member
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #1 on: December 19, 2012, 08:44:30 am » |
Check out this page. http://msdn.microsoft.com/en-us/library/efzty53k(v=vs.71).aspxthis 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. 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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6391
-
|
 |
« Reply #2 on: December 19, 2012, 09:08:16 am » |
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
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #3 on: December 19, 2012, 09:46:52 am » |
Even simpler: 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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1872
|
 |
« Reply #4 on: December 19, 2012, 11:04:26 am » |
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): 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
Edison Member
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #5 on: December 19, 2012, 11:25:34 am » |
@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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #6 on: December 19, 2012, 10:57:50 pm » |
Update! mine didn't work properly before, but it does now, and Arrch code does not work at the moment. 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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
0
Offline
Tesla Member
Karma: 50
Posts: 6552
Arduino rocks
|
 |
« Reply #7 on: December 19, 2012, 11:06:28 pm » |
You may want to look into using TextFinder.h library. Seems to be made to do what you want to do.
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #8 on: December 20, 2012, 03:21:05 pm » |
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
Edison Member
Karma: 41
Posts: 1872
|
 |
« Reply #9 on: December 20, 2012, 04:07:14 pm » |
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
Edison Member
Karma: 29
Posts: 1574
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #10 on: December 20, 2012, 04:20:43 pm » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1872
|
 |
« Reply #11 on: December 20, 2012, 04:38:50 pm » |
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
Tesla Member
Karma: 50
Posts: 6552
Arduino rocks
|
 |
« Reply #12 on: December 20, 2012, 06:21:17 pm » |
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! 
|
|
|
|
« Last Edit: December 20, 2012, 07:38:58 pm by zoomkat »
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1872
|
 |
« Reply #13 on: December 20, 2012, 07:32:16 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 50
Posts: 6552
Arduino rocks
|
 |
« Reply #14 on: December 20, 2012, 07:55:34 pm » |
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
|
|
|
|
|
|