Use GPIO pins from LUA. How? Topic is solved

Moderator: leecollings

Post Reply
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Use GPIO pins from LUA. How?

Post by mjdb »

I want to set GPIO-pins from LUA.

I searched the internet and this forum for days, but I can't find the solution.

Main question: What is the "#include" or "require" instruction in LUA to use GPIO-pins from WiringPi?

I installed WiringPi according to the Wiki (https://www.domoticz.com/wiki/GPIO).
On the command line "gpio -v" is working and so is "gpio readall".

Can someone give a LUA example how to call the WiringPi/GPIO library and how to set the GPIO-pins from LUA?

I tried alternatives from internet. None of them gave the final solution.
- WiringPi WIKI is about using GPIO with switches.
- Github is about developing, but I can't find any practical application.
- Installation of http://andre-simon.de/doku/rpi_gpio_lua ... io_lua.php can be completed, but the require "GPIO" in LUA fails.

Side-question: Is the current installation of Domotics using LUA 5.1 or LUA 5.2
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
BarryT
Posts: 358
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Use GPIO pins from LUA. How?

Post by BarryT »

mjdb wrote:I want to set GPIO-pins from LUA.

I searched the internet and this forum for days, but I can't find the solution.

Main question: What is the "#include" or "require" instruction in LUA to use GPIO-pins from WiringPi?

I installed WiringPi according to the Wiki (https://www.domoticz.com/wiki/GPIO).
On the command line "gpio -v" is working and so is "gpio readall".

Can someone give a LUA example how to call the WiringPi/GPIO library and how to set the GPIO-pins from LUA?

I tried alternatives from internet. None of them gave the final solution.
- WiringPi WIKI is about using GPIO with switches.
- Github is about developing, but I can't find any practical application.
- Installation of http://andre-simon.de/doku/rpi_gpio_lua ... io_lua.php can be completed, but the require "GPIO" in LUA fails.

Side-question: Is the current installation of Domotics using LUA 5.1 or LUA 5.2
Hi Marco,

What do you want do with the pin(s)?
Sensor reading?
Turn on/off relais?
Blinking a led?
Raspberry / ESP Boards / Relais / Milight / Hue / OTGW / P1 / Xiaomi / RFXCom / RFLink / ZWave / Conbee II / Z2M / MQTT / A lot of scripts and many more..
Software: Linux, Android and Windows
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Re: Use GPIO pins from LUA. How?

Post by mjdb »

I want to build a 'schematic house' with leds in various rooms. The leds should indicate the status of sensors, activities, alarms, lights and other devices.

Since I am using both KaKu, Z-wave and internet-devices I only have the complete overview of all statuses available from within Domoticz.

I have a basic system working with Arduino with copies of received 433MHZ commands, but I also want Z-wave etc.

I want to make a script (preferably in LUA) that says: 'If device X is On' then 'put PIN x High'. So the Led should lighten up.

Because of the number of devices, I am sure I will need to use a shift-register. So executing commands via os.execute probably won't work.

Hope you can help me...
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
BarryT
Posts: 358
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Use GPIO pins from LUA. How?

Post by BarryT »

mjdb wrote:I want to set GPIO-pins from LUA.
First add "dtoverlay=w1-gpio" in /boot/config.txt
Then 'export' the gpio # of your need..

go to etc/init.d/domoticz.sh and add following code, after the line with:
### END INIT INFO

Code: Select all

sudo sh -c 'echo 16 > /sys/class/gpio/export'
sudo sh -c 'echo high > /sys/class/gpio/gpio16/direction'
sudo sh -c 'echo 1 > /sys/class/gpio/gpio16/active_low'
In here, the first code gives your gpio pin 16 (in my case) state exported as an output.
The second code is the direction (if needed).
I use relais on the gpio pins, and in Domoticz the states of the relais where on=off and off=on, so i reversed the direction.
The last code is giving the gpio port the output to LOW, so it gives 0 voltage (so closed).
If u want to use the gpio as INPUT, use the same code but then with IMPORT settings..
Do this for every gpio port u want to use.
Repeat this codes to your needs..

Hope you have enough information to play with.
After you added this code into the domoticz.sh, reboot and after that u could use gpio in Domoticz.
Go to Hardware, add GPIO.
Then to go the switches, add manual switch, add gpio..

Edit*
https://www.youtube.com/watch?v=7ve0KLruhU4
See this video for the code in domoticz, and a lua example :)
Last edited by BarryT on Tuesday 30 May 2017 11:53, edited 2 times in total.
Raspberry / ESP Boards / Relais / Milight / Hue / OTGW / P1 / Xiaomi / RFXCom / RFLink / ZWave / Conbee II / Z2M / MQTT / A lot of scripts and many more..
Software: Linux, Android and Windows
gohan
Posts: 36
Joined: Monday 13 February 2017 23:42
Target OS: Windows
Domoticz version:
Contact:

Re: Use GPIO pins from LUA. How?

Post by gohan »

if you have inverted logic relays you have to use HW "generic gpio fs" otherwise if you use GPIO only it will only behave the normal way (at least it is what it did on my rpi)
BarryT
Posts: 358
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Use GPIO pins from LUA. How?

Post by BarryT »

gohan wrote:if you have inverted logic relays you have to use HW "generic gpio fs" otherwise if you use GPIO only it will only behave the normal way (at least it is what it did on my rpi)

Code: Select all

sudo sh -c 'echo high > /sys/class/gpio/gpio16/direction'
i use this for all my (14) relais, its working perfect.
On=On Off=Off
Raspberry / ESP Boards / Relais / Milight / Hue / OTGW / P1 / Xiaomi / RFXCom / RFLink / ZWave / Conbee II / Z2M / MQTT / A lot of scripts and many more..
Software: Linux, Android and Windows
gohan
Posts: 36
Joined: Monday 13 February 2017 23:42
Target OS: Windows
Domoticz version:
Contact:

Re: Use GPIO pins from LUA. How?

Post by gohan »

that's what I said, but if you add "sudo sh -c 'echo 1 > /sys/class/gpio/gpio16/active_low'" it will continue to work ON=ON OFF=OFF unless in the HW page you add the "generic gpio fs" and then you can do the pin HIGH=OFF LOW=ON
BarryT
Posts: 358
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Use GPIO pins from LUA. How?

Post by BarryT »

gohan wrote:that's what I said, but if you add "sudo sh -c 'echo 1 > /sys/class/gpio/gpio16/active_low'" it will continue to work ON=ON OFF=OFF unless in the HW page you add the "generic gpio fs" and then you can do the pin HIGH=OFF LOW=ON
Good, allright... But what does this have to do with the question of mjdb, to use gpio in lua? ;)
Last edited by BarryT on Tuesday 30 May 2017 11:55, edited 1 time in total.
Raspberry / ESP Boards / Relais / Milight / Hue / OTGW / P1 / Xiaomi / RFXCom / RFLink / ZWave / Conbee II / Z2M / MQTT / A lot of scripts and many more..
Software: Linux, Android and Windows
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Re: Use GPIO pins from LUA. How?

Post by mjdb »

Thank you Barry!

You helped me a lot. The first trick was to place the GPIO initialization commands in domoticz.sh near the top. In the Wiki it says to put them at the bottom, so after Domoticz is started. This seems not correct.

Now, I have my GPIO-switch in Domoticz. I understand how to use it from LUA. That is a great step forward.

I suppose, for a second switch I have to 'export' another pin.

But... If I want to use a shift-register, I have to send a large series of on/off commands to several pins.

Is there a way to do that from LUA without the use of 'PIN-switches' ?
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
BarryT
Posts: 358
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Use GPIO pins from LUA. How?

Post by BarryT »

mjdb wrote:Thank you Barry!

You helped me a lot. The first trick was to place the GPIO initialization commands in domoticz.sh near the top. In the Wiki it says to put them at the bottom, so after Domoticz is started. This seems not correct.
No thanks.
mjdb wrote:Now, I have my GPIO-switch in Domoticz. I understand how to use it from LUA. That is a great step forward.

I suppose, for a second switch I have to 'export' another pin.
yes, correct. Do this for every led/switch/input.
mjdb wrote:But... If I want to use a shift-register, I have to send a large series of on/off commands to several pins.
Is there a way to do that from LUA without the use of 'PIN-switches' ?
What do you mean with pin-switches? I think the only way to use the pin(s) in domoticz is trough switches, inputs or states (high low)?
Raspberry / ESP Boards / Relais / Milight / Hue / OTGW / P1 / Xiaomi / RFXCom / RFLink / ZWave / Conbee II / Z2M / MQTT / A lot of scripts and many more..
Software: Linux, Android and Windows
BarryT
Posts: 358
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Use GPIO pins from LUA. How?

