Learn how to use IoTIFY network simulator to create a waste management application for smart cities.
Waste collection is one of the most important public services in any city. Currently, most municipalities have a fixed schedule with fixed routes for collecting garbage, and this can result in various inefficiencies: sometimes garbage trucks pick up trash cans that were almost empty, and sometimes overflowing cans are left in this state for days. This is because with a traditional approach the state of a trash can is unknown until the operator goes physically where the can is located. These problems can be overcome by making the trash cans smart, i.e. placing a sensor in each can to detect the garbage fill level, and periodically sending this data to a central server: in this way, waste colllection schedules and routes can be optimized based on the actual need, increasing the level of service and decreasing costs.
IoTIFY network simulator can be used to simulate any IoT device, including trash cans! The following is a step-by-step guide that illustrates this use case.
Losant is an IoT platform with various tools to manage IoT applications. We are going to use it to manage our simulated trash cans. If you don’t have an account at Losant, sign up here. After logging in, click on the Applications tab in the upper menu and then Create Application; in the new application screen, choose a name for your application (for example “Waste Management”):
Click the Create Application button and you will be taken to the application home page. Our simulated trash cans will be defined in Losant by creating devices in the application. In order to facilitate the creation of multiple devices of the same type, Losant offers the possibility to create a recipe and instantiate devices from this recipe. In the Devices section of the application home page, click the Add button:
In the CREATE FROM RECIPE section, click Manage My Device Recipes and then Add Recipe; in the next screen, give a name to the recipe (from example, “Trash Can”) and select the option “Set my own default names, tags and attributes”:
Click Create Recipe; in the next screen, we are going to define the device properties, which will be used to visualize data coming from any of our simulated devices. In the DEVICE TYPE section, select Standalone:
In the DEVICE ATTRIBUTES section, add an attribute with data type “GPS string” and name “location”, and another attribute with data type “Number” and name “level”; these attributes correspond to the data sent by each trash can, i.e. its physical location and the garbage fill level:
Finally, click Save Recipe. Now we can create multiple devices taking this recipe as starting point: go back to the application home page, click Add to add a new device, then in the CREATE FROM RECIPE section select the recipe just created:
Click Create from Recipe, then in the device creation screen change the device name to something unique (e.g. “TrashCan001”):
Note the device ID displayed in the upper right part of the screen: it will be used later when simulating this trash can. Click Save Device to save the last change. Following the same procedure we can quicky create other devices; in this example, we are creating 3 devices, which will be visible in the application home screen:
Any device connecting to our application will need to provide authentication credentials. In Losant, these credentials are defined in the form of an access key and a corresponding access secret. A given access key can be shared by more than one device; in our application we are going to create a single access key which will be used by all trash cans.
From the application home page, click on the Security tab and then click the Add Access Key button at the top right corner; in the key definition page, give a descriptive name to tha key (for example, “Garbage Collection”), and select “All Devices” in the access restrictions section:
Click Create Access Key; a popup widget will appear displaying the auto-generated key and secret:
Make sure to copy at least the access secret (since it won’t be recoverable if you lose it), then select “I have copied my access key and secret to a safe place.” and click Close Window. The newly created access key is now ready to use.
Losant allows creating personalized dashboards to display data coming from any device in a given application, and also to send commands to a device. We are going to create a dashboard to monitor the status of the 3 trash cans we created in the previous steps and to empty the trash cans.
Click the Dashboards item in the top menu in the home page of your Losant account and click Create Dashboard; in the next screen, choose a name for your dashboard, for example “Garbage collection”:
Click Create Dashboard at the bottom of the page. Now it’s time to define the contents of the dashboard:
Since our trash cans will be sending location and garbage level data, we are going to display a map with the location of the trash cans and a bar chart indicating the garbage level in each trash can.
To create the map, click Customize in the GPS HISTORY block and you will be taken to the block definition screen; select the Waste Management application; in the Duration drop-down menu, select “Last received data point” (we don’t need to track the location history, because our trash cans will always stay at the same place); in the “Device IDs/Tags” drop-down menu, select “DeviceRecipe=Trash Can”, and in the Attribute drop-down menu select “location”, so that the map will display the location of all devices created from our recipe:
A Losant map can also show in a popup widget additional information on a device when clicking on it. We are going to use this feature to visualize the identifier of each trash can, its location and its garbage level; in the Popup Template text box, write the following text:
Note that in the last line of the above text, “level” must correspond to the name of the attribite you assigned to the device recipe to hold the garbage level value.
Click Add Block to add the map to the dashboard.
Now we going to add a bar chart with the graphic visualization of the garbage level in all trash cans. In the dashboard page, click the settings icon located at the top right corner and then click Add Block:
Locate the BAR CHART block in the list of widgets and click its Customize button. In the widget editing screen, select the Waste Management application, and in the Duration drop-down menu select “Last received data point”; then, in the Axis Configuration section, assign a label to the axis (for example, “Garbage Level”), and insert 0 and 100 as minimum and maximum values, respectively, since we are going to express the garbage level as a percentage:
Next, we are going to add a segment for each trash can: in the BLOCK DATA section, select the first device from the “Device IDs / Tags” drop-down menu, select “level” as attribute, and insert a label. Then click Add Segment and do the same for the other two devices:
Finally, click Add Block.
Now we need a way to be able to empty each simulated trash can, just like what happens in a real-world scenario when the garbage is collected; to do so, we need a widget that is able to send commands to the trash cans, and this is implemented in Losant with input controls: click Add Block, and choose INPUT CONTROLS from the widget list; in the widget editing page, select the Waste Management application, then click Add Control and Button Trigger:
In the button definition section, assign a label to the button (for example, “Empty 001”), select “Send Device Command”, select the first device from the “Device IDs / Tags” drop-down menu, and insert the string “empty” in the Command Name text box:
Repeat the same procedure for the other trash cans. Finally, we want to be able to empty all trash cans with a single command, for example to simulate the event where a garbage truck is dispatched to empty all trash cans in a given street: add a new button trigger, give it an appropriate label such as “Empty All”, select “DeviceRecipe=Trash Can” in the “Device IDs / Tags” drop-down menu, and insert the string “empty” as command name:
Click Add Block to add the buttons to the dashboard.
Any widget in the dashboard can be moved around with drag-and-drop or resized to fit your preferences.
Now that we have our Losant application and dashboard set up, we need to simulate the trash cans. Each trash can will correspond to a device as defined in the Losant application, and we need to create a template for each device in the simulator.
Click on Create a new Template in IoTIFY network simulator, then give a unique name to the template, select MQTT as network connection type, select mqtt (TCP) as protocol, and insert “broker.losant.com” as endpoint URL; in the ClientID text box, insert the device ID that has been assigned to the first device you created (the device ID is displayed in the upper right corner in the Losant webpage that lists the device properties):
Now we need to provide the authentication credentials to connect to our Losant application: expand the Provide Credentials section; in the Username field, insert the access key previously created when setting up the Losant application; in the Password field, insert the corresponding access secret:
In the Message Contents section, insert as topic a string with the format “losant/<device-id>/state”, where <device-id> is the device ID of our first trash can; select the JSON checkbox, and insert the following string in the Message Contents text box:
In the above string, the “data” attribute of the myret JSON object contains two attributes: “location”, which indicates the physical location of our trash can, and “level”, which is the garbage level expressed in percentage points. While the location is fixed, the expression used for the garbage level simulates a trash can initially empty, which at each iteration fills up by a random amount between 0 and 5%, until it reaches the 100% level:
Now we need to define how a trash can reacts when we send it the “empty” command to simulate that it has been emptied. This is easy thanks to the topic subscription feature of IoTIFY MQTT client: in the template, enable the Subscription check box, insert in the topic field a string with format “losant/<device-id>/command”, and copy in the handler function text box the following string:
The semantics of the above text should be easy to understand: our client parses the JSON-formatted message sent by the Losant broker and extracts the value of the “name” attribute, which corresponds to the name of the command that we set up in the Losant dashboard input control; if the name matches the string “empty”, then the garbage level is set to 0%.
The template for our first trash can is now complete: click Save.
Now we can create the templates for the remaining trash cans: these will be the same as the first template, except for the following settings:
- the template name
- the ClientID field, which corresponds to the Losant device ID
- the topic used for publishing, which must match the device ID
- the topic used for subscribing, which again must match the device ID
- the value of the “location” attribute in the message JSON object; in our example, we are going to use “47.363961, 8.535817” for the second trash can and “47.363463, 8.534150” for the third trash can, so our 3 cans will all be located in the same street
Now that we have everything in place, we can start simulating our trash cans, monitoring their status from the Losant dashboard.
In IoTIFY network simulator, start a simulator from each of the 3 templates, making sure that the number of clients in each simulation is set to 1. Then go to the Losant dashboard: when the trash cans send data to Losant, the dashboard will be updated to reflect the current status:
You can update manually the dashboad by clicking on the Refresh icon in the top right toolbar, and you can adjust the automatic refresh frequency in the dashboard settings.
If you click on any trash can in the map, you will see a popup indicating the trash can identifier, its GPS coordinates and its garbage level:
Now we can simulate the event where one trash can is emptied: first, unlock the input controls widget by clicking on the slider in its toolbar; then, you can click any of the Empty 001, Empty 002 and Empty 003 buttons to empty the corresponding trash can:
An emptied trash can will reset its garbage level to 0% and then will gradually fill up again; you can see this in the dashboard as soon as the trash can sends its next message after having been emptied.
You can empty all trash cans with a single command by clicking the Empty All button.