Particle Photon Weather Sensor

Updated 12 March 2020

Recently I finished my first Particle Photon project. It’s basically a temperature, humidity, and barometric pressure monitor all in one! I’m not a programmer, but I know enough to tweak code when necessary. I have, on occasion programmed code using C and Visual Basic. I Hope you find the project simple and interesting.

 

Parts List

Setup

First I created a Particle account through the particle website. I then connected my photon to my network and registered my device with Particle. Once signed in I navigated to the Particle Console and selected Web Ide from the left-hand side of the menu.

When the Web Ide was open, I selected the libraries Icon which brings up the Particle Apps Menu I then did a search for Sparkfun weather shield example.ino and used that as a starting place for coding. The code was a mix from spark fun and code form electronic wings.com I take no credit for the code. I just deleted, pasted and tweaked the code.

I created an account with ThingSpeak. For those that don't know, Thingspeak is an analytics platform service which can be used for various purposes like to control things remotely, to visualize and analyse live data stream in cloud, etc. I created a channel on ThingSpeak Cloud to visually display output form photon.

The code

// This #include statement was automatically added by the Particle IDE.

// Code is made up of code form Sparkfun and ElectronicWings

#include 

#include "SparkFun_Photon_Weather_Shield_Library.h"

 

double humidity = 0;

double tempf = 0;

double pascals = 0;

double baroTemp = 0;

double tempc = 0;

double pressure = 0;

int myChannelNumber = your channel number;  /*Thingspeak channel id*/

char * myWriteAPIKey = your API Key;/*Channel's write API key*/

 

//Create Instance of HTU21D or SI7021 temp and humidity sensor and MPL3115A2 barometric sensor

Weather sensor;

TCPClient client; 

//---------------------------------------------------------------

void setup()

{

//ThingSpeak.begin(client);

// Create Particle.variables for each piece of data for easy access

Particle.variable("humidity", humidity);

Particle.variable("tempF", tempf);

    Particle.variable("pressurePascals", pascals);

Particle.variable("baroTemp", baroTemp);

//Initialize the I2C sensors and ping them

sensor.begin(); 



/*You can only receive accurate barometric readings or accurate altitude

readings at a given time, not both at the same time. The following two lines

tell the sensor what mode to use. You could easily write a function that

takes a reading in one made and then switches to the other mode to grab that

reading, resulting in data that contains both accurate altitude and barometric

readings. For this example, we will only be using the barometer mode. Be sure

to only uncomment one line at a time. */

sensor.setModeBarometer();//Set to Barometer Mode

//baro.setModeAltimeter();//Set to altimeter Mode

 

//These are additional MPL3115A2 functions that MUST be called for the sensor to work.

sensor.setOversampleRate(7); // Set Oversample rate

//Call with a rate from 0 to 7. See page 33 for table of ratios.

//Sets the over sample rate. Datasheet calls for 128 but you can set it

//from 1 to 128 samples. The higher the oversample rate the greater

//the time between data samples.

 

 

sensor.enableEventFlags(); //Necessary register calls to enble temp, baro and alt

ThingSpeak.begin(client);

}

//---------------------------------------------------------------

void loop()

{

   

   getWeather();

 

     ThingSpeak.writeField(myChannelNumber, 1, String(humidity), myWriteAPIKey);

     delay(5000);

     ThingSpeak.writeField(myChannelNumber, 2, String(tempf), myWriteAPIKey);

     delay(5000);

     ThingSpeak.writeField(myChannelNumber, 3, String(tempc), myWriteAPIKey);

     delay(5000);

     ThingSpeak.writeField(myChannelNumber, 4, String(pressure), myWriteAPIKey);

     delay(5000);

   



} 

//---------------------------------------------------------------

void getWeather()

{

  // Measure Relative Humidity from the HTU21D or Si7021

  humidity = sensor.getRH();

 

  // Measure Temperature from the HTU21D or Si7021

  tempf = sensor.getTempF();

  // Temperature is measured every time RH is requested.

  // It is faster, therefore, to read it from previous RH

  // measurement with getTemp() instead with readTemp()

  tempc = (tempf-32)*(0.5555); //added to convert fahrenheit to celcius

  //Measure the Barometer temperature in F from the MPL3115A2

  baroTemp = sensor.readBaroTempF();

 

  //Measure Pressure from the MPL3115A2

  pascals = sensor.readPressure();

  pressure = (pascals)/(100); //added to convert pascals to mbars

  //If in altitude mode, you can get a reading in feet with this line:

 //float altf = sensor.readAltitudeFt();

 

}

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.