Skip to content

Commit 737a95b

Browse files
committed
Adding more basic example. Renaming weather station example.
1 parent 3874dc2 commit 737a95b

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
Weather Shield Example
3+
By: Nathan Seidle
4+
SparkFun Electronics
5+
Date: June 10th, 2016
6+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7+
8+
This example prints the current humidity, air pressure, temperature and light levels.
9+
10+
The weather shield is capable of a lot. Be sure to checkout the other more advanced examples for creating
11+
your own weather station.
12+
13+
*/
14+
15+
#include <Wire.h> //I2C needed for sensors
16+
#include "SparkFunMPL3115A2.h" //Pressure sensor - Search "SparkFun MPL3115" and install from Library Manager
17+
#include "SparkFunHTU21D.h" //Humidity sensor - Search "SparkFun HTU21D" and install from Library Manager
18+
19+
MPL3115A2 myPressure; //Create an instance of the pressure sensor
20+
HTU21D myHumidity; //Create an instance of the humidity sensor
21+
22+
//Hardware pin definitions
23+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24+
const byte STAT_BLUE = 7;
25+
const byte STAT_GREEN = 8;
26+
27+
const byte REFERENCE_3V3 = A3;
28+
const byte LIGHT = A1;
29+
const byte BATT = A2;
30+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
31+
32+
//Global Variables
33+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
34+
long lastSecond; //The millis counter to see when a second rolls by
35+
36+
float tempf = 0; // [temperature F]
37+
//float baromin = 30.03;// [barom in] - It's hard to calculate baromin locally, do this in the agent
38+
//float dewptf; // [dewpoint F] - It's hard to calculate dewpoint locally, do this in the agent
39+
40+
float batt_lvl = 11.8; //[analog value from 0 to 1023]
41+
float light_lvl = 455; //[analog value from 0 to 1023]
42+
43+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
44+
45+
void setup()
46+
{
47+
Serial.begin(9600);
48+
Serial.println("Weather Shield Example");
49+
50+
pinMode(STAT_BLUE, OUTPUT); //Status LED Blue
51+
pinMode(STAT_GREEN, OUTPUT); //Status LED Green
52+
53+
pinMode(REFERENCE_3V3, INPUT);
54+
pinMode(LIGHT, INPUT);
55+
56+
//Configure the pressure sensor
57+
myPressure.begin(); // Get sensor online
58+
myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
59+
myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
60+
myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
61+
62+
//Configure the humidity sensor
63+
myHumidity.begin();
64+
65+
lastSecond = millis();
66+
67+
Serial.println("Weather Shield online!");
68+
}
69+
70+
void loop()
71+
{
72+
//Print readings every second
73+
if (millis() - lastSecond >= 1000)
74+
{
75+
digitalWrite(STAT_BLUE, HIGH); //Blink stat LED
76+
77+
lastSecond += 1000;
78+
79+
//Check Humidity Sensor
80+
float humidity = myHumidity.readHumidity();
81+
82+
if (humidity == 998) //Humidty sensor failed to respond
83+
{
84+
Serial.println("I2C communication to sensors is not working. Check solder connections.");
85+
86+
//Try re-initializing the I2C comm and the sensors
87+
myPressure.begin();
88+
myPressure.setModeBarometer();
89+
myPressure.setOversampleRate(7);
90+
myPressure.enableEventFlags();
91+
myHumidity.begin();
92+
}
93+
else
94+
{
95+
Serial.print("Humidity = ");
96+
Serial.print(humidity);
97+
Serial.print("%,");
98+
float temp_h = myHumidity.readTemperature();
99+
Serial.print(" temp_h = ");
100+
Serial.print(temp_h, 2);
101+
Serial.print("C,");
102+
103+
//Check Pressure Sensor
104+
float pressure = myPressure.readPressure();
105+
Serial.print(" Pressure = ");
106+
Serial.print(pressure);
107+
Serial.print("Pa,");
108+
109+
//Check tempf from pressure sensor
110+
float tempf = myPressure.readTempF();
111+
Serial.print(" temp_p = ");
112+
Serial.print(tempf, 2);
113+
Serial.print("F,");
114+
115+
//Check light sensor
116+
float light_lvl = get_light_level();
117+
Serial.print(" light_lvl = ");
118+
Serial.print(light_lvl);
119+
Serial.print("V,");
120+
121+
//Check batt level
122+
batt_lvl = get_battery_level();
123+
Serial.print(" VinPin = ");
124+
Serial.print(batt_lvl);
125+
Serial.print("V");
126+
127+
Serial.println();
128+
}
129+
130+
digitalWrite(STAT_BLUE, LOW); //Turn off stat LED
131+
}
132+
133+
delay(100);
134+
}
135+
136+
//Returns the voltage of the light sensor based on the 3.3V rail
137+
//This allows us to ignore what VCC might be (an Arduino plugged into USB has VCC of 4.5 to 5.2V)
138+
float get_light_level()
139+
{
140+
float operatingVoltage = analogRead(REFERENCE_3V3);
141+
142+
float lightSensor = analogRead(LIGHT);
143+
144+
operatingVoltage = 3.3 / operatingVoltage; //The reference voltage is 3.3V
145+
146+
lightSensor = operatingVoltage * lightSensor;
147+
148+
return (lightSensor);
149+
}
150+
151+
//Returns the voltage of the raw pin based on the 3.3V rail
152+
//This allows us to ignore what VCC might be (an Arduino plugged into USB has VCC of 4.5 to 5.2V)
153+
//Battery level is connected to the RAW pin on Arduino and is fed through two 5% resistors:
154+
//3.9K on the high side (R1), and 1K on the low side (R2)
155+
float get_battery_level()
156+
{
157+
float operatingVoltage = analogRead(REFERENCE_3V3);
158+
159+
float rawVoltage = analogRead(BATT);
160+
161+
operatingVoltage = 3.30 / operatingVoltage; //The reference voltage is 3.3V
162+
163+
rawVoltage = operatingVoltage * rawVoltage; //Convert the 0 to 1023 int to actual voltage on BATT pin
164+
165+
rawVoltage *= 4.90; //(3.9k+1k)/1k - multiple BATT voltage by the voltage divider to get actual system voltage
166+
167+
return (rawVoltage);
168+
}

firmware/Weather_Shield/Weather_Shield.ino renamed to firmware/Weather_Shield_Weather_Station/Weather_Shield_Weather_Station.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
88
Much of this is based on Mike Grusin's USB Weather Board code: https://www.sparkfun.com/products/10586
99
10+
This is a more advanced example of how to utilize every aspect of the weather shield. See the basic
11+
example if you're just getting started.
12+
1013
This code reads all the various sensors (wind speed, direction, rain gauge, humidty, pressure, light, batt_lvl)
1114
and reports it over the serial comm port. This can be easily routed to an datalogger (such as OpenLog) or
1215
a wireless transmitter (such as Electric Imp).

0 commit comments

Comments
 (0)