Author Topic: Wireless IoT Sensor Network using mcThings and Losant!!  (Read 514 times)

mc-T2

  • Administrator
  • Sr. Member
  • *****
  • Posts: 252
  • mc-Things! The opportunities are endless!
  • Location: Canada
    • View Profile
Wireless IoT Sensor Network using mcThings and Losant!!
« on: January 16, 2017, 03:12:24 pm »
Hello everyone!
We just posted a new video that showcases how to display information from your mcThings platform into the Losant IoT application! Using Losant, you can create some fantastic dashboards to visualize the data from your mcModules (such as number gauges and dials, multiple types of graphs, etc) and also the ability to setup workflows to be alerted on critical information. You can use your mcModules as well as SIGFOX devices (Use the mcDemo205 and Losant to plot locations onto a map on your dashboard!)
Losant is perfect for individuals, businesses and IoT integrator's who want to capture data for viewing, analysis, etc. Check them out here: www.losant.com

There is a great walk-through on how to setup your modules and your Losant account and bring data into the application. We recommend checking out this walk-through to help you out.

mcModule & Losant walkthrough:  https://www.losant.com/blog/getting-started-with-mc-things-and-losant

SIGFOX (mcDemo205) and Losant walkthrough: https://www.losant.com/blog/collect-monitor-visualize-sigfox-data

In the video, we also walk-through setting up modules and pushing the information into a Losant Dashboard! Below the video, please see examples of the codes we used for some of the widgets that you see on the live dashboard.

Here is a link to the live dashboard!! https://app.losant.com/#/dashboards/575f90f3e7a2700100d1ddb0






Code for temperature and battery voltage:

Code: [Select]
Class LosantTempBattery
    Shared Event Boot()
        Lplan.SetBeaconTime(120)
    End Event
   
    // 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 59 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


Code for Door Open/Closed status and door count

Code: [Select]
Class LosantXTRA
   
   
    // 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

Code for Moisture Sensor into Losant -Note - This code is not optimized (Events could be grouped together into one payload as is done in the code above for the door/open closed state and count)

Code: [Select]
Define 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 = "58730cd41b29cb0100c8b870"
    // 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

Here is a link to the live dashboard!! https://goo.gl/xLRzPB
« Last Edit: January 16, 2017, 05:17:45 pm by mc-T2 »
Need more mc-Modules, mc-Gateways or other mc-Things? Check out our product page: www.mcthings.com/products. mc-Development kits are available too!
Check out a live Dashboard using mcThings and Losant! Click here: https://goo.gl/ST43hB

Share on Facebook Share on Twitter


GarnetT

  • Newbie
  • *
  • Posts: 7
    • View Profile
I've just created my own dashboards and am quite impressed. Once I got the first module talking the rest went very easily.
What I haven't been able to figure out yet is how you got all your dashboards to come together in one link?
Like Like x 1 View List

mc-T2

  • Administrator
  • Sr. Member
  • *****
  • Posts: 252
  • mc-Things! The opportunities are endless!
  • Location: Canada
    • View Profile
Hey Garnet,
Glad you were able to try it out! Losant does make a pretty slick dashboard!

Not sure what you mean when you say all the dashboards into one link? You can add multiple 'blocks' to display multiple types of data from multiple modules all in one dashboard. On the dashboard above, I believe there is about 35 different 'blocks' with a mix between number gauges, graphs, charts and indicators. If you are within one of your dashboards, you can click on the top right green 'plus' square to add another block to that dashboard.

Does that help?
Need more mc-Modules, mc-Gateways or other mc-Things? Check out our product page: www.mcthings.com/products. mc-Development kits are available too!
Check out a live Dashboard using mcThings and Losant! Click here: https://goo.gl/ST43hB

GarnetT

  • Newbie
  • *
  • Posts: 7
    • View Profile
That works. I was simply creating more and more dashboards - not adding blocks.

Thanks much!