s4

Hello and welcome to the series of tutorials for internet of things (Iot) .if you are complicate choosing Raspberry Pi or Arduino for your Iot project read this article.

So without further due here are Prerequisite for this tutorial.

Hardware:

  • Raspberry PI 3 (Pi 2 model B will also work)
  • BMP 280 sensor ( Buy from Ebay)
  • Bread Board
  • Male/Female jumper wires

Software:

  • Windows 10 Iot core
  • Visual studio 2015

About BMP 280:-

This Bmp 280 sensor you can buy from Ebay or ali Express which is low cost well it is less reliable and less robust. I already burned 2 of them one by holding lighter closed to it, another by putting it on a car dashboard for a while and 1 of them is malfunction so they are not guaranteed to be long lasting.

Setup Hardware.

Ok. Connect the 4 jumper wire to raspberry Pi3 through bread board

  • BMP 280 vcc to Pi 5 pin voltage
  • BMP 280 GND to Pi GND
  • BMP 280 SDA Pin to Pi GPIO 2 (adjacent to 5v Pin)
  • BMP 280 SCL Pin to Pi to GPIO 3 (adjacent to Gnd Pin)

Here’s Schematic and physical image for your ease.

 

 

s4

And the Rest…Software:

Open Visual studio 2015 create new blank windows Universal app. and give it a name.

s1

Add the reference for Windows IoT Extensions for UWP” Windows Universal Extension.

s2

Add class BMP 280 to your solution.

Note:-this sensor is full of surprises after much testing I found out its register header is 0x76 rather than 0x77 so change it in BMP 280 class.

s3

Time to make a meaningful UI which looks like this:

s5

Application Flow:

On start analyzing button timer will kick off and start showing temperature.

On stop analyzing button timer will stop and resets all variables.

User can set threshold and once it’s exceeded it will start adding in list

add the following methods in MainPage.xaml.cs

protected override async void OnNavigatedTo(NavigationEventArgs navArgs)
{

base.OnNavigatedTo(navArgs);

Debug.WriteLine("MainPage::OnNavigatedTo");

//Create a new object for our barometric sensor class

BMP280 = new BMP280();

//Initialize the sensor

await BMP280.Initialize();

try

{

_timer = new DispatcherTimer();

_timer.Interval = TimeSpan.FromSeconds(1);

_timer.Tick += _timer_Tick;

}

 

catch (Exception ex)

{

Debug.WriteLine(ex.Message);

}

}

private async void _timer_Tick(object sender, object e)

{

temper = await BMP280.ReadTemperature();

pressure = await BMP280.ReadPreasure();

altitude = await BMP280.ReadAltitude(seaLevelPressure);

if (temper != 0 && Thresholdtemp != 0 && temper > Thresholdtemp)

{

float tempdiff = temper - Thresholdtemp;

thresherror.Items.Add("Temperature has exceeded threshold by " + tempdiff + " deg C at " + DateTime.Now);

}

if (pressure != 0 && Thresholdpress != 0 && pressure > Thresholdpress)

{

float pressdiff = pressure - Thresholdpress;

thresherror.Items.Add("Pressure has exceeded threshold by " + pressdiff + " Pascal at " + DateTime.Now);

}

 

if (altitude != 0 && Thresholdaltid != 0 && altitude > Thresholdaltid)

{

float altidiff = altitude - Thresholdaltid;

thresherror.Items.Add("Altitude has exceeded threshold by " + altidiff + " meter at " + DateTime.Now);

}

// Write the values to your debug console

Debug.WriteLine("Temperature: " + temper.ToString() + " deg C");

Debug.WriteLine("Pressure: " + pressure.ToString() + " Pa");

Debug.WriteLine("Altitude: " + altitude.ToString() + " m");

 

temp.Text = temper.ToString() + " C";

press.Text = pressure.ToString() + " Pa";

altid.Text = altitude.ToString() + " m";

 

 

 

}

private void SetValues(object sender, RoutedEventArgs e)

{

if (threshtemp.Text != string.Empty)

{

Thresholdtemp = float.Parse(threshtemp.Text);

threshtemp.IsEnabled = false;

}

if (threshpress.Text != string.Empty)

{

Thresholdpress = float.Parse(threshpress.Text);

threshpress.IsEnabled = false;

}

if (threshaltid.Text != string.Empty)

{

Thresholdaltid = float.Parse(threshaltid.Text);

threshaltid.IsEnabled = false;

}

 

 

}

 

private void StartAnalysing(object sender, RoutedEventArgs e)

{

_timer.Start();

}

 

private void StopAnalysing(object sender, RoutedEventArgs e)

{

_timer.Stop();

temp.Text = "";

press.Text = "";

altid.Text = "";

threshtemp.Text = "";

threshpress.Text = "";

threshaltid.Text = "";

threshtemp.IsEnabled = true;

threshpress.IsEnabled = true;

threshaltid.IsEnabled = true;

temper = 0;

pressure = 0;

altitude = 0;

Thresholdtemp = 0;

Thresholdpress = 0;

Thresholdaltid = 0;

thresherror.Items.Clear();

}

  • On OnNavigatedTo initialize timer and BMP 280 object
  • On _timer_Tick it will read temperature and pressure and checks if it exceeds its limit add it in list. and finally display temp pressure and altitude in text boxes
  • On SetValues sets threshold values
  • On StartAnalysing kicks off the timer
  • On StopAnalysing stops timer and resetting its variable

Here is the output:

s7

Next I will show the same thing using DHT11/DHT22 with comparison between them in the end.

So stay tuned for more blog coming up.

Leave a Reply

Recent Comments

    Archives

    Categories