From 8f81512053a49234dbf64d7e3235530c4e70f5fd Mon Sep 17 00:00:00 2001 From: Raktbastr Date: Fri, 27 Mar 2026 11:41:24 -0500 Subject: [PATCH] Added barometric sensor and accelerometer/gyroscope. Improved lidar sensor. Improved index.html. --- main.py | 56 ++++++++++++++++++++++++++++++-------------- templates/index.html | 47 +++++++++++++++++++++++++++---------- 2 files changed, 74 insertions(+), 29 deletions(-) diff --git a/main.py b/main.py index 082a245..2e67dc1 100644 --- a/main.py +++ b/main.py @@ -1,22 +1,44 @@ # These two modules allow us to run a web server. +from contextlib import nullcontext from flask import Flask, render_template from flask_socketio import SocketIO from tfluna import TFLuna - -# This module lets us pick random numbers, you can remove it later. +from bmp180 import BMP180 +from mpu6050 import mpu6050 import random # Here, we create the neccesary base app. You don't need to worry about this. app = Flask(__name__) socketio = SocketIO(app) +# Sensor initialization +bmp = BMP180() +mpu = mpu6050(0x68) tfluna = TFLuna() tfluna.open() tfluna.set_samp_rate(10) -def printLidar(): - distance, strength, temperature = tfluna.read() - lidarValues = {distance, strength, temperature} - return str(lidarValues) + +# tfl Class to format the getters like the other sensors. +class tfl: + @staticmethod + def get_distance(): + return tfluna.read()[0] + @staticmethod + def get_strength(): + return tfluna.read()[1] + @staticmethod + def get_temperature(): + return tfluna.read()[2] + +# All the sensor data getters, for reference +# bmp.get_temperature() +# bmp.get_pressure() +# bmp.get_altitude() +# mpu.get_accel_data() +# mpu.get_gyro_data() +# tfl.get_distance() +# tfl.get_strength() +# tfl.get_temperature() # When someone requests the root page from our web server, we return 'index.html'. @app.route('/') @@ -26,21 +48,21 @@ def index(): # This function runs in the background to transmit data to connected clients. def background_thread(): while True: - # We sleep here for a single second, but this can be increased or decreased depending on how quickly you want data to be pushed to clients. - socketio.sleep(0.1) - # Then, we emit an event called "update_data" - but this can actually be whatever we want - with the data being a dictionary - # where 'randomNumber' is set to a random number we choose here. You should replace the data being sent back with your sensor data - # that you fetch from things connected to your Pi. + socketio.sleep(0.5) socketio.emit( 'update_data', { - 'randomNumber': random.randint(1, 100), - # you can add more here! for instance, something along the lines of: - # 'mySensor': mysensor.get_sensor_data(), - 'lidar': printLidar() + 'randomNumber': random.randint(1, 100), # Keeping this here to make sure the page is being updated + 'bmpTemp': bmp.get_temperature(), + 'bmpPressure': bmp.get_pressure(), + 'bmpAltitude': bmp.get_altitude(), + 'mpuAccel': mpu.get_accel_data(), + 'mpuGyro': mpu.get_gyro_data(), + 'tflDistance': tfl.get_distance(), + 'tflStrength': tfl.get_strength(), + 'tflTemp': tfl.get_temperature(), } ) - # To add a your first new sensor, try giving https://docs.aerospacejam.org/getting-started/first-sensor a read! # This function runs when someone connects to the server - and all we do is start the background thread to update the data. @socketio.on('connect') @@ -51,7 +73,7 @@ def handle_connect(): # This function is called def main(): # These specific arguments are required to make sure the webserver is hosted in a consistent spot, so don't change them unless you know what you're doing. - socketio.run(app, host='0.0.0.0', port=8085, allow_unsafe_werkzeug=True) + socketio.run(app, host='0.0.0.0', port=8080, allow_unsafe_werkzeug=True) if __name__ == '__main__': main() diff --git a/templates/index.html b/templates/index.html index 0ab2cc1..f283daa 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,15 +2,28 @@ - Aerospace Jam Example + Team 2B2T Drone -

Aerospace Jam Example

+

Team 2B2T Drone

-

Random number chosen by the Pi: Loading...

-

Lidar Values: Loading...

+

Random Number (lost connection if stopped): Loading...

+

+

Barometric Sensor Values

+

Temperature: Loading...

+

Pressure: Loading...

+

Altitude: Loading...

+

+

Accelerometer Sensor Values

+

Acceleration: Loading...

+

Gyroscope: Loading...

+

+

Lidar Sensor Values

+

Distance: Loading...

+

Strength: Loading...

+

Temperature: Loading...