Developing a temp/humidity sensor

Hello!
I'm a hardware engineer developing a multi-sensor board that will attach to a Raspberry Pi. The first part of the design - the temperature and humidity sensor is working on my Pi Zero (the development platform) but being a hardware guy, I'm getting a little lost in the Octoprint plug in world.

I've gotten to the point where my plugin will read the manufacturing ID from the sensor in a plugin shell and prints it to the log. I also have a standard python script that does the temperature/humidity conversion and prints the results - all good and sane so far. The problem comes when I try to get my plug in to be called on a regular basis. To do the conversion I have to initiate it with an i2c register write, then check a status bit to determine if it is complete. The conversion can take a reasonable amount of time (up to a second). Alternately I can set the sensor up to interrupt the processor with a GPIO transition on a predetermined rate (1/10/100 seconds) - but again, I have no idea how to make a plugin respond to that.

Ideally I would like to add the output of the conversions to the temp graph.

I found code that I might be able to adopt (if I knew how it was called and how the data was inserted) here: https://github.com/1r0b1n0/OctoPrint-Tempsgraph.git

If anyone can point me in the direction of simple example code - I would be very appreciative!

You might want to look into my plugin PlotlyTempgraph, it will allow fairly easy integration of the data into the graph. The enclosure plugin (which also supports temperature and humidity sensors) actually has a PR right now to integrate with it. And an example of how you can add the data from your own plugin into it can be found here. It's only putting in random data, but hopefully makes enough sense.

Here's the relevant PR I was talking about...

1 Like

This is great! Thanks, that gave me a lot to work with.

Well I have a problem with the callback:

2021-02-13 01:06:45,116 - octoprint.util.comm - ERROR - Error while processing temperatures in octosafe, skipping
Traceback (most recent call last):
File "/home/pi/oprint/lib/python3.7/site-packages/octoprint/util/comm.py", line 2065, in _processTemperatures
parsedTemps = hook(self, parsedTemps)
File "/home/pi/oprint/lib/python3.7/site-packages/octoprint/util/init.py", line 1890, in wrapper
return f(*args, **kwargs)
File "/home/pi/devel/OctoSafe/octoprint_octosafe/init.py", line 124, in callback
parsed_temps.update(ambient = (convert_temp(temp_h, temp_l),None))
AttributeError: 'MachineCom' object has no attribute 'update'

Here is the code in my init.py

               def callback(comm, parsed_temps, ignored):
                temp_l = comm.bus.read_byte_data(DEVICE_ADDRESS, HDC2080_TEMPL)
                temp_h = comm.bus.read_byte_data(DEVICE_ADDRESS, HDC2080_TEMPH)
                humid_l = comm.bus.read_byte_data(DEVICE_ADDRESS, HDC2080_HUMID_L)
                humid_h = comm.bus.read_byte_data(DEVICE_ADDRESS, HDC2080_HUMID_H)
                parsed_temps.update(ambient = (convert_temp(temp_h, temp_l),None))
                parsed_temps.update(humidity = (convert_humid(humid_h, humid_l),None))
                # start the conversion for the next time around
                comm.bus.write_byte_data(DEVICE_ADDRESS, HDC2080_CONFIG, 0x1)
                return parsed_temps

__plugin_pythoncompat__ = ">=2.7,<4"
__plugin_implementation__ = OctoSafePlugin()

__plugin_hooks__ = {
        "octoprint.plugin.softwareupdate.check_config":
        __plugin_implementation__.get_update_information,
        "octoprint.comm.protocol.temperatures.received":
        __plugin_implementation__.callback
}

Thanks in advance!

My example was using a single file plugin, so it's formatted a little different. You need to switch it to

def callback(self, comm, parsed_temps, ignored):

I think.

1 Like

that makes sense! the ignored variable I added because there was a third mysterious argument that was being passed (I didn't know it was "self").

I made a dozen or so more changes to get it to work finally!
Here is a screen shot: Temp - humidity graph

Thanks! This should work great!

Nice, glad you got it worked out. It looks just like my Tasmota plugin's graphing with temp/humidity sensor connected.

The Tasmota stuff looks interesting. I'm planning on integrating a few functions on a very small board. Appreciate the help!