SerialWeather \ Learning \ Wiring ALPHA 1.0
Author: Hernando Barragán
Description: Wiring is an electronic sketchbook and hardware electronics for developing ideas. It is a context for learning fundamentals of computer programming and prototyping with electronics within the context of the electronic arts.
Keywords: Wiring, Processing, Interactive Media, Electronic Arts, Programming, C, C++, Hernando Barragán

Search wiring.org.co:

This example is for Wiring version 0024+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know.

Advanced Input, sparkfun usb weather board. by BARRAGAN
Demonstrates how to read data from the sparkfun usb weather board using the Serial library. The weather board includes temperature, humidity and barometric pressure. The weather board is connected to the Serial1 serial port.

    String buffer = "";  // buffer to read data from the weather board
    int ready = 0;       // used to mark when we have a complete string to process
    char val;            // use to read a byte from the serial
    unsigned long humidity=0, fahrenheit=0, celcius=0, SCPfahrenheit=0, presure=0;

    void setup() {
      Serial1.begin(9600);  // starts the second hardware serial port at 9600 to communicate with the weather board
      Serial.begin(9600);  // starts the serial at 9600
      pinMode(48, OUTPUT);  // turn ON the board LED for diagnostics
      digitalWrite(48, HIGH);
    }

    void loop() {
      while(Serial1.available() > 0) {  // if data vailable from the weather board
        val = Serial1.read();           // read it
        if((val != '\n')) {             // if no end of line
          buffer += val;                // add it to the buffer
          // buffer.concat(String(val)); // an alternative
        }
        else {  // if end of line reached, readu to parse the buffer
          ready = 1;
          break;
        }
      }
      if(ready == 1) {                // parse the buffer
        if(buffer.startsWith("#")) {  // verify if it is a good reading
          // Serial.println(buffer);  // just to see what we got
          processReading(buffer.toCharArray());  // parse the buffer extracting the data
        }
        buffer = "";  // clean up the buffer for next reading
        ready = 0;
      }
    }

    // parse the buffer received extracting the data
    void processReading(const char packet[]) {
      byte i;
      char* endptr;

      // start parsing
      i = 0;
      i++;
      humidity = strtod(&packet[i], &endptr);       // next field: humidity

      while(packet[i++] != ',');			// next field: fahrenheit
      fahrenheit = strtod(&packet[i], &endptr);

      while(packet[i++] != ',');			// next field: celcius
      celcius = strtod(&packet[i], &endptr);

      while(packet[i++] != ',');			// next field: SCPfahrenheit
      SCPfahrenheit = strtod(&packet[i], &endptr);

      while(packet[i++] != ',');			// next field: presure
      presure = strtod(&packet[i], &endptr);

      while(packet[i++] != ',');			// next field: counter

      while(packet[i++] != '$');			// next field: checksum

      Serial.print("humidity: ");
      Serial.print(humidity, DEC);
      Serial.print(" fahrenheit: ");
      Serial.print(fahrenheit, DEC);
      Serial.print(" celcius: ");
      Serial.print(celcius, DEC);
      Serial.print(" SCPfahrenheit: ");
      Serial.print(SCPfahrenheit, DEC);
      Serial.print(" presure: ");
      Serial.println(presure, DEC);
    }