Published on

How to Use WhatsApp on a Raspberry Pi 2

Authors
  • avatar
    Name
    how-to.digital
    Twitter

How to Use WhatsApp on a Raspberry Pi 2

WhatsApp is a popular messaging app that allows you to send messages, make voice and video calls, and share media files with friends and family. While WhatsApp is primarily designed for smartphones, it is possible to use WhatsApp on a Raspberry Pi 2, a credit-card-sized computer. In this tutorial, we will walk you through the steps to set up and use WhatsApp on a Raspberry Pi 2.

Prerequisites

Before we begin, please make sure you have the following:

  • A Raspberry Pi 2 board.
  • An SD card (16GB recommended) with Raspbian installed.
  • A USB webcam (optional for video calls).
  • A USB sound card or USB headset (optional for audio calls).

Step 1: Install Dependencies

  1. Ensure your Raspberry Pi 2 is connected to the internet.
  2. Open a terminal on your Raspberry Pi 2 or connect to it remotely using ssh.
  3. Update the package list by running the following command:
sudo apt update
  1. Install the necessary dependencies by running the following commands:
sudo apt install -y libopenjp2-7 libtiff5 libqt5gui5 libfontconfig1 libjpeg62-turbo-data libatlas-base-dev libjasper-dev libqt4-test libilmbase12 libopenexr22 libilmbase-dev libgstreamer1.0-dev libhdf5-dev libharfbuzz0b libjasper1 libhdf5-serial-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libqtgui4 python3-pyqt5 libqt4-test python3-dev python3-pip python3-numpy
  1. Upgrade pip to the latest version:
sudo pip3 install --upgrade pip

Step 2: Set Up WhatsApp

  1. Open a web browser on your Raspberry Pi 2 and go to the WhatsApp web page.
  2. Using the web browser, scan the QR code displayed on the website with the WhatsApp app on your smartphone. This will link your WhatsApp account to your Raspberry Pi 2.

Step 3: Install Selenium and ChromeDriver

  1. Install Selenium, a web testing framework, using pip:
sudo pip3 install selenium
  1. Next, we need to install ChromeDriver, which allows us to automate the Chromium browser. Download the appropriate version of ChromeDriver for your Raspberry Pi 2 from the official ChromeDriver website. Make sure you select the ARM version.
  2. Extract the downloaded file to a folder on your Raspberry Pi 2.
  3. Rename the extracted file to chromedriver:
mv /path/to/downloaded/chromedriver /usr/local/bin/chromedriver
  1. Set the appropriate permissions:
sudo chmod +x /usr/local/bin/chromedriver

Step 4: Create a Python Script

  1. Create a new Python script using your preferred text editor:
nano whatsapp_bot.py
  1. Add the following code to the script:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")

# Set the path to the chromedriver executable
driver = webdriver.Chrome(options=chrome_options, executable_path='/usr/local/bin/chromedriver')

# Set the URL to the WhatsApp web page
driver.get("https://web.whatsapp.com/")

# Wait for the user to connect their WhatsApp account
input("Scan the QR code on the screen and press Enter to continue...")

# You can now automate WhatsApp using the Selenium WebDriver API

# Example: Send a message to a contact
search_contact = driver.find_element_by_xpath("//*[@id='side']/div[1]/div/label/input")
search_contact.send_keys("Contact Name")
contact = driver.find_element_by_xpath("//*[@id='pane-side']/div[1]/div/div/div[1]")
contact.click()
message_box = driver.find_element_by_xpath("//*[@id='main']/footer/div[1]/div[2]/div/div[2]")
message_box.send_keys("Hello from Raspberry Pi!")
send_button = driver.find_element_by_xpath("//*[@id='main']/footer/div[1]/div[3]/button/span")
send_button.click()

driver.quit()
  1. Save and exit the file (Ctrl + X, Y, Enter).

Step 5: Run the Script

  1. Open a terminal on your Raspberry Pi 2.
  2. Navigate to the directory where you saved the Python script using the cd command:
cd /path/to/directory
  1. Run the script:
python3 whatsapp_bot.py
  1. Follow the instructions on the terminal to scan the QR code with the WhatsApp app and press Enter to continue.
  2. You can now modify the script to automate various WhatsApp actions, such as sending messages, making calls, or sending media files.

That's it! You have successfully set up and used WhatsApp on your Raspberry Pi 2. You can explore the Selenium WebDriver API to automate additional WhatsApp features according to your needs.