Python interface

Finally, we need to build an application running on your computer to send the order to get new measurements from the Arduino board, retrieve the data, and display it on your screen. I chose Python for this interface but it's quite easy to interface with the Serial port with PySerial, and it is also easy to build an interface with Tkinter which is installed by default with Python.

I won't detail every piece of the code here because it's way too long, but you can of course find the complete code in the GitHub repository for this project.

The Python code starts by initialising the Serial connection:

serial_speed = 115200
serial_port = '/dev/cu.AdafruitEZ-Link06d5-SPP'
ser = serial.Serial(serial_port, serial_speed, timeout=1)
Then, the core of the Python script is the measure() function that is continuously executed every second:
# Measure data from the sensor
def measure(self):

   # Request data and read the answer
   ser.write("m")
   data = ser.readline()

   # If the answer is not empty, process & display data
   if (data != ""):
      processed_data = data.split(",")
      self.temp_data.set("Temperature: " + str(processed_data[0]))
			self.temperature.pack()

      self.hum_data.set("Humidity: " + str(processed_data[1]))
			self.humidity.pack()

   # Wait 1 second between each measurement
   self.after(1000,self.measure)
Then, the rest of the script simply displays this data on a simple Tkinter window. You can get the complete code from the GitHub repository for this project. Finally, type "python sensor_gui.py" in a terminal. This is what you should get:

Congratulations, you just built a Bluetooth temperature & humidity sensor ! Of course, you can use the code found in this project to interface other sensors to your computer via Bluetooth, for example ambient light sensors or motion sensors.

Each new Bluetooth module that you add will appear as a new Serial Port on your computer, so you can perfectly have several of these Bluetooth-based sensors in your home and build a whole home automation system from it!

Last updated on 2015-05-04 at 04.26.50 PM Published on 2014-02-07 at 02.47.15 PM