Published on

How to Use WhatsApp on a Raspberry Pi 2 or 3 for Tech Projects

Authors
  • avatar
    Name
    how-to.digital
    Twitter

How to Use WhatsApp on a Raspberry Pi 2 or 3 for Tech Projects

In this tutorial, we will walk you through the process of using WhatsApp on a Raspberry Pi 2 or 3. The Raspberry Pi is a versatile single-board computer that can be utilized for various tech projects. By setting up WhatsApp on your Raspberry Pi, you can use it for automated messaging, notifications, or even building a WhatsApp bot.

To get started, follow the step-by-step guide below:

Prerequisites

  • Raspberry Pi 2 or 3
  • MicroSD card (8GB or larger)
  • Raspberry Pi power supply
  • HDMI cable and monitor
  • USB keyboard and mouse
  • Internet connectivity
  • WhatsApp account (can be registered using a smartphone)

Step 1: Setting Up Raspberry Pi

  1. Download the latest version of Raspberry Pi OS from the official Raspberry Pi website (https://www.raspberrypi.org/software/operating-systems/).
  2. Use the Raspberry Pi Imager tool to write the downloaded OS image onto the MicroSD card.
  3. Insert the MicroSD card into the Raspberry Pi.
  4. Connect the HDMI cable, keyboard, mouse, and power supply to the Raspberry Pi.
  5. Power on the Raspberry Pi and follow the on-screen instructions to complete the setup.

Step 2: Enabling SSH

  1. Once you have completed the Raspberry Pi setup, open the terminal on your Raspberry Pi.
  2. Type sudo raspi-config command and hit Enter.
  3. In the Raspberry Pi configuration menu, navigate to Interfacing Options and select SSH.
  4. Choose Yes to enable SSH.
  5. Exit the configuration menu.

Step 3: Installing Required Software

  1. Open the terminal on your Raspberry Pi or connect to it remotely using SSH.
  2. Update the package repositories by running the following command:
    sudo apt update
    
  3. Install the required dependencies by running the following command:
    sudo apt install python3 python3-pip git
    
  4. Install the yowsup library, which provides the interface to use WhatsApp on Raspberry Pi:
    sudo pip3 install yowsup2
    

Step 4: Obtaining WhatsApp Configuration

  1. Create a new directory to clone the yowsup repository by running the following command:

    mkdir yowsup
    
  2. Navigate to the created directory:

    cd yowsup
    
  3. Clone the yowsup repository using Git:

    git clone https://github.com/tgalal/yowsup.git .
    
  4. Now, you need to register a new WhatsApp account. To do so, run the following command:

    ./yowsup-cli registration --requestcode sms --phone <your_phone_number> --cc <your_country_code>
    

    Replace <your_phone_number> with your phone number (without the leading "+" sign) and <your_country_code> with your country code (e.g., 1 for the United States).

  5. After executing the previous command, you will receive a 6-digit registration code on your phone via SMS.

  6. Register your WhatsApp account by running the registration command again, replacing <registration_code> with the code received:

    ./yowsup-cli registration --register <registration_code> --phone <your_phone_number> --cc <your_country_code>
    

Step 5: Using WhatsApp on Raspberry Pi

  1. To use WhatsApp on your Raspberry Pi, you need to create a Python script. Use the following code as a starting point:
import yowsup2

credentials = ("your_phone_number", "your_registration_code")  # Use the credentials obtained in Step 4

class WhatsAppClient:
    def __init__(self, credentials):
        self.credentials = credentials

    def send_message(self, phone_number, message):
        from yowsup.layers.protocol_messages import YowMessagesProtocolLayer
        from yowsup.stacks import YowStack
        from yowsup.connectionmanager import YowConnectionManager

        stack = YowStack(YowMessagesProtocolLayer, credentials=self.credentials)
        stack.setProp(YowAuthenticationProtocolLayer.PHONE, credentials[0])
        stack.setProp(YowAuthenticationProtocolLayer.PASSWORD, credentials[1])

        stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
        stack.broadcastEvent(YowLayerEvent(YowLayersProtocolLayer.EVENT_STATE_CONNECT))
        stack.send(lambda _: (YowMessagesProtocolLayer.EVENT_SEND_MESSAGE, (phone_number, message)))
        stack.loop()

whatsapp_client = WhatsAppClient(credentials)  # Create an instance of the WhatsApp client
whatsapp_client.send_message("recipient_phone_number", "Hello from Raspberry Pi!")  # Replace with recipient phone number and desired message
  1. Save the above code into a file with a .py extension, such as whatsapp.py.

  2. Customize the code according to your requirements by editing the credentials and the send_message parameters.

  3. Execute the Python script by running the following command:

    python3 whatsapp.py
    

    The script will send the specified message to the provided phone number using your WhatsApp account registered on the Raspberry Pi.

Congratulations! You have successfully set up WhatsApp on your Raspberry Pi for tech projects. You can now explore further possibilities by integrating it into your own projects or automation workflows.