Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Published © CC BY-NC-SA

The Google Trends Powered Christmas Tree

Want to know how trendy Christmas is? Find out with this Google trends powered Christmas tree! Party mode included.

BeginnerFull instructions provided20 hours724
The Google Trends Powered Christmas Tree

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Flora RGB Neopixel LEDs- Pack of 4
Adafruit Flora RGB Neopixel LEDs- Pack of 4
×2
Big Red Dome Button
SparkFun Big Red Dome Button
×1
Adafruit Mini External USB Stereo Speaker
×1
Pipe Cleaner
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
Google Trends

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

GiftBox

GiftLid

Code

Code

Python
import board
import neopixel
import time
from pprint import pprint
from pytrends.request import TrendReq
import datetime
import threading
import os
import RPi.GPIO as GPIO

# Button Setup
buttonPin = 21
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# On a Raspberry pi, use this instead, not all pins are supported
pixel_pin = board.D12

# The number of NeoPixels
num_pixels = 8

# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neopixel.RGB
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.9, auto_write=False, pixel_order=ORDER)

# Google trends setup
pytrends = TrendReq(hl='en-US', tz=0)
kw_list = ["christmas"]
pytrends.build_payload(kw_list)

# Some cross thread variables
global currentTrendValue
currentTrendValue = 10

global partyMode
partyMode = False


global ledSleep
ledSleep = 1

def getTrendData():

    while True:

        global currentTrendValue
        now = datetime.datetime.now()

        trendData = pytrends.get_historical_interest(kw_list, year_start=now.year, month_start=now.month, day_start=now.day, hour_start=now.hour, 
            year_end=now.year, month_end=now.month, day_end=now.day, hour_end=now.hour, cat=0 ,sleep=0)

        trendValue = int(trendData.get('Christmas').values[0])

        currentTrendValue = trendValue

        time.sleep(3600)

def checkStatus():

    global partyMode
    global ledSleep

    if partyMode:
        ledSleep = 0.01                          
    else:   
        global currentTrendValue
        ledSleep = (1/currentTrendValue) * 10


def controlLeds():

    red = (0,255,0)
    green = (255, 0,0)
    gold = (255,255,0)

    while True:

        for i in range(1,num_pixels+1):
            pixels[i-1] = red
            pixels.show() 
            time.sleep(ledSleep)
            checkStatus()

        for i in range(1,num_pixels+1):
            pixels[i-1] = green
            pixels.show() 
            time.sleep(ledSleep)
            checkStatus()

        for i in range(1,num_pixels+1):
            pixels[i-1] = gold
            pixels.show() 
            time.sleep(ledSleep)
            checkStatus()

def playPartyMusic():

    name = '/home/pi/Desktop/twittertree/audio/realSong.mp3'

    os.system('mpg123 ' + name)
    time.sleep(0.2)

def checkButton():
    
    global partyMode

    while True:
        if GPIO.input(buttonPin) == GPIO.LOW:
            print("Party!")
            partyMode = True
            playPartyMusic()
            partyMode = False




trendDataThread = threading.Thread(target=getTrendData)
trendDataThread .start()

ledControlThread = threading.Thread(target=controlLeds)
ledControlThread.start()

checkButtonThread = threading.Thread(target=checkButton)
checkButtonThread.start()

Credits

Comments