I want to interface sparkfun’s usb weather board http://www.sparkfun.com/commerce/produc … ts_id=8311 with the Arduino, but I can’t get the serial communication working
I tried using the software serial library, but am new to this so I could get the lights blinking on the weather board indicating communication, but my serial.print would not show anything.
I found the following code, but the “string” type won’t compile:
#include <SoftwareSerial.h>
//Created August 15 2006
//Heather Dewey-Hagborg
//http://www.arduino.cc
#include <ctype.h>
#define bit9600Delay 84
#define halfBit9600Delay 42
#define bit4800Delay 188
#define halfBit4800Delay 94
byte rx = 6;
byte tx = 7;
byte SWval;
String buffer = String(" "); // buffer to read data from the weather board
int ready = 0; // used to mark when we have a complete string to process
int val; // use to read a byte from the serial
unsigned long humidity=0, fahrenheit=0, celcius=0, SCPfahrenheit=0, presure=0;
void setup() {
pinMode(rx,INPUT);
pinMode(tx,OUTPUT);
digitalWrite(tx,HIGH);
digitalWrite(13,HIGH); //turn on debugging LED
SWprint('h'); //debugging hello
SWprint('i');
SWprint(10); //carriage return
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 SWprint(int data)
{
byte mask;
//startbit
digitalWrite(tx,LOW);
delayMicroseconds(bit9600Delay);
for (mask = 0x01; mask>0; mask <<= 1) {
if (data & mask){ // choose bit
digitalWrite(tx,HIGH); // send 1
}
else{
digitalWrite(tx,LOW); // send 0
}
delayMicroseconds(bit9600Delay);
}
//stop bit
digitalWrite(tx, HIGH);
delayMicroseconds(bit9600Delay);
}
int SWread()
{
byte val = 0;
while (digitalRead(rx));
//wait for start bit
if (digitalRead(rx) == LOW) {
delayMicroseconds(halfBit9600Delay);
for (int offset = 0; offset < 8; offset++) {
delayMicroseconds(bit9600Delay);
val |= digitalRead(rx) << offset;
}
//wait for stop bit + extra
delayMicroseconds(bit9600Delay);
delayMicroseconds(bit9600Delay);
return val;
}
}
void loop()
{
SWval = SWread();
SWprint(toupper(SWval));
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.append(char(val)); // add it to the buffer
}
else { // if end of line reached, readu to parse the buffer
ready = 1;
break;
}
}
if(ready == 1) { // parse the buffer
if(buffer.contains("#")) { // verify if it is aggod reading
// Serial.println(buffer); // just to see what we got
processReading(buffer.toCharArray()); // parse the buffer extracting the data
}
buffer = String(""); // clean up the buffer for next reading
ready = 0;
}
}
// parse the buffer received extracting the data
void processReading(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++] != '
If someone could help me with the code I would be very grateful.); // 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);
}
If someone could help me with the code I would be very grateful.