Netduino and VB are cool... as shown in this Tempature Monitoring project
- Posted: Nov 04, 2011 at 6:00 AM
- 10,660 Views
Today's Hardware Friday post provides a working example of using Visual Basic in a Netduino based project.
That’s when I saw this little device called the Netduino which runs something called the .NET Micro-framework. This seems just what I’ve been looking for. A microcontroller device that I can put together a few electronics items which are available off-the-shelf and write some .NET code in my language of choice (VB) to implement some basic functionality.
So the next question is what did I think would be a good first project?
My wife is studying to be a sommelier which means we have quite a few bottles of wine in the house. Sometimes we open one up an it’s developed a fault. Now there are numerous reasons for wines developing faults but incorrect storage is one possible cause. Wine is very temperamental and is best stored in a cool dark climate that doesn’t change much through the seasons which is why cellars or caves are great for storage but unfortunately in normal modern houses a cave or wine cellar is not a standard item. So we use our pantry to store our wine.
With this in mind I wanted to be able to know if the pantry conditions were really fluctuating that much. So I had my project but where to start? A simple device to monitor the wine cellar (eer pantry!!!) and provide warning indications of hi or low temperature conditions
The applicable needs to do a number of tasks
- · Read the sensors
- · Write the data to the SD Card
- · Detect any temperature warning conditions.
- · Provide visual indications of working and warning conditions
- · Provide a reset capability
My approach was to take each task and build an evolving system. So the first thing was to wire up the sensor and the LED’s. Using a breadboard makes it real easy and is really a case of plugging wires in to make the connections.
The design looked like the following. It looks a bit crazy when prototyping but I’ll eventually clean this up before putting the item in the pantry and having a worried wife come tell me there is a mysterious device with wires coming out of it in the pantry.
Reading the sensor
The code for this application is a simple Console application which is running indefinitely. As I’m powering the final device by means of a 9v battery I want to do enough to get the job done but want to make sure that I have sufficient battery life. So I will be taking a reading every 10 minutes which means that most of the time the device is really asleep.
There are both analogue and digital temperature sensors. Mine original sensor is a simple thermistor (Temperature dependent resistor) which means that the resistance varies with temperature changes. Using the Analogue input ports on the Netduino in conjunction with this allows me to determine a temperature. There are two revisions of the Netduino (Rev A and Rev B). The significant difference between the two is related to Analogue references and the need to provide an extra link between the 3.3v pin and the Aref0 pin – without this the values read on the Analogue port will be inconsistent. Luckily almost all boards out there are Rev B boards with this already internally connected on the board preventing you from having to remember this.
The formula for calculating a temperature is as follows….
Imports Microsoft.SPOT Imports Microsoft.SPOT.Hardware Imports SecretLabs.NETMF.Hardware Imports SecretLabs.NETMF.Hardware.Netduino Public Module Module1 Dim PollingDelay As Integer = 2 Dim tempSensor As New AnalogInput(Pins.GPIO_PIN_A5) Dim CurrentTemperature As Double = 0 Public Sub Main() Dim Temperature As Double = 0 '//Setup Event Handlers PollingTemperatureMethod() '//Get the initial temperature CurrentTemperature = GetAverageTemperatureReading() While True Thread.Sleep(Timeout.Infinite) End While End Sub Function GetCurrentTemperature() As Double ''Read the raw sensor value Return tempSensor.Read() End Function 'Get the average temperature of 101 readings over a second Function GetAverageTemperatureReading() As Double Dim totalTemp As Double Dim averagetemp As Double = 0 For i = 0 To 99 totalTemp += GetCurrentTemperature() Thread.Sleep(10) Next averagetemp = totalTemp / 100 Dim Kelvin As Double = (((averagetemp / 1023) * 3.3) * 100) Dim Celsius As Double = (Kelvin - 273) - 17.8 Dim Fahrenheit = (Celsius) * (9 / 5) + 32 Debug.Print(WarningStatus.ToString & " " & averagetemp.ToString() & " " & Fahrenheit.ToString & " / " & Celsius.ToString & " ") Return Celsius End Function Private PollingTimer As Timer Private Sub PollingTemperatureMethod() Dim PollingDelegate As New TimerCallback(AddressOf PollForTemperature) PollingTimer = New Timer(PollingDelegate, Nothing, 1000, PollingDelay * 1000) End Sub Public Sub PollForTemperature(stateInfo As Object) Module1.CurrentTemperature = GetAverageTemperatureReading() End Sub End Module
Detecting Warning Conditions
I wanted my code to be able to detect hot and cold warning conditions as both of these can cause problems with wine storage. During development I wanted a much narrower temperature range allowing me to trigger it easily but chose to read these settings into the application from a file on the SD Card to make it flexible. Allowing me modify the settings without having to redeploy the application. Once I have these Maximum and Minimum values I can then use these to determine if the sensor reading is a Warning Condition and if this has occurred turn on one of the two warning LED to provide a clear indication that we have had an temperature event occur.
Dim HiWarningLED As New OutputPort(Pins.GPIO_PIN_D9, False) Dim LowWarningLED As New OutputPort(Pins.GPIO_PIN_D11, False) Public Enum LevelCheck LowTemp HiTemp End Enum Dim LowWarningStatus As Boolean = False Dim WarningStatus As Boolean = False Dim MinTemp As Single = 20 Dim MaxTemp As Single = 26 Function CheckTemperatureForWarning(Temp As Double, Optional Level As LevelCheck = LevelCheck.HiTemp) As Boolean Dim WarnStateOccured As Boolean = False If Level = LevelCheck.LowTemp Then 'Is Temperature Exceeding MaxTemp If Temp <= MinTemp Then Debug.Print("Low Warning Triggered") Debug.Print("Temp: " & Temp.ToString) Debug.Print("maxTemp: " & MaxTemp.ToString) Debug.Print("minTemp: " & MinTemp.ToString) WarnStateOccured = True End If ElseIf Level = LevelCheck.HiTemp Then 'Is Temperature Exceeding MaxTemp If Temp >= MaxTemp Then Debug.Print("Hi Warning Triggered") Debug.Print("Temp: " & Temp.ToString) Debug.Print("maxTemp: " & MaxTemp.ToString) Debug.Print("minTemp: " & MinTemp.ToString) WarnStateOccured = True End If End If Return WarnStateOccured End Function Sub WarningStatusLight(State As Boolean, Optional Level As LevelCheck = LevelCheck.HiTemp) 'Code to turn on/off LED If Level = LevelCheck.LowTemp Then LowWarningLED.Write(State) If Level = LevelCheck.HiTemp Then HiWarningLED.Write(State) End Sub Public Sub PollForTemperature(stateInfo As Object) Module1.CurrentTemperature = GetAverageTemperatureReading() 'If the warning status light is off and a warning temperature occurs then 'set Warning Light State to True and don't bother checking any more as the 'alarm condition has been met If LowWarningStatus = False AndAlso CheckTemperatureForWarning(CurrentTemperature, LevelCheck.LowTemp) = True Then LowWarningStatus = True WarningStatusLight(LowWarningStatus, LevelCheck.LowTemp) End If If WarningStatus = False AndAlso CheckTemperatureForWarning(CurrentTemperature, LevelCheck.HiTemp) = True Then WarningStatus = True WarningStatusLight(WarningStatus, LevelCheck.HiTemp) End If End Sub
I now have a basic functioning sensor that I can used to retrieve sensor readings from my Wine Cellar which will give me a visual indication of spoilage temperature conditions. I’ve managed to read a build a circuit, read a sensor, use timers to poll for reading, turn on/off LED’s for Visual Indication, Interact with device via a button input and write data to a Micro SD Card. These are basic functions which can be used over and over again in many different applications.
The idea is that it's not hard to get started playing with Netduino, and if you're a C#'er or a VB, the .Net Micro Framework is speaks in your language...
Here’s a few more links you might find interesting: