Show Posts
Pages: [1]
1  Using Arduino / Networking, Protocols, and Devices / Re: Standalone Gmail Notifier on: March 15, 2012, 03:04:20 pm
The original post was in the old forum which I thought was now read-only, did I miss something or did you?

Anyway, the above works great from a browser but long wordy URLs with authentication embedded are not possible for the Arduino and Ethernet lib AFAIK.  I guess it all comes back to Googles tricky authentication process once again.

I did get this working using Arduino code that talked to an intermediate PHP script on a web server.

Any other ideas on how to make this work with a stand-alone 'Duino and an enet shield?
2  Using Arduino / Networking, Protocols, and Devices / Standalone Gmail Notifier on: February 24, 2012, 05:05:57 pm
I noticed this topic http://arduino.cc/forum/index.php/topic,44958.msg325691.html#msg325691 had no replies so I thought I'd add one:

Seems the Gmail Atom Feed allows an easy way to get info on a gmail account using just a URL.

This URL returns some XML with a list of unread messages in your inbox:
     https://username@gmail.com:password@mail.google.com/mail/feed/atom/

Simply extract the number between the <fullcount> and </fullcount> tags to get the count.

The TextFinder library would make this pretty simple (http://www.arduino.cc/playground/Code/TextFinder)!

Also, you can check for unread messages using tags.  The following gets counts of unread messages tagged as "Family":
     https://username@gmail.com:password@mail.google.com/mail/feed/atom/Family

I hope this is useful!
Bob

3  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help with strtok function? on: January 16, 2009, 09:07:17 am
That was it!  Not sure why I didn't realize this by looking at the ref pages!  I guess this would allow you to define a string of several delimiters to tokenize with, pretty powerful.

The following now works as expected, thanks again!

Code:
//   strtok test                                                               */

#include <string.h>

#define MAX_STRING_LEN  20

char *record1 = "one two three";
char *record2 = "Hello there friend";
char *p, *i;

void setup() {

   Serial.begin(9600);
  
   Serial.println("Split record1: ");
   Serial.println(subStr(record1, " ", 1));
   Serial.println(subStr(record1, " ", 2));
   Serial.println(subStr(record1, " ", 3));

   Serial.println("Split record2: ");
   Serial.println(subStr(record2, " ", 1));
   Serial.println(subStr(record2, " ", 2));
   Serial.println(subStr(record2, " ", 3));
}

void loop () {
}

// Function to return a substring defined by a delimiter at an index
char* subStr (char* str, char *delim, int index) {
   char *act, *sub, *ptr;
   static char copy[MAX_STRING_LEN];
   int i;

   // Since strtok consumes the first arg, make a copy
   strcpy(copy, str);

   for (i = 1, act = copy; i <= index; i++, act = NULL) {
      //Serial.print(".");
      sub = strtok_r(act, delim, &ptr);
      if (sub == NULL) break;
   }
   return sub;

}

output:

Split record1:
one
two
three
Split record2:
Hello
there
friend
4  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help with strtok function? on: January 16, 2009, 08:43:45 am
Thanks mikalhart, that makes sense.  I changed:

    char copy[MAX_STRING_LEN];
to
    static char copy[MAX_STRING_LEN];

The strange chars are gone from the output, but still not what I'd expect.  There must be something else going on here?

Output:

Split record1:
one
two
t
Split record2:
Hello
t
ere

Thanks for your help on this!
5  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help with strtok function? on: January 16, 2009, 07:53:14 am
Ok, with that problem solved its on to the next mystery!

I'd like he subStr function below to return a sub-string at a certain index such that

    subStr("this is a test", " ", 2)

would return "is"

This seems to work but not consistently.  I suspect some problem with my string handling here but just not sure what.

Thanks for any help!

Code:
//   strtok test                                                               */

#include <string.h>

#define MAX_STRING_LEN  20

char *record1 = "one two three";
char *record2 = "Hello there friend";
char *p, *i;

void setup() {

   Serial.begin(9600);
  
   Serial.println("Split record1: ");
   Serial.println(subStr(record1, ' ', 1));
   Serial.println(subStr(record1, ' ', 2));
   Serial.println(subStr(record1, ' ', 3));

   Serial.println("Split record2: ");
   Serial.println(subStr(record2, ' ', 1));
   Serial.println(subStr(record2, ' ', 2));
   Serial.println(subStr(record2, ' ', 3));
}

void loop () {
}

// Function to return a substring defined by a delimiter at an index
char* subStr (char* str, char delim, int index) {
   char *act, *sub, *ptr;
   char copy[MAX_STRING_LEN];
   int i;

   // Since strtok consumes the first arg, make a copy
   strcpy(copy, str);

   for (i = 1, act = copy; i <= index; i++, act = NULL) {
      //Serial.print(".");
      sub = strtok_r(act, &delim, &ptr);
      if (sub == NULL) break;
   }
   return sub;

}

output:

Split record1:
one
two
tQQQ
Split record2:
Hello
t
eQQQ
6  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help with strtok function? on: January 14, 2009, 04:01:56 pm
Aha!  Thanks so much!  The following code works as expected:

Code:
#include <string.h>

char *record = "name:bob";
char *p, *i;

void setup() {

   Serial.begin(9600);
   Serial.println("Starting..");
  
    //  First strtok iteration
    p = strtok_r(record,":",&i);
    Serial.print(p);
    Serial.print(" = ");

    //  Second strtok iteration
    p = strtok_r(NULL,":",&i);
    Serial.print(p);
    Serial.println("");

}

void loop () {
}

Output:

Starting...
name = bob
7  Forum 2005-2010 (read only) / Syntax & Programs / Help with strtok function? on: January 14, 2009, 02:34:02 pm
Hello,

I'm attempting to use the strtok (string token) command but keep getting "strtok not declared in this scope" error.

Can anyone tell me what I'm missing here?

Thanks!

Code:

#include <string.h>

char *record = "name:bob";
char *p;

void setup() {

   Serial.begin(9600);
   Serial.println("Starting..");
  
    //  First strtok iteration
    p = strtok(record,":");
    Serial.print(p);
    Serial.print(" = ");

    //  Second strtok iteration
    p = strtok(NULL,":");
    Serial.println(p);

}

void loop () {
}
8  Forum 2005-2010 (read only) / Interfacing / Re: Parse XML to Extract Weather Data from Web on: January 19, 2009, 01:58:08 pm
Yes, I used this code with the "official" Arduino ethernet shield.  I'm sure it would probably work with some of the others as well.
9  Forum 2005-2010 (read only) / Interfacing / Parse XML to Extract Weather Data from Web on: January 12, 2009, 09:03:50 pm
I couldn't find anything like this posted so I thought I'd throw it out here.  I wanted the Arduino to get XML formatted weather data from a NOAA web site.  

This kind of thing definitely isn't Arduino's strong suite but it seems to work great.  As I'm no C lang guru, any hints for improvement appreciated.

Code:
//////////////////////////////////////////////
// Get XML formatted data from the web.
// 1/6/08 Bob S. - Created
//
//  Assumptions: single XML line looks like:
//    <tag>data</tag> or <tag>data
//
// Get current weather observation for Raleigh from weather.gov in XML format
//
//////////////////////////////////////////////

// Include description files for other libraries used (if any)
#include <string.h>
#include <Ethernet.h>

// Define Constants
// Max string length may have to be adjusted depending on data to be extracted
#define MAX_STRING_LEN  20

// Setup vars
char tagStr[MAX_STRING_LEN] = "";
char dataStr[MAX_STRING_LEN] = "";
char tmpStr[MAX_STRING_LEN] = "";
char endTag[3] = {'<', '/', '\0'};
int len;

// Flags to differentiate XML tags from document elements (ie. data)
boolean tagFlag = false;
boolean dataFlag = false;

// Ethernet vars
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 41 };
byte server[] = { 140, 90, 113, 200 }; // www.weather.gov

