Skip to content

Commit

Permalink
plotting real time android sensor data
Browse files Browse the repository at this point in the history
  • Loading branch information
imvickykumar999 committed Nov 23, 2023
1 parent b13fcb1 commit 0bc757e
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ADB GUI/ScrCpy_GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Press `CTRL + PAUSE/BREAK` keys to exit.
Press ENTER for default IP
192.168.0.103
192.168.0.102
----------------------------------------
Paste IP Address of Device : ''')
Expand Down
11 changes: 11 additions & 0 deletions Real_Time_Sensor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
![rot_vector](https://github.com/imvickykumar999/Android-Sensors/assets/50515418/2fa50fe6-1e35-4009-8e3d-9eec602b3483)
![proximity](https://github.com/imvickykumar999/Android-Sensors/assets/50515418/5bf043ce-f434-4185-8c3e-e07c9af412ca)
![mag](https://github.com/imvickykumar999/Android-Sensors/assets/50515418/6ae3c745-af0e-46e2-900f-22d74d83ab63)
![lin_accel](https://github.com/imvickykumar999/Android-Sensors/assets/50515418/ddd9e4d9-e248-4244-9051-7155730b33fb)
![gyro](https://github.com/imvickykumar999/Android-Sensors/assets/50515418/e37a20c3-2769-4984-a1f3-67aa945eebcc)
![gravity](https://github.com/imvickykumar999/Android-Sensors/assets/50515418/086d2d37-b036-4f32-a536-9b8d7642551e)
![battery_voltage](https://github.com/imvickykumar999/Android-Sensors/assets/50515418/c92ace71-b316-48c8-a0b4-3081b97a5e61)
![battery_temp](https://github.com/imvickykumar999/Android-Sensors/assets/50515418/ec2027fe-851e-471f-9aeb-b11d6d86f131)
![battery_level](https://github.com/imvickykumar999/Android-Sensors/assets/50515418/9e81f2ac-911c-4ae0-b43b-9d43fe97087b)
![battery_charging](https://github.com/imvickykumar999/Android-Sensors/assets/50515418/c165525c-ea1a-46f1-9502-eff350ee79c5)
![accel](https://github.com/imvickykumar999/Android-Sensors/assets/50515418/cfb50d2f-759f-4c7b-8268-a78a201a4d31)
119 changes: 119 additions & 0 deletions Real_Time_Sensor/live_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@

# Download IP WebCam and Enable Data Logging.
# https://play.google.com/store/apps/details?id=com.pas.webcam&hl=en&gl=US

# On starting Server after scrolling all the way to bottom,
# Data will be visible on http://{ip}:{port}/sensors.json

import matplotlib.animation as animation
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib import style
import requests, json

def convert(ts):
ts /= 1000
unix = datetime.utcfromtimestamp(ts)
mili = unix.strftime('%Y-%m-%d %H:%M:%S')
return mili

username = 'imvickykumar999'
password = 'imvickykumar999'

ip,port = '192.168.0.102', 8080
link = f"http://{ip}:{port}/sensors.json"

style.use('fivethirtyeight')
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

print()
sensor = {}
filename = 'sensor.json'

try:
r = requests.get(link, auth=(username, password))
data = r.json()

with open(filename, 'w') as outfile:
json.dump(data, outfile)
except:
with open(filename, 'r') as f:
data = json.load(f)

for i, j in enumerate(data.keys()):
print(i, j)
sensor.update({i : j})

inp = input('\nEnter sensor number : ')
if inp == '':
inp = 0
else:
inp = int(inp)

def animate(k):
try:
r = requests.get(link, auth=(username, password))
data = r.json()

xs, ys = [], []
keys = data[sensor[inp]]

for i in keys['data']:
print(
format(i[1][0], ".5f"),
keys['unit'],
'\t',
convert(int(i[0]))
)
xs.append(float(i[0]))
ys.append(float(i[1][0]))

ax1.clear()
ax1.plot(xs, ys)
ax1.set_ylim(-50, 50)

plt.title(' '.join(sensor[inp].title().split('_')))
plt.savefig(f"static/{sensor[inp]}.png")
return fig

except Exception as e:
print('\nPress CTRL+PAUSE/BREAK to exit.\n', e)

plt.savefig(f"static/{sensor[inp]}.png")
exit()

print(f'Graph of {sensor[inp]}')
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()


rf'''
>>> python live_plot.py
0 accel
1 battery_charging
2 mag
3 gyro
4 proximity
5 gravity
6 lin_accel
7 rot_vector
8 battery_voltage
9 battery_level
10 battery_temp
Enter sensor number : 0
Graph of accel
.
.
.
-0.7111 m/s² 2023-11-23 07:46:25
-1.3647 m/s² 2023-11-23 07:46:25
-1.1636 m/s² 2023-11-23 07:46:25
-0.9625 m/s² 2023-11-23 07:46:25
'accel'
QObject::~QObject: Timers cannot be stopped from another thread
^C
'''
1 change: 1 addition & 0 deletions Real_Time_Sensor/sensor.json

Large diffs are not rendered by default.

Binary file added Real_Time_Sensor/static/Enable Data Logging.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Real_Time_Sensor/static/accel.mp4
Binary file not shown.
Binary file added Real_Time_Sensor/static/accel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Real_Time_Sensor/static/battery_charging.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Real_Time_Sensor/static/battery_level.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Real_Time_Sensor/static/battery_temp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Real_Time_Sensor/static/battery_voltage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Real_Time_Sensor/static/gravity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Real_Time_Sensor/static/gyro.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Real_Time_Sensor/static/lin_accel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Real_Time_Sensor/static/mag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Real_Time_Sensor/static/proximity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Real_Time_Sensor/static/rot_vector.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0bc757e

Please sign in to comment.