Post by BarryT »

Btw, i cannot help you with questions about the 'shift-register' with a raspberry (in domoticz)..
I don't use it :)
Raspberry / ESP Boards / Relais / Milight / Hue / OTGW / P1 / Xiaomi / RFXCom / RFLink / ZWave / Conbee II / Z2M / MQTT / A lot of scripts and many more..
Software: Linux, Android and Windows
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Re: Use GPIO pins from LUA. How?

Post by mjdb »

What do you mean with pin-switches?
I meant the devices/switches that you define in Domoticz to turn a PIN high or low.
I think the only way to use the pin(s) in domoticz is trough switches, inputs or states (high low)?
Well, that in fact was/is my main question. Is there a way to use pins (set high/low) without the use of switches?

Anyway, I have progressed now that I can use at least the available number of pins.

For anyone who comes at this point of the thread... Do you know how to set the pin-state directly from LUA? Without having to wait that the script finishes.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
BarryT
Posts: 358
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Use GPIO pins from LUA. How?

Post by BarryT »

Ik denk dan toch echt dat je aan het bashen komt..
Misschien dat het met json mogelijk is middels een http call, maar dat durf ik niet te zeggen.
Neem hier eens een kijkje:

http://iqjar.com/jar/programming-the-ra ... sh-script/
Raspberry / ESP Boards / Relais / Milight / Hue / OTGW / P1 / Xiaomi / RFXCom / RFLink / ZWave / Conbee II / Z2M / MQTT / A lot of scripts and many more..
Software: Linux, Android and Windows
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Re: Use GPIO pins from LUA. How?

Post by mjdb »

Ik denk dan toch echt dat je aan het bashen komt..
Yes, I know. I have the GPIO command from WiringPi working on the command line (see my initial post).

I know I can set a series of those commands in a file and run it, for example through os.execute (= LUA command).

The main obstacle will be to use the shift-register. This has to be controlled from three pins with a synchronized (clocked) output.

I will experiment. When I have a solution, I surely will come back here and publish it.

Thank you all for your help till this point.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
gohan
Posts: 36
Joined: Monday 13 February 2017 23:42
Target OS: Windows
Domoticz version:
Contact:

Re: Use GPIO pins from LUA. How?

Post by gohan »

why do you need shift register? How many outputs do you need?
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Re: Use GPIO pins from LUA. How?

Post by mjdb »

I want to make a "virtual" representation of al devices I have in Domoticz, devided per room.

That is (about) 40 lights, 8 motion sensors, 8 door/window sensors (x 2 open/close = red/green), ventilation, 12 activity-statuses from other sources, physical mailbox, telephone answering-machine, NAS status, power usage (high/low) etc.

In total I assume 80 leds will be needed (or more when I am finished). So that is about 10 shift registers SN74HC595N.

Is it necessary to have such a gadget? Nope! But I want it anyway.

By the way, I have it working with the physical pins from the Raspberry Pi for 14 devices. My next challenge now is the shift register.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Re: Use GPIO pins from LUA. The next step...

Post by mjdb »

OK, so I managed to get a shift-register working.

A short explanation: A shift-register can hold up to 8 bits of information, which would be equal to 8 output pins on a Raspberry Pi or an Arduino.
You can combine several shift-registers to hold 16 bits, 24 bits, 32 bits and so on, of information. This is much more than the number of physical pins on a Raspberry Pi. For the transfer of data to the shift-register, you only need 3 pins, independent of the number of bits you will use.
A shift-register has a data-in-pin where the new bit-status is offered and a clock-pin that - when high - imports that data-bit. This is repeated for all 8, 16, 24 etc bits. When all bits are read, the latch-pin must be set high. At that moment the imported bits are copied to the output-pins.

The clock-timing should be about 1 milli-second. Well, this is the part I have not yet solved. If I set switches in Domoticz on and off without delay this goes far to fast for the shift register to process. And the only delay I know of in Domoticz is the 'AFTER 1' seconds option. This is of course way to slow, but... it is nice to follow the process happen.

If you build the following prototype, you can exactly follow what happens.
RaspberryPiWithShiftRegister.jpg
RaspberryPiWithShiftRegister.jpg (208.55 KiB) Viewed 12505 times
And from the back:
Backside.jpg
Backside.jpg (233.31 KiB) Viewed 12505 times
The connection of the pins is explained in the script.

Code: Select all

-- GpioTest

commandArray = {}

  -- Make three gpio-switches in Domoticz.
  -- I choose names that tell where the switch is related to.
  
  -- In this script I use variables that indicate the purpose of the switches:
  local Latch = 'gpio16 (pin36)' 
  local Clock = 'gpio17 (pin11)'
  local DataIn = 'gpio18 (pin12)'

  -- Pins on the 74x595 shift register
  --                __ __ 
  -- pin 1, Out 2  |  U  |  pin 16, Vcc
  -- pin 2, Out 3  |     |  pin 15, Out 1
  -- pin 3, Out 4  |     |  pin 14, Data in
  -- pin 4, Out 5  |     |  pin 13, Gnd
  -- pin 5, Out 6  |     |  pin 12, Latch
  -- pin 6, Out 7  |     |  pin 11, Clock
  -- pin 7, Out 8  |     |  pin 10, Vcc
  -- pin 8, Gnd    |_____|  pin 9,  Data out

  -- 'My' colors for the test
  -- Latch; on the 74x595, pin 12 = purple
  -- Clock; on the 74x595, pin 11 = green
  -- DataIn; on the 74x595, pin 14 = brown

  -- Connect pins 16 and 10 to power/plus/Vcc
  -- Connect pins 8 and 13 to ground/minus

  -- The shift register is powered by 3,3 volt.
  -- Otherwise the output-signals of the Raspberry Pi Pins (also 3,3 volt)
  -- would not be recognized as high.
  
  -- If you use two (or more) shift-registers, connect pin 9
  -- to pin 14 of the next chip.
  -- Also connect pin 12 to pin 12 and pin 11 to pin 11.

  print ("GpioTest started")
  
  local CmdCount = 0

  function SetLow(DeviceName)
    -- This function turns OFF the specified switch
    -- and so the related gpio-pin.
    CmdCount = CmdCount + 1
    table.insert(commandArray, {[DeviceName] = 'Off AFTER ' .. tostring(CmdCount)})
  end

  function SetHigh(DeviceName)
    -- This function turns ON the specified switch
    -- and so the related gpio-pin.
    CmdCount = CmdCount + 1
    table.insert(commandArray, {[DeviceName] = 'On AFTER ' .. tostring(CmdCount)})
  end

  -- These functions use the 'AFTER x' seconds delay.
  -- This means that each bit-state-change takes one full second.
  -- The shift-in of one data-bit needs four bit-state-changes and therefore 4 seconds.
  -- Of course this is way to long, it should be 1 milli-second.
  
  -- For now this is a proof-of-concept that nicely shows how the 
  -- shift-register works.

  SetLow(Latch) -- Freeses the current state of the shift-register
  
  function ShiftOut(DataArray)
    SetLow(Clock) -- start clean
    SetLow(DataIn) -- no data yet
    SetLow(Latch) -- Close the Latch, this freeses the previous data
    for i = 8,1,-1 do
      -- the last bit is processed first, so it can shift to the end
      SetLow(Clock) -- prepare for receiving a new bit
      if (DataArray[i] == 0) then
        SetLow(DataIn) -- when the bit is low, set the input low
      else
        -- when the bit is low, set the input low
        SetHigh(DataIn) -- when the bit is high, set the input high
      end
      SetHigh(Clock) -- this changes makes the shift-register read the data-bit
      SetLow(DataIn) -- reset the data-bit
    end -- repeat for all 8 bits
    SetLow(Clock) -- finishes the last read
    SetHigh(Latch) -- Open the latch, this makes the input available on the output pins
  end

  ShiftOut({1,0,0,1,1,1,0,1})

  print ("GpioTest ended")

