Skip to content

Commit

Permalink
sensorserver based scripts - to mqtt and to velocities and position
Browse files Browse the repository at this point in the history
  • Loading branch information
JAlcocerT committed Mar 3, 2024
1 parent 330bc57 commit 48fd956
Show file tree
Hide file tree
Showing 14 changed files with 960 additions and 506 deletions.
96 changes: 96 additions & 0 deletions Z_IoT/SensorServer/SensorServer-Acel2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
##python3 SensorServer-Acel2.py


import asyncio
import websockets
import json
import plotly.graph_objects as go
from datetime import datetime


async def receive_sensor_data(uri, timeout=5):
async with websockets.connect(uri) as websocket:
try:
await asyncio.wait_for(websocket_handler(websocket), timeout)
except asyncio.TimeoutError:
print("Timeout reached, stopping data reception.")

import asyncio
import websockets
import json
import plotly.graph_objects as go
from datetime import datetime

async def websocket_handler(websocket):
acceleration_data = []
velocity_data = []
distance_data = []
initial_velocity = 0 # Starting with an initial velocity of 0 m/s
last_local_timestamp = None # To store the local timestamp of the previous reading
total_distance = 0 # Initialize total distance traveled

while True:
message = await websocket.recv()
data = json.loads(message)

# Use local timestamp
local_timestamp = datetime.now().timestamp() # Get current local timestamp in seconds

acceleration = data['values'][2] # Assuming Z-axis acceleration in m/s^2

if last_local_timestamp is not None:
# Calculate time interval in seconds
delta_t = local_timestamp - last_local_timestamp
# Calculate velocity in m/s using the time interval
velocity = initial_velocity + acceleration * delta_t
# Calculate distance traveled during delta_t assuming constant velocity over the interval
distance = initial_velocity * delta_t + 0.5 * acceleration * delta_t**2
total_distance += distance
else:
velocity = initial_velocity
distance = 0 # No movement for the first data point

# Store data for plotting
acceleration_data.append(acceleration)
velocity_data.append(velocity)
distance_data.append(total_distance)

# Update for next iteration
initial_velocity = velocity # Update initial velocity
last_local_timestamp = local_timestamp # Update the local timestamp

# Plotting function call (assuming it's called less frequently in practice)
plot_data(acceleration_data, velocity_data, distance_data)

def plot_data(acceleration_data, velocity_data, distance_data):
fig = go.Figure()
# Adding acceleration data to the plot
fig.add_trace(go.Scatter(y=acceleration_data, mode='lines+markers', name='Acceleration (m/s^2)'))
# Adding velocity data to the plot
fig.add_trace(go.Scatter(y=velocity_data, mode='lines+markers', name='Velocity (m/s)'))
# Adding distance data to the plot
fig.add_trace(go.Scatter(y=distance_data, mode='lines+markers', name='Distance (m)'))
fig.update_layout(
title='Sensor Data over Time',
xaxis_title='Time (s)',
yaxis_title='Values',
legend_title="Data Type"
)
fig.write_html("sensor_data_plot.html")

def print_averages(acceleration_data, velocity_data, distance_data):
avg_acceleration = np.mean(acceleration_data) if acceleration_data else 0
avg_velocity = np.mean(velocity_data) if velocity_data else 0
avg_distance = np.mean(distance_data) if distance_data else 0
print(f"Average Acceleration: {avg_acceleration} m/s^2")
print(f"Average Velocity: {avg_velocity} m/s")
print(f"Average Distance: {avg_distance} m")

# Assuming the rest of your code remains the same and you have defined the main and other necessary functions.

async def main():
#uri = "ws://192.168.3.201:8080/sensor/connect?type=android.sensor.gravity"
uri = "ws://192.168.3.201:8080/sensor/connect?type=android.sensor.linear_acceleration"
await receive_sensor_data(uri, timeout=5)

asyncio.run(main())
92 changes: 92 additions & 0 deletions Z_IoT/SensorServer/SensorServer-Acel2Veloc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#python3 SensorServer-Acel2Veloc.py



import asyncio
import websockets
import json
import plotly.graph_objects as go

