# Controlling GPIO on a cluster machine

**URL:** https://discourse.julialang.org/t/controlling-gpio-on-a-cluster-machine/5516
**Category:** Julia at Scale
**Created:** [August 22, 2017, 10:28pm UTC](https://discourse.julialang.org/t/controlling-gpio-on-a-cluster-machine/5516 "2017-08-22T22:28:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)
#### Post date: [August 22, 2017, 10:28pm UTC](https://discourse.julialang.org/t/controlling-gpio-on-a-cluster-machine/5516/1 "2017-08-22T22:28:28Z")

</div>

Hi,

I have several development boards set up in a Beowulf cluster [with passwordless ssh] along with my desktop machine (a Dell Optiplex 755). I have 4 nodes: NODE-DELL755 [which is the master], NODE-RPI2 [a Raspberry Pi 2], NODE-BBB [a Beaglebone Black], and NODE-UDOOX86 [a Udoo x86]. I am able to control the GPIO on the Raspberry Pi with the following code:

rpi = addprocs([“julia-user@NODE-RPI2”],dir=“/home/julia-user/julia-0.6.0/bin/”)

include(“GPIO\_Module.jl”)

import GPIO  
using GPIO

# 17 is the GPIO for an LED and 27 is the GPIO for a PIR sensor

remotecall(blink\_led\_sensor2, rpi[1], “17”, “27”)

So, if the PIR sensor sees movement - it blinks and LED 5 times and breaks out of the loop. See my question at the bottom.

The GPIO\_Module looks like:

@everywhere module GPIO

export export\_pin, unexport\_pin, setdirection\_pin, setvalue\_pin, getvalue\_pin, blink\_led, blink\_led\_sensor, blink\_led\_sensor2

function export\_pin(pin::String)  
export\_str = “/sys/class/gpio/export”  
f = open(export\_str,“w”)  
write(f, pin)  
close(f)  
end

function unexport\_pin(pin::String)  
unexport\_str = “/sys/class/gpio/unexport”  
f = open(unexport\_str, “w”)  
write(f, pin)  
close(f)  
end

function setdirection\_pin(pin::String, dir::String)  
setdir\_str = “/sys/class/gpio/gpio” \* pin \* “/direction”  
f = open(setdir\_str, “w”)  
write(f, dir)  
close(f)  
end

function getvalue\_pin(pin::String)  
setval\_str = “/sys/class/gpio/gpio” \* pin \* “/value”  
f = open(setval\_str, “r”)  
val::String = readline(f)  
close(f)  
return val  
end

function setvalue\_pin(pin::String, val::String)  
setval\_str = “/sys/class/gpio/gpio” \* pin \* “/value”  
f = open(setval\_str, “w”)  
write(f, val)  
close(f)  
end

function blink\_led(node::Int, pin::String, howMany::Int)  
remotecall\_fetch(export\_pin, node, pin)  
remotecall\_fetch(setdirection\_pin, node, pin, “out”)  
for n = 1:howMany  
remotecall\_fetch(setvalue\_pin, node, pin, “1”)  
sleep(1)  
remotecall\_fetch(setvalue\_pin, node, pin, “0”)  
sleep(1)  
end  
remotecall\_fetch(unexport\_pin, node, pin)  
end

function blink\_led\_sensor(node::Int, pinLED::String, pinSensor::String)  
remotecall\_fetch(export\_pin, node, pinLED)  
sleep(1)  
remotecall\_fetch(setdirection\_pin, node, pinLED, “out”)

```
remotecall_fetch(export_pin, node, pinSensor)
sleep(1)
remotecall_fetch(setdirection_pin, node, pinSensor, "in")

while (true)
    ret = remotecall_fetch(getvalue_pin, node, pinSensor)  
    sleep(1)
    if ret == "1"
        for n = 1:5
            remotecall_fetch(setvalue_pin, node, pinLED, "1")
            sleep(1)
            remotecall_fetch(setvalue_pin, node, pinLED, "0")
            sleep(1)
        end
        break
    end
end
remotecall_fetch(unexport_pin, node, pinLED)
sleep(1)
remotecall_fetch(unexport_pin, node, pinSensor)

```

end

function blink\_led\_sensor2(pinLED::String, pinSensor::String)  
export\_pin(pinLED)  
sleep(1)  
setdirection\_pin(pinLED, “out”)

```
export_pin(pinSensor)
sleep(1)
setdirection_pin(pinSensor, "in")

while (true)
    ret::String = getvalue_pin(pinSensor)  
    sleep(1)
    if ret == "1"
        for n = 1:5
            setvalue_pin(pinLED, "1")
            sleep(1)
            setvalue_pin(pinLED, "0")
            sleep(1)
        end
        break
    end
end
unexport_pin(pinLED)
sleep(1)
unexport_pin(node, pinSensor)

```

end

end

My question is: Is there a way to make the remote call communicate back to the “master” Julia environment? What I would like to do is if the PIR sensor “sees” movement to set a variable that the “master” would be able to interrogate and say “hey - something moved in front of the PIR sensor.” And, the routine checking the sensor would never break. It would just run - like a thread.

Is this possible?

Frank

---

<div class="post-metadata">

### Author: ![avik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avik/32/17_2.png) [@avik](https://discourse.julialang.org/u/avik)
#### Post date: [August 22, 2017, 10:54pm UTC](https://discourse.julialang.org/t/controlling-gpio-on-a-cluster-machine/5516/2 "2017-08-22T22:54:09Z")

</div>

Set up a ZMQ based channel between the nodes? [https://github.com/JuliaInterop/ZMQ.jl](https://github.com/JuliaInterop/ZMQ.jl)

---

<div class="post-metadata">

### Author: ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)
#### Post date: [August 22, 2017, 11:23pm UTC](https://discourse.julialang.org/t/controlling-gpio-on-a-cluster-machine/5516/3 "2017-08-22T23:23:32Z")

</div>

I’ve done that with sockets in Java and C++, but I was wondering if there was a way using Julia’s remote procs or tasks or something like that. Thanks.