return commandArray
You can see what the script is sending by the green, blue and white leds:
Leds4.jpg
Leds4.jpg (212.03 KiB) Viewed 12505 times
When the blue led is off, the shift register is ready to receive information.
When the white led is on there is a bit with the ON state presented. When the white led is OFF, there is a bit with the OFF state presented.
When the green led turns ON, the state as indicated by the white led is read and processed by the shift-register.
This is repeated 8 times.
After that you will see the blue led turn on. At that moment, the input-bits are copied to the output-pins of the shift register.

Well, my next challenge is to find a way to set Domoticz switches with an interval of 1 milli-second.
If you would be the one that knows how to achieve this... please tell me.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Use GPIO pins from LUA ... This way !

Post by mjdb »

And I found the method to set gpio-pins directly from LUA. If you know it, it is soooo simple.

On the command line you can set a gpio-pin with a command: gpio write <pin> 1 or off gpio write <pin> 0

And, since you can execute shell-commands with the LUA command os.execute(<command>) you can set gpio-pins with the LUA command:

os.execute('gpio write <pin> 1')

You don't need to to export the gpio-pins from domoticz.sh or define gpio-switches in Domoticz.

Furthermore, the overhead from the os.execute is enough to give the shift-register time to process the new setting before a new gpio-command is executed.

I modified my demo-program. It uses the current Minute and Hour to a sort-of binary representation to show that the bits are really changing.

Please, be aware that the pins on the Raspberry Pi are different from the previous example.

Code: Select all

-- GpioTest

