Arduino code :
#include <Wire.h>
#include "SparkFunMPL3115A2.h"
#include "SparkFun_Si7021_Breakout_Library.h"
MPL3115A2 myPressure;
Weather myHumidity;
const byte WSPEED = 3;
const byte RAIN = 2;
const byte STAT1 = 7;
const byte STAT2 = 8;
const byte REFERENCE_3V3 = A3;
const byte LIGHT = A1;
const byte BATT = A2;
const byte WDIR = A0;
long lastSecond;
byte seconds;
byte seconds_2m;
byte minutes;
byte minutes_10m;
long lastWindCheck = 0;
volatile long lastWindIRQ = 0;
volatile byte windClicks = 0;
byte windspdavg[120];
#define WIND_DIR_AVG_SIZE 120
int winddiravg[WIND_DIR_AVG_SIZE];
float windgust_10m[10];
int windgustdirection_10m[10];
volatile float rainHour[60];
int winddir = 0;
float windspeedmph = 0;
float windgustmph = 0;
int windgustdir = 0;
float windspdmph_avg2m = 0;
int winddir_avg2m = 0;
float windgustmph_10m = 0;
int windgustdir_10m = 0;
float humidity = 0;
float temperature = 0;
float rainin = 0;
volatile float dailyrainin = 0;
float pressure = 0;
float batt_lvl = 11.8;
float light_lvl = 455;
volatile unsigned long raintime, rainlast, raininterval, rain;
void rainIRQ()
{
raintime = millis();
raininterval = raintime - rainlast;
if (raininterval > 10)
{
dailyrainin += 0.011;
rainHour[minutes] += 0.011;
rainlast = raintime;
}
}
void wspeedIRQ()
{
if (millis() - lastWindIRQ > 10)
{
lastWindIRQ = millis();
windClicks++;
}
}
void setup()
{
Serial.begin(9600);
Serial.println("Weather Shield Example");
pinMode(STAT1, OUTPUT);
pinMode(STAT2, OUTPUT);
pinMode(WSPEED, INPUT_PULLUP);
pinMode(RAIN, INPUT_PULLUP);
pinMode(REFERENCE_3V3, INPUT);
pinMode(LIGHT, INPUT);
myPressure.begin();
myPressure.setModeBarometer();
myPressure.setOversampleRate(7);
myPressure.enableEventFlags();
myHumidity.begin();
seconds = 0;
lastSecond = millis();
// attach external interrupt pins to IRQ functions
attachInterrupt(0, rainIRQ, FALLING);
attachInterrupt(1, wspeedIRQ, FALLING);
// turn on interrupts
interrupts();
Serial.println("Weather Shield online!");
}
void loop()
{
if (millis() - lastSecond >= 1000)
{
digitalWrite(STAT1, HIGH); //Blink stat LED
lastSecond += 1000;
if (++seconds_2m > 119) seconds_2m = 0;
float currentSpeed = get_wind_speed();
windspeedmph = currentSpeed;
int currentDirection = get_wind_direction();
windspdavg[seconds_2m] = (int)currentSpeed;
winddiravg[seconds_2m] = currentDirection;
if (currentSpeed > windgust_10m[minutes_10m])
{
windgust_10m[minutes_10m] = currentSpeed;
windgustdirection_10m[minutes_10m] = currentDirection;
}
if (currentSpeed > windgustmph)
{
windgustmph = currentSpeed;
windgustdir = currentDirection;
}
if (++seconds > 59)
{
seconds = 0;
if (++minutes > 59) minutes = 0;
if (++minutes_10m > 9) minutes_10m = 0;
rainHour[minutes] = 0;
windgust_10m[minutes_10m] = 0;
}
printWeather();
digitalWrite(STAT1, LOW); //Turn off stat LED
}
delay(100);
}
void calcWeather()
{
winddir = get_wind_direction();
float temp = 0;
for (int i = 0 ; i < 120 ; i++)
temp += windspdavg[i];
temp /= 120.0;
windspdmph_avg2m = temp;
long sum = winddiravg[0];
int D = winddiravg[0];
for (int i = 1 ; i < WIND_DIR_AVG_SIZE ; i++)
{
int delta = winddiravg[i] - D;
if (delta < -180)
D += delta + 360;
else if (delta > 180)
D += delta - 360;
else
D += delta;
sum += D;
}
winddir_avg2m = sum / WIND_DIR_AVG_SIZE;
if (winddir_avg2m >= 360) winddir_avg2m -= 360;
if (winddir_avg2m < 0) winddir_avg2m += 360;
windgustmph_10m = 0;
windgustdir_10m = 0;
for (int i = 0; i < 10 ; i++)
{
if (windgust_10m[i] > windgustmph_10m)
{
windgustmph_10m = windgust_10m[i];
windgustdir_10m = windgustdirection_10m[i];
}
}
humidity = myHumidity.getRH();
temperature = myPressure.readTemp();
rainin = 0;
for (int i = 0 ; i < 60 ; i++)
rainin += rainHour[i];
pressure = myPressure.readPressure();
light_lvl = get_light_level();
batt_lvl = get_battery_level();
}
float get_light_level()
{
float operatingVoltage = analogRead(REFERENCE_3V3);
float lightSensor = analogRead(LIGHT);
operatingVoltage = 3.3 / operatingVoltage;
lightSensor = operatingVoltage * lightSensor;
return (lightSensor);
}
float get_battery_level()
{
float operatingVoltage = analogRead(REFERENCE_3V3);
float rawVoltage = analogRead(BATT);
operatingVoltage = 3.30 / operatingVoltage;
rawVoltage = operatingVoltage * rawVoltage;
rawVoltage *= 4.90;
return (rawVoltage);
}
float get_wind_speed()
{
float deltaTime = millis() - lastWindCheck;
deltaTime /= 1000.0;
float windSpeed = (float)windClicks / deltaTime;
windClicks = 0;
lastWindCheck = millis();
windSpeed *= 1.492;
return (windSpeed);
}
int get_wind_direction()
{
unsigned int adc;
adc = analogRead(WDIR);
if (adc < 380) return (113);
if (adc < 393) return (68);
if (adc < 414) return (90);
if (adc < 456) return (158);
if (adc < 508) return (135);
if (adc < 551) return (203);
if (adc < 615) return (180);
if (adc < 680) return (23);
if (adc < 746) return (45);
if (adc < 801) return (248);
if (adc < 833) return (225);
if (adc < 878) return (338);
if (adc < 913) return (0);
if (adc < 940) return (293);
if (adc < 967) return (315);
if (adc < 990) return (270);
return (-1);
}
void printWeather()
{
calcWeather();
Serial.println();
Serial.print("winddir=");
Serial.print(winddir);
Serial.println();
Serial.print("windspeedmph=");
Serial.print(windspeedmph, 1);
Serial.println();
Serial.print("windgustmph=");
Serial.print(windgustmph, 1);
Serial.println();
Serial.print("windgustdir=");
Serial.print(windgustdir);
Serial.println();
Serial.print("windspdmph_avg2m=");
Serial.print(windspdmph_avg2m, 1);
Serial.println();
Serial.print("winddir_avg2m=");
Serial.print(winddir_avg2m);
Serial.println();
Serial.print("windgustmph_10m=");
Serial.print(windgustmph_10m, 1);
Serial.println();
Serial.print("windgustdir_10m=");
Serial.print(windgustdir_10m);
Serial.println();
Serial.print("humidity=");
Serial.print(humidity, 1);
Serial.println();
Serial.print("tempf=");
Serial.print(temperature, 1);
Serial.println();
Serial.print("rainin=");
Serial.print(rainin, 2);
Serial.println();
Serial.print("dailyrainin=");
Serial.print(dailyrainin, 2);
Serial.println();
Serial.print("pressure=");
Serial.print(pressure, 2);
Serial.println();
Serial.print("batt_lvl=");
Serial.print(batt_lvl, 2);
Serial.println();
Serial.print("light_lvl=");
Serial.print(light_lvl, 2);
Serial.println();
Serial.print("");
Serial.println();
delay(3000);
}
Processing code:
import processing.serial.*;
float winddir;
float windspeedmph;
float windgustmph;
float windgustdir;
float windspdmph_avg2m;
float winddir_avg2m;
float windgustmph_10m;
float windgustdir_10m;
float humidity;
float tempf;
float rainin;
float dailyrainin;
float pressure;
float batt_lvl;
float light_lvl;
String myString = null;
Serial myPort;
void setup()
{
size(700,700);
String portName = Serial.list()[5];
myPort = new Serial(this, portName, 9600);
myPort.clear();
myString = myPort.readStringUntil('\n');
myString = null;
}
void draw()
{
while ( myPort.available() > 0)
{
myString = myPort.readStringUntil('\n');
if (myString != null) {
println(myString);
String myString = myPort.readString();
String[] q = splitTokens(myString);
winddir = parseFloat(q[0]);
windspeedmph = parseFloat(q[1]);
windgustmph = parseFloat(q[2]);
windgustdir = parseFloat(q[3]);
windspdmph_avg2m = parseFloat(q[4]);
winddir_avg2m = parseFloat(q[5]);
windgustmph_10m = parseFloat(q[6]);
windgustdir_10m = parseFloat(q[7]);
humidity = parseFloat(q[8]);
tempf = parseFloat(q[9]);
rainin = parseFloat(q[10]);
dailyrainin = parseFloat(q[11]);
pressure = parseFloat(q[12]);
batt_lvl = parseFloat(q[13]);
light_lvl = parseFloat(q[14]);
background(0);
textSize(20);
text("Wind direction:",50,50);
text(winddir,250,50);
text("Wind speed:" ,50,100);
text(windspeedmph,250,100);
text("Wind gust:",50,150);
text(windgustmph,250,150);
text("Wind gust direction:",50,200);
text(windgustdir,250,200);
text("Humidity:",50,250);
text(humidity,250,250);
text("Temperature",50,300);
text(tempf,250,300);
text("Rainfall:",50,350);
text(rainin,250,350);
text("Daily rain:",50,400);
text(dailyrainin,250,400);
text("Pressure",50,450);
text(pressure,250,450);
text("Battery level:",50,500);
text(batt_lvl,250,500);
text("Light level:",50,550);
text(light_lvl,250,550);
}
}
}