Code Monkey home page Code Monkey logo

sample-java-mqttsmartlight's Introduction

An MQTT sample app subscribes and publishes to ARTIK Cloud

Let's build a Java console application that communicates to ARTIK Cloud MQTT endpoint using ARTIK Cloud's Java/Android SDK. This app emulates a smart light, which turns on or off based on Actions from ARTIK Cloud and sends the latest state (on or off) back to ARTIK Cloud.

After completing this sample, you will learn:

Requirements

  • Java version >= 1.8
  • Apache Maven >= 3.3.9

Setup / Installation

Setup at ARTIK Cloud

  1. At My ARTIK Cloud, connect a device of the type "Example Simple Smart Light" (unique type name cloud.artik.example.simple_smartlight). You can use the one that you already own.
  2. Get the device ID and token. You will need them when running the console app (smart light emulator).

Setup Java project

  1. Clone this repository if you haven't already done so.

  2. At the root directory and run the command:

    mvn clean package

    The executable mqtt-smartlight-x.x.jar is generated under the target directory.

  3. Run the command at the target directory to learn the usage:

    java -jar mqtt-smartlight-x.x.jar

Demo

  1. Start the smart light emulator. Run the command at the target directory:
    java -jar mqtt-smartlight-x.x.jar -d YOUR_DEVICE_ID -t YOUR_DEVICE_TOKEN

Remember that you got the device ID and token of your smart light in the ARTIK Cloud setup phase.

  1. Send Actions to the emulator using the web tool at My ARTIK Cloud.

  2. The emulator terminal should show that Actions are received from the subscribed topic and updated states (on or off) are published back to ARTIK Cloud. The following is the example:

    Start mqtt session....
    Connecting to broker: ssl://api.artik.cloud:8883......
    Succeeded: CONNECT
    
    subscribe to: /v1.1/errors/7d469
    subscribe to: /v1.1/actions/7d469
    Succeeded: SUBSCRIBE_ERRORS
    Succeeded: SUBSCRIBE_ACTIONS
    
    Received message. Payload: {"actions":[{"name":"setOn"}]}; Topic:/v1.1/actions/7d469; Qos:0
    Handle Action: setOn
    Set State to true
    Publishing to topic: /v1.1/messages/7d469; message payload: {"state":true}; QoS:2.....
    Succeeded: PUBLISH
    Published to topic: /v1.1/messages/7d469
    
    Received message. Payload: {"actions":[{"name":"setOff"}]}; Topic:/v1.1/actions/7d469; Qos:0
    Handle Action: setOff
    Set State to false
    Publishing to topic: /v1.1/messages/7d469; message payload: {"state":false}; QoS:2.....
    Succeeded: PUBLISH
    Published to topic: /v1.1/messages/7d469
  3. Enter Ctrl+C to terminate the console application as following:

    ^C
    Prepare shutting down. Disconnecting... Please wait...
    Succeeded: DISCONNECT
    

More about ARTIK Cloud

If you are not familiar with ARTIK Cloud, we have extensive documentation at https://developer.artik.cloud/documentation

The full ARTIK Cloud API specification can be found at https://developer.artik.cloud/documentation/api-spec

Check out sample applications at https://developer.artik.cloud/documentation/tutorials/

To create and manage your services and devices on ARTIK Cloud, create an account at https://developer.artik.cloud

Also see the ARTIK Cloud blog for tutorials, updates, and more: http://artik.io/blog/cloud

License and Copyright

Licensed under the Apache License. See LICENSE.

Copyright (c) 2017 Samsung Electronics Co., Ltd.

sample-java-mqttsmartlight's People

Contributors

yujingwu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.