Blog 4 - Development 1 - Igor Kapusniak
Blog 4 - Development 1
Introduction
This week I began the development of new the features for the binbot. In this sprint I focused on updating the microbit cloud board code, Binbot fullness sensor, and a fullness percentage display.
Microbit Cloud Board Update
The microbit cloud board required a complete overhaul of the current code as MQTT topic to publish to needed to be updated dynamically based on the radio data it received.
The Code is now split into 3 sections; An initialisation section, on radio receive, and on MQTT message receive.
The Initialisation Section:
This section sets up global variable classID, leaderboard_topic, and topic. ClassID is a unique number assigned to each classroom, this makes it easily searchable identifiable. leader board_topic represents the MQTT topic where information about the class rankings can be obtained. topic is initially assigned to "binbot" but is later updated dynamically, it represents the MQTT topic which data should be published to.
Next a connection to WiFi is established using the library for the cloud board.
Once a WiFi connection is made, the code creates a connection with a Beebotte MQTT broker.
When the MQTT connection is setup a subscription to the leaderboards_topic is established. This allows for the leader board data to be sent to the microbit.
The radio channel is setup. The channel number between the slave devices and the cloud board has to match for communication to occur.
Finally, a confirmation LED image is displayed temporarily to indicate a successful setup.
On Radio Received:
This section published data to a MQTT topic every time it receives a radio message from the slave devices.
It takes radio message and splits it into into a string and number. The string is used to create the topic to which the number will be published to using MQTT.
Once the message is sent a diamond shape is displayed on the led matrix to show a successful reception and publishing of the data.
On MQTT Received:
The MQTT Reception Section gets triggered anytime data is published to a specific topic, in this case the leader board topic. Once triggered variables for leaderboard_rank and leaderboard_total_ranks are initialised to 0,
leaderboard_rank represents the position at which this class is in the ranking
leaderboard_total_ranks represents the total number of classes on the leader board.
The leaderboard variable is a list, that is obtained by splitting the message received from MQTT using the comma symbol. The expected data from MQTT is a string in a form like: "class0,class40,class12,class4". In this example there a four classes where class 0 is in first places followed by class40, and then class12.
Splitting on the comma creates a list of classes which can be iterated on and compared to this classes ID.
For each item in the list leaderboard_total_ranks is incremented by one and when the classID of this class matches that of the item in the list leaderboard_rank is set to leaderboard_total_ranks.
Once the classes position in the leaderboard and total number of classes is calculated its sent out using the microbits radio. This message is picked up by a slave device and shown on a display.
Ultrasonic Sensor and Husky Lens Display
To collect data on the binbit an slave microbit is required. This microbit has an ultrasonic sensor and huskylens camera connected to it.
The ultrasonic sensor enables monitoring of how full the binbot is. It does this by sending out an ultrasonic wave and listening for it, the time it takes for the sound to bounce back is converted to a distance. To calculate the percentage of how full the bin is, the depth of the bin was measured and found to by 85cm.
The ultrasonic sensor is mounted underneath the top lid of the bin pointing down. This makes the calculation of how full the bin is bin depth(85cm) subracted from the measured distance then divided by the bin depth or ((bin depth - distance) / bin depth).
This value is then sent via radio as a string value pair to the cloud board. With the string representing the MQTT topic for bin fullness and value the percentage full the bin is at.
The Huskylens is only used for its display this slave device does not require the camera. Upon the display the fullness percentage and leader board rank the class is at is displayed. The leader rank is obtained listening for radio transmissions from the cloud board.
Initialisation of Slave Device:
On Radio Receive:
Main loop:
Comments
Post a Comment