Our project deals with the implementation of a tracking system of the customers of a gym. In this way the owner can monitor the attendance of his gym, then he can improve gym's services.
This can be done analyzing the presence in the different rooms to make statistics using user personal data (age, gender,...).
HardwareEstimote Beacons
It is a Bluetooth Low Energy component that communicates its presence to the devices around it. It is powered by a non-replaceable lithium battery. There are different versions of Beacon, as you can see below:
Its function is therefore to estimate the presence of the devices in a given room and then presence of the customers of the gym, using Bluetooth Low Energy technology, allowing a minimum battery consumption.
It must use Estimote SDK to allow the communication between beacons and smartphone application.
Android Device
For this project, there is an Android application, used to communicate with Azure and display information to customer. So it needs for an android smartphone or tablet with Android 5 or above. You simply need for install apk file in GitHub repository.
IdeaWhen Android application notes the presence of a beacons, it transmits Beacon's ID to Azure IoT Hub and displays current room to gym client. IoT Hub forwards flow to dedicated Stream Analytics Job, which adds it in a SQL Database.
Finally Grafana queries Azure SQL database to create useful statistics for gym's owner.
In the application, during the first access, it is required to make a local registration stored in cache application. After it, the application sends information to Azure SQL Database and is possible to display also in Grafana Dashboard.
The data are required to collect information about customer's real time position, otherwise Azure drops eventually updates.
- Azure Portal: IoT Hub
First of all it's necessary to create an IoT Hub to receive data from Android application.
To do this, you have to select Create a resource > Internet of Things > IoT Hub.
Then, you choose the parameters of your IoT Hub, it's important to choose the same region for all resources you create, to avoid extra cost, due to the transfer from a region to another.
Finally, after you choose your size and plan (depending from the dimension and number of the message flow, you confirm the creation.
At this point, you have to register devices which IoT hub interacts with. To do this you have to put this command in Azure Cloud Shell:
az iot hub device-identity create --hub-name YourIoTHubName --device-id MyAndroidDevice
YourIoTHubName: obviously you must put your IoTHub name.
MyAndroidDevice: name of your device, we advise you to put this name, because our Android application uses this.
Finally, you have to put this command in your Cloud Shell:
az iot hub device-identity show-connection-string --hub-name YourIoTHubName --device-id MyAndroidDevice --output table
As for the previous command, you have to change the field with your values, in this way you get DeviceConnectionString
you have to put in Android application.
NB: For other information, we advise to follow the official tutorial (you find the link at the end of this post).
- Azure Portal: Stream Analytics Job
After creating IoT Hub, you have to create Stream Analytics Job. Before to do it, you need for a SQL Database that you use as output for the Stream Analytics Job (see here).
First of all, Select Data+Analytics > Stream Analytics job.
Use the same location of IoT Hub and put as Job name. After you create it, open the configuration panel and set as input the Iot Hub created previously, Inputs > Add Stream input > IoT Hub:
Now, you have to set the output for your Stream Analytics Job. So select Outputs > Add > SQL Database.
To do this, you have to complete the form with your SQL Database information. Then you have to define transformation query that Stream Analytics Job does with input and output. So you can do it as below:
1) Select Query and update the query as follows:
SELECTroom, COUNT(distinct email) as people, CURRENT_TIMESTAMP as time
INTO outputDatabase
FROM inputIoTHuB
GROUP BY room,TumblingWindow(minute, 1)
In this example, the query reads the data from IoT Hub and copies it to SQL Database.
2) Select Save.
NB: for the complete tutorial, please visit official tutorial using the links at the end of post.
- Android application
It's necessary to download and install Android Studio. Then, clone GitHub repository using the link at the bottom of this page.
You have to modifygradle.properties
file in/ConnectedGym
directory:
DeviceConnectionString=HostName=Gym.azure-devices.net;DeviceId=MyAndroidDevice;SharedAccessKey=############################################
This string permits the app to send information to your Azure IoT Hub, so you must put your IoT Hub value to DeviceConnectionString
variable.
At this point, you have to select Sync with gradle file
in file menu, then Build APK
and install it in your device.
Sending registration and updating data function
For sending data, we use dedicated functions, contained in MainActivity.java
file. In particular, the project is interested to exchange information like full name, age, gender and room where customer is. This information are sent to Azure IoT Hub.
The data used in this application is:
- Full name;
- Email, used to identify every user;
- Age and gender, to make statistics about customer's presence.
In particular, as you see below, the exchanged messages have also messageType
field, used to distinguish a registration message (R) from an updating one (U).
Registration function
private void sendRegMessages() {
prefs = getApplicationContext().getSharedPreferences("userData", MODE_PRIVATE);
msgReg = "{\n" +
" \"name\": \""+ prefs.getString("full_name", null) + "\", \n" +
" \"email\": \""+ prefs.getString("email", null) + "\", \n" +
" \"age\": "+ prefs.getString("age", null) + ", \n" +
" \"gender\": \""+ prefs.getString("gender", null) + "\", \n" +
" \"messageType\": \"R\"\n" +
"}";
try {
sendReg = new Message(msgReg);
sendReg.setMessageId(java.util.UUID.randomUUID().toString());
System.out.println("Message Sent: " + msgReg);
EventCallback eventCallback = new EventCallback();
client.sendEventAsync(sendReg, eventCallback, msgRegSentCount);
msgRegSentCount++;
} catch (Exception e) {
System.err.println("Exception while sending event: " + e);
}
}
This function is used to send registration data from android application to Azure IoT Hub. In the first instruction, it takes data to send from the application cache. In the second step, it builds the JSON format message using the information.
Then it tries to send message. So it creates Message
object, gives to its an ID to distinguish from the other. Then function creates an Event Callback
and finally send message, calling a method from client object that represents a Device
object.
Updating function
private void sendUpdMessages() {
prefs = getApplicationContext().getSharedPreferences("userData", MODE_PRIVATE);
msgUpd = "{\n" +
" \"email\": \""+ prefs.getString("email", null) + "\", \n" +
" \"room\": "+ currentRoom + ", \n" +
" \"messageType\": \"U\"\n" +
"}";
try {
sendUpd = new Message(msgUpd);
sendUpd.setMessageId(java.util.UUID.randomUUID().toString());
System.out.println("Message Sent: " + msgUpd);
EventCallback eventCallback = new EventCallback();
client.sendEventAsync(sendUpd, eventCallback, msgUpdSentCount);
msgUpdSentCount++;
} catch (Exception e) {
System.err.println("Exception while sending event: " + e);
}
}
This function takes, as the previous, the information from the cache application to update the position of the customer.
In particular, it takes email to identify the user and room he is actually in.
Then, as the previous function, it sends this information using the same instruction of Registration function
.
- Grafana Dashboard
The last step is how to monitor all data and make it understandable for gym's owner. Grafana helps to do it.
Grafana permits to create a dashboard where we put all the charts we need for. The charts take data querying Azure SQL Database.
First of all you need to setup the data source,
choosing it in menu:
So you must select Azure Monitor and Microsoft SQL Server plugins.
Then you need to configure Microsoft SQL Server plugin putting username and password used for SQL Server in Azure portal:
The final step is to create a new panel and put in it a query to show data.
After choosing the source of your queries (Microsoft SQL Server
), you need to put the query and then choose how to represent data, choosing the kind of the chart, as you can below:
We try the application in a real gym during a morning.
Gym's owner was very enthusiastic to have this tool to monitor his customers and their activities.
Also the customers were very enthusiastic because in this way, they can know in which room they stay.
Linkedin Profile
Presentation
Resources and tutorials
Comments