The following global variables are used throughout our server. First, we have the GPIO pin and an led variable, which will later be assigned a GPIOZero PWMLED instance for controlling our LED:Â
# Global variables
LED_GPIO_PIN = 21
led = None # PWMLED Instance. See init_led()
state = { # (6)
'level': 50 # % brightness of LED.
}
On line (6), we have a state dictionary structure that we will use to track the brightness level of our LED. We could have used a simple variable instead but have opted for a dictionary structure since it's a more versatile option because it will be marshaled into JSON to send back to a client, as we will see later on.
Next, we create and initialize our led instance.