in reply to Re^2: XS Modules - why and when?
in thread XS Modules - why and when?

If we look at RPi::PIGPIO::Device::DHT22 we can see a pure Perl method of reading the output from the device (DHT11 and DHT22 are read the same way).

Ahhh, but wait! Understand that the library you mention is a TCP connector to a daemon that is running on the Pi hardware. That daemon is what does all of the GPIO register manipulation directly. That daemon is written in guess what? C! So the library you mention does not interact with the hardware directly. It communicates with software that does. My software directly interfaces the hardware, which is why I needed C/XS to do so. This library communicates with someone elses software via a TCP socket that does the down and dirty work.

See here for info on pigpio.

From the pigpio web page:

The pigpio library is written in the C programming language. The pigpio daemon offers a socket and pipe interface to the underlying C library.

So that's why the library you mention can be written in Pure Perl. Note that the author of the Perl library you mention and the author of pigpio are not the same person.

Replies are listed 'Best First'.
Re^4: XS Modules - why and when?
by cavac (Prior) on Dec 15, 2023 at 08:55 UTC

    Just a quick note on those DHT11/DHT22 sensors. Ignoring the fact that their humidity sensor is absolutely craptastic and permanently breaks if humidity goes too high(*), the one wire sensor protocol needs very tight timing, see Basics of Interfacing DHT11/DHT22 Humidity and Temperature Sensor with MCU.

    The problem with running these (or other protocols that have implicit timing requirements like WS28xx LED strings) is that you simply can't guarantee that from a user land process (or even "at all") on a system like Linux or Windows. Especially, if you have a high process load. Your process simply might not get scheduled at the right times. In all likelyhood, the sensor will work most of the times, but you'll sometimes get random data and/or errors. For me, a cheap microcontroller and Device::SerialPort usually solve the trick for these kind of problems.


    (*) DHT11 - temp ok, but RH values wildly off. I generally now recommend using a Bosch BME280 breakout board instead. At least from the limited tests i've done on my projects, this sensor seems to be a lot more reliable long term.

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
      Just a quick note on those DHT11/DHT22 sensors

      Thanks for the information. It might help to explain what I am thinking of doing...I might be wildly off with my plans...

      At home, we have a smart thermostat. It is controllable by Alexa and remotely from our phones. It is supposed to have geofencing but doesn't handle having two people connected. Sometimes, I will go out, and the heating will turn right down, which would be fine if my partner were not still at home wanting the heating!

      I've also noticed that the thermostat can read 18 degrees in the summer, and we feel nice and warm. Yet in the winter, the thermostat can read 20 degrees and we feel chilly. So, something more than temperature is at play. I am guessing at humidity.

      My plan iswas to fit a DHT22 connected to a Raspberry Pi Zero in an enclosure outside. Then, to have a more powerful Raspberry Pi inside with a DHT11 connected. That would allow me to measure the indoor and outdoor temperature and humidity and set the heating accordingly. Some experimentation would be needed to set it up correctly, but that would be a software issue once the hardware is set up.

      It would be nice to have the heating keeping the perceived temperature constant throughout the year rather than the absolute temperature.

      I only have to worry about switching the central heating boiler on and off - here in the UK we don't have air conditioning. Only me office has that luxury.

        I've also noticed that the thermostat can read 18 degrees in the summer, and we feel nice and warm. Yet in the winter, the thermostat can read 20 degrees and we feel chilly. So, something more than temperature is at play. I am guessing at humidity.

        It might or might not have to do anything with humidity. The temperature sensor is measuring the temperature in one location, not the whole living area. Let's say for the moment it is mounted on an internal wall. That would mean, in summer the temperature near an external wall far from the sensor is possibly higher than at the measured location. And in winter, the opposite is true.

        (this paragraph is mostly a general explanation for future readers, OP seems to be already considering how this): Modern heating systems sometimes come with multiple sensors to get a better average. Also, i have seen more and more heating systems that also install one or more outdoor sensors (especially central heating systems that circulate hot water through radiators). The external sensor allows those systems to compensate for the time it takes them to warm up the water by detecting the dropping temperatures before the chill has time to seep through the walls.

        A temperature controller (just as most other regulation systems) also has to have a deadband ("switch off when reaching 20°C, switch back on when falling below 19°C") to reduce the on/off cycles and prevent prematurely wearing out the system. This deadband can also account for perceived and real temperature differences. For more complex control loops, you might have to read into PID controllers and such.

        As for your project, if you only need to read temperature and make it available over the network, an ESP32 controller (Microcontroller with Wifi) with a reliable temperature sensor like a DS18 could be a way to go, especially if you need multiple of those.

        Especially for stuff running outside, i strongly recommend not to use mains voltage directly. For these kinds of projects i usually run 12V with a proper fuse (car fuse holder+fuses are fine) at the source! Run the 12V outside, then use a step-down/DC-DC converter next to your electronics to step down to the proper voltage. This way you won't get electrocuted if something goes wrong, and you won't have to worry about drooping voltages in the long cable runs.

        If you like to design your own PCB to host the DC-Converter, the pinheaders for the ESP32/Arduino/whatever and connector to the temperature sensor and power lines, you can freely copy stuff from my NDC1 modular Arduino based computer project. The (KiCAD) schematic for the DC regulators on the PowerBoard is pretty much my goto design for most of my electronics projects and has been tested extensibly under outdoor conditions in the range of about -25°C to +60°C (no guarantees though, it's a hobby project after all).

        PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
        At home, we have a smart thermostat. It is controllable by Alexa ...

        ours is controlled by Ursula ;)