Author Topic: Sending MQTT messages to a MC-Product  (Read 152 times)

DanielV

  • Newbie
  • *
  • Posts: 5
    • View Profile
Sending MQTT messages to a MC-Product
« on: January 08, 2017, 07:05:03 pm »
I have a 205Demo.   
I have practiced sending data to Cloutmqtt (GPS, BATTERY, Switch State, etc)

Now, I want to use the web/phone to send MQTT messages to my Demo205.

Messages that I can turn into commands like: "Turn on Lights".

Can you provide some code, or a video on how to send commands to the Demo205.

Thanks.

Social Buttons


DanielV

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Sending MQTT messages to a MC-Product
« Reply #1 on: January 08, 2017, 09:11:42 pm »
Found This: 

Shared Event Boot()
        Lplan.Subscribe("mcModule/000104e6/test")           
    End Event
   
    Shared Event SubscriptionDelivery()
        Dim msg As Message = Lplan.GetDelivery()
       
        If msg.Topic= "mcModule/000104e6/test" Then
            Dim payload As ListOfByte = msg.PayLoad     
            If payload.ToString() = "abc123" Then
                LedGreen = True
            Else
                LedGreen = False
            End If
        End If
    End Event

---
Will give it a try.

mc-T2

  • Administrator
  • Full Member
  • *****
  • Posts: 239
  • mc-Things! The opportunities are endless!
  • Location: Canada
    • View Profile
Re: Sending MQTT messages to a MC-Product
« Reply #2 on: January 09, 2017, 11:18:17 am »
Yes, using that code should give you a starting point, let us know if you have any questions!

Note - With the mcDemo205 right now, if you connect the device via USB cable (using the testboard gateway), the function to debug using MQTT has not been implemented. You can debug using MQTT if you connect the device to a gateway and work wirelessly. You can also still program the device to use MQTT using the serial port connection however.
We will be implementing this feature down the road
(I assume that since you have already been working with MQTT wirelessly already using the mcGateway?)
Thanks
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

DanielV

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Sending MQTT messages to a MC-Product
« Reply #3 on: January 09, 2017, 09:12:52 pm »
Works.  Thanks.
One problem ... it is slow.
Like a 30sec to 1min wait time.

How to i get the response time faster?
I doubt people would accept a delay of 1 minute to unlock the front door or turn on the lights.

Thanks.



'-------- code --------------------------------
Class ReceiveMQTT
   
    Shared Event Boot()
        Lplan.Subscribe("mcThings/000205D8")           
    End Event
   
    Const GNSSTimeout_uS As Integer = 120000000
    Const GNSS_MIN_SAT_COUNT As Integer = 3
   
   
        Shared Event SubscriptionDelivery()
        Dim msg As Message = Lplan.GetDelivery()
       
        If msg.Topic= "mcThings/000205D8" Then
            Dim payload As ListOfByte = msg.PayLoad     
            If payload.ToString() = "LightOn" Then
                Led3 = True
            End If   
            If payload.ToString() = "LightOff" Then
                Led3 = False
            End If
            If payload.ToString() = "GPS" Then
                Led2 = True
                Thread.Sleep(50000)
                Led2 = False
               
                Device.StartGPS(GNSSTimeout_uS, GNSS_MIN_SAT_COUNT)
            End If
        End If
    End Event
       
   
    Shared Event LocationDelivery()
        Dim lat As Float = Device.GetLatitude()       
        Dim lon As Float = Device.GetLongitude()
        Dim time As Integer = Device.GetGpsFixTime()
       
        Led2 = False
       
        Dim lat_String As String
        Dim lon_String As String
       
        lat_String = lat.ToString()
       
        lon_String = lon.ToString()     
       
        Dim payload As ListOfByte = New ListOfByte
       
        Dim paystring As String = "Location: " + lat_String + "  " + lon_String       
       
        payload.Add(paystring)
       
        Lplan.Publish("/water", payload)
        Thread.Sleep(100000)
        Thread.ClearHardwareEvent()
        Dim update As Integer = Device.Uptime()
       
    End Event   
End Class
'----------------------------------------

mc-T2

  • Administrator
  • Full Member
  • *****
  • Posts: 239
  • mc-Things! The opportunities are endless!
  • Location: Canada
    • View Profile
Re: Sending MQTT messages to a MC-Product
« Reply #4 on: January 10, 2017, 12:17:24 am »
Hey Daniel,

Refer to this thread: http://mcthings.createaforum.com/mc-gateway/long-mqtt-subscription-delay/

You can set to mid-power mode (high power mode is not yet implemented) with 100mSec.

Try this in your code:

Code: [Select]
Shared Event Boot()
Lplan.SetMidPowerMode(500)
End Event 

The reason for the delay using the default setting (low power mode) is found on page 44 of the mcScript guide:

Quote
Low – is the most common power mode. In this mode the module initiates
the connection to the mc‑Gateway™. If both the module and gateway don’t
have any data to exchange the connection is terminated. This mode
consumes very little power and is ideal for sensors and devices that needs
to indicate that something is happening because it can start the
communication immediately. Because the gateway cannot make a
connection the gateway has to wait for the device to connect before it can
send something to the device. A maximum silent time can be specified to
prevent the device from not connecting to the gateway. The default is 1
minutes

Mid-power mode description:

Quote
Mid – is the power mode where the connection is initiated by the module
and the connection between both gateway and device are permanently
connected to each other. They communicating with each other on at regular
time intervals. This can be between 20mSec and 2 seconds so this mode is
useful when both devices want to initiate the communication but one or both
have power consumption restrictions. Only eight devices configured in this
mode can connect to a gateway.

Hope that helps!
« Last Edit: January 10, 2017, 12:54:26 am 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