App Development: IoT - MQTT Connects Mobile App & IoT Devices

15
MQTT – Connects IoT Devices & Mobile App Marvin Heng Twitter : @hmheng Blog : http://hmheng.pinsland.com Github: https://github.com/hmheng

Transcript of App Development: IoT - MQTT Connects Mobile App & IoT Devices

Page 1: App Development: IoT - MQTT Connects Mobile App & IoT Devices

MQTT – ConnectsIoT Devices &Mobile App

Marvin HengTwitter : @hmhengBlog : http://hmheng.pinsland.comGithub: https://github.com/hmheng

Page 3: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Mobile App that Connects IoT Device via MQTT1. First, we need to create a Xamarin Cross Platform Mobile App. Don’t have one yet? Click to learn how to create one!

Page 4: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Mobile App that Connects IoT Device via MQTT2. Secondly, we need to add a NuGet Packages - MQTTnet, right click Solution in the Solution Explorer -> Manage NuGet Packages for Solution…

2b

2a

Page 5: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Mobile App that Connects IoT Device via MQTT3. Let’s search MQTTnet and install it to all the projects.

3b3a

3c

Page 6: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Mobile App that Connects IoT Device via MQTT4. Now, we need to add some components to MainPage.xaml as specified below.

4b4a

4c

4

Page 7: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Mobile App that Connects IoT Device via MQTT5. After that, click the code behind – MainPage.xaml.cs and add the following usings.

4b4a

4c

Page 8: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Mobile App that Connects IoT Device via MQTT6. Now, replace the constructor as below and declare a mqttClient.

4b4a

4c

Page 9: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Mobile App that Connects IoT Device via MQTT7. Create an Async function for handling BtnConnect’s click event with the following code.

4b4a

4c

Page 10: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Mobile App that Connects IoT Device via MQTT8. Create an Async function to send the message to MQTT Broker/Server whenBtnSend is being clicked.

4b4a

4c

Page 11: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Mobile App that Connects IoT Device via MQTT9. Create an Async function for handling connected event when MqttClient is successfully connected with MQTT Server.

4b4a

4c

Page 12: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Mobile App that Connects IoT Device via MQTT10. Lastly, let’s create a function to handle the message it received from Broker.

4b4a

4c

Page 13: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Mobile App that Connects IoT Device via MQTT11. Finally, let’s compile and run it on your mobile device. Your MQTT Broker should be able to receive the message for the topic it subscribed to.

Desktop MQTT BrokerClick to Get Source Code for Testing

Page 14: App Development: IoT - MQTT Connects Mobile App & IoT Devices

Awesome!Your Mobile App is Now Able to Connect IoT Devices via the Most Popular Protocol - MQTT

Page 15: App Development: IoT - MQTT Connects Mobile App & IoT Devices

MQTT – ConnectsIoT Devices &Mobile App

Marvin HengTwitter : @hmhengBlog : http://hmheng.pinsland.comGithub: https://github.com/hmheng