element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Pi IoT
  • Challenges & Projects
  • Design Challenges
  • Pi IoT
  • More
  • Cancel
Pi IoT
Blog [Pi IoT] Smart Competition Home #11: User's Node all in one
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: clazarom
  • Date Created: 30 Aug 2016 7:29 AM Date Created
  • Views 1265 views
  • Likes 2 likes
  • Comments 4 comments
  • design challenge
  • mqtt
  • smart competition home
  • paho
  • android app
  • piot
Related
Recommended

[Pi IoT] Smart Competition Home #11: User's Node all in one

clazarom
clazarom
30 Aug 2016

This new post finalized the User Node (an Android device). It will include the smart-house functionalities to that of the competition system. This way, any resident will be able to check the smart house information while connected to the WiFi and switch to Competition mode when leaving to gain some miles.imageimage

 

*In other words... I will make the Smart Competition button work image

 

 

 

 

User's node - include MQTT Publisher Client

 

 

                                                                imageimageimage

      NOT CONNECTED                                                                                                              EXAMPLES OF SUBSCRIBE RESPONSE

Smart competition Activity

Initial setup: Nexus 5 / Android / SmartCompetitionHome App v 3

 

http://www.eclipse.org/paho/images/paho_logo_400.png

Functionalities

It is a direct implementation of the MQTT Clients, thanks to the Paho library

 

MQTT Clients subscriber & publisher

I create both kind of clients in the app. To do so, the code needs:

  • Client id
  • url - local IP of the broker
  • port - that of MQTT Service (or the one our broker is listening to) Default port = 1883

 

Both types of client (subscriber, with its callbacks and publisher) are implemented in the Paho libraries. Very great news image

public static void createMQTTDefaultClients(){
String url = protocol + broker + ":" + port;
clientId = "phone_"+action;

try {
    
   //Publisher:
  //Create an instance of this class
   sampleClient = new MyCustomMqttClient(url, clientId, cleanSession, quietMode,userName,password);

   // Perform the requested action
   sampleClient.publish(pubTopic,qos,message.getBytes());

   //Subscriber:
   //For the async
   clientId = "phone_"+action_async;
   sampleSubscriber = new SampleAsyncCallBack(url,clientId,cleanSession, quietMode,userName,password);
   sampleSubscriber.subscribe(subTopic,qos);


} catch(Throwable me) {
   // Display full details of any exception that occurs
   System.out.println("reason "+((MqttException) me).getReasonCode());
   System.out.println("msg "+me.getMessage());
   System.out.println("loc "+me.getLocalizedMessage());
   System.out.println("cause "+me.getCause());
   System.out.println("excep "+me);
   me.printStackTrace();
}
}

 

Subscriber

In order to create the subscriber, I instantiate the class SampleAsyncCallback (implementing MqttCallback). The subscription will be performed as a combination of the function subscribe() method (which starts and manages the process)and the waitForStateChange(). As a result, the code will navigate through all the connection steps:

  • Normally - BEGIN, CONNECTED, SUBSCRIBED, DISCONNECT, DISCONNECTED

While the client is subscribed, the information will get to the phone as a callback, messageArrived(). This method is used to:

  • Get new data of the topic
  • Update the interface to include this new information

More details of this callback:

/**
 * @see MqttCallback#messageArrived(String, MqttMessage)
 */
public void messageArrived(String topic, MqttMessage message) throws MqttException {
   // Called when a message arrives from the server that matches any
  // subscription made by the client
   String time = new Timestamp(System.currentTimeMillis()).toString();
   System.out.println("Time:\t" +time +
   " Topic:\t" + topic +
   " Message:\t" + new String(message.getPayload()) +
   " QoS:\t" + message.getQos());

  if (topic.equals("sensors/door")){
   //Change door values
  //MainActivity.doorState.setText(new String(message.getPayload()));
   SmartHomeActivity.readDoor = new String (message.getPayload());
   receivedDoor = true;
   }else if (topic.equals("sensors/temperature")){
   //Change temperature values
  //MainActivity.tempState.setText(new String(message.getPayload()));
   SmartHomeActivity.readTemp = new String (message.getPayload());

   receivedTemp = true;

   }else if (topic.equals("sensors/pressure")){
   //Change pressure values
  //MainActivity.pressState.setText(new String(message.getPayload()));
   SmartHomeActivity.readPress = new String (message.getPayload());

   receivedPres = true;

   }else if (topic.equals("sensors/warning")){
   //Change warning
  //MainActivity.warningState.setText(new String(message.getPayload()));
   SmartHomeActivity.readWarning = new String (message.getPayload());

   receivedWar = true;

   }else if (topic.equals("sensors/altitude")){
  SmartHomeActivity.readAlt = new String (message.getPayload());

   }else{
   //Change anything
   SmartHomeActivity.readTemp = ("?");
   SmartHomeActivity.readWarning = ("?");
   SmartHomeActivity.readDoor = ("?");
   SmartHomeActivity.readPress = ("?");

   }
   if (receivedDoor && receivedTemp && receivedPres ){
   receivedDoor = false;
   receivedTemp = false;
   receivedPres = false;
   receivedWar = false;
   //Go to the next step of the connection
   SmartHomeActivity.subscribed = false;

   }
}

 

At this point, I use the subscribe() function when pressing SUBSCRIBE button.

 

 

Publisher

I have been using it mainly for debugging purposes: I can check whether messages are received by the broker when the sensor data seems to be lagging.

 

NOTE: Clients id! Along this project, I have been creating a few different clients. It might be obvious, but sometimes it is not... I have been given them different ids. The broker will refuse any connection if there is already a client with that name

 

Not the best features

 

I wanted to refined this Smart home activity, since its missing both a nicer look and additional useful commands. Regarding this commands, I wan to point out that:

  • The connection IP is hardcoded, which means that the user has no way of selecting a different device. Consequently, an extra field needs to be included for that purpose
  • There is no alert when the connection fails.
  • It is not automated - I have to press subscribe button every time a want to get new data
  • There is the "Publish" option on the interface, which will actually create MQTT_Publisher and send the message to the broker. However, the Central Node only logs the result:
    • With a bit more of coding, we can use the smart home application to control some actuators (if some are installed in the house)

All in all... it will get the job done but it is still not the most comfortable to deal with

 

Conclusion

This post closes the implementation of our User's Node. Now, we have the two main functions of the system:

  • A distance tracker linked to the competition server
  • An MQTT client which can be use to read data from Sensors Node
  • Sign in to reply

Top Comments

  • DAB
    DAB over 9 years ago +2
    Nice update. I like nice simple implementations. It would not take much modification to make a more robust system, so you have a good basis for future expansion. DAB
  • rhe123
    rhe123 over 9 years ago +1
    You're making some impressive progress in the last 24 hours, clazarom !
  • clazarom
    clazarom over 9 years ago in reply to rhe123 +1
    Kind of overflow of posts ^^u ... deadline came upon us and I wanted to have a complete project, even if late ^^
  • DAB
    DAB over 9 years ago

    Nice update.

     

    I like nice simple implementations.

     

    It would not take much modification to make a more robust system, so you have a good basis for future expansion.

     

    DAB

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • fvan
    fvan over 9 years ago in reply to clazarom

    I think the deadline is more of a guideline, a goal to work towards. I don't think they would ignore your work because you posted a bit later image

     

    Well done!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clazarom
    clazarom over 9 years ago in reply to rhe123

    Kind of overflow of posts ^^u ... deadline came upon us and I wanted to have a complete project, even if late ^^

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • rhe123
    rhe123 over 9 years ago

    You're making some impressive progress in the last 24 hours, clazarom!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube