Twitter Enabled Automatic Dice Roller

by hombremagnetico in Circuits > Raspberry Pi

5086 Views, 26 Favorites, 0 Comments

Twitter Enabled Automatic Dice Roller

20161007_183754.jpg
VX-700x420.jpg

The aim of this project is to create an automatic dice roller controlled via Twitter. It uses a Raspberry PI v3 interfaced with a modified dice roller via a relay hat. The code is written in Python, using Open CV and Twython.

Materials Needed

20161007_183345.jpg
20161007_185701.jpg

The hardware needed for this project is:

  • Raspberry PI 3 + PI Camera
  • 115 degrees FOV lens and lens adapter for the PI Camera
  • Raspberry PI case with Go Pro mount
  • Relay HAT
  • Camera tripod + GO Pro mount adapter
  • USB power supply
  • 3 VDC power supply
  • USB light
  • 3 standard dice with black dots (the ones supplied with the dice roller are quite ugly)
  • a battery operated dice roller

Furthermore you'll need a monitor, keyboard and mouse just during the setup phase, in order to align and focus the camera.

I got all my hardware on Amazon, but I think it's easily available on eBay as well...

The software needed for this project is:

  • Python 2.7.12
  • OpenCV 3.1.0
  • Twython 3.4.0

Dice Roller Modification

20161007_183817.jpg
20161007_184103.jpg
20161007_183908.jpg
20161007_190252.jpg

The dice roller need to be modified in order to be powered with a 3 VDC supply and controlled with the relay HAT:

  1. Remove dome and the dome cover
  2. choose a paper cup of a suiting size, and cut it in order to get a tube. fit the cup on the dice roller. In this way you can take a clean picture of the dice from above
  3. Remove batteries
  4. Remove the bottom plate to access the battery and push button terminals
  5. Solder a couple of wires in parallel with the battery leads and a couple of wires in parallel with the push button
  6. Route the wires out of the container and reassemble the bottom plate

Job done!

note:

  • I used a USB connector but the motor is a 3 VDC one. Indeed I'm using a 3 VDC power supply with a USB adaptor.

Picamera Modification

20161007_190921.jpg
20161007_190749.jpg
20161007_190719.jpg
20161007_185921.jpg

The PI Camera is a fixed focus device, while we need to adjust the focus to our needs. So we can upgrade the camera with a standard M12 2.8mm 115ยบ fixed IRIS lens for security CCTV cameras and a lens Holder with 21mm hole spacing. Proceed as follows:

  • use pliers to remove the original lens
  • modify the lens holder in order to fit the camera connector (see picture)
  • screw in the lens
  • modify the camera case (if needed) in order to fit the lens holder

Job done!

Relay HAT Connection

20161007_190133.jpg
20161007_190032.jpg
20161007_190002.jpg
20161007_190010.jpg

Mount the relay HAT on the back of the case using biadhesive tape. Connect terminals as shown.

Final Assembly

20161007_183451.jpg
CuBjlmSWcAEjOkK.jpg

Let's assembly the system:

  • Attach the raspberry to the tripod as shown
  • Position the led light and the dice roller
  • Connect raspberry to USB power
  • Connect led light to USB power
  • Connect dice roller to to 3 VDC power
  • Connect dice roller push button wires to relay HAT

At the beginning you'll need to connect monitor, keyboard and mouse as well (not shown), in order to align the camera properly

  • Adjust position and focus in order to get an image of the dice similar to the one shown

Getting Twitter API Keys

To use this device via twitter you'll need a twitter account and access tokens. I'm not writing down a tutorial for this part, but you can check this instructable:

https://www.instructables.com/id/Raspberry-Pi-Twitterbot/

...it's explained pretty well! :)

Coding

CuBjfsNWAAAI_0i.jpg
Ct83wSRWgAApRDg.jpg

So you finally got everything ready... what else? You need the code! Just replace the twitter authorization data in it with your own and everything should work. the code is widely commented, so if you want to change something just go trough it...

You'll probably want to change the "userCommand" to suit your twitter account. For me the commands were:

@hombremagnetico roll the dice!

The RasPI will activate the relay shield starting the dice roller for 1 second, then it will switch the relay off. It will then wait for two seconds for the dice to stop, and then it will take a picture, process it and update the stats (file and graph). The processed picture will be tweeted back to the user.

@hombremagnetico stats please!

to get a graph with the updated statistics. The updated stats graph will be tweeted back to the user.

Have fun!

Downloads