I am trying to controll motors and a servos connected to a Raspberry Pi Zero 2w from a windows machine via python. I dont have much expierience with RasPi but I managed to get an Led to blink via remote GPIO, changed the code a little bit so my H-Bridge turned the motor:
from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep
factory = PiGPIOFactory(host='') #IP is cut out for publishing
led1 = LED(17, pin_factory=factory)
led2 = LED(27, pin_factory=factory)
def forward():
led2.off()
led1.on()
def backward():
led1.off()
led2.on()
def stop():
led1.off()
led2.off()
while True:
forward()
sleep(1)
stop()
sleep(1)
backward()
sleep(1)
This works without problems. Then I found out gpiozero offers a Motor class. I tryed this:
from gpiozero import Motor
from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep
factory = PiGPIOFactory(host='') #IP is cut out for publishing
motor1 = Motor(17, 27, pin_factory=factory)
while True:
motor1.forward()
sleep(3)
motor1.stop()
sleep(1)
motor1.backward()
sleep(1)
This doesn`t work. Following is the output:
C:\Users\gerre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\gpiozero\devices.py:288: PinFactoryFallback: Falling back from rpigpio: No module named 'RPi'
warnings.warn(
C:\Users\gerre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\gpiozero\devices.py:288: PinFactoryFallback: Falling back from lgpio: No module named 'lgpio'
warnings.warn(
C:\Users\gerre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\gpiozero\devices.py:288: PinFactoryFallback: Falling back from rpio: No module named 'RPIO' warnings.warn(
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at localhost(8888)
Did you start the pigpio daemon? E.g. sudo pigpiod
Did you specify the correct Pi host/port in the environment
variables PIGPIO_ADDR/PIGPIO_PORT?
E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888
Did you specify the correct Pi host/port in the
pigpio.pi() function? E.g. pigpio.pi('soft', 8888)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C:\Users\gerre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\gpiozero\devices.py:288: PinFactoryFallback: Falling back from pigpio: failed to connect to localhost:8888
warnings.warn(
C:\Users\gerre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\gpiozero\devices.py:288: PinFactoryFallback: Falling back from native: [Errno 2] No such file or directory: '/proc/cpuinfo'
warnings.warn(
Traceback (most recent call last):
File "[cut out]\RemoteMotor.py", line 7, in <module>
motor1 = Motor(17, 27, pin_factory=factory)
File "C:\Users\gerre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\gpiozero\devices.py", line 108, in __call__
self = super(GPIOMeta, cls).__call__(*args, **kwargs)
File "C:\Users\gerre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\gpiozero\output_devices.py", line 1223, in __init__
super(Motor, self).__init__(_order=devices.keys(), **devices)
File "C:\Users\gerre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\gpiozero\mixins.py", line 85, in __init__
super(SourceMixin, self).__init__(*args, **kwargs)
File "C:\Users\gerre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\gpiozero\devices.py", line 432, in __init__
super(CompositeDevice, self).__init__(pin_factory=pin_factory)
File "C:\Users\gerre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\gpiozero\devices.py", line 250, in __init__
Device.pin_factory = Device._default_pin_factory()
File "C:\Users\gerre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\gpiozero\devices.py", line 291, in _default_pin_factory
raise BadPinFactory('Unable to load any default pin factory!')
gpiozero.exc.BadPinFactory: Unable to load any default pin factory!
What am I missing? I dont see why using LED works and Motor doesn´t.
Edit: Servo works fine, too. Only Motor has issues.