Python talks to Arduino

This blog post gives an overview of how I created a simple python app for sending data to the firebase database from Arduino. I used Arduino, an array of Sensors, Raspberry Pi and firebase realtime Database. I have included links to the Github repository of this project.

First up, let me explain what the eventual goal was. The goal was to set up a series of low-cost climate/environment modules that capture various types of data like temperature, humidity and various pollutants and gases in the environment. Then take all this data and put it in the cloud where we can eventually build out dashboards, alerts and more. Also, We will be able to show real-time data of a particular place.

This blog post explains how we build one single setup where we create a system comprising an Arduino, an array of sensors, a Python application that can read the data from the Arduino and post that data to the cloud. To automate all the process, We used Raspberry PI booted with Linux(Raspbian) where we will execute a few bash scripts to automate the whole process.

After trying various cloud storage API(s), We didn’t find any convenient for our uses. The main challenge that we were facing was to send data from Arduino to cloud. Either the APIs documentation was not good or the SDK itself.

The Hardware Setup

We used the following:

  • Arduino microcontroller
  • An array of sensors for collecting various parameters.
  • We will have the Raspberry Pi that interfaces with the Arduino to read and transmit off the values but to validate things, for now, the Arduino was powered via Battery source. PI is also powered by same with Linux on it. The communication between the Arduino and the Microprocessor is via serial port communication

Of course, we used a breadboard to connect all this together but I am not going to explain the circuitry here. Our main aim is to show how we used python to get data from Arduino and store it at firebase using python SDK provided by google firebase and how we can automate all the process.

Our data from Arduino looks like this on the output screen

[Add Image]

14:49:00 32, 34, 67, 88.9, 90.5, 33.0
14:49:01 32, 34, 76, 88.7, 91.2, 33.4
14:49:02 33, 34, 78, 88.9, 91.7, 34.5
14:49:03 34, 34, 76, 88.9, 91.8, 34.6

Once this happens, we know that the Arduino setup is looking good and all we need to do now is to write a client program on the PC that interfaces with this Arduino, read the values via the Serial port and then push them to cloud.

Reading data from Arduino using Python

We used pySerial module to encapsulates the access for the serial port. It provides backends for Python running on all major OS. The module named “serial” automatically selects the appropriate backend. You can install it using pip or easy_install.

pip install pyserial

Now you need to figure out which Serial Port on your machine is interfaced to the Arduino. In our case it was ‘/dev/ttyS1’.

class ArdClient:
    port = '/dev/ttyS1'
    baudrate = 9600
    timeout = 0.1

    def __init__(self, port=port, baudrate=baudrate, timeout=timeout):
        self.port = port
        self.baudrate = baudrate
        self.timeout = timeout
   def startSerial(self):
            self.arduino = serial.Serial(self.port, self.baudrate, self.timeout)
            return True
            return False
            return False

To check if we are connected to open serial port.

ac = ArdClient('/dev/ttyS1')
if startSerial():
    print("Something Wrong")

Once, We get connected to open port, it’s time to read data

def readData(self):
        start = 0
        st = []        
        while True:
            # Ignore the last two character             
            data = self.arduino.readline()[:-2]
            if data:
                #Ignore first two lines
                if start <= 2:
                    start += 1
                st = []


Sending to Firebase RealTime Database

Once we get the data, we will send those to firebase cloud database using python SDK provided. To make things easier, Firebase has good documentation available for SDKs that you can use to ease your setup.

The Firebase Admin Python SDK is available via pip.

$ sudo pip install firebase-admin

Setting up firebase realtime database

  1. Setup python admin SDK and initialise the SDK following the steps below

    You can follow the official documentation for  adding and initialising the SDK


  2. Integrate the Firebase Realtime Database SDKs

    The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client.

    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import db
    # Fetch the service account key JSON file contents
    cred = credentials.Certificate('path/to/serviceAccountKey.json')
    # Initialize the app with a service account, granting admin privileges
    firebase_admin.initialize_app(cred, {
        'databaseURL': ''
  3. Create Realtime Database References.
    # As an admin, the app has access to read and write all data, 
    # regradless of Security Rules
    ref = db.reference()
  4. Set Data and Listen for Changes
  5. # Initialise data to 0
    def __init__(self, data):
        self.root = fa.createdb(data)
    def setRoot(self):
        'SensorA': 00,
        'SensorB' : 00,
        'SensorC': 00,
        'SensorD' : 00
  6. Updating Saved Data
def updateData(self, datalist):
            'SensorA': float(datalist[0]),
            'SensorB': float(datalist[1]),
            'SensorC': float(datalist[2]),

            'SensorD': float(datalist[3])
         print("Data Updated")
   except IOError:
       print('Error! Something went wrong.')

Checking our data

Assuming that all the hardware is wired up and you have your Python code ready, executing the code is straightforward. A sample run of the Python code is shown below:

~ python src/

The final step was to validate if our data was being transmitted successfully and into Firebase.

Visit the Dashboard for your Firebase Database and you should start seeing some entries over there i.e. the Sensor data.

Github Link

The project is hosted at Github