To create a simple web server on a Raspberry Pi that displays readings from a BME680 sensor, you’ll need to set up a few things. Here’s a guide on how to do it:
- Install Required Libraries:
- Ensure that you have
Flask
(a micro web framework) installed to create a simple web server. - Install
Adafruit_BME680
to interact with the BME680 sensor. - Run the following commands to install the required libraries:
- Ensure that you have
sudo apt-get update
sudo apt-get install python3-pip
pip3 install flask
pip3 install adafruit-circuitpython-bme680
Wire the BME680 Sensor to the Raspberry Pi:
- Connect the BME680 sensor to the Raspberry Pi via I2C.
- Enable I2C on the Raspberry Pi using
raspi-config
. - Make the following connections: SDA to SDA, SCL to SCL, VCC to 3.3V, GND to GND.
Create a Python Script for the Web Server:
- I am using Flask to create a simple web server that displays sensor readings.
- Using Adafruit’s library to fetch readings from the BME680 sensor.
from flask import Flask, render_template_string
import adafruit_bme680
import board
import busio
# Create Flask app
app = Flask(__name__)
# Set up I2C bus and BME680 sensor
i2c = busio.I2C(board.SCL, board.SDA)
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c)
# Set the temperature offset (optional, adjust if needed)
bme680.sea_level_pressure = 1013.25 # Standard sea level pressure in hPa
# Flask route for the home page
@app.route('/')
def index():
# Get sensor readings
temperature = bme680.temperature # Celsius
pressure = bme680.pressure # hPa
humidity = bme680.humidity # %
gas_resistance = bme680.gas # Ohms
# Display the readings on a simple web page
html_content = f"""
<!DOCTYPE html>
<html>
<head>
<title>BME680 Sensor Readings</title>
</head>
<body>
<h1>BME680 Sensor Readings</h1>
<p>Temperature: {temperature:.2f} °C</p>
<p>Humidity: {humidity:.2f} %</p>
<p>Pressure: {pressure:.2f} hPa</p>
<p>Gas Resistance: {gas_resistance:.2f} Ohms</p>
</body>
</html>
"""
return render_template_string(html_content)
# Run the Flask app
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
Save the script to a file, e.g., webserver.py
.
Run the script with Python:
python3 webserver.py
You should see a message indicating that the server is running and available on your Raspberry Pi’s IP address on port 5000.
Accessing the Web Server:
Open a web browser on your Raspberry Pi or any other device on the same network. Navigate to http://<your_raspberry_pi_ip>:5000/. You should see the sensor readings displayed on the web page.
Port 5000 is often used as a default port for running development servers in frameworks like Flask. It is not reserved for any specific service by default, and it’s commonly used in tutorial and development environments.
Here’s why I picked port 5000:
- Common Flask Default:
- Flask’s default behavior when running a development server is to use port 5000. Many examples and tutorials use this port, so it’s recognizable.
- Unassigned by IANA:
- The Internet Assigned Numbers Authority (IANA) maintains a list of officially assigned ports. Port 5000 is not specifically reserved for any known services, reducing the risk of conflicts with other common applications or system services.
- Convenience:
- It is a high-numbered port, less likely to require administrative privileges to bind to it (as might be required for lower-numbered ports).
- Flexibility:
- You can change the port if needed by modifying the code or using an environment variable. This makes it easy to adapt to different environments or to avoid conflicts.
Despite these reasons, you can use other ports if port 5000 doesn’t suit your environment. If there’s already a service running on port 5000, or if you have specific security or network constraints, you can select a different port. Just make sure it doesn’t conflict with well-known or commonly used ports. To change the port in Flask, you can specify a different port when starting the server or change the app.run
line in the script:
# Example: Changing to port 8000
app.run(host='0.0.0.0', port=8000)
However, if you prefer to run it on port 80, that is possible too