Problem Connecting to Losant with mcThings

#1

Hello,

I’ve been attempting to run the magnetic reed switch code and the getting started with Losant guide. I’ve tried it multiple times, made sure all case is accurate etc but I cannot get the modules/gateway to connect to Losant. Here is the guide I’m referencing: https://www.losant.com/blog/getting-started-with-mc-things-and-losant

Here is the code:

    Class LosantDoor
  
  
    // Device ID of Peripheral in Losant
    Const LosantDeivceId As String = "594445eb57dcc600060018d5"
    // 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
0 Likes

#2

Hi, Jordan!

If you go into your device, on the left you should see a panel called “Debug”. Then, scroll down and take a look at your connection log. It should look like this:

Are you seeing any connection attempts here? Generally, this is the first place I check.

I’m sure you may have done this, but, can you verify all your credentials are correct? For mc-Things, here is what that would be:

MQTT Server - broker.losant.com
MQTT Port - 1883
MQTT Username - Your Losant Access Key
MQTT Username - Your Losant Access Secret
MQTT ClientId - This is the Device ID of the gateway you created in Losant.
0 Likes

#3

Thanks for your reply. I see no connection logs. It appears to not be connecting at all. I’ve verified that all credentials are correct.

0 Likes

#4

I forced a state and it appears to be working after doing so. Thanks!

0 Likes

#5

I’m happy it’s working! Just to make sure I understand your solution. Did you Force State in Losant? Or, did you just send state from the device without the door sensor connected?

0 Likes