commandArray = {}

  -- In this script I use variables that indicate the purpose of the switches:
  local LatchGpio = 21 -- pin 29 on Raspberry Pi
  local ClockGpio = 22 -- pin 31 on Raspberry Pi
  local DataGpio =  23 -- pin 33 on Raspberry Pi

  -- This example uses a shift-register that has 8 outputs.
  -- It is controlled from a Raspberry Pi with 3 gpio-pins.

  -- Pins on the 74x595 shift register
  --                __ __ 
  -- pin 1, Out 2  |  U  |  pin 16, Vcc
  -- pin 2, Out 3  |     |  pin 15, Out 1
  -- pin 3, Out 4  |     |  pin 14, Data in
  -- pin 4, Out 5  |     |  pin 13, Gnd
  -- pin 5, Out 6  |     |  pin 12, Latch
  -- pin 6, Out 7  |     |  pin 11, Clock
  -- pin 7, Out 8  |     |  pin 10, Vcc
  -- pin 8, Gnd    |_____|  pin 9,  Data out

  -- 'My' colors for the test
  -- Latch; pin 12 on the 74x595, purple
  -- Clock; pin 11 on the 74x595, green
  -- DataIn; pin 14 on the 74x595, brown

  -- Connect pins 16 and 10 to power/plus/Vcc
  -- Connect pins 8 and 13 to ground/minus

  -- The shift register is powered by 3,3 volt.
  -- Otherwise the output-signals of the Raspberry Pi Pins (also 3,3 volt)
  -- would not be recognized as high.
  
  -- If you use two (or more) shift-registers, connect pin 9
  -- to pin 14 of the next chip.
  -- Also connect pin 12 to pin 12 and pin 11 to pin 11.

  print ("GpioTest started")
  
  function SetGpioLow(Gpio)
    local Cmd = 'gpio write ' .. tonumber(Gpio) .. ' 0'
    os.execute(Cmd)
  end

  function SetGpioHigh(Gpio)
    local Cmd = 'gpio write ' .. tonumber(Gpio) .. ' 1'  
    os.execute(Cmd)
  end

  SetGpioLow(LatchGpio) -- Freeses the current state of the shift-register
  
  function ShiftOut(DataArray)
    -- ShiftOut is used like: ShiftOut({bit1, bit2, bit3, bit4, bit5, bit6, bit7, bit8})
    -- The bits are in fact integer values 0 or 1.
    -- Example: ShiftOut({1, 0, 0, 1, 1, 1, 0, 1})
    SetGpioLow(ClockGpio) -- start clean
    SetGpioLow(DataGpio) -- no data yet
    SetGpioLow(LatchGpio) -- Close the Latch, this freeses the previous data
    for i = 8,1,-1 do
      -- the last bit is processed first, so it can shift to the end
      SetGpioLow(ClockGpio) -- prepare for receiving a new bit
      if (DataArray[i] == 0) then
        SetGpioLow(DataGpio) -- when the bit is low, set the input low
      else
        -- when the bit is low, set the input low
        SetGpioHigh(DataGpio) -- when the bit is high, set the input high
      end
      SetGpioHigh(ClockGpio) -- this change makes the shift-register read the data-bit
      SetGpioLow(DataGpio) -- reset the data-bit
    end -- repeat for all 8 bits
    SetGpioLow(ClockGpio) -- finishes the last read
    SetGpioHigh(LatchGpio) -- Open the latch, this makes the input available on the output pins
  end

  -- the function bitand is used only for the example
  -- to get a binary representation of the time
  function bitand(a, b)
    local result = 0
    local bitval = 1
    while (a > 0) and (b > 0) do
      if (a % 2 == 1) and (b % 2 == 1) then -- test the rightmost bits
          result = result + bitval      -- set the current bit
      end
      bitval = bitval * 2 -- shift left
      a = math.floor(a/2) -- shift right
      b = math.floor(b/2)
    end
    return result
  end

  local CurrMin = tonumber(os.date("%M"));
  local CurrHour = tonumber(os.date("%H"));

  ShiftOut({bitand(CurrMin, 1), bitand(CurrMin, 2), bitand(CurrMin, 4), bitand(CurrMin, 8), bitand(CurrMin, 16), bitand(CurrMin, 32), bitand(CurrHour, 1), bitand(CurrHour, 2)})

  print ("GpioTest ended")

return commandArray
Thank you all that have contributed to reach this point.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Re: Use GPIO pins from LUA. How?

Post by mjdb »

Just to show, this is my led-panel.

Each led shows the status of a light, activity, door or window, machine etc. on three floors.
The (bigger) leds at the bottom show communication with Domoticz. The thingy is powered by a Raspberry Pi Zero W with Domoticz. The small black blocks are the shift-registers.
LedPanelSmall.jpg
LedPanelSmall.jpg (95.33 KiB) Viewed 12141 times
For the foto I used the test-button. It puts all leds on to see if they are functioning.

The "connection" from the Raspberry Pi to the other electronic stuff is done by opto-couplers. From the RaspPi-section to the rest there is no electrical connection.
OptoCouplers.jpg
OptoCouplers.jpg (37.09 KiB) Viewed 12141 times
Some people asked me why I need this. Well I don't.
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
ikkezelf
Posts: 5
Joined: Thursday 01 October 2020 12:23
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Use GPIO pins from LUA. How?

Post by ikkezelf »

I see 2017, but very nice. I love to see you accomplishing this.
there is no question why.. just because it's possible.

Any progress and pictures of the end result?
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests