This script is a simple CLI tool developed in Go that fetches data from a CO2 sensor server (like https://co2.leyrer.io) and displays it in the console. It uses the Charm-Bracelet library for the user interface and a loading spinner.
The script does the following:
-
Loads environment variables from a
.env
file, including a required API key (X_API_KEY
) and the API URL (API_URL
) if present. -
Starts an infinite loop that periodically (every 100 milliseconds) sends an event to a channel to signal script activity.
-
Periodically (every 60 seconds), checks the CO2 sensor server by sending an HTTP GET request and verifies the server response status.
-
Updates the user interface with the CO2 data and temperature information retrieved from the server or displays an error message if an error occurs.
-
Allows the user to exit the program by pressing any key.
To run this script:
-
Ensure you have a
.env
file in your working directory containing theAPI_URL
andX_API_KEY
required for authentication with the CO2 sensor server. -
Execute the script by compiling and running the
main.go
file or by running it directly if you have Go installed on your system. -
The program displays the current CO2 data, temperature, and the timestamp of the last measurement. If an error occurs, it will be displayed.
-
Press any key to exit the program.
The script uses external Go modules that can be downloaded using go get
:
- Charm-Bracelet for the user interface:
github.com/charmbracelet/bubbletea
- Loading spinner:
github.com/charmbracelet/bubbles/spinner
- Lipgloss for text formatting:
github.com/charmbracelet/lipgloss
- Godotenv for loading environment variables from a
.env
file:github.com/joho/godotenv
Ensure that these dependencies are properly installed before running the script.
If you encounter issues or receive error messages when running the script:
- Ensure you have a
.env
file with a validAPI_URL
andX_API_KEY
in your working directory. - Verify that the mentioned dependencies are correctly installed.
- Make sure your internet connection is active, and the CO2 sensor server is reachable.