async def receive_sensor_data(uri, timeout=5):
async with websockets.connect(uri) as websocket:
try:
await asyncio.wait_for(websocket_handler(websocket), timeout)
except asyncio.TimeoutError:
print("Timeout reached, stopping data reception.")

async def websocket_handler(websocket):
acceleration_data = []
velocity_data = []
initial_velocity = 0 # Initial velocity
while True:
message = await websocket.recv()
data = json.loads(message)
acceleration = data['values'][2] # Assuming the third value is acceleration in m/s^2
acceleration_data.append(acceleration)
# Calculate velocity in m/s
velocity = initial_velocity + acceleration
velocity_data.append(velocity)
initial_velocity = velocity # Update initial velocity for next iteration
# Plot acceleration and velocity
plot_data(acceleration_data, velocity_data)

def plot_data(acceleration_data, velocity_data):
fig = go.Figure()
fig.add_trace(go.Scatter(y=acceleration_data, mode='lines', name='Acceleration', yaxis='y1'))
fig.add_trace(go.Scatter(y=velocity_data, mode='lines', name='Velocity', yaxis='y2'))
fig.update_layout(
title='Acceleration and Velocity vs Time',
xaxis_title='Time',
yaxis_title='Acceleration (m/s^2)', # Units for acceleration
yaxis2=dict(title='Velocity (m/s)', overlaying='y', side='right') # Secondary Y-axis for velocity
)
fig.write_html("acceleration_velocity_plot.html")

async def main():
#uri = "ws://192.168.3.201:8080/sensor/connect?type=android.sensor.gravity"
uri = "ws://192.168.3.201:8080/sensor/connect?type=android.sensor.linear_acceleration"
await receive_sensor_data(uri, timeout=5)

asyncio.run(main())


# import asyncio
# import websockets
# import json
# import plotly.graph_objects as go

# async def receive_sensor_data(uri, timeout=5):
# async with websockets.connect(uri) as websocket:
# try:
# await asyncio.wait_for(websocket_handler(websocket), timeout)
# except asyncio.TimeoutError:
# print("Timeout reached, stopping data reception.")

# async def websocket_handler(websocket):
# acceleration_data = []
# velocity_data = []
# initial_velocity = 0 # Initial velocity
# while True:
# message = await websocket.recv()
# data = json.loads(message)
# acceleration = data['values'][2] # Assuming the third value is acceleration
# acceleration_data.append(acceleration)
# # Calculate velocity
# velocity = initial_velocity + acceleration
# velocity_data.append(velocity)
# initial_velocity = velocity # Update initial velocity for next iteration
# # Plot acceleration and velocity
# plot_data(acceleration_data, velocity_data)

# def plot_data(acceleration_data, velocity_data):
# fig = go.Figure()
# fig.add_trace(go.Scatter(y=acceleration_data, mode='lines', name='Acceleration'))
# fig.add_trace(go.Scatter(y=velocity_data, mode='lines', name='Velocity'))
# fig.update_layout(title='Acceleration and Velocity vs Time', xaxis_title='Time', yaxis_title='Value')
# fig.write_html("acceleration_velocity_plot.html")

# async def main():
# uri = "ws://192.168.3.201:8080/sensor/connect?type=android.sensor.linear_acceleration"
# await receive_sensor_data(uri, timeout=5)

# asyncio.run(main())
64 changes: 64 additions & 0 deletions Z_IoT/SensorServer/SensorServer-Acel2Veloc2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#python3 SensorServer-Acel2Veloc2.py


import asyncio
import websockets
import json
import plotly.graph_objects as go

async def receive_sensor_data(uri, timeout=5):
async with websockets.connect(uri) as websocket:
try:
await asyncio.wait_for(websocket_handler(websocket), timeout)
except asyncio.TimeoutError:
print("Timeout reached, stopping data reception.")

async def websocket_handler(websocket):
acceleration_data = []
velocity_data = []
initial_velocity = 0 # Initial velocity
last_timestamp = None # Initialize last timestamp

while True:
message = await websocket.recv()
data = json.loads(message)

acceleration = data['values'][2] # Assuming the third value is acceleration in m/s^2
timestamp = data['timestamp'] # Timestamp of the current reading

