I'm working on a project with a RGB LED which has to indicate what the status of the internet connection is. It is running as a service. I'm focused on two files, one to check if there is an internet connection and one for controlling the LED.
The following code is where we check if there is an internet connection and trying to call the LED function:
import logging from status_server import status_led def testInternetConnection(): logging.info("Testing the internet connection...") while True: try: r = requests.get(https://google.com) # HTTP 200 OK if r.status_code == 200: logging.info("Internet connection is OK!") status_led.turn_green() break except KeyboardInterrupt: sys.exit() except requests.exceptions.RequestException: logging.warning("Re-trying the internet connection....") status_led.turn_red() time.sleep(10)
It is working fine with the following code in the LED file:
import RPi.GPIO as GPIO class _RGBLed: def __init__(self, red_pin, green_pin, blue_pin): self.RED_PIN = red_pin self.GREEN_PIN = green_pin self.init_pins() def init_pins(self): GPIO.setmode(GPIO.BCM) GPIO.setup(self.RED_PIN, GPIO.OUT) GPIO.setup(self.GREEN_PIN, GPIO.OUT) def turn_red(self) GPIO.output(self.RED_PIN, GPIO.HIGH) def turn_green(self): GPIO.output(self.GREEN_PIN, GPIO.HIGH) _rgb_led = _RGBLed(red_pin=26, green_pin=19) def turn_red(): _rgb_led.turn_red() def turn_green(): _rgb_led.turn_green()
The LED is quite bright so I wanted to add
PWM to control the LED and lower the duty cycle. I've tested a bit with
PWM in the Python live environment and that wasn't that hard. I've changed the
turn_red functions with the following:
def turn_green(): turn_green_on = GPIO.PWM(GREEN_PIN, 100) turn_green_on.start(20) def turn_red(): turn_red_led_on = GPIO.PWM(RED_PIN, 100) turn_red_led_on.start(20)
I'm calling those functions from another
When I do this it says
name GREEN_PIN is not defined. When I change
GREEN_PIN to 19 it doesn't give me warnings anymore, but the LED does nothing too. I've checked if the function is called by adding a log statement so that's not the problem either.
Can someone help me with this? Thanks in advance :)