npm install @influxdata/influxdb-client
Feature Requests
- Allow custom x-axis, to plot sensor value against another value
- [completed] Overlay of graphs (Influxdb graph viewer supports natively)
- Pull units from InfluxDB and display on the graph
- clone https://github.com/Western-Formula-Racing/car_to_influx
- The relevant Python script here is readCAN3batchSender.py
- Make sure to set the correct TOKEN in the script
- Install Docker
- in cmd:
sudo docker run -d \
--name influxwfr \
-p 8086:8086 \
-v ~/influxdb/data:/var/lib/influxdb2 \
-v ~/influxdb/config:/etc/influxdb2 \
-e DOCKER_INFLUXDB_INIT_MODE=setup \
-e DOCKER_INFLUXDB_INIT_USERNAME=myuser \
-e DOCKER_INFLUXDB_INIT_PASSWORD=mypassword123 \
-e DOCKER_INFLUXDB_INIT_ORG=WFR \
-e DOCKER_INFLUXDB_INIT_BUCKET=ourCar \
influxdb:2
In the address bar at the top, type: http://localhost:8086
You should see a login screen
Log in using:
- Username: myuser
- Password: mypassword123
Then:
- Find API Key
- Create an organization called WFR
- click your profile icon, then click Create Organization
- Create a new bucket in Influx: call it "ourCar" <- you can change this in the python script, just make sure it matches.
- Consider changing the data retention policy to 1 hour to keep it clean for every testing session
Now, start running the Python code and use the graph viewer to view the testing data:
If you want something that's constantly moving:
ourCar-canBus-sensorReading-M166_Current_info-166-INV_Phase_A_Current
-
Query Execution (executeQuery function):
- Takes a Flux query as input
- Makes a POST request to InfluxDB's API endpoint with:
- URL:
${influxConfig.url}/api/v2/query?org=${influxConfig.org} - Authentication: Token-based via headers
- Request format: Flux query language (
application/vnd.flux) - Response format: CSV (
application/csv)
- URL:
- Checks for successful response (response.ok)
- Converts response to text (CSV format)
- Passes CSV to parseInfluxResponse
-
CSV Parsing (parseInfluxResponse function):
Input Validation:
if (!csvData || csvData.trim() === '') { return []; }
- Checks if data exists and isn't empty
Data Structure Analysis:
const lines = csvData.trim().split('\n'); if (lines.length < 2) { return []; }
- Splits CSV into lines
- Ensures there's at least a header and one data row
Header Processing:
const headers = lines[0].split(','); const timeIndex = headers.findIndex(h => h === '_time'); const valueIndex = headers.findIndex(h => h === '_value');
- Extracts column headers
- Locates critical columns: '_time' and '_value'
Data Transformation Pipeline:
return lines.slice(1) .filter(line => line.trim() !== '') .map(line => { const values = line.split(','); return { _time: values[timeIndex], _value: parseFloat(values[valueIndex]) }; }) .filter(point => !isNaN(point._value));
slice(1): Skips header row- First
filter: Removes empty lines map: Transforms each line into an object with _time and _value- Second
filter: Removes entries with invalid numerical values
The final output is an array of objects, each containing:
_time: Timestamp from InfluxDB_value: Numerical value (sensor reading)
Example transformation:
Input CSV:
_time,_value,_field,_measurement
2024-02-09T12:00:00Z,23.5,temperature,sensors
2024-02-09T12:00:01Z,24.0,temperature,sensors
Output:
[
{ _time: "2024-02-09T12:00:00Z", _value: 23.5 },
{ _time: "2024-02-09T12:00:01Z", _value: 24.0 }
]
Future Development: On the live monitor dashboard, the x-axis of the plot should be -60s to 0s (current), instead of absolute time.
sudo apt update
sudo apt upgrade -y
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce
sudo docker run hello-world
- Export local docker: docker tag influxdb:latest myusername/influxdb:latest
docker push myusername/influxdb:latest
๐ Step 1: Pull the Latest InfluxDB Image
sudo docker pull influxdb:2
๐ Step 2: Create Directories for Persistent Storage
Create directories on your Lightsail instance to store InfluxDB data and configurations:
mkdir -p ~/influxdb/data
mkdir -p ~/influxdb/config
Give Docker permission to access them:
sudo chown -R 1000:1000 ~/influxdb
๐ Step 3: Run the InfluxDB Container with the Correct Settings
Now, run the container using your updated configuration:
sudo docker run -d \
--name influxwfr \
-p 8086:8086 \
-v ~/influxdb/data:/var/lib/influxdb2 \
-v ~/influxdb/config:/etc/influxdb2 \
-e DOCKER_INFLUXDB_INIT_MODE=setup \
-e DOCKER_INFLUXDB_INIT_USERNAME=myuser \
-e DOCKER_INFLUXDB_INIT_PASSWORD=mypassword123 \
-e DOCKER_INFLUXDB_INIT_ORG=WFR \
-e DOCKER_INFLUXDB_INIT_BUCKET=ourCar \
influxdb:2
๐ Step 4: Verify the Container is Running
Check the status:
sudo docker ps -a
โ โข If STATUS is Up, the container is running. โ
โ โข If it exits, check logs:
sudo docker logs influxwfr
๐ Step 5: Test the Connection
1๏ธโฃ Check if InfluxDB is listening inside the container:
sudo docker exec -it influxwfr influx ping
Expected output:
OK
2๏ธโฃ Test the API on your Lightsail instance:
curl -i http://localhost:8086/ping
Expected output:
HTTP/1.1 204 No Content
3๏ธโฃ Test from your local machine:
curl -i http://YOURIP:8086/ping
You can also try to access the GUI through http://YOURIP:8086
If this fails, double-check AWS Lightsail Firewall Rules to allow inbound traffic on port 8086.
๐ Step 6: Retrieve Your InfluxDB Token
Run:
sudo docker exec influxwfr influx auth list
Copy the admin token, as youโll need it for your Python script.
๐ Step 7: Update Your Python Script (readCANxxx.py)
Modify your script to use the correct InfluxDB credentials:
influx_url = "http://YOURIP:8086"
token is saved in a seperate txt
Run the script:
python readCAN3batchSender.py
sudo docker update --restart unless-stopped influxwfr
sudo systemctl enable docker
Check if it works:
sudo reboot
sudo docker ps
This project now includes a comprehensive authentication system using MongoDB and JWT (JSON Web Tokens).
- User authentication with JWT tokens
- Protected routes for authorized access
- MongoDB integration for user storage
- Role-based access control (admin users)
-
Make sure MongoDB is properly configured with your connection string in
.envfile: DATABASE_URI=mongodb+srv://your_username:your_password@your_cluster_url/?retryWrites=true&w=majority JWT_SECRET=your_jwt_secret -
Create an admin user by running:
cd backend
node scripts/setup-admin.js
npm install
npm install -g nodemon
nodemon index.js- Default admin credentials:
- Username: admin
- Password: admin123
- Login via the login page to receive a JWT token
- Protected routes will automatically check for valid authentication
- The token is stored in localStorage and will persist until logout
- Admin users have additional privileges to manage other users
- Query:
?type=location - Description: Returns the most recent GPS coordinate (latitude and longitude) of the vehicle.
- Response:
{
"location": {
"lat": 42.06648123,
"lon": -84.24137456
}
}- Example Usage:
curl "http://127.0.0.1:8050/api/track?type=location"fetch("http://127.0.0.1:8050/api/track?type=location")
.then(res => res.json())
.then(data => console.log(data.location));- Query:
?type=lap - Description: Returns the latest completed lap with GPS path and start/end timestamps.
- Response:
{
"lap": {
"points": {
"lats": [42.0664, 42.0665, ...],
"lons": [-84.2413, -84.2412, ...]
},
"start_time": 1711931802.745823,
"end_time": 1711931823.462191
}
}- Example Usage:
curl "http://127.0.0.1:8050/api/track?type=lap"import requests
res = requests.get("http://127.0.0.1:8050/api/track?type=lap")
lap = res.json()["lap"]
print(lap["start_time"], lap["end_time"])- If an unsupported
typeis passed, the response will be:
{
"error": "Invalid request type"
}- HTTP Status:
400 Bad Request
| Endpoint | Type | Description | Response Key |
|---|---|---|---|
/api/track?type=location |
location |
Returns latest GPS point | location |
/api/track?type=lap |
lap |
Returns last completed lap | lap |
InfluxDB 2.x User Management Notecard
1. Creating a New User
To create a new user (e.g., โadminโ) within the organization โWFRโ, use:
sudo docker exec -it influxwfr influx user create \
--name admin \
--password pwd \
--org WFR
Note: The --role flag is not supported in the current CLI version.
2. Changing a Userโs Password
To update the password for a user (e.g., โadminโ), run:
sudo docker exec -it influxwfr influx user password --name admin
You will be prompted to enter the new password interactively.
3. Deleting a User
To delete a user, first list users to retrieve the userโs ID:
sudo docker exec -it influxwfr influx user list
Then delete the user using its unique ID (for example, 0eaa6d2e8865b000):
sudo docker exec -it influxwfr influx user delete --id 0eaa6d2e8865b000
Note: The delete command requires the --id flagโnot --name.
The React frontend is automatically deployed to the production server using GitHub Actions.
- Production URL: http://3.98.181.12:8060
- Auto-deployment: Triggers on every push to
mainbranch
cd my-react-app
npm install
npm run dev # Runs on http://localhost:5173
