1717import binascii
1818import json
1919import board
20- import busio
2120from adafruit_pyportal import PyPortal
22- from digitalio import DigitalInOut
23- from adafruit_esp32spi import adafruit_esp32spi
2421import adafruit_esp32spi .adafruit_esp32spi_requests as requests
25- import adafruit_logging as logging
2622
2723username = 'codewisdom'
2824
29- logger = logging .getLogger ('main' )
30- logger .setLevel (logging .DEBUG )
3125try :
3226 from secrets import secrets
3327except ImportError :
34- logger . critical ("""WiFi settings are kept in secrets.py, please add them there!
28+ print ("""WiFi settings are kept in secrets.py, please add them there!
3529the secrets dictionary must contain 'ssid' and 'password' at a minimum""" )
3630 raise
3731
3832def halt_and_catch_fire (message , * args ):
3933 """Log a critical error and stall the system."""
40- logger . critical (message , * args )
34+ print (message % args )
4135 while True :
4236 pass
4337
44- def connect_esp ():
45- """Connect the ESP to the network."""
46- pyportal .neo_status ((0 , 0 , 100 ))
47- while not esp .is_connected :
48- # secrets dictionary must contain 'ssid' and 'password' at a minimum
49- if secrets ['ssid' ] == 'CHANGE ME' or secrets ['ssid' ] == 'CHANGE ME' :
50- change_me = "\n " + "*" * 45
51- change_me += "\n Please update the 'secrets.py' file on your\n "
52- change_me += "CIRCUITPY drive to include your local WiFi\n "
53- change_me += "access point SSID name in 'ssid' and SSID\n "
54- change_me += "password in 'password'. Then save to reload!\n "
55- change_me += "*" * 45
56- raise OSError (change_me )
57- pyportal .neo_status ((100 , 0 , 0 )) # red = not connected
58- try :
59- esp .connect (secrets )
60- except RuntimeError as error :
61- logger .error ("Could not connect to internet: %s" , error )
62- logger .error ("Retrying in 3 seconds..." )
63- time .sleep (3 )
64-
65- #pylint:disable=redefined-outer-name
6638def get_bearer_token ():
6739 """Get the bearer authentication token from twitter."""
6840 raw_key = secrets ['twitter_api_key' ] + ':' + secrets ['twitter_secret_key' ]
@@ -77,57 +49,36 @@ def get_bearer_token():
7749 if response_dict ['token_type' ] != 'bearer' :
7850 halt_and_catch_fire ('Wrong token type from twitter: %s' , response_dict ['token_type' ])
7951 return response_dict ['access_token' ]
80- #pylint:enable=redefined-outer-name
81-
82- #setup esp interface
83- esp32_ready = DigitalInOut (board .ESP_BUSY )
84- esp32_gpio0 = DigitalInOut (board .ESP_GPIO0 )
85- esp32_reset = DigitalInOut (board .ESP_RESET )
86- esp32_cs = DigitalInOut (board .ESP_CS )
87- spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
88- esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready ,
89- esp32_reset , esp32_gpio0 )
9052
9153# determine the current working directory
9254# needed so we know where to find files
9355cwd = ("/" + __file__ ).rsplit ('/' , 1 )[0 ]
56+ url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=1&screen_name=' + username
9457
9558
9659# Initialize the pyportal object and let us know what data to fetch and where
9760# to display it
98- pyportal = PyPortal (status_neopixel = board .NEOPIXEL ,
61+ pyportal = PyPortal (url = url ,
62+ json_path = (0 , 'text' ),
63+ status_neopixel = board .NEOPIXEL ,
9964 default_bg = cwd + '/twitter_background.bmp' ,
100- external_spi = spi ,
101- esp = esp ,
65+ # external_spi=spi,
66+ # esp=esp,
10267 text_font = cwd + '/fonts/Helvetica-Bold-16.bdf' ,
10368 text_position = (20 , 60 ),
10469 text_color = 0xFFFFFF ,
70+ text_wrap = 35 ,
10571 caption_text = '@' + username ,
10672 caption_font = cwd + '/fonts/Helvetica-Bold-16.bdf' ,
10773 caption_position = (5 , 210 ),
10874 caption_color = 0x808080 )
10975
110- logger .debug ("Connecting to AP %s..." , secrets ['ssid' ])
111- connect_esp ()
112- logger .debug ('Connected' )
113-
114- logger .debug ('Getting bearer token...' )
11576bearer_token = get_bearer_token ()
116- logger .debug ('Got it' )
11777
118- headers = {'Authorization' : 'Bearer ' + bearer_token }
78+ #pylint:disable=protected-access
79+ pyportal ._headers = {'Authorization' : 'Bearer ' + bearer_token }
80+ #pylint:enable=protected-access
11981
120- url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=1&screen_name=' + username
12182while True :
122- logger .debug ('fetching latest tweet from @%s' , username )
123- response = requests .get (url ,
124- headers = headers )
125- if response .status_code != 200 :
126- logger .error ('Tweet fetch status: %d' , response .status_code )
127- else :
128- tweet = json .loads (response .content )[0 ]['text' ]
129- logger .debug ('Tweet: %s' , tweet )
130- lines = PyPortal .wrap_nicely (tweet , 35 )
131- pyportal .set_text ('\n ' .join (lines ))
132- # check every hour
133- time .sleep (3600 )
83+ pyportal .fetch ()
84+ time .sleep (3600 ) # check every hour
0 commit comments