简体   繁体   中英

Using RPi.GPIO with django

I have django project set on my pc and copy of that project on Raspberry pi 3. I deploy changes through pyCharm to raspi. Raspberry is my server where I host my website. I wanted to play with led light through web app. In my app I've imported import RPI.GPIO as GPIO but after server run there was ImportError: No module named 'RPi' . I've managed to install only gpio on pc (No matching distribution found for RPi), but there is still ImportError : No module named 'gpio' . Here is my code in views.py

import gpio
LED_PIN = 18
def turnOn(request):
    gpio.setmode(gpio.BOARD)
    gpio.output(LED_PIN, 1)
    return HttpResponse('')

Is there any possibility to use RPi.GPIO in django on pc?

RPi.GPIO is Raspberry-specific and you really cannot use it on your computer - it even has no GPIO ports. You should deploy your code to RPi and use it there.

I found a solution. With help of wiringPi and subprocess I could execute command.

def turnOn(request):
subprocess.call(['gpio', '-g', 'mode', '3', 'out'])
subprocess.call(['gpio', '-g', 'write', '3', '1'])
return HttpResponse('')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM