Arduino sends temperature data via USB (Serial)
A Python script reads this data and exposes it as a Prometheus metric at http://localhost:8000/metrics
Prometheus scrapes the data every 5 seconds
Grafana visualizes the temperature data
Create a folder on your Desktop called:
Arduino_To_Grafana
This folder will contain:
Your Python script
The Prometheus config file
Eventually, Prometheus binaries
Open Arduino IDE, connect your Arduino Uno via USB, and upload the following sketch:
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
float value = random(200, 350) / 10.0; // Generate values from 20.0 to 35.0
Serial.println(value);
delay(1000); // 1 second
}
This test sketch simulates temperature data if you don't yet have a sensor.
Open Serial Monitor and ensure you're seeing values like:
28.00
27.10
33.50
Open PowerShell and run:
pip install prometheus_client pyserial
Open Visual Studio Code, create a new file and paste the following:
import serial
from prometheus_client import start_http_server, Gauge
import time
# Configure your serial port and baud rate
SERIAL_PORT = 'COM5' # Adjust this to match your port
BAUD_RATE = 9600
# Prometheus metric
temperature_gauge = Gauge('arduino_temperature_celsius', 'Temperature read from Arduino')
def read_serial():
with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) as ser:
while True:
try:
line = ser.readline().decode('utf-8').strip()
if line:
print(f"Received: {line}")
temperature = float(line)
temperature_gauge.set(temperature)
except Exception as e:
print(f"Error: {e}")
time.sleep(1)
if __name__ == "__main__":
start_http_server(8000) # Prometheus will scrape from here
print("Prometheus metrics available at http://localhost:8000/metrics")
read_serial()
Save it in Arduino_To_Grafana folder as:
serial_to_prometheus.py
Open PowerShell and run:
cd "C:\Users\<yourname>\Desktop\Arduino_To_Grafana"
python serial_to_prometheus.py
You should see output like:
Prometheus metrics available at http://localhost:8000/metrics
Received: 26.80
Received: 30.50
Create a file in the same folder named:
prometheus.yml
Paste this config:
global:
scrape_interval: 5s
scrape_configs:
- job_name: 'arduino_serial_reader'
static_configs:
- targets: ['localhost:8000']
Make sure you downloaded Prometheus for Windows and placed the prometheus.exe inside the same folder or use the full path.
In PowerShell:
cd "C:\Users\<yourname>\Desktop\Arduino_To_Grafana"
.\prometheus.exe --config.file="prometheus.yml"
Open your browser to:
http://localhost:9090/
Enter this query:
arduino_temperature_celsius
You should see the current temperature value!
Open Grafana:
http://localhost:3000/
Log in with default credentials (admin / admin) and change password.
Go to Connections > Data Sources > Add new
Select Prometheus
URL: http://localhost:9090
Click Save & Test
Go to Dashboards > + Create > Dashboard
Click Add new panel
Use this query:
arduino_temperature_celsius
Choose a visualization (e.g., Graph, Stat, Gauge)
Click Apply, then Save Dashboard