2

Asking the other day this question I discovered a new fascinating world, the gpio zero library.

My first code was this (an example to blink a led):

from gpiozero import LED
from time import sleep

red = LED(21)

while True:
    red.on()
    sleep(1)
    red.off()
    sleep(1)

It works when I run it on my raspberry.

But the purpose to learn to work with this new library is to control my gpios remotely from my laptop. And to do this, they explain it here.

But I don't understand how to do it, because they said here that I only have to write this on my laptop (windows, pycharm) program:

PIGPIO_ADDR=192.168.1.3 python3 led.py

When I run it, appears the error:

SyntaxError: invalid syntax

It's obviously I am missing things, and I'm sure it's easy but I really don't know what can I try, and I don't find to much info.

(I don't know if it is important but my code led.py is /home/pi)

2

PIGPIO_ADDR=192.168.1.3 python3 led.py is a terminal command. If you're using PyCharm on Windows, you need to either:

  1. Set an environment variable within PyCharm
  2. Set an environment variable within Windows
  3. Use another approach, set the pin factory within the code:
from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep

pi = PiGPIOFactory('192.168.1.3')

red = LED(21, pin_factory=pi)

while True:
    red.on()
    sleep(1)
    red.off()
    sleep(1)

or:

from gpiozero import Device, LED
from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep

Device.pin_factory = PiGPIOFactory('192.168.1.3')  # set the default pin factory

red = LED(21)

while True:
    red.on()
    sleep(1)
    red.off()
    sleep(1)
  • Sorry for the delay, one more question, the variables that I have to add in pycharm and windows configurations is my variable called red? In the windows conf I had to put a path in the value, this path is just the path of my directory? Thank you :) – NEA Jun 16 at 9:41
  • No, the environment variable is PIGPIO_ADDR=192.168.1.3 and that's outside of Python. Alternatively, don't use environment variables and just do one of the code examples in my #3. – ben_nuttall Jun 16 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.