The init_led() method simply creates a GPIOZero PWMLED instance and assigns it to the global led variable that we saw previously:Â
def init_led():
"""Create and initialize an PWMLED Object"""
global led
led = PWMLED(LED_GPIO_PIN)
led.value = state['level'] / 100 # (7)
We explicitly set the LED's brightness to match the value of our server's brightness state on line (7) to ensure the server's managed state and the LED are in sync when the server starts. We are dividing by 100 because led.value expects a float value in the range of 0-1, while our API will be using an integer in the range 0-100.
Next, we start to see the code that defines our server and its service endpoints, starting with the code that serves the web page we visited earlier.