In this example we'll show you how to setup an awesome wireless, ultra-low power, coin-cell operated sensor network using the mcThings platform and Losant's IoT cloud application!
Within this project, we'll show you how to measure and send information via JSON over MQTT to Losant where you can visualize your data, compare for trends, setup alerts, workflows and analyze historical & real-time information such as: Temperature, battery voltage, door open/closed status and count, soil moisture and humidity.
Click below to see live Dashboards:
Keep reading to learn how to complete this project. There's a YouTube video at the end for a visual walk-through!
How the platform works: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!
There is a great walk-through that was put together by Taron Foxworth from Losant. It can be found here. We'll be using the first part of Taron's code and then add some additional variables that we want to measure.
Code for Temperature and Battery VoltageThe below is the original code from the aforementioned walk-through and since we aren't going through the setup steps within Losant, please be sure to read it. There is also a YouTube walk-through at the end of this example!
Class LosantTempBattery
      
    // Device ID of Peripheral in Losant
    Const LosantDeviceId As String = "YourLosantDeviceIDHERE"
    // MQTT topic in Losant
    Const LosantTopic As String = "losant/" + LosantDeviceId + "/state"
  
    Shared Event CheckTemp() RaiseEvent Every 60 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 Losant preferred JSON object - { "data" : {"temperature" : "23.06"}}
        Dim losantPayload As Json = New Json
        losantPayload.Add("data", tempJson)
  
        // Publish to Losant MQTT
        Lplan.Publish(LosantTopic, losantPayload.ToListOfByte)
    End Event
  
    Shared Event CheckVoltage() RaiseEvent Every 5 Minutes
        Dim BattVolt As Integer = Device.BatteryVoltage// Get battery voltage
  
        // Create temp JSON object - { "batteryVoltage" : "2816" }
        Dim batteryJson As Json = New Json
        batteryJson.Add("BatteryVoltage", BattVolt)
  
        // Create Losant preferred JSON object - { "data" : {"batteryVoltage" : "2816"}}
        Dim losantPayload As Json = New Json
        losantPayload.Add("data", batteryJson)
  
        // Publish to Losant MQTT
        Lplan.Publish(LosantTopic, losantPayload.ToListOfByte)
  
    End Event
End Class
The above code programs the module to check the temperature every 60 seconds and the battery voltage every 5 minutes. It also formats the MQTT message to the format required by Losant.
*Note - Keep in mind that you need to add the temperature sensor library to your project. The library is included in the IDE
Modified code for door open/closed dataWe modified the above code and added the function of using the built-in reed switch to send its status and open count as well as temperature and voltage.
 Class LosantDoor
  
  
    // Device ID of Peripheral in Losant
    Const LosantDeivceId As String = "YourLosantDeviceIDHERE"
    // MQTT topic in Losant
    Const LosantTopic As String = "losant/" + LosantDeivceId + "/state"
  
    Shared _doorState As String
    Shared _doorcount As Integer
  
    Shared Event Boot()
        _doorState = "Open"
        _doorcount = 0
    End Event
  
    Shared Event ReedSwitchChanged()
        'debounce interrupt
        Thread.Sleep(100000)
        Thread.ClearHardwareEvent()
        LedRed = True
        If ReedSwitch = True Then
            _doorState = "Open"   
            _doorcount = _doorcount + 1
        Else
            _doorState = "Closed"
        End If
        sendMQTTData()
        LedRed = False
    End Event
  
    Shared Event CheckSensors() RaiseEvent Every 60 Seconds 
        sendMQTTData()
    End Event
  
    Private Sub sendMQTTData() 
        Dim tempTMP102string As String = TempSensor.GetTemp().ToString()
        Dim battShort As Short = Device.BatteryVoltage()
        Dim battFloat As Float = battShort / 1000
        Dim battString As String = battFloat.ToString()
  
  
        // Create data JSON object 
        Dim dataJson As Json = New Json
        dataJson.Add("battery", battString)
        dataJson.Add("temperature", tempTMP102string)
        dataJson.Add("doorState", _doorState)
        dataJson.Add("doorCount", _doorcount)
  
  
        // Create Losant preferred JSON object
        Dim losantPayload As Json = New Json
        losantPayload.Add("data", dataJson)
  
        // Publish to Losant MQTT
        Lplan.Publish(LosantTopic, losantPayload.ToListOfByte)
  
    End Sub
  
End Class
*Note - mcModule enclosures are available! They come with an additional magnet clip with two neodymium magnets so you can easily use the reed switch.
Modified code to send soil moisture dataDefine PinMode Pin0 As AnalogInput Alias MoistureLevel
Define PinMode Pin6 As DigitalOutput Alias enableMoistureLevel
Class LosantMoisture
  
  
    Shared Event Boot()
        Lplan.SetBeaconTime(175)
    End Event
  
    // Device ID of Peripheral in Losant
    Const LosantDeivceId As String = "YourLosantDeviceIDHERE"
    // MQTT topic in Losant
    Const LosantTopic As String = "losant/" + LosantDeivceId + "/state"
  
    Shared Event measureMoisture() RaiseEvent Every 60 Seconds
  
        enableMoistureLevel = True 'turn on voltage divider
        Thread.Sleep(40000) 'sleep 40ms for voltage to stabilize
        Dim voltage As Short = MoistureLevel
        Dim payload As ListOfByte = New ListOfByte
        Dim payString As String = ""
        If voltage > 1500 Then
            payString = "Dry - I NEED WATER!!"
        ElseIf voltage <= 1499 And voltage >= 800 Then
            payString = "Getting Low"             
        ElseIf voltage <= 799 And voltage >= 1 Then
            payString = "Watered!"                
  
        Else
        End If
        enableMoistureLevel = False 'turn off voltage divider
  
        // Create temp JSON object - { "MoistureLevel" : "Moisture Message" }
        Dim moistureJson As Json = New Json
        moistureJson.Add("Moisture", payString)
  
        // Create Losant preferred JSON object - { "data" : {"MoistureLevel" : "Moisture Message"}}
        Dim losantPayload As Json = New Json
        losantPayload.Add("data", moistureJson)
  
        // Publish to Losant MQTT
        Lplan.Publish(LosantTopic, losantPayload.ToListOfByte)
    End Event 
    Shared Event CheckTemp() RaiseEvent Every 60 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 Losant preferred JSON object - { "data" : {"temperature" : "23.06"}}
        Dim losantPayload As Json = New Json
        losantPayload.Add("data", tempJson)
  
        // Publish to Losant MQTT
        Lplan.Publish(LosantTopic, losantPayload.ToListOfByte)
    End Event
  
    Shared Event CheckVoltage() RaiseEvent Every 5 Minutes
        Dim BattVolt As Integer = Device.BatteryVoltage// Get battery voltage
  
        // Create temp JSON object - { "batteryVoltage" : "2816" }
        Dim batteryJson As Json = New Json
        batteryJson.Add("BatteryVoltage", BattVolt)
  
        // Create Losant preferred JSON object - { "data" : {"batteryVoltage" : "2816"}}
        Dim losantPayload As Json = New Json
        losantPayload.Add("data", batteryJson)
  
        // Publish to Losant MQTT
        Lplan.Publish(LosantTopic, losantPayload.ToListOfByte)
  
    End Event
  
  
End Class 
We modified the code we used when we created a soil moisture sensor to measure a plant (every 30 seconds) along with battery voltage (every 5 minutes) and temperature (every 60 seconds).
Visualizing the information in LosantUsing Losant, you can easily display the incoming variables using the provided data visualization tools.
*Note - Be sure to check out the documentation provided by Losant on setting up a dashboard
This is a really great example of how easy the mcThings platform is to use to gather multiple amounts of customizable data from multiple locations! Keep in mind that the mcModules allow you to connect to almost any other sensor (using UART, SPI and I2C interfaces) and is a perfect platform for building IoT solutions for businesses and individuals. And because the modules are ultra-low power and battery operated, they can last for years on a single coin cell battery!
Please become a member of our community here on Hackster so you can follow us with our many upcoming examples and projects!
Thanks for reading!
YouTube Video walk-through!














Comments