Temperature and Humidity loggin while printing

I am looking for a way to log the temperature and Humidity while printing and then getting a csv file at the end of the print. Is there a plugin that can do something like that? I was not able to find one that can do that.
I use a Raspberry Pi 5 with octoprin_deploy. It work fine.
I connected a BME280 with i2c to the raspberry. That also works fine. I can see the temp and humidity with the plugin Print enclosure where I can select a sensor typ and see the live values. That works fine. Now I just need a way to creat a csv file for every print. I have someone that wants to use the data to try and find correlation between print environment and print quality and strength for a given filament.
It is not needes to be a csv file, I just need something that I can open and see the logged data.

I have little to no idea what I am doing and learning while trying to do something.

What did you already try to solve it?

-searched for plugins that can do the job
-looked into how to create my own plugin (dont have any knowledge of programming)

Have you tried running in safe mode?

is it needed?

Systeminfo Bundle

Raspberry Pi 5 4Gb
Running Raspberry Pi OS
Installed Ocotprint with octoprint_deploy (has not created any problems so far)
Anycubic Vyper dont know if that is needed
BME280 (i2c)
Octoprint 1.10.0

1 Like

The Event Manager bundled plugin can execute system commands on "PrintStarted" and "PrintDone" so that should be all we need for OctoPrint.

How did you connect the BME280 to the RPi? Perhaps a pointer to where you bought it?

I did a Google search for "raspberry pi bme280 i2c python" and found lots of information on how to use Python to capture data from it.

I have a Sparkfun BME280 and a spare RPi so I could probably help get you started. I'm afraid "some" knowledge of programming will be required.

With those examples you could easily make a single file plugin to integrate into Plotly Temp Graph plugin that would at least give graphing of the data in near real-time. I haven't personally made one for plotly, but have integrated sensor data into my Tasmota plugin, so if you had an esp based device with a BME280 connected to it, that could graph the information as well as log it to sqllite database.

You can find examples of how to integrate into Plotly Temp Graph plugin here or with Tasmota plugin, you just need to set the sensor identifier to BME280. I've actually done the BME280 personally with Tasmota while developing the feature.

I connected the BME280 via i2c (GPIO). I bought it from amazon:

I did a lot of trial and error to first get my two scrips Running without Octoprint.
Sadly I dont know anyone personally who could have helped me. And I am somewhat stubborn and wantet to try myself first. I used to power of Google and ChatGPT and a lot of trying, then I started to understand a large size of the code. Right now it works great when I use it while I am in a virtual environment which I needed to get the import of libraries working.
I know there is probably a better way to do it, but I didnt know a better way.
So right now my script consists of two scripts.
The Main scripts loggs all the massurements of the BME280 and creats a csv file when it detects a stop_signal.txt in a folder.
The second script creats that stop_signal.txt file in a specific folder.
The second script does not need any imports (besides time & os) which is probably the reason why I cant trigger it with the eventmanager.

But now to my new problem. The main script needs a lot of imported libraries that are not installed by default:
time, csv, board, busio, from adafruit_bme280 import basic as adafruit_bme280, threading, os

so if I want to trigger the new script with the eventmanager nothing happends. I did somewhat expectet that.
Is there a good way to solve this?
I thought about using a bash.sh script which starts a seperated venv and then executes it. No idea if that is even possible.
Is there a good way to implement the scripts in a special Octoprint folder in which I can add these libraries?
I read some logs and there was a OctoPrint venv mentioned, which I asume is needed to run Octoprint. Could I add the libraries there?
I put both scripts in the zip file as plain text files to read.

Scripts.zip (1.4 KB)

this is how I would approach it if not writing my own plugin.

you would first activate the OctoPrint venv with source ~/oprint/bin/activate and then you can run your pip commands to install the dependencies.

Just re-read the problem... If it works in the Enclosure plugin, all you need to do is record the data during the print, All you should have to do is write out the values inside the script you use to read the sensor.

I believe you would need three scripts because the OctoPrint Event Manager is going to want scripts that complete immediately. These three scripts would be:

  1. A long running "main" script that collects the data. I believe this would be your "main.txt"
  2. A script that launches the "main" script in the background, call this "start"
  3. A script that causes the "main" script to shutdown, call this "end"

The Event Manager would use "start" in PrintStarted, "end" in PrintDone and maybe PrintFailed. The "main" script would do all the work.

You might get away with adding a "&" at the end of the command line for the PrintStarted script.

A general observation about the scripts you posted. For Python, the extension is normally ".py", not ".txt" and the first line is often "#!/usr/bin/python" which helps the command shell (bash) identify the program to use.

Since you are using the Adafruit library, I'm pretty sure they have detailed instructions on how to install their library on your system. I used:
sudo pip3 install adafruit-circuitpython-bme280
on my Raspberry Pi 4, renamed your scripts to main.py and end.py, added the first line above, and converted them to Unix line endings (with Notepad++).

Scripts.zip (1.5 KB)

I'll see if I can connect my Sparkfun BME280 and get it to work.

Is there a location where I should put the scripts?
I cant get the main.py to start.
Right now I am trying to start the main.py by using a start.py
In the Eventmanager I use Upload as the trigger (its faster and just a drag an drop away...).
The end.py works fine by doing it like this. But I dont know the problem that are occuring with the script. I cant find any clues in the log. Why does the Eventmanager need a script that can be executed instantly?

2024-05-18 14:25:01,658 - octoprint.plugins.tracking - INFO - Sent tracking event webui_load, payload: {'browser_name': 'Chrome', 'browser_version': '125.0.0.0', 'os_name': 'Windows', 'os_version': '10'}
2024-05-18 14:25:07,807 - octoprint.filemanager.analysis - INFO - Starting analysis of local:TestUploadCaliperCase.gcode
2024-05-18 14:25:07,830 - octoprint.events - INFO - Executing system command: python3 /home/tom/OctoPrint/Test/start.py &
2024-05-18 14:25:08,330 - octoprint.filemanager.analysis - INFO - Analysis of entry local:TestUploadCaliperCase.gcode finished, needed 0.52s
2024-05-18 14:39:53,910 - octoprint.server.heartbeat - INFO - Server heartbeat <3

Right now I am using the command: python3 /home/tom/OctoPrint/Test/start.py &
I dont know if I should change anyting, because if I use the end.py it works fine.

2024-05-18 15:00:29,496 - octoprint.filemanager.analysis - INFO - Starting analysis of local:TestUploadCaliperCase.gcode
2024-05-18 15:00:29,513 - octoprint.events - INFO - Executing system command: python3 /home/tom/OctoPrint/Test/end.py &
2024-05-18 15:00:30,010 - octoprint.filemanager.analysis - INFO - Analysis of entry local:TestUploadCaliperCase.gcode finished, needed 0.51s

I installed all the needed libraries in the OctoPrint environment and can run them from the terminal just fine.

I put all my OctoPrint scripts in "/home/pi/.octoprint/scripts/" so the backup and restore will capture them. (I would hate to have the SD card go and have to rebuild "everything".
You want 'quick' scripts cause it is an 'event'. Think if it took an hour to run the script and the event happened every minute...
You say you can run them fine from the CLI on the Pi. Sounds like a permissions problem. In the CLI, go to your scripts directory and do "ls -l" to get a listing and make sure the permissions are set the same for all your scripts.
It is 'normal' to use the 'full path' to the python3 file, usually "/usr/bin/python3".

I believe that where is put the scripts doesn't matter if you use the full path in the Event Manager plugin. Scripts will need "execute" permission (chmod +x <script>). The scripts used in the Event Manager plugin need to do their thing and exit so the "event" can finish.

The & at the end of a command line tells the command interpreter to create a "background" task. I'm not sure if that will work with scripts run from the Event Manager plugin but it should be obvious, i.e. a check from another SSH terminal session

I'll have to do some experimenting. In the mean time, please post your scripts again so we can take a look.

I am using the pi with a screen attached because the raspberry Pi imager did not have the Octopi Version available for the Pi 5. So I installed Raspberry Pi OS (64bit) and installed Octoprint via Octoprint_deploy from github.

I had no problem with this. yet.
I dont know if my problem is caused by this. I sadly can't tell.

The attached files worked. I just checked it via terminal in the RP OS.
The end.py workes fine when I want to trigger it from the Eventmanager.
I dont know if it is a problem with permissions. I used the chmod +x command (and the files went to green if I use the ls command again). And now when I double click them the OS is asking me if I want to open, execute or execute in terminal the .py file. Sadly nothing happens when I want to execute it from the file explorer.

ScriptsV2.zip (1.8 KB)

I think we are over engineering this so I have a simpler approach...

  1. Create a "main" script that watches for a "start file" and when seen, deletes it, starts logging temperatures, and watches for a "stop file". When "stop file" is seen, delete it, clean up (close) the temperature log, and go back to watching for a "start file".
  2. Create a "start" script that creates the "start file" and exits.
  3. Create an "end" script that creates the "end file" and exits.
  4. In the Octoprint Event Manager, add the "start" script to the appropriate events and the "end" script to appropriate events.
  5. In an SSH terminal session, run the "main" script.

While you can do something more exotic with subprocesses, threads, daemons, etc. This approach should be simple to debug. The "main" script doesn't have any connection to OctoPrint, it just runs in a terminal window.

The first line of each of these scripts should be #!/usr/bin/python3 if the script is written in Python or #!/usr/bin/bash if it is a script written for the bash command shell. Alternatively, #!/usr/bin/python works if the script is written for the "default" python version (which should be Python 3 these days. Type /usr/bin/python3 --version and /usr/bin/python --version to verify).

See https://docs.octoprint.org/en/master/events/index.html for additional information that could be sent to (and used by) the "start" and "end" scripts. The name of the file being printed might be useful to pass to the "main" script.

1 Like