if last_timestamp is not None:
# Calculate time interval in seconds (assuming timestamp is in microseconds)
delta_t = (timestamp - last_timestamp) / 1_000_000
# Calculate velocity in m/s using the time interval
velocity = initial_velocity + acceleration * delta_t
else:
# No previous timestamp, so can't calculate a meaningful velocity
velocity = initial_velocity

acceleration_data.append(acceleration)
velocity_data.append(velocity)

initial_velocity = velocity # Update initial velocity for next iteration
last_timestamp = timestamp # Update last timestamp

# Plot acceleration and velocity
plot_data(acceleration_data, velocity_data)

def plot_data(acceleration_data, velocity_data):
fig = go.Figure()
fig.add_trace(go.Scatter(y=acceleration_data, mode='lines', name='Acceleration', yaxis='y1'))
fig.add_trace(go.Scatter(y=velocity_data, mode='lines', name='Velocity', yaxis='y2'))
fig.update_layout(
title='Acceleration and Velocity vs Time',
xaxis_title='Time',
yaxis_title='Acceleration (m/s^2)',
yaxis2=dict(title='Velocity (m/s)', overlaying='y', side='right')
)
fig.write_html("acceleration_velocity_plot.html")

async def main():
#uri = "ws://192.168.3.201:8080/sensor/connect?type=android.sensor.gravity"
uri = "ws://192.168.3.201:8080/sensor/connect?type=android.sensor.linear_acceleration"
await receive_sensor_data(uri, timeout=5)

asyncio.run(main())
73 changes: 73 additions & 0 deletions Z_IoT/SensorServer/SensorServer-Acel2Veloc3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
##python3 SensorServer-Acel2Veloc3.py


import asyncio
import websockets
import json
import plotly.graph_objects as go
from datetime import datetime


async def receive_sensor_data(uri, timeout=5):
async with websockets.connect(uri) as websocket:
try:
await asyncio.wait_for(websocket_handler(websocket), timeout)
except asyncio.TimeoutError:
print("Timeout reached, stopping data reception.")

async def websocket_handler(websocket):
acceleration_data = []
velocity_data = []
initial_velocity = 0 # Starting with an initial velocity of 0 m/s
last_local_timestamp = None # To store the local timestamp of the previous reading

while True:
message = await websocket.recv()
data = json.loads(message)

# Use local timestamp
local_timestamp = datetime.now().timestamp() # Get current local timestamp in seconds

acceleration = data['values'][2] # Assuming Z-axis acceleration in m/s^2

if last_local_timestamp is not None:
# Calculate time interval in seconds
delta_t = local_timestamp - last_local_timestamp
# Calculate velocity in m/s using the time interval
velocity = initial_velocity + acceleration * delta_t
else:
# For the first reading, we can't calculate delta_t or change in velocity
velocity = initial_velocity

# Store acceleration and velocity for plotting
acceleration_data.append(acceleration)
velocity_data.append(velocity)

# Update for next iteration
initial_velocity = velocity # Update initial velocity
last_local_timestamp = local_timestamp # Update the local timestamp

# Plotting function call (assuming it's called less frequently in practice)
plot_data(acceleration_data, velocity_data)


def plot_data(acceleration_data, velocity_data):
fig = go.Figure()
fig.add_trace(go.Scatter(y=acceleration_data, mode='lines', name='Acceleration', yaxis='y1'))
fig.add_trace(go.Scatter(y=velocity_data, mode='lines', name='Velocity', yaxis='y2'))
fig.update_layout(
title='Acceleration and Velocity vs Time',
xaxis_title='Time',
yaxis_title='Acceleration (m/s^2)',
yaxis2=dict(title='Velocity (m/s)', overlaying='y', side='right')
)
fig.write_html("acceleration_velocity_plot.html")



async def main():
#uri = "ws://192.168.3.201:8080/sensor/connect?type=android.sensor.gravity"
uri = "ws://192.168.3.201:8080/sensor/connect?type=android.sensor.linear_acceleration"
await receive_sensor_data(uri, timeout=5)

asyncio.run(main())
Loading

0 comments on commit 48fd956

Please sign in to comment.