In this project, we show you how to connect some NTC thermistors to the mcMod120 to create a wireless, ultra-low power, coin-cell powered temperature sensor and then display the incoming real-time information into thethings.iO IoT cloud application!
Potential use-cases?The mcMod120 already features a TMP102 temperature sensor, but what about when you need to measure liquids, extreme temperatures, specific locations, etc?
- Cold-chain management, Food service, trucking, etc
- Agriculture: livestock trough temps, water storage, commercial greenhouses
- Measure aquariums, ponds, birdbaths, puddles (Imagine, an IoT puddle!), etc
- connect your lasagna to the internet! Literally, you could! :)
- Etc!
The mcThings platform includes 2 main components: mcModules and mcGateways. A powered and connected mcGateway creates the mcAir network (up to 200m range under optimal conditions) and bridges the information between the mcModules (within range) to and from the Internet. Using the IDE (mcStudio) and mcScript (ultra-low power programming language - a subset of VB.NET) allows you to wirelessly connect, debug and program modules with your customized scripts.
Note - You also require an mcDongle to complete firmware updates on the modules/devices and gateways! We recommend looking at the mcModule120 Dev kit which includes everything you need to get going!!
We soldered two different NTC thermistors directly to the mcMod120. For the first sensor, we added the resistor to act as the voltage divider and for the second sensor, we used the built-in voltage divider on the module.
PINS used:
Resistor: PIN 0 & PIN 6
Thermistor #1: PIN 0 (to resistor) & GND
Thermistor #2: PIN 7 & GND
*Note - If you are hesitant to solder right away or want to connect other sensors before soldering, mcThings offers the mcDev board that allows you to use breakout pins among other features.
EnclosureWe picked up a little case from a local electronic store and used a step bit to move the connected sensors through the case and then closed it up.
Using thethings.iO recent walk-through on how to connect mcThings devices to their IoT application (more on this below), we modified the code from Github to send the information from the thermistors to the dashboard.
MAIN Program: Notice that we define the Pins to ensure that we are measuring properly (based on where they were soldered, modifiable based on where you connect the sensor on your module).
Define PinMode Pin0 As AnalogInput Alias USP10972_VOLTAGE
Define PinMode Pin6 As DigitalOutput Alias USP10972_DIVIDER_ENABLE
Define PinMode Pin7 As AnalogInputPullUp Alias CWF1B104F3950_VOLTAGE
Class ThermistorTheThings
Shared token As String
Shared thethings As thethingsiO
'Constants for Thermistor 1
Const CWF1B104F3950_BETA As Integer = 3950
Const CWF1B104F3950_R25 As Integer = 100000 '100kOhm
Const CWF1B104F3950_DIVIDER_R1 As Integer = 100000 '100kOhm
'Constants for Thermistor 2
Const USP10972_BETA As Integer = 3892
Const USP10972_R25 As Integer = 10000 '10kOhm
Const USP10972_DIVIDER_R1 As Integer = 10000 '10kOhm
Shared CWF1B104F3950 As NTCTermistor
Shared USP10972 As NTCTermistor
Shared Event Boot()
'disable USP10972 voltage divider
USP10972_DIVIDER_ENABLE = False
'Create new NTC Thermistors with beta and R25 vaules
CWF1B104F3950 = New NTCTermistor(CWF1B104F3950_BETA, CWF1B104F3950_R25)
USP10972 = New NTCTermistor(USP10972_BETA, USP10972_R25)
'Assign token for thethings.io payload
token = "YOURTOKENHERE"
thethings = New thethingsiO(token)
End Event
Shared Event measureTemperature() RaiseEvent Every 30 Seconds
'Turn on Resistor Divider to enable reading of USP10972 NTC probe
USP10972_DIVIDER_ENABLE = True
'Turn on OPAMP to enable reading of CWF1B104F3950 NTC probe
Device.EnableOpamp()
'sleep for 10ms for voltage dividers to stabilize
Thread.Sleep(10000)
'Get thermistor resistance from resistor dividers
Dim thermistorResistance As Float = CWF1B104F3950_DIVIDER_R1 * (1 / ((Device.BatteryVoltage().ToFloat/ CWF1B104F3950_VOLTAGE.ToFloat) - 1))
Dim thermistor2Resistance As Float = USP10972_DIVIDER_R1 * (1 / ((Device.BatteryVoltage().ToFloat/ USP10972_VOLTAGE.ToFloat) - 1))
'turn off voltage divider to conserve power
USP10972_DIVIDER_ENABLE = False
'turn off opamp to conserve power
Device.DisableOpamp()
'measure and convert batter voltage to float
Dim battShort As Short = Device.BatteryVoltage()
Dim battFloat As Float = battShort / 1000
'Convert data to string to add to JSON data
Dim battString As String = battFloat.ToString()
Dim tempTMP102string As String = Temperature.GetTemp().ToString
Dim tempstring As String = CWF1B104F3950.TemperatureCFromResistance(thermistorResistance).ToString
Dim temp2string As String = USP10972.TemperatureCFromResistance(thermistor2Resistance).ToString
'create thethings.io payload and publish
Dim tempJson As Json = New Json
thethings.addValues("battery", battString)
thethings.publish()
thethings.addValues("temperature", tempstring)
thethings.publish()
thethings.addValues("temperature2", temp2string)
thethings.publish()
thethings.addValues("TMP102temperature", tempTMP102string)
thethings.publish()
End Event
End Class
LIBRARIESThere are two libraries for this project, for the NTC sensors and the TMP102 (yes, we also measured the ambient temperature using the built-in temperature sensor)
thethings.io library
Class thethingsiO
Private topic As String = ""
Private payload As String = ""
Private firstValue As Boolean
Public Sub New(thingToken As String)
topic = thingToken
End Sub
Public Function addValues(key As String, value As String) As Integer
If Not firstValue Then
payload += ","
End If
payload += "{\x22key\x22: \x22" + key + "\x22, \x22value\x22:\x22" + value.ToString() + "\x22}"
firstValue = False
Return 1
End Function
Public Function publish() As Integer
Dim pay As ListOfByte = New ListOfByte
pay.Add("{\x22values\x22:[")
pay.Add(payload)
pay.Add("]}")
'mcThings modification
Dim paystring As String = pay.ToString()
Lplan.Publish("v2/things/" + topic, pay)
pay.Clear()
payload = ""
Thread.Sleep(10000)
Thread.ClearHardwareEvent()
firstValue = True
Return 1
End Function
Public Function setTopic(top As String) As Integer
topic = top
Return 1
End Function
Public Function getTopic() As String
Return topic
End Function
End Class
NTC Thermistor library
Class NTCTermistor
Private beta As Integer
Private R25 As Integer
Public Sub New(betaIn As Integer, R25In As Integer)
'set Beta value
beta = betaIn
'Set R25 resistance value
R25 = R25In
End Sub
Public Function TemperatureFromResistance(resistance As Float) As Float
'returns temperature in Kelvin from resistance of thermistor using Beta parameter equation
Dim temp1 As Float = -1 * beta.ToFloat() / 298.15
Dim temp2 As Float = resistance / (R25.ToFloat() * temp1.Exp())
Return (beta / temp2.Log())
End Function
Public Function TemperatureFFromResistance(resistance As Float) As Float
'returns temperature in Fahrenheit from resistance of thermistor
Return (TemperatureFromResistance(resistance) * (9 / 5)) - 459.67
End Function
Public Function TemperatureCFromResistance(resistance As Float) As Float
'returns temperature in Celcuis from resistance of thermistor
Return TemperatureFromResistance(resistance) - 273.15
End Function
End Class
TMP102 library (built-in library, available within the IDE)
'
' See the datasheet at http://www.ti.com/lit/ds/symlink/tmp102.pdf
'
Class Temperature
// Function returns the temperature in degree celcius or
// Float.NaN if something is wrong
Shared Function GetTemp() As Float
// Define the properties of the I2C peripheral and device address
Dim sensor As I2c
sensor = I2c.Create(250000, Pin.SCL, Pin.SDA, 0x48)
// Power up the sensor and give it some time to settle
Device.EnableTempSensor()
Thread.Sleep(40000) // See page 13 of the datasheet
// Read the sensor (only 2 bytes to read
Dim res As ListOfByte = sensor.Read(2)
// See Tmp102 documentation how to interpret the data (page 8)
Dim temp As Float = Float.NaN
If res <> Nothing Then
// Shift the partial part to the right nibble
Dim part As Float = res(1) >> 4
// Temperature partial is 1/16*n where n is between 0 and 15
part = part / 16
// Sign extend the byte to an integer
temp = res(0).SignExtend() + part
End If
// power off
Device.DisableTempSensor()
Return temp
End Function
Shared Function GetDieTemp() As Float
// Just get the temperature and return
Return Device.TempDie
End Function
Shared Function ToFarenheit(celcius As Float) As Float
Return (celcius * 9) / 5 + 32
End Function
Shared Function ToCelcius(farenheit As Float) As Float
Return (farenheit - 32) * 5 / 9
End Function
End Class
Viewing the information on thethings.iO!Check out the recently posted walk-through by Carles Garriga on connecting to thethings.io dashboard! There is also a YouTube video (posted at the end of this project) for a visual walk-through!
thethings.io provides an easy to use and clean dashboard to easily visualize your data, set alerts and much more
As seen in the cover picture of this project, we used one of the thermistors to measure the rise in temperature when we used our electric water boiler. The below is the graph (time lapse gif) from the real-time data going into the dashboard!
We would love for you to join the mcThings hub here on Hackster! That way you won't miss all the upcoming cool projects we'll be posting soon!
Thanks for reading!
Comments
Please log in or sign up to comment.