TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Raspberry Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • The Joker
    • Hologram Machine
    • Google Vision: Camera Tell
    • Yoda Tweets
    • Pi Phone
    • Darth Beats
    • Twitter Keyword Finder
    • Crimbo Lights Hack
    • Xmas Elf
    • Halloween 2016
    • Halloween Hack 2015
    • Socrative Zombie
    • Voice Translation
    • The Blue-Who Finder
    • GPIO, Twitter
    • Pi Chat Bot >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me

RASPBERRY PI BATTLESHIPS


After I built my Giant 10 x 10 LED board, I thought what do I do with it now?  Simple, build a GIANT game of battleships.  I had coded one before on the SenseHAT so I knew that it was possible.  However, there were other, better versions out their which people had made  I found a program by Tim Mulford (28/8/2015) and set about adapting it for the LED screen.  The game is controlled by dialling in the x and y coordinates via the dial on an old Swiss Bakelite phone from the 1960s.  It houses a Raspberry Pi which responds to the 'dialling' and then sends data to the board triggering the responses and game play up dates. 

One of the main challenges was finding a schematic for the telephone.  Each model and brand of phone has a different dial wire configuration.  There are some excellent sites available that list the schematics, but I was not having much luck.  Another issue was that the dial was not turning at a constant speed.  I thought that it was broken but some GT spray soon fixed it.  On removing the phone cover I found the model number and he manufacturer on the inside and a quick Goggle search led me to a really interesting Swiss website about the company.  It turns out that they supplied 'listening devices' and 'phone surveillance systems' during the second world war.   I was a little concerned that I  had acquired the phone of a prominent world leader!  But, still no schematic.  In the end I took a punt and emailed the owner of one of the websites and 'Bob' (not a very spy sounding name) replied with his take on the wiring layout.  Guess what, his suggestion worked, but the program kept reading the same number no matter which number you dialled.  A little bit of trial and error with various GPIO pins and wires and the dial was sorted.  If you are interested in how to hook up a phone for yourself then check out the video below on how to get started.


ISSUES, FAILS and CHALLENGES:

Sockets; Python has a built in module called sockets which enables you to send data to and from different Python programs that are running on separate machines.  In my project, between the LED board and the phone.  I got a version of this working with a test program between the phone and LED board but I needed two way communication, the LED board needed to feedback a Hit, Miss and so on to the Phone.  A big shout out to Cleo_QC who looked at my code and suggested using a newer version of Sockets, she even created a working example, thank you so much. Cleo_QC also helped with socket timings as one of the key issues was that the socket was timing out before the data was sent or received, which meant that the game could not respond in time or respond correctly.

Responding to the outcomes:  This feature uses a basic IF ELIF ELSE statement to respond to an incoming message from the LED board.  Key words or phrases are sent as data and the Raspberry Pi in the Phone picks these up and then responds.  An example code is shown below:

​
# THIS FUNCTION HANDLES THE INCOMING DATA AND THEN RESPONDS WITH THE AUDIO FILE
def voices():
        global ammo
        try:
            sock.settimeout(4) # Adjust to meet the network lag!
            data = sock.recv(1024)
            sock.settimeout(None)
            print ("received message:", data)
            voice = (data.decode())
            print(voice)

            if voice == "MISS":
                print ("audio You missed") #Replace with sound
                pygame.mixer.music.load("/home/pi/PHONE/sounds/miss.mp3")
                pygame.mixer.music.play()
                time.sleep(3)
                ammo = ammo - 1
                ammoupdate = (ammo, " Torpedo Left")
                engine.say(ammoupdate)
                engine.runAndWait()
                #print ("Your ammo is now ", ammo)
               
​  # ASK PLAYER TO DIAL
                pygame.mixer.music.load("/home/pi/PHONE/sounds/First_Dial.mp3") 
                pygame.mixer.music.play()
                time.sleep(1) 
            elif voice == "HIT":
                print ("audio You HIT") #Replace with sound
                led.on()





Sound: Sound and feedback is an important feature of the game and I wanted to use the speaker in the hand set so that the player could hold the phone to their ear.  This was achieved by soldering (very carefully) the two wires from the speaker to the back of the Raspberry Pi.  This means that the sound plays through the speaker whilst still enabling the audio jack to be used with an external speaker so that everybody can hear what is happening.
Picture
Enabling VNC: Eventually I completed the internal hardware setup and the code was working, so time to put it all back together.  I screwed the last screw in and was happy, then I realised, I had not enabled VNC or SSH on the Pi inside the phone so, I had no way to update it.  Finds the screwdriver, unscrews everything.....   

Cleaning the Phone:  To clean the phone and remove the discolouration I used a brush and wiped the phone case down with hair dye.  I then placed the phone into a clear plastic bag and left it in the sun for the day.  After a quick wash under warm water the phone looked restored. 

Starting the Crontab: Use of the crontab caused me several headaches and frustrations, but, it was user error.  The NeoPixels require the Python code to be run in Sudo.  Guess what?  The crontab I entered was
@reboot pythoon battleships.py
No sudo used, no NeoPixels working! 


Drilling the Bakelite: Make sure you use a mask!  When I drilled into the phone it created a lovely yellow colour.  I was intrigued until I want online and read that old Bakelite was made with arsenic and asbestos and should not be cracked!
Copyright 2020 TeCoEd @dan_aldred