Things used in this project

Hardware components:
Mc devkit100a scojkvv1ol
mcThings mcModule120 Dev Kit
We recommend a kit for this example so that you have multiple modules to gather information from (Not required but you will need at least one mcModule, one mcGateway and an mcDongle)
×1
Product mcmod120 top front back p8hs3xepwv
mcThings mcMod120 Module
You'll need at least one mcModule120 for this project
×1
Mcgateway110 perspective 1000px iqkgknswa5
mcThings mcGateway110
You'll need an mcGateway for this project
×1
Mcdongle perspective 1000px pcdfntbxl3
mcThings mcDongle
You'll need one of these to complete your firmware updates! Be sure to update your firmware to the latest versions before starting
×1
Software apps and online services:
Mcstudio h8lhxdnyfz
mcThings mcStudio
Gkdo38ewsiknnyjwwfdv
Wia
Mqtt
MQTT

Schematics

mcModule120 Product brief

Code

mcModule120 Temp code for Wia.iomcScript
Class TempLogger
    // Device ID in Wia
    Const wiaDeviceId As String = "Wia Device ID"
    // MQTT topic in Wia
    Const wiaTopic As String = "devices/" + wiaDeviceId + "/events/temperature"

    Shared Event CheckTemp() RaiseEvent Every 30 Seconds 
        Dim temp As Float = TempSensor.GetTemp() // Get Temp from sensor
        Dim tempString As String = temp.ToString() 

        // Create temp JSON object - { "temperature" : "23.06" }
        Dim tempJson As Json = New Json
        tempJson.Add("temperature", tempString)

        // Create Wia preferred JSON object - { "data" : {"temperature" : "23.06"}}
        Dim wiaPayload As Json = New Json
        wiaPayload.Add("data", tempJson)

        // Publish to wia MQTT
        Lplan.Publish(wiaTopic, wiaPayload.ToListOfByte)
    End Event   
End Class
Temperature sensor librarymcScript
Temperature sensor library for the mcModule120
Class TempSensor
    // 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

Credits

Cu3cvrhf tdqeqwcew0
mcThings
17 projects • 38 followers
Use mcThings to quickly & easily create, test and deploy IoT solutions for industrial, business/commercial and individual needs!
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog