Location Based IOT Devices
By Robin Malhotra — “On the journey to find the biggest Goliath and slay it down!”
There would be multiple Bikes/Scooters/Cycles in a city that are available on a various stations spread intelligently in the city. Where a customer would just scan the QR code of the bike and the lock would pop open and is available for the ride. After the ride is done, customer would put the lock back in its place and the system detects it and the customer app would allow him to end the ride. How does this happen? Well there are numerous steps involved so relax, get comfortable.
I would unthread the architecture that we followed to develop this system. This application took about 3 months to build and whole 1 year of fixes to reach its working capacity that it has now. This is being used in many cities outside India and mainly in major cities of India like Chandigarh, Delhi, Hyderabad, Chennai.
Each Bike would have an IOT device, a kind of a lock, something looking like this.
Now, these locks are capable of many services, main ones being.
1: Location Based Services: Sending Location Data at a set frequency.
2: Real-Time Data: Heartbeat data, Current battery, Voltage, Speed, Altitude.
3: Locking/ Unlocking Commands
These locks come with a documentation, that is made available by the manufacturer once the company buys the locks from them and these IOT devices would then be needed to get implemented using a server based architecture. Since these are IOT devices, it would be using the raw socket functionalities.
Since, our server side environment is node.js, our natural approach would be to use socket.io, which actually wouldn’t work, because socket.io requires the same protocols be used on both server and client side. So, we would be using node.js internal module “net”.
Now, this is used only when the device that you are implementing is socket based device. There could be some scenarios where the IOT companies delegate the firmware part to other companies and they implement all of it on there own server and can therefore sell the APIs, and you would need to implement those APIs. Well, they would also need to use this same strategy.
It is advised to follow the Micro-services Architecture here and design this server only for the IOT communications. So you need to set up a server and preferably a MongoDB or it can be a MySQL DB if you want on the same server. using MongoDB would actually be better since there are close to none relations between tables. Unless you want to have an optimized architecture of multiple types of devices with there types and all there commands stacked in the database itself, then using a relational DB makes more sense.
Handle Locations
1: Parse the locations from the incoming packet and expose them in an api (REST/GraphQL).
— ‘/update_location’. Use JOI for validations.
//Request Params
{"secret_key": <YOUR SECRET KEY>,"device_id": <123456>,"location": [ { "lat": "30.3397809", "lng": "76.3868797", "tm_stmp": "1617952186837", } ]}
2: For a particular device, start saving the locations data.
Handle Heartbeats
//battery voltagefunction decodeBatteryOmniSmart(value){var base_volt = 320;var high_volt = 420;if (value < base_volt) { value = base_volt;}if (value < high_volt) { return Math.floor(((value - base_volt)/(high_volt - base_volt))*100);} else { return 100;}}
Scalability
Author
Robin Malhotra
Reviewer
Ajaypaul
Editor
Mridula Saravanan
We at CaratLane are solving some of the most intriguing challenges to make our mark in the relatively uncharted omnichannel jewellery industry. If you are interested in tackling such obstacles, feel free to drop your updated resume/CV to careers@caratlane.com!
Leave a Reply