IoT application using LoRaWAN Network Server
Tags: iot, python, lorawan, vue
Published: 2022-04-07 16:45:52

This story is about how my team and I developed an IoT application for parking that received data from IoT sensors, processed data, and displayed it. Also, in the solution, we have many features connected with booking parking slots, etc. 
Of course, since the time this application was created and is running (2019), other competitive solutions have significantly developed. The solution that uses computer vision or cheap sensors for underground parking is more effective, more inexpensive, or easy to deploy in some cases. The experience described below can also help create IoT applications to interact with various other sensors that work using sensors of movement, door opening, light, temperature and humidity, water meter, kilowatt-hour meter, etc.

Сomponents used in this solution:

- Smart Parking devices (Libelium Smart Parking, TBS-220 geomagnetic vehicle detector)
- LoRa Gateway (Cisco Wireless Gateway for LoRaWAN)
- LoRaWAN Network Server (Actility or Open source [LoRaWAN server](https://github.com/gotthardp/lorawan-server))
- IoT app can be [deployed on IoT](https://developer.cisco.com/docs/iox/) compute gateway


![IoT scheme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4mizqt4t23rk2uua0x0.png)

**Why LoRa?**
1. Extended range of one radio gateway - up to 15 km radius outside the city area (up to 10 in the city).
2. Unlicensed frequency band - no use permit is required. 
3. Scalability - up to 20,000 end sensors per 1 gateway. 
4. Low cost of both end devices and radio gateways. 
5. Due to its low power consumption, the sensor runs on an average of 4-5 years of battery life, allowing it to be placed anywhere without wiring and power. 
6. Data Security - All data is encrypted by AES-128 by default; each sensor has its unique encryption keys.

In general, security aspects are also problems in this area. For example, there were a lot of cases when a home IoT system, based on an open source, was hacked, and the attacker was in control of turning on the light and getting control of other devices (blinds, electrical appliances, etc.) And also getting access to IP cameras.


![different technologies for IoT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pwc7swizhrgk9dhxcql7.png)
Table comparing different technologies for IoT

Are Sigfox and LoRa still competitors? Unfortunately, yes. But people aren’t as focused on network technology these days — they’re focused on use cases and applications. As a result, these technologies have been relegated to their proper place — they’re tools, nothing more.

Each LoRa device sensor has unique parameters for recognizing in LoRa network and encrypting the data payload:

- Device EUI
- Device Address
- Network session key
- Application session key

![IoT device interface](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hx2v7i6d222872s7vflj.png)
Many IoT device manufacturers have apps and interfaces for configuring devices and firmware.

Some other settings:

- LoRaWAN join mode, ABP or OTAA
- LoRaWAN port
- LoRaWAN application key
- LoRaWAN application EUI
- LoRaWAN EUI that will be loaded from the device.

Network Server interface, device configuration
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cndpke70uuve2ka06lzr.png)


## Interaction with the sensors

For interaction, we use Uplink and Downlink. Uplink is what we receive from a sensor or IoT device, and Downlink is the data we can send to IoT devices.
We can have 12 bytes payload in the uplink, more precisely 96 bits. 


![uplink](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gb3da5zcrvacde2tpub2.png)

What information can we extract from this?
ID of the device, Battery level, Status of the parking slot, Temperature (Celsius degrees) from the node’s internal sensor.

For example 
Payload: `0400003f34b909173f600fc2`

![payload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwscyq7kvje903385v7y.png)

Python function that we use for extract the data

```
    def parsing_payload_hex(data, payload_key="{http://uri.actility.com/lora}payload_hex"):
        try:
            _hex = data[payload_key]
        except IndexError:
            raise IndexError('No frame in payloads')
        else:
            byte_binary = "{0:08b}".format(int(_hex[:2], 16))

            parking_slot = int(byte_binary[:1])     ## Occupation status    0 - free; 1 - occupied;
            battery = int(byte_binary[1])           ## Charge level         0 - Good; 1 - little;
            frame_type = int(byte_binary[4:8], 10)  ## Frame type           0 - Info; 1 - Keep-alive; 2 - daily update; ...

            return parking_slot, battery, frame_type, int(_hex[2:4], 16)
            ##       parking_slot|battery|frame_type|frame_counter
```

## Application
Technology stack: Vue.js, Python, C

**Basic capabilities:**
The Application with GUI, where admin can add parkings, configure parking slots, assign parking slots to certain IoT devices. Essential features for users like displaying which parking slot is occupied/free. Review which slots are booked/available on a specific date and time. Send links for external users to book places for themselves.


![App GUI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4vac4tchrqvcvdaiysc.png)

**Extra features:**
Automatic cancellation of the booked place if the place was not being occupied within 15 minutes of the scheduled time.
Ability to define a fixed parking space as available for booking during a specific period (for example, in case of vacation or business trip)
Display users booking statistics
Integration with 3d party messaging service


![App menu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dsu0680acpr4n9rh627f.png)

Also, the admin can see statistics. For example, is the slot occupied during the booking period and the number of cancellations.

We use this solution for corporate parking, where we have slots for guests and need to book it for guest customers or partners. Through API app can also send the card number to the 3d party system.


![Booking](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aexzjdaclhoxzp0mf5ag.png)

You can test your app using this [sandbox](https://devnetsandbox.cisco.com/RM/Diagram/Index/5f411c0e-b387-4ae6-9ee7-ff793d925cff?diagramType=Topology)