Make your phone and pi work together seamlessly with these simple steps

In this tutorial, we will learn how to connect an Android phone to a Raspberry Pi to unlock the potential of these amazing devices working in tandem. …


Updated September 29, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


In this tutorial, we will learn how to connect an Android phone to a Raspberry Pi to unlock the potential of these amazing devices working in tandem.

Introduction

The Raspberry Pi is a powerful little computer that can be used for many purposes, including Internet of Things (IoT) projects and home automation. With the right tools and software, you can connect your Android phone to a Raspberry Pi and make them work together like never before.

Prerequisites

Before we begin, there are some prerequisites you should have:

  1. A Raspberry Pi device (preferably model 3 or higher) with Raspbian OS installed.
  2. An Android phone that supports USB-OTG (On-The-Go) feature and has a Micro USB to USB-A adapter.
  3. Basic knowledge of Linux command line interface and Python programming language.

Steps to Connect Your Android Phone to Raspberry Pi

  1. Enable USB OTG on your Android phone: This step varies from device to device, but generally involves enabling a developer option in the Settings menu. Search for “USB OTG” or “On-The-Go” and follow the instructions to enable this feature.

  2. Connect your Android phone to Raspberry Pi: Connect the Micro USB end of the adapter to your phone and the USB-A end to any available USB port on your Raspberry Pi. Ensure that you have enough power for both devices, as not all Raspberry Pi models come with a powered USB port.

  3. Install required packages: Open a terminal window on your Raspberry Pi and run the following command to update your package list:

sudo apt-get update

Then, install the necessary packages for communication between the devices using Python:

sudo apt-get install python3-pip python3-pyqt5 adb
  1. Install Android Debug Bridge (ADB): ADB is a versatile command-line tool that lets you communicate with an emulator instance or connected Android device. To set up ADB on your Raspberry Pi, run the following commands:
sudo apt-get install android-tools-adb
  1. Check if your phone is detected: Run the following command to see a list of connected devices:
adb devices

If your phone is recognized, you should see its serial number displayed in the terminal. If not, try unplugging and reconnecting the USB cable or restarting both devices.

  1. Install PyQt5 for GUI: PyQt5 is a set of Python bindings for the Qt application framework, which allows us to create graphical user interfaces (GUI) on our Raspberry Pi. To install it, run the following command:
sudo pip3 install pyqt5
  1. Create a Python script: Create a new file called phone_control.py using your favorite text editor and add the following code:
import sys
from PyQt5 import QtWidgets, uic
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWidgets import QPushButton

class PhoneControl(QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi('phone_control.ui', self)
        
        self.button1.clicked.connect(self.send_sms)
        
    @pyqtSlot()
    def send_sms(self):
        os.system("adb shell am start -a android.intent.action.SENDTO -d sms:phone_number --es sms_body 'Message from Raspberry Pi'")
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = PhoneControl()
    window.show()
    sys.exit(app.exec_())

This script creates a simple GUI with a button that sends an SMS message to a specified phone number when clicked. Note: Replace phone_number with the actual phone number you want to send the message to.

  1. Create a UI file: Create another file called phone_control.ui in the same directory as your Python script and add the following code:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Phone Control</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QPushButton" name="button1">
      <property name="text">
       <string>Send SMS</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>button1</sender>
   <signal>clicked()</signal>
   <receiver>MainWindow</receiver>
   <slot>send_sms()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>237</x>
     <y>180</y>
    </hint>
    <hint type="destinationlabel">
     <x>294</x>
     <y>265</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

This file defines the layout of your GUI.

  1. Run the Python script: Navigate to the directory containing both phone_control.py and phone_control.ui in your terminal and run the following command:
python3 phone_control.py

A window should appear on your Raspberry Pi’s display with a button labeled “Send SMS.” Clicking this button will send an SMS message to the specified phone number using ADB.

Conclusion

With these simple steps, you have successfully connected your Android phone to a Raspberry Pi and created a GUI that allows you to control basic functions of your phone. The possibilities are endless! You can now use this setup for various IoT projects or home automation tasks, such as monitoring your home security system, controlling smart lights, or managing your music playlist. The sky’s the limit!