Bod has asked for the wisdom of the Perl Monks concerning the following question:

I have a blind uncle for whom we installed electric curtains for Christmas a couple of years ago as he doesn't know when it goes dark. Currently these are on a rather unreliable timer. Even if the timer were reliable, it doesn't account for the changing sunset time or the change from BST to GMT and back. Generally this hasn't been a problem as we visit and reset the timer monthly. But the COVID lockdown restrictions here in the UK have shown the problems in this arrangement.

Having looked at several other options I am considering building a controller using a Raspberry Pi Zero and a relay module. I have two choices for connecting the relay module to the RPI - USB or GPIO. My preference is USB but want to check what is involved in controlling the USB port with Perl. Is USB::LibUSB going to be sufficient to control two relays on a module like this one or are there pitfalls awaiting me with USB?

My plan is that the RPI will get sunset time for each day, open the curtains at 8:30am local time (GMT or BST) and close them at sunset (perhaps with a fixed offest if necessary). I've not used a Raspberry Pi before - any advice or suggestions?

Replies are listed 'Best First'.
Re: Controlling USB on Raspberry Pi
by stevieb (Canon) on Dec 05, 2020 at 02:59 UTC
    "I've not used a Raspberry Pi before - any advice or suggestions?"

    My advice is to use a micro controller instead. A Wemos D1, ESP8266 NodeMCU etc. Since this project is very specific, there's no need to have a full blown computer OS running on the device.

    I spent numerous years writing software for the Raspberry Pi (RPi::WiringPi), and several years developing automation devices using Arduino and other such micro controllers. For projects that require back-end processing, the Pi is definitely the way to go, but for things such as the task you want to perform, a Pi is overkill, and there are a lot more software aspects that can break and fail.

    With all that said, controlling a relay using my software is very simplistic. Have a look at the comprehensive documentation, and feel free to ask any questions that you might have. The FAQ/Tutorial page is a good place to start.

    My entire vast indoor grow rooms used to be controlled using a Pi and my software, until it got too complex and I converted it over to using the Wemos D1 minis I mentioned above. They cost me $10 for five of them, and they have wifi and bluetooth built in.

Re: Controlling USB on Raspberry Pi
by jmlynesjr (Chaplain) on Dec 04, 2020 at 23:56 UTC

    Bod:

    Use the HiPi distribution from CPAN. The documentation is at raspberry.znix.com. Use a GPIO pin to control the relay and time of day and a light sensor to trigger an open/close cycle.

    Scroll down in the Cool Uses For Perl section to see several HiPi examples that I have recently posted.

    James

    There's never enough time to do it right, but always enough time to do it over...

      Thanks James,
      Given that the HiPi documentation is a bit lacking, I'm sure I will have plenty more questions once my Pi and required adaptors have arrived...

        Ask away. The docs at the znix site are actually pretty good. Just a little short on examples.

        James

        There's never enough time to do it right, but always enough time to do it over...

Re: Controlling USB on Raspberry Pi
by afoken (Canon) on Dec 05, 2020 at 11:55 UTC
    I am considering building a controller using a Raspberry Pi Zero and a relay module.

    So you need to configure two GPIO pins as outputs and set the logic level to high or low. And you don't have any strict timing requirements. It simply does not matter if it takes 0.1 ms, 1 ms, 10 ms or 100 ms to switch the relais. So, you can use the sysfs interface of the Raspi. All you need to do is to write some virtual files, which is trivial in Perl and bash.

    Controlling relais isn't that hard either, Amazon has a lot of simple relais modules that accept a 3.3 V logic level input and can switch 110 V or 230 V. Just make sure the relais can switch the current (A) and voltage (V) required by the electric curtains.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      It simply does not matter if it takes 0.1 ms, 1 ms, 10 ms or 100 ms to switch the relais

      Too right...the script to open/close the curtains will probably run from CRON every 10 minutes or so with an updater script running from CRON a couple of times a day. So timing is not critical.

      Just make sure the relais can switch the current (A) and voltage (V) required by the electric curtains

      The curtain motor is 12V 1A so no worries there. It probably doesn't actually need a relay as a thyristor should do it but relays are simpler as I won't have much access to the motor setup whilst building the controller.

        It probably doesn't actually need a relay as a thyristor should do it

        Thyristors have a very different switching behaviour compared to a relais. Once triggered by a gate pulse, they stay on until the load current drops (close) to zero. On a DC supply, a thyristor stays on until power is switched off or the motor burns out. However, with pulsing DC current (i.e. AC after a rectifier, but no capacitor), the thyristor will switch off at the end of a half wave. This allows tricks like a crude PWM regulator by triggering the thyristor somewhat after the begin of a half wave (Proxxon uses this trick for their "DC" minimot tools).

        For driving AC motors, a thyristor won't help you much, as it either isolates or behaves like a diode, so the AC motor gets either no current or pulsed DC.

        For full control of a DC motor on a DC supply, you typically use an H-bridge setup with bipolar or MOSFET transistors. This allows PWM for speed control, turning left and right, braking (not with every controller) and coasting. There are severel very clever H-bridge driver ICs available that add a lot of protection for both motor (e.g. stall and overcurrent detection) and driver transistors (overcurrent, short circuit detection). Some H-bridge driver ICs even include the transistors. And yes, there are cheap, ready-to-use modules with H-bridges.

        H-Bridge drivers either accept a direction and a speed signal (on/off or PWM), or a left-turn and a right-turn signal (again either on/off or PWM). If breaking is supported, the direction-and-speed signals need a third signal to switch between breaking and coasting, the left/right approach can use the illegal state (left and right on at the same time) for breaking, or also use a third signal line. After all, driving a solid-state H-bridge is not too different from driving two relays. Two "slow" bits are sufficient for switching left/off/right, you may need to add a third bit for breaking. With a direction-and-speed driver, change one "slow" bit to PWM output to allow speed control.

        L298-based modules seem to be very popular (two motor up to 46 V, each 3 A abs max peak, each 2 A operating, logic supply 4.5 V to 7 V, logic levels should be just about compatible with 3.3 V signals, motor drivers can be paralleled for double output current), just add a heatsink, four diodes per motor and some passives to the chip and you have a driver module. Only downside is that the L298 is everything but smart, no protection except for shorting out the motor supply.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Controlling USB on Raspberry Pi
by marto (Cardinal) on Dec 05, 2020 at 09:26 UTC

    An off topic non perl solution, people use home assistant to automate all sorts of 'smart' home devices, one example from their community forum on this subject.

Re: Controlling USB on Raspberry Pi
by Bod (Hermit) on Dec 05, 2020 at 19:11 UTC

    I deliberately included USB in the title but all the responses have dealt with using GPIO on the RPi Zero. I've actually ordered an RPi without the soldered header and my soldering skills are somewhat rusty so I was rather hoping to to be able to connect the OTG USB of the RPi to one of these modules which I have also ordered.

    Is this a non-starter and, if so, why?

      Do you have the datasheet for the USB module? USB is simply a serial interface, for which you can use my RPi::Serial module.

      You need the communication specification so you know how to set up the serial interface, as well as what you need to send to the unit to trigger high/low states of each relay on the board.

      I've never even heard of a USB relay breakout board before, let alone used one, but I do have vast experience communicating over USB/serial to dozens of other device types, which is why I stuck with the GPIO scenario.

        Do you have the datasheet for the USB module?

        Not at present but I have a module on order and I'm hoping it will come with some clues as to what needs to happen to make it operate.