element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Help with weather project codes!
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 6 replies
  • Subscribers 416 subscribers
  • Views 702 views
  • Users 0 members are here
Related

Help with weather project codes!

e14 Contributor
e14 Contributor over 12 years ago

Hey guys, i have done the following codes below, but it doesn't seem to be working for me. It shows in the serial port that i manage to connect but no information was extracted. I would also like to display it on a LCD 16x2 display. Any help will be great!

 

// Include description files for other libraries used (if any)

#include <SPI.h>

#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[] = {0x84, 0x2B, 0x2B, 0xAD, 0xEA, 0xDE };

IPAddress ip ( 172, 16, 156, 39 );

byte server[] = { 140, 90, 113, 200 }; // www.weather.gov

 

 

// Start ethernet client

EthernetClient client;

 

 

 

 

void setup()

{

  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for Leonardo only

  }

 

 

  // start the Ethernet connection:

  if (Ethernet.begin(mac) == 0) {

    Serial.println("Failed to configure Ethernet using DHCP");

    // no point in carrying on, so do nothing forevermore:

    // try to congifure using IP address instead of DHCP:

  Ethernet.begin(mac, ip);

  }

  Serial.println("Starting WebWx");

  Serial.println("connecting...");

  delay(1000);

 

 

  if (client.connect(server, 80)) {

    Serial.println("connected");

    client.println("GET /xml/current_obs/KRDU.xml HTTP/1.1");   

    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(server, 80)) {

      //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;

   }

}

Attachments:
image
  • Sign in to reply
  • Cancel
Parents
  • shabaz
    shabaz over 12 years ago

    Hi Cho Zi,

     

    Does it read anything from the site? i.e. if you try to print the first few characters received, what do you see?

    Please confirm that first.

    If that works, then it is likely that the tag parsing code is faulty, but it was a little hard to follow (when inserting

    code, highlight it and then click on the right arrow icon (looks like ">>") to see the "Syntax Highlighting" option.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 12 years ago in reply to shabaz

    hi, sorry what do you mean by printing the first few characters?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • e14 Contributor
    e14 Contributor over 12 years ago in reply to shabaz

    hi, sorry what do you mean by printing the first few characters?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • shabaz
    shabaz over 12 years ago in reply to e14 Contributor

    If you can confirm that you are receiving some data from the website, then you know that the fault elsewhere, perhaps in the parsing code that you have written.

    So far from the description it is not clear where the problem lies beyond connecting.

    Can you display the first few characters that you retrieve from the web page? If that works, then you have slightly narrowed down the problem further.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 12 years ago in reply to shabaz

    Ah, i see. Right now i am not in the office. So far i have only tried downloading the code and opening up the serial monitor to see how it goes. Isn't the data supposed to be displayed in the serial monitor?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • shabaz
    shabaz over 12 years ago in reply to e14 Contributor

    Cho Zi Yang wrote:

    Isn't the data supposed to be displayed in the serial monitor?

    Exactly; use it as a debug tool. Print (either to serial or display) the data (or just the first few characters received) without the tag parsing complication.

     

    Currently your code tries to receive data, parse it and then display.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 12 years ago in reply to shabaz

    I will try it tomorrow as soon as i get to my computer. Thanks for the advice.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube