Build a Robust Temperature & Sensor API on Raspberry Pi Using the GY‑91 Module
Learn how to create a reliable Flask API on a Raspberry Pi that reads temperature, pressure, humidity, and more from a GY‑91 multi‑sensor module. The guide walks through wiring, software setup, and sample code that you can adapt for your own projects.
Connect the Sensor via GPIO
The GY‑91 board uses a 4‑pin I²C interface. Connect the pins as follows:
- 3.3 V → Pin 1 (3V3POWER)
- GND → Pin 6 (GROUND)
- SCL → Pin 5 (I²C Clock)
- SDA → Pin 3 (I²C Data)
Refer to the GY‑91 schematic for exact pin colors and layout.
Prepare Python Dependencies
Install Python 3 on your Raspberry Pi. The following commands install the latest 3.6.x release from source, ensuring you have all required libraries for the sensor libraries that follow.
$ sudo apt-get update
$ sudo apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
$ wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
$ tar xf Python-3.6.5.tar.xz
$ cd Python-3.6.5
$ ./configure
$ make
$ sudo make altinstall
Next, install the lightweight web stack you’ll use to expose sensor data:
$ pip3 install flask flask-cors flask-restful
Sample Code
Clone the ready‑to‑run repository that contains a minimal Flask API skeleton and the sensor wrapper.
$ git clone https://gist.github.com/wdelenclos/4f0c4b8d564aa5c58f47653fd184eaa7
The core of the program pulls data from the bme280 driver, which in turn communicates with the MPU9250 and BMP280 chips on the GY‑91 board. A single line fetches all relevant readings:
temperature, pressure, humidity = bme280.readBME280All()
All other routes are standard Flask handlers that return the latest sensor values as JSON.
Run the API
Start the server locally:
$ python3 app.py
To keep the service running in the background, you can use:
$ sudo nohup python3 app.py &
Demo
After launching, visit https://your‑pi‑ip:5000/status to see a live JSON payload of sensor readings. The example on the original page shows how to fetch this endpoint with Ajax and display it on a web dashboard.
Feel free to ask for additional explanations or customizations in the comments.
Source: Environmental sensor API with a RPi
Manufacturing process
- Line Tracking Sensor with Raspberry Pi – Simple KY‑033 Door/Line Detector
- Installing a Raspberry Pi Camera in a Birdhouse – Step‑by‑Step Guide
- Integrating a Thermocouple Sensor with the Arduino Portenta H7 Using the MAX6675 IC
- K30 CO2 Sensor: Real‑Time Indoor Air Quality Monitoring
- Integrating a DFRobot Capacitive Fingerprint Sensor with Arduino or ESP8266
- Smart 3D Printed Pet Feeder Powered by Arduino Nano
- Master Vibration Detection with Arduino: A Simple Sensor & LED Setup
- MKR1000 Multi‑Mode Environmental Sensor Deck – Lightning, Gas, UV, Temp, Humidity & Pressure
- Build an IR Sensor Project with Arduino UNO – Simple Guide
- PIR Motion Sensor: Working Principles & Arduino Integration Guide