-
1Step 1
@realDonaldTrump Latest Twitter Feed on Inky WHAT E-Ink Display.
Latest @realDonaldTrump Tweet using Raspberry Pi B3+ and inkywHAT e-ink display
Instructions
Requirements
For this project you will need:
- ● Raspberry Pi B3+
- ● Pimoroni Inky WHAT.
- ● Keyboard/Mouse/Monitor to set everything up. You may need HDMI and micro USB adaptors.
- ● Micro SD power supply.
- ● MicroSD card. 16GB
- ● MicroSD card reader
- ● register with Twitter to create keys to use their API.
- Initial Setup:
- I got this working in Python 2,
- 1. download an image of Raspbian Stretch. I used the one dated “raspbian-2019-04-09”. Download the 1.1GB zip file.
- 2. Format your micro SD card with https://www.sdcard.org/downloads/formatter/
- 3. Use the free balenaEtcher to “flash” your zipped image onto a blank micro SD card. Slot your microSD card into your Pi Zero.
- 4. Plug your Raspberry Pi B3+ into a monitor and keyboard, and go through the steps of connecting your Pi B3+ to a WiFi network. Run updates as prompted.
- 5. To use the Inky WHAT, you need to turn on the Pi’s I2C interface. To do this, from a command prompt type:
- sudo raspi-config
- Navigate to I2C and press enter. Turn it ON. And then Finish.
- 6. Shut down your Pi and you can fit your Inky WHAT to the Pi’s GPIO board
- 7. Boot the Pi back up,
- 8. Install the software for the Inky WHAT. From a command prompt type:
- curl https://get.pimoroni.com/inky | bash
- Follow the prompts all the way through. It can take a while to install this code so be patient.
- 9. Check that everything is working, you might want to follow the instructions on Pimoroni’s tutorial page, to install their quote generator. This way you can be sure that the inWHAT software library is properly installed and all is well with the device.
- 10. Go to Twitter’s development site, you need to “Create an app” and give it a few details. They want to know a little about what you’re doing so fill in details as needed. We also really only need “Read” permission.
- The important things that we need from here are details of the tokens. These need to kept secret, so don’t post them anywhere! Someone might use them to post as you if you’re not careful.
- There are four long alphanumeric strings you need:
- ● Consumer API key
- ● Consumer API secret key
- ● Access token
- ● Access secret token
- Save these somewhere safe, as you will need them in your Python code.
- You also need to import a Twitter library into your Pi:
- 11. Install the python-twitter library. From the command prompt type:
- pip install python-twitter
- Coding:
- The following code can be downloaded as a plain text file on the project page: https://cdn.hackaday.io/files/1671437151046112/wHAT_rasppi_code.txt
- import sys,twitter
- import datetime
- api = twitter.Api()
- # Populate your twitter API details below, replacing
- # CONSUMER_KEY_HERE etc with your details from Twitter
- consumer_key = ‘REDACTED, INSERT YOUR KEY HERE’
- consumer_secret = ‘REDACTED, INSERT YOUR KEY HERE’
- access_token_key = 'REDACTED, INSERT YOUR KEY HERE’'
- access_token_secret = 'REDACTED, INSERT YOUR KEY HERE’'
- api = twitter.Api(
- consumer_key=consumer_key,
- consumer_secret=consumer_secret,
- access_token_key=access_token_key,
- access_token_secret=access_token_secret
- )
- # Setting up the Inky WHAT
- from inky import InkyWHAT
- inky_display = InkyWHAT("black")
- inky_display.set_border(inky_display.WHITE)
- from PIL import Image, ImageFont, ImageDraw
- img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT))
- draw = ImageDraw.Draw(img)
- font = ImageFont.truetype("/home/pi/arial font/Arial.ttf", 18)
- font_small = ImageFont.truetype("/home/pi/arial font/Arial.ttf", 14)
- def reflow_tweet(quote, width, font):
- words = quote.split(" ")
- reflowed = ' '
- line_length = 0
- for i in range(len(words)):
- word = words[i] + " "
- word_length = font.getsize(word)[0]
- line_length += word_length
- if line_length < width:
- reflowed += word
- else:
- line_length = word_length
- reflowed = reflowed[:-1] + "\n " + word
- # reflowed = reflowed.rstrip() + '"'
- return reflowed
- # I used the code from here - https://zone13.io/post/python-code-latest-tweet/
- # to grab the most recent Tweet
- # realDonaldTrump
- def realDonaldTrump_tweet(realDonaldTrump):
- statuses = api.GetUserTimeline(screen_name="realDonaldTrump")
- return statuses[0].text
- if __name__ == "__main__":
- realDonaldTrump_latest_tweet = realDonaldTrump_tweet(sys.argv[1] if len(sys.argv) > 1 else 0)
- reflowed_realDonaldTrump_latest_tweet = reflow_tweet(realDonaldTrump_latest_tweet, inky_display.WIDTH, font)
- draw.text((0, 0), "@realDonaldTrump", inky_display.RED, font)
- draw.text((0, 20), reflowed_realDonaldTrump_latest_tweet, inky_display.BLACK, font)
- # The following just inserts the last updated time
- # in the bottom right corner.
- now = datetime.datetime.now()
- tweet_update = "Updated: " + now.strftime("%d-%m-%y %H:%M")
- draw.text((230,285), tweet_update, inky_display.RED, font_small)
- # At this point we finally show the information
- inky_display.set_image(img)
- inky_display.show()
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.