Products for USB Sensing and Control
Products for USB Sensing and Control

Remote Temperature Monitoring


by Lucas

Source Code

Introduction

This project will show you how to create a remote temperature monitoring system using Phidgets. These systems are often used to ensure the temperature at a remote location (vacation home, server room, etc.) are not at dangerous levels. This system allows you to set a minimum temperature that you are comfortable with, and if the temperature drops below that limit, a notification will be sent. The program could easily be modified to send notifications if the temperature gets too high, or just to send notifications every day, hour, or minute!

Hardware

Here is everything you'll need if you want to replicate this project:

Software

Libraries and Drivers

This project assumes that you are somewhat familiar with the basic operation of Phidgets (i.e. attaching and opening devices, reading data, etc). If this is your first time building a Phidgets project with C#, please refer to the C# page for instructions on how to setup your environment and get started.

Software Overview

A simple C# program was written for this project. At the top of the form the current temperature is displayed and it is updated every 30 seconds. Below the temperature, there are a few settings:

  • Temperature Limit: If the temperature is consistently below this value for more than 5 minutes, the user will be notified. An email will then be sent every hour until the temperature rises.
  • Send Notification To: Specify an email address that should be notified when the temperature drops below the threshold. Note: many wireless providers offer an email to text option, so notification can be sent directly to a phone.

Under the Email Settings tab, there are a few more options:

  • Server Address: email server address. If you aren't using Gmail, a quick Google search will result in articles like this that will help you out.
  • Username: The email you would like to send notifications from. For this program I created a new Gmail account and allowed less secure apps to use it.
  • Password: Password for account.

After entering information into all the required fields, the status in the bottom right corner of the form will indicate that the program is running. After that, you can simply minimize the program and forget about it!

The program can be accessed from the system tray. Hovering over it with your mouse will show the state.

Code Overview

Code for this project is available in the TemperatureMonitor.zip file above. Before compiling the program, make sure you have the Phidget libraries installed on your computer.
Here is a quick overview of the code:

  • When the form loads, create a TemperatureSensor object and subscribe to attach, detach, and error events.
  • In the attach handler, set the DataInterval to 30 seconds.
  • In the event handler, update the temperature label and check if the temperature is below the limit. If the temperature is below the limit, increment a counter and exit. If the counter indicates that the temperature has been below the limit for 5 minutes, send a notification.
  • If a notification has been sent, start a 1 hour timer that will prevent any more notifications from being sent until the time has elapsed.

Phidget Code

When the form loads, a new temperature sensor is created and events are subscribed to.

  
temp = new TemperatureSensor();
temp.Attach += temp_attach;
temp.Detach += temp_detach;
temp.Error += temp_error;
temp.TemperatureChange += temp_change;
temp.Open();

In the attach handler, the data interval is set to 30 seconds.

  
void temp_attach(object sender, Phidget22.Events.AttachEventArgs e)
{
    temp.DataInterval = 30000; //measure temperature every 30 seconds
    updateStatus(); 
}

In the event handler, the temperature label is updated and the decision to send a notification is made. If a notification is sent a timer is started which will force the program to wait 1 hour before trying to send another.

  
void temp_change(object sender, Phidget22.Events.TemperatureSensorTemperatureChangeEventArgs e)
{
    temperatureLabel.Text = e.Temperature.ToString("F1") + "°C";
    if (isRunning())
    {
        if (e.Temperature < defaultSettings.temperatureLimit)
        {
            if (++count > 9 && timeOK)//if temperature is below limit for 5 minutes and has been at least 1 hour
            { 
                Log.WriteLine(LogLevel.Info, "Send email.");
                timeOK = false;
                sendEmail(e.Temperature);
                timer1.Enabled = true;
            }
        }
        else
            count = 0;
    }
}

Email Code

Below is the code that is run when an email should be sent.

  
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient();
client.Port = 587;
client.Host = defaultSettings.emailServer;
client.EnableSsl = true;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(defaultSettings.username, defaultSettings.password);

MailMessage mm = new MailMessage(defaultSettings.username, defaultSettings.notifyEmail, "Temperature Warning", "Temperature has dropped below limit. Last recorded temperature was: "
    + temperature + "°C");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

client.Send(mm);

Conclusion

If you have any questions, let us know!