In [1]:
# Imports
import pandas as pd
import numpy as np
In [2]:
# Define functiont to connect to the DB
from pymongo import MongoClient
def _connect_mongo(host, port, db):
mongo_str = 'mongodb://%s:%s/%s' % (host, port, db)
client = MongoClient(mongo_str)
return client[db]
def read_mongo(db, collection, query={}, host = 'localhost', port = 27017, no_id = True):
"""Read from MongoDB and store into DataFrame"""
# Connect to MongoDB
db = _connect_mongo(host = host, port = port, db = db)
# Query to specific DB and collection
collection = db[collection].find(query)
# Create DataFrame
global df
df = pd.DataFrame(list(collection))
# Delete the _id
if no_id:
del df['_id']
In [3]:
# Connect to the DB
read_mongo('workshopdb','zapis2',{}, '192.168.84.17')
In [4]:
df.head(10)
Out[4]:
In [5]:
# Function that helps calculate distance rather than rssi
def rssi_to_distance(rssi):
### Declare local variable TxPower
# This is value of rssi @ 1m
TxPower = -65
ratio = rssi * 1.0 / TxPower
# If rssi was 0
if (rssi == 0):
return -1
if (ratio < 1.0):
return ratio**10
else:
dist = (0.89976) * (ratio ** 7.7095) + 0.111
return dist
In [6]:
# Add column that will store distance from the GTW
df["dist"] = df.apply(lambda row: rssi_to_distance(row['rssi']), axis=1)
df.head(20)
Out[6]:
In [12]:
df.to_csv(obejscie_biura_data, sep='\t', encoding='utf-8')
#Calculate the max delay between MQTT broker and the real time
max(df['real_timestamp'] - df['timestamp'])
In [8]:
import matplotlib.pyplot as plt
In [9]:
# In order to plot we need to replace the sourceId with different unique number
def replace_sourceId(sourceID):
if (sourceID == '8JRGb'):
return 0
if sourceID == '9McaT':
return 1
if sourceID == 'H3vx9':
return 2
if sourceID == 'HNenF':
return 3
if sourceID == 'QQhDc':
return 4
if sourceID == 'ZN6Xd':
return 5
if sourceID == 'rNt0R':
return 6
In [11]:
list_sourceIds = ['8JRGb', '9McaT', 'H3vx9', 'HNenF', 'QQhDc','ZN6Xd', 'rNt0R']
# Apply numbers to the DF
df['sourceId_number'] = df['sourceId'].apply(lambda sid: replace_sourceId(sid))
area = 10
plt.xlabel('Time [s]')
plt.ylabel('Source Id')
plt.yticks(df.index, list_sourceIds)
plt.scatter(df['timestamp'], df['sourceId_number'], s=area, c=df['rssi'], alpha=0.8)
plt.colorbar()
plt.show()
In [ ]: