95-733 Internet Technologies Tuesday, September 13, 2016 Projects 2 MQTT Due Date : 11:59 PM, Tuesday, September 27 The objective of the this project is to learn to use an MQTT broker named Mosquitto. MQTT is targeted to low power constrained devices. It will likely play an important role in a mature IoT and is certainly an important internet technology. Project 2 ========= This is a two part assignment. The first part asks that you develop a small system using Javascript and an MQTT broker. The second part asks that you build a small system using a Java program as a publisher to MQTT and a Javascript program as a subscriber to messages coming from MQTT. The internet of things (IoT) often collects real time data from sensors and transmits these data to a service offering publish and subscribe capabilities. The service is usually called a broker. The sensor acts as a publisher. The sensor has no idea who its subscribers are. It simply transmits data to the broker. The subscriber subscribes to messages from the broker and, in this way, is able to read data from the sensors. In Project 1, we used a whiteboard application from Oracle to build an application that allowed us to collaborate in real time without polling. We leveraged HTML WebSockets and wrote our own server side code that sent a message to all listeners. If we wanted to, we could write a more involved service that would allow for publishing and subscribing as outlined above. But this would a bigger challenge than it seems. And, fortunately, we don"t have to. We have many such brokers to choose from. In this project, we will not write our own server side code. Instead, we will use an open source MQTT broker called Mosquitto. mosquitto can be downloaded from here: http://mosquitto.org/download/. You can install Mosquitto on your MAC using brew. See http://brew.sh and then use "brew install mosquitto". There are directions on the mosquitto web site for Windows users. When you run mosquitto, it will run with its default configuration. We need to change its configuration to allow for WebSocket connections (we want to visit the server from Javascript). To change the configuration of Mosquitto, we can modify the configuration file found at /usr/local/etc/mosquitto/mosquitto.conf (on a MAC). Make one change to this file. Change the line that reads "protocol mqtt" to "protocol websockets" (without the quotes). My copy of mosquitto is located at /usr/local/sbin. If I change to that directory, I can run mosquitto (picking up the configuration file) with the command mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf -v Once you have mosquitto running, we want to connect to the server from Javascript running in a browser. We do not want to write the client side MQTT code ourselves. Even though we have easy access to WebSockets, it would be far better to use an existing Javascript library that provides a convenient API to MQTT. The implementation of the API will, of course, use Websockets below the scenes. In our web pages, we will include the Javascript library from Paho. It is called mqttws31.js. There is a very nice getting started section at the following link. It has a simple example of a Javascript client that will run in a web page : https://eclipse.org/paho/clients/js/ The mqttws31.js Javascript library can be directly downloaded from here: https://raw.githubusercontent.com/eclipse/paho.mqtt.javascript/master/src/mqttws31.js There is a nice discussion here on MQTT. https://www.ibm.com/support/knowledgecenter/#!/SSFKSJ_7.5.0/com.ibm.mm.tc.doc/tc60302_.htm Part 1. ======= Your task in Project 2 Part 1 is to: 1) 10 Points. Build a Netbeans project named Project2Basic with the code found here: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onmousemove Note that every time the mouse moves (within the box) an event is generated and the x and y coordinates are displayed. It is required that you add your own detailed comments to this code - explaining clearly how it works. 2) 20 Points. Here we want to sense the mouse movements and pass them along to Mosquitto(MQTT). Copy Project2Basic (along with your comments) to a new Netbeans project called MouseTrackerPublisher. This will need to be modified so that every mouse movement publishes the coordinates to mosquitto. It must make good use of the Javascript library mqttws31.js. 3) 20 Points. Create a new Netbeans project called MouseTrackerSubscriber. It too will make use of mqttws31.js. MouseTrackerSubscriber will subscribe to events published to mosquitto. It will simply display the coordinates being received (there will be no box as is shown in MouseTrackerPublisher). Part 2. ======= In Part 2, we will be communicating with mosquitto in two ways. We will use Websockets from within our Javascript code - running within a browser. And we will also use standard TCP sockets (and a client library) from within a stand alone Java program. In order to make this happen, we need to change the mosquitto configuration to include the following four lines. You will need to carefully use the appropriate ports within your code. listener 1883 protocol mqtt listener 9002 protocol websockets For the Java client, we will be using the Eclipse Paho Java Client libraries. This is very easy to do using Maven and Netbeans. In short, Maven is an opinionated build tool. That is, it is based around the idea of “convention over configuration”. There is a nice 15 minute video here on using Maven on the Netbeans IDE: https://www.youtube.com/watch?v=mGmUH-SxFHA The following URL has the repository definition and the dependency definition needed for your pom.xml. It also has a very nice sample Java client that you may work from. https://eclipse.org/paho/clients/java/ 1) 25 Points. Build a Netbeans project named TemperatureSensorProject. This project will hold a stand alone Java program named TemperatureSensor.java. Every 5 seconds, TemperatureSensor.java will generate a random temperature (uniformly distributed) between the values of 0 and 100 degrees fahrenheit. If the random temperature is between 0 and 45 degrees inclusive, TemperatureSensor.java sends the temperature and a timestamp to the topic called “pittsburgh/coldTemps” on mosquitto. If the temperature is greater than 45 degrees but less than 80 degrees, TemperatureSensor.java sends the temperature and a timestamp to the topic called “pittsburgh/niceTemps” on mosquitto. If the temperature is greater than 80 degrees then TemperatureSensor.java sends the temperature and a timestamp to the topic called “pittsburgh/hotTemps” on mosquitto. 2) 25 Points. Build a Netbeans web application that allows the user to subscribe to a particular topic from Mosquitto. The user might be interested in “pittsburgh/coldTemps”, “pittsburgh/niceTemps”, “pittsburgh/hotTemps”, or all temperatures. Name the web application project TemperatureSubscriberProject. This project will require the Javascript library mqttws31.js. To get data on all temperatures, use topic wildcards. The detailed design of the site is in your hands. It will allow the user to select from the four possible options and will then show each temperature and timestamp pair published to that topic. Submission guide ================ Part 1: Zip the three projects into a single zip file and post to the assignment section of Blackboard. There should be one zip file named Project2Part1.zip. Part 2: Zip the two projects into a single zip file and post to the assignment section of Blackboard. There should be one zip file named Project2Part2.zip. Documentation is required. Spend some time cleaning up your code and adding good comments to your code. We may require that you schedule some time during the instructor or TA office hours to demonstrate your solution. If you have difficulty running mosquitto on your machine see us soon. If we are unable to get mosquitto running there is a free online version available. Get started soon and let us know of any difficulties with installation. Bonus points ============ Completion of all of the above can earn 100%. To earn 5 bonus points, include Google charts in your web application and display the temperature data in an engaging way. Using a Remote Broker ===================== If you are unable to install Mosquito on your local machine, I have had success with the following broker on the cloud: broker.hivemq.com for MQTT use port 1883 and for Websockets use port 8000 In my Javascript, I am using a line like this: var loc = {'hostname' : 'broker.hivemq.com', 'port' : '8000' }; In my Java code, I am using a line like this: String broker = "tcp://broker.hivemq.com:1883"; Mosquito is fussy about the client ID. In the following line of code, I have used my Andrew ID. You should do that as well. In this way, we will not step on each other. client = new Paho.MQTT.Client(loc.hostname, Number(loc.port), ‘mm6’); Installing Mosquito on a PC ===================== Download mosquitto setup file from http://www.eclipse.org/downloads/download.php?file=/mosquitto/binary/win32/mosquitto-1.4.10-install-win32.exe Run the setup file and follow the steps to complete the installation. note: Default destination folder is C:\Program Files (x86)\mosquitto Download and Install Win32 OpenSSL v1.0.2h from here http://slproweb.com/products/Win32OpenSSL.html Install it to C:\temp\OpenSSL-Win32 Copy libeay32.dll and ssleay32.dll from C:\temp\OpenSSL-Win32\bin to C:\Program Files (x86)\mosquitto Download pthreadVC2.dll from ftp://sources.redhat.com/pub/pthreads-win32/dll-latest/dll/x86/ and Copy it to C:\Program Files (x86)\mosquitto Testing: --------- open command prompt and run the following commands cd C:\Program Files (x86)\mosquitto mosquitto.exe -v -c mosquitto.conf You should get messages similar to this 1474406476: mosquitto version 1.4.10 (build date 24/08/2016 21:03:24.73) starting 1474406476: Config loaded from mosquitto.conf. 1474406476: Opening ipv6 listen socket on port 1883. 1474406476: Opening ipv4 listen socket on port 1883. 1474406479: mosquitto version 1.4.10 terminating