Advertisement
GravityDeficient

WEATHERSHIELD-WUNDERGROUND

Feb 19th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.31 KB | None | 0 0
  1. // This #include statement was automatically added by the Particle IDE.
  2. #include <SparkFun_Photon_Weather_Shield_Library.h>
  3. #include <SparkJson.h>
  4. #include "math.h"   //For Dew Point Calculation
  5.  
  6. //Station Identification
  7. char ID [] = "xxxxxxxxxx"; //Your station ID here
  8. char PASSWORD [] = "xxxxxxxx"; //your Weather Underground password here
  9.  
  10. float humidity = 0;
  11. float humTempF = 0;  //humidity sensor temp reading, fahrenheit
  12. float humTempC = 0;  //humidity sensor temp reading, celsius
  13. float baroTempF = 0; //barometer sensor temp reading, fahrenheit
  14. float baroTempC = 0; //barometer sensor temp reading, celsius
  15. float tempF = 0;     //Average of the sensors temperature readings, fahrenheit
  16. float tempC = 0;     //Average of the sensors temperature readings, celsius
  17. float dewptF = 0;
  18. float dewptC = 0;
  19. float pascals = 0;
  20. float inches = 0;
  21.  
  22. //Create Instance of HTU21D or SI7021 temp and humidity sensor and MPL3115A2 barometric sensor
  23. Weather sensor;
  24.  
  25. void setup() {
  26.     Serial.begin(9600);   // open serial over USB at 9600 baud
  27.  
  28.     //Initialize the I2C sensors and ping them
  29.     sensor.begin();
  30.  
  31.     /*You can only receive acurate barrometric readings or acurate altitiude
  32.     readings at a given time, not both at the same time. The following two lines
  33.     tell the sensor what mode to use. You could easily write a function that
  34.     takes a reading in one made and then switches to the other mode to grab that
  35.     reading, resulting in data that contains both acurate altitude and barrometric
  36.     readings. For this example, we will only be using the barometer mode. Be sure
  37.     to only uncomment one line at a time. */
  38.     sensor.setModeBarometer();//Set to Barometer Mode
  39.     //baro.setModeAltimeter();//Set to altimeter Mode
  40.  
  41.     //These are additional MPL3115A2 functions the MUST be called for the sensor to work.
  42.     sensor.setOversampleRate(7); // Set Oversample rate
  43.     //Call with a rate from 0 to 7. See page 33 for table of ratios.
  44.     //Sets the over sample rate. Datasheet calls for 128 but you can set it
  45.     //from 1 to 128 samples. The higher the oversample rate the greater
  46.     //the time between data samples.
  47.  
  48.     sensor.enableEventFlags(); //Necessary register calls to enble temp, baro ansd alt
  49. }
  50.  
  51. void loop() {
  52.     //Get readings from all sensors
  53.     getWeather();
  54.    
  55.     //Print to console
  56.     printInfo();
  57.    
  58.     //Send data to Weather Underground
  59.     sendToWU();
  60.    
  61.     //Power down between sends to save power, measured in seconds.
  62.     System.sleep(SLEEP_MODE_DEEP,120);
  63. }
  64.  
  65. void printInfo()
  66. {
  67. //This function prints the weather data out to the default Serial Port
  68.  
  69.   Serial.print("Temp:");
  70.   Serial.print(tempF);
  71.   Serial.print("F, ");
  72.  
  73.   Serial.print("Humidity:");
  74.   Serial.print(humidity);
  75.   Serial.print("%, ");
  76.  
  77.   Serial.print("Baro_Temp:");
  78.   Serial.print(baroTempF);
  79.   Serial.print("F, ");
  80.  
  81.   Serial.print("Humid_Temp:");
  82.   Serial.print(humTempF);
  83.   Serial.print("F, ");
  84.  
  85.   Serial.print("Pressure:");
  86.   Serial.print(pascals/100);
  87.   Serial.print("hPa, ");
  88.   Serial.print(inches);
  89.   Serial.println("in.Hg");
  90.   //The MPL3115A2 outputs the pressure in Pascals. However, most weather stations
  91.   //report pressure in hectopascals or millibars. Divide by 100 to get a reading
  92.   //more closely resembling what online weather reports may say in hPa or mb.
  93.   //Another common unit for pressure is Inches of Mercury (in.Hg). To convert
  94.   //from mb to in.Hg, use the following formula. P(inHg) = 0.0295300 * P(mb)
  95.   //More info on conversion can be found here:
  96.   //www.srh.noaa.gov/images/epz/wxcalc/pressureConversion.pdf
  97. }
  98.  
  99. void sendToWU()
  100. {
  101.     StaticJsonBuffer<300> jsonBuffer;
  102.    
  103.     JsonObject& object = jsonBuffer.createObject();
  104.     object["ID"] = ID;
  105.     object["PASSWORD"] = PASSWORD;
  106.     // object["winddir"] = 0;
  107.     // object["windspeedmph"] = 0;
  108.     // object["windgustmph"] = 0;
  109.     // object["windgustdir"] = 0;
  110.     object["humidity"] = humidity;
  111.     object["dewptf"] = dewptF;
  112.     object["tempf"] = tempF;
  113.     object["baromin"] = inches;
  114.    
  115.     char buffer[256];
  116.     object.printTo(buffer, sizeof(buffer));
  117.    
  118.     Serial.println("Publishing...");
  119.     if(!Particle.publish("WUnderground PWS Upload", buffer, PRIVATE)) {
  120.         Serial.println(F("Publish failed."));
  121.     } else {
  122.         Serial.println("Publish complete.");
  123.     }
  124. }
  125.  
  126. void getWeather()
  127. {
  128.   // Measure Relative Humidity from the HTU21D or Si7021
  129.   humidity = sensor.getRH();
  130.  
  131.   // Measure Temperature from the HTU21D or Si7021
  132.   humTempC = sensor.getTemp();
  133.   humTempF = (humTempC * 9)/5 + 32;
  134.   // Temperature is measured every time RH is requested.
  135.   // It is faster, therefore, to read it from previous RH
  136.   // measurement with getTemp() instead with readTemp()
  137.  
  138.   //Measure the Barometer temperature in F from the MPL3115A2
  139.   baroTempC = sensor.readBaroTemp();
  140.   baroTempF = (baroTempC * 9)/5 + 32; //convert the temperature to F
  141.  
  142.   //Measure Pressure from the MPL3115A2
  143.   pascals = sensor.readPressure();
  144.   inches = pascals * 0.0002953; // Calc for converting Pa to inHg (Wunderground expects inHg)
  145.  
  146.   //If in altitude mode, you can get a reading in feet with this line:
  147.   //float altf = sensor.readAltitudeFt();
  148.  
  149.   //Average the temperature reading from both sensors
  150.   tempC=((humTempC+baroTempC)/2);
  151.   tempF=((humTempF+baroTempF)/2);
  152.  
  153.   //Calculate Dew Point
  154.   dewptC = dewPoint(tempC, humidity);
  155.   dewptF = (dewptC * 9.0)/ 5.0 + 32.0;
  156. }
  157.  
  158. //---------------------------------------------------------------
  159. // dewPoint function from NOAA
  160. // reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
  161. // reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
  162. //---------------------------------------------------------------
  163. double dewPoint(double celsius, double humidity)
  164. {
  165.     // (1) Saturation Vapor Pressure = ESGG(T)
  166.     double RATIO = 373.15 / (273.15 + celsius);
  167.     double RHS = -7.90298 * (RATIO - 1);
  168.     RHS += 5.02808 * log10(RATIO);
  169.     RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
  170.     RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  171.     RHS += log10(1013.246);
  172.  
  173.   // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  174.     double VP = pow(10, RHS - 3) * humidity;
  175.  
  176.   // (2) DEWPOINT = F(Vapor Pressure)
  177.     double T = log(VP/0.61078);   // temp var
  178.     return (241.88 * T) / (17.558 - T);
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement