---
# System prepended metadata

title: Raspberry - Python led RGB et bouton

---

# Raspberry - Python led RGB et bouton

```
# Import des librairies
import RPi.GPIO as GPIO
from time import sleep
import urllib

# Assignation des pins
greenpin = 12
redpin = 32
bluepin = 33
buttonpin = 7

# Initialisation
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(buttonpin,GPIO.IN)
GPIO.setup(greenpin,GPIO.OUT)
GPIO.setup(redpin,GPIO.OUT)
GPIO.setup(bluepin,GPIO.OUT)
red = GPIO.PWM(redpin,100)
red.start(0)
blue = GPIO.PWM(bluepin,100)
blue.start(0)
green = GPIO.PWM(greenpin,100)
green.start(0)

# Boucle
while True:
    # Récupération des valeurs
	page=urllib.urlopen('http://195.83.128.21/~dev1905/API/recup_red.php')
	redstatut=page.read()
	page=urllib.urlopen('http://195.83.128.21/~dev1905/API/recup_green.php')
	greenstatut=page.read()
	page=urllib.urlopen('http://195.83.128.21/~dev1905/API/recup_blue.php')
	bluestatut=page.read()
	redstatut=float(redstatut)
	bluestatut=float(bluestatut)
	greenstatut=float(greenstatut)
    # Changement de l'intensité
	red.ChangeDutyCycle(redstatut)
	blue.ChangeDutyCycle(bluestatut)
	green.ChangeDutyCycle(greenstatut)
    # Lecture du pin du bouton
	buttonstatut=GPIO.input(buttonpin)
    # Si le bouton est activé
	if buttonstatut == 1:
		urllib.urlopen('http://195.83.128.21/~dev1905/API/button.php?statut=1')
    # Si le bouton est désactivé
	else:
		urllib.urlopen('http://195.83.128.21/~dev1905/API/button.php?statut=0')
	sleep(1)

```