// Start ethernet client
Client client(server, 80);

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting WebWx");
  Serial.println("connecting...");
  Ethernet.begin(mac, ip);
  delay(1000);

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");    
    client.println();
    delay(2000);
  } else {
    Serial.println("connection failed");
  }  
}

void loop() {

  // Read serial data in from web:
  while (client.available()) {
    serialEvent();
  }

  if (!client.connected()) {
    //Serial.println();
    //Serial.println("Disconnected");
    client.stop();

    // Time until next update
    //Serial.println("Waiting");
    for (int t = 1; t <= 15; t++) {
      delay(60000); // 1 minute
    }

    if (client.connect()) {
      //Serial.println("Reconnected");
      client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");    
      client.println();
      delay(2000);
    } else {
      Serial.println("Reconnect failed");
    }      
  }
}

// Process each char from web
void serialEvent() {

   // Read a char
      char inChar = client.read();
   //Serial.print(".");
  
   if (inChar == '<') {
      addChar(inChar, tmpStr);
      tagFlag = true;
      dataFlag = false;

   } else if (inChar == '>') {
      addChar(inChar, tmpStr);

      if (tagFlag) {      
         strncpy(tagStr, tmpStr, strlen(tmpStr)+1);
      }

      // Clear tmp
      clearStr(tmpStr);

      tagFlag = false;
      dataFlag = true;      
      
   } else if (inChar != 10) {
      if (tagFlag) {
         // Add tag char to string
         addChar(inChar, tmpStr);

         // Check for </XML> end tag, ignore it
         if ( tagFlag && strcmp(tmpStr, endTag) == 0 ) {
            clearStr(tmpStr);
            tagFlag = false;
            dataFlag = false;
         }
      }
      
      if (dataFlag) {
         // Add data char to string
         addChar(inChar, dataStr);
      }
   }  
  
   // If a LF, process the line
   if (inChar == 10 ) {

/*
      Serial.print("tagStr: ");
      Serial.println(tagStr);
      Serial.print("dataStr: ");
      Serial.println(dataStr);
*/

      // Find specific tags and print data
      if (matchTag("<temp_f>")) {
            Serial.print("Temp: ");
         Serial.print(dataStr);
      }
      if (matchTag("<relative_humidity>")) {
            Serial.print(", Humidity: ");
         Serial.print(dataStr);
      }
      if (matchTag("<pressure_in>")) {
            Serial.print(", Pressure: ");
         Serial.print(dataStr);
         Serial.println("");
      }

      // Clear all strings
      clearStr(tmpStr);
      clearStr(tagStr);
      clearStr(dataStr);

      // Clear Flags
      tagFlag = false;
      dataFlag = false;
   }
}

/////////////////////
// Other Functions //
/////////////////////

// Function to clear a string
void clearStr (char* str) {
   int len = strlen(str);
   for (int c = 0; c < len; c++) {
      str[c] = 0;
   }
}

//Function to add a char to a string and check its length
void addChar (char ch, char* str) {
   char *tagMsg  = "<TRUNCATED_TAG>";
   char *dataMsg = "-TRUNCATED_DATA-";

   // Check the max size of the string to make sure it doesn't grow too
   // big.  If string is beyond MAX_STRING_LEN assume it is unimportant
   // and replace it with a warning message.
   if (strlen(str) > MAX_STRING_LEN - 2) {
      if (tagFlag) {
         clearStr(tagStr);
         strcpy(tagStr,tagMsg);
      }
      if (dataFlag) {
         clearStr(dataStr);
         strcpy(dataStr,dataMsg);
      }

      // Clear the temp buffer and flags to stop current processing
      clearStr(tmpStr);
      tagFlag = false;
      dataFlag = false;

   } else {
      // Add char to string
      str[strlen(str)] = ch;
   }
}

// Function to check the current tag for a specific string
boolean matchTag (char* searchTag) {
   if ( strcmp(tagStr, searchTag) == 0 ) {
      return true;
   } else {
      return false;
   }
}

10  Forum 2005-2010 (read only) / News / Re: Messenger library on: December 30, 2008, 02:31:47 pm
Yes, strings are memory hogs and are not easily handled in the Arduino environment.  But something like Messenger could sure open a lot of interfacing possibilities!

I'd love to see support that allowed me to pick a string out of a sequence by position perhaps like:

readStr(index)
returns the element at the index as a string (defined by the message separator).

Thanks for all you work on this so far!
11  Forum 2005-2010 (read only) / News / Re: Messenger library on: December 30, 2008, 10:38:36 am
This will be very useful!  I would love to use this with my new ethernet shield to parse caller ID messages and perhaps scrape web pages but, unfortunately, it doesn't support strings yet.

I would take this on myself but efficient string handling requires more C expertise than I have!

Any hope that string support will be added to the messenger library anytime soon?
Pages: [1]