Python Forum
Absolute newbie asking for help with blinking LEDs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Absolute newbie asking for help with blinking LEDs
#1
Hello Forum,
(edit: Added emphasis so this is actually a question instead of a ramble.)

to set the stage: I'm an absolute python newbie (Really, used it for the first time about a week ago) and I am also not a person that is in forums very much, so I apologize in advance if I break any unwritten rules!

Situation: As a novelty gift I was planning to build a notifier that blinks an LED when a new subscriber is added to the YouTube or Instagram account of my better half. That notifier is on a Pi Zero W with the Pimoroni Blinkt LED HAT. I figured out how to make it blink with the provided examples and also how to edit those examples. Yay me! Then I figured "Can't be that hard to have it check subscribers and blink red for new YT subscribers and green for IG followers!" Ohhh boy.
Long story short: With some help from a nice person I made up the stuff below and it actually does show the correct numbers for subs/follows in my SSH terminal window, it also blinks the leds ONCE on start. It does not, however, blink them when the subs/followers increase. How do I fix this?

Could you people have a quick look and tell me what my (probably very beginner-) mistake is? I would very much appreciate the help!
(Also: If you want to just chime in with "this is not how you are supposed to learn this and you totally bit of more than you can chew!" go ahead, I totally agree with you! You're soooo right! If you have a good beginner course you want to recommend I'd also be thankful :-) )

Code for blinking... Blinkt?
import blinkt
import time
# ----------------------------------------
blink_ontime = 2


def notify_new_sub_yt():

    # blink RED for YouTube
    blinkt.set_all(255, 0, 0)
    blinkt.show()
    time.sleep(int(blink_ontime))
    blinkt.set_all(0, 0, 0)
    blinkt.show()


def notify_new_follower_insta():

    # blink GREEN for InstaGram
    blinkt.set_all(0, 255, 0)
    blinkt.show()
    time.sleep(int(blink_ontime))
    blinkt.set_all(0, 0, 0)
    blinkt.show()
And the code that actually is checking for the counts(note: I named the LED ibrary "pOmoroni", but I carried that typo over everywhere, so that should be fine, right?):
import json
import requests

from threading import *
import time
from PomoroniBlinkt_blink import *
# -------------------------------Global Variables----------------------------------------
YT_followers_count = 0
insta_followers_count = 0
YT_followers_count_old = 0
insta_followers_count_old = 0

# ------------------------Youtube Credentials------------------------------
GOOGLE_API_KEY = "fill_in_your_api_key"
YOUTUBE_CHANNEL_ID = "your_channel_ID"

# ----------------------Instagram Credentials------------------------------
# INSTA_ACCESS_TOKEN = "fill_in"
INSTA_ACCESS_TOKEN = "fill_in"  
INSTA_ID = "fill_in"  

# ----------------Main Thread to check followers/subscription---------------------------


def followers_count():
    global YT_followers_count
    global insta_followers_count
    global YT_followers_count_old
    global insta_followers_count_old
    # -------------------------------------API CALLS----------------------------------------------------
    yt_url = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" + YOUTUBE_CHANNEL_ID + \
             "&fields=items/statistics/subscriberCount&key=" + GOOGLE_API_KEY
    insta_url = "https://api.instagram.com/v1/users/"+INSTA_ID+"/?access_token="+INSTA_ACCESS_TOKEN

    while True:
        # ---------------------Get data from API endpoint------------------------------------------------------
        yt_response = requests.get(yt_url)
        insta_response = requests.get(insta_url)
        # ----------------------format in json-----------------------------------------------------------
        insta_data = json.loads(insta_response.content)
        YT_data = json.loads(yt_response.content)
        # -----------------------Check if API request is successful------------------------------------
        if yt_response.status_code == 200:
            YT_followers_count = int(YT_data['items'][0]['statistics']['subscriberCount'])
            print "Youtube followers are "+str(YT_followers_count)

        if insta_response.status_code == 200:
            insta_followers_count = int(insta_data['data']['counts']['followed_by'])
            print "Instagram followers are " + str(insta_followers_count)
        # ------------------------Check if there is a new subscriber/follower----------------------------
        if YT_followers_count - YT_followers_count_old > 0:
            notify_new_sub_yt()
            YT_followers_count_old = YT_followers_count

        if insta_followers_count - insta_followers_count_old > 0:
            notify_new_follower_insta()
            insta_followers_count_old = insta_followers_count

        time.sleep(300)
# -------------------------------------------------------------------------------------------------------------


def main():
    t1 = Thread(target=followers_count)
    t1.daemon = True
    t1.start()
    while True:
        time.sleep(1)


if __name__ == "__main__":
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute paths in subprocess - file not found kittyticker 4 397 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  functional LEDs in an array or list? // RPi user Doczu 5 1,521 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  Make these LEDs stay on duckredbeard 5 2,913 Apr-08-2021, 10:26 PM
Last Post: duckredbeard
  automatically get absolute paths oclmedyb 1 2,062 Mar-11-2021, 04:31 PM
Last Post: deanhystad
  Absolute beginner am I missing something? kamren 7 3,107 Sep-25-2020, 05:32 AM
Last Post: buran
  chkFile with absolute paths JarredAwesome 7 2,912 Sep-21-2020, 03:51 AM
Last Post: bowlofred
  absolute imports between folders mikisDW 0 1,500 Aug-05-2020, 12:26 PM
Last Post: mikisDW
  I am an absolute newbie.Help me! feynarun 4 2,440 Apr-11-2020, 09:25 PM
Last Post: feynarun
  Orange PI Win steering LEDs connect to GPIO djmcg 0 1,503 Dec-27-2019, 08:26 AM
Last Post: djmcg
  Absolute Beginner Python Question matchamochi7 5 3,603 Nov-02-2018, 12:09 PM
Last Post: wookie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020