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

No, it's primarily about interfacing with an existing C library

Thanks...that makes a lot of sense. cavac's point about security resonates here -> Re^2: XS Modules - why and when?

But in RPi::DHT11, there doesn't seem to be any reason to use XS unless the C code already existed. I had assumed stevieb wrote the C code as well as the Perl code, but I have nothing to base that on.

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). As Perl is rather good at bitwise and mathematical operations, it just seems weird to me that XS would be used in this situation.

Replies are listed 'Best First'.
Re^3: XS Modules - why and when?
by LanX (Saint) on Dec 05, 2023 at 12:02 UTC
    I quickly scanned the POD for RPi::DHT11 and found:

    > This module requires the wiringPi library to be installed

    And http://wiringpi.com/ states:

    > WiringPi is a PIN based GPIO access library written in C

    And

    > is intended for use by experienced C/C++ programmers.

    So, doesn't this answer your question?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Re^3: XS Modules - why and when?
by stevieb (Canon) on Dec 05, 2023 at 18:25 UTC
    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.

      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.