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

Python’s SymPy module is really cool

So I was just browsing some code, and I came across a cool module I’d never seen before: SymPy

Basically, SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS).

What is symbolic mathematics?

Symbolic computation deals with the computation of mathematical objects symbolically. This means that the mathematical objects are represented exactly, not approximately, and mathematical expressions with unevaluated variables are left in symbolic form. Symbolic computation is handling non-numerical values, this means symbols like in algebra. Variables are defined as

In simple word, “Variables are defined as Symbols in Symbolic Computation instead of defining variables as numerical values ”

This will be more clear from an example from SymPy official documentation.

Let us define a symbolic expression, representing the mathematical expression x+2xy+2y.

>>> from sympy import symbols
>>> x, y = symbols('x y')
>>> expr = x + 2*y
>>> expr
x + 2*y

Instead of evaluating to something by convention, the expression remains as just, x+2*y

>>> x*expr
x*(x + 2*y)

Here, we might have expected x(x+2y) to transform into x^2+2xy, but instead, we see that the expression was left alone. This is a common theme in SymPy.


The Power of Symbolic Computation

The real power of a symbolic computation system such as SymPy is the ability to do all sorts of computations symbolically.

SymPy can simplify expressions, compute derivatives, integrals, and limits, solve equations, work with matrices, and much, much more, and do it all symbolically. It includes modules for plotting, printing (like 2D pretty printed output of math formulas,), code generation, physics, statistics, combinatorics, number theory, geometry, logic, and more.

Examples from official SymPy tutorial

Solve x^2 – 2 = 0

>>> solve(x**2 - 2, x)
[-√2, √2
Compute  sin(x2)d
>>> integrate(sin(x**2), (x, -oo, oo))


To install SymPy run:

sudo pip install SymPy

If you already have Anaconda and want to update SymPy to the latest version, use:

conda update sympy

After installation, it is best to verify that your freshly-installed SymPy works. To do this, start up Python and import the SymPy libraries:

$ python
>>> from sympy import *

From here, execute some simple SymPy statements like the ones below:

>>> x = Symbol('x')
>>> limit(sin(x)/x, x, 0)
>>> integrate(1/x, x)
 I am looking forward to using this library for mathematical computation for my IoT projects.

Currently, I am going through the tutorial and documentation to get familiar with using the software.


Github: Introduction to contribution



Download: Releases 


Writing a Simple terminal based Dice Game in Python

Last week, We were given a task and that was to show a project (Game, app or script), where we had to use tools that we have learned so far.

I was busy this weekend shifting my hostel and I forgot to complete the task. So I decided to write a simple Dice Game.

Coming to the code of the game. This is a terminal based game without using any graphics. You will roll a die and If Six comes, You will win the Game :). I used random module to generate random numbers. When user will roll the die, randit function generates a random number between 1,6. You will have a better luck if you get a Six.

Since writing code was not only part of the task, but to use tools taught during the session. So I used following tools for my development

  • Vim – Code Editor
  • Git
  • Github
  • Markdown
  • Python

I created Python Small Projects repo on GitHub and pushed code there.

I will write more scripts or silly games to that repo as I am practicing Python these days. I will also add python scripts, that I wrote a few days back and already using.

Please Look at Code and Repo and If find error and improvement, Create and Send a pull request.

Github Repo Link:

Switched to Fedora as my Main OS & It’s going Great!

After having used Windows as the Main operating system for about the past two years of Engineering, I’m finally switched to Fedora as my main OS. So far the Fedora experience has been going excellent.

What is Fedora?

Fedora is a Unix-like operating system based on the Linux kernel and GNU programs (a Linux distribution), developed by the community-supported Fedora Project and sponsored by the Red Hat company.

Basically, Fedora is a popular open source Linux-based operating system.

How I came to know about Fedora?

I saw fedora OS for the first time in my college lab during ‘Basic concepts of operating systems like UNIX, MS DOS class’. I was not familiar with Linux at that time. Later, when I joined dgplug, They introduced us to Linux and suggested to use Fedora for all work during the session.

Switching to Fedora finally

After using fedora for more than a year, I was impressed by its simplicity, flexibility and easy to use considering myself as a newbie to Linux. It offers pure GNOME with clean Desktop.

I think Fedora is best OS if you want to move to Linux from Windows. It is easy to use and Desktop Environment provides smooth and is easy for users, especially for those who are moving from windows OS. Because It’s not easy to switch directly to the terminal based environment.

I still use Windows for gaming and some other’s work. I am trying to find alternatives to those app or software in Fedora. My machine is running on dual boot currently, Fedora 26 and Windows 10.

I’ve run Fedora for last one years with a consistently reliable experience, and I look forward to what the next one brings. My most used Linux applications are the GNOME Terminal, Mozilla Firefox, Google Chrome, Mozilla Thunderbird, Gedit, and ViM.

I will write more about Fedora in my next posts. 🙂

Looking forward to exploring more “How does a computer works”