Part 4: How-to Create an IoT Project with the TICK Stack on the Google Cloud Platform

Navigate to:

Part 4 : Integrating InfluxDB into the IoT Project

So far in the series, we have looked at what InfluxDB is, setup an InfluxDB Host on Google Compute Engine and wrote a Python application that can interact with it.

This part now integrates the Python Client application code that we wrote in the previous section with an actual IoT project that uses an Arduino, a Temperature Sensor and InfluxDB as our database to store all the temperature readings that we collect.

First up, let me explain what the eventual goal is and how this is a first step in that process. The goal is to set up a series of low cost climate/environment modules that capture various types of data like temperature, humidity and more. Then take all this data and put it in the cloud where we can eventually build out dashboards, alerts and more. All the good stuff in the cloud will be powered by InfluxDB.

We will now explain a setup where we create a system comprising an Arduino Uno, a temperature sensor, a Python application that can read the data from the Arduino Uno (yes, I did not use an Arduino Internet Shield) and post that data to the cloud.

The Hardware Setup

I used the following:

  • Arduino Uno microcontroller
  • LM35 Temperature Sensor
  • Eventually 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 Uno was powered via a laptop/desktop with Python installed on it. The communication between the Uno and the PC is via serial port communication.

Arduino Uno + Temperature Sensor Setup

Here is how the LM35 sensor is connected to the Arduino Uno board.


Of course, we used a breadboard to connect all this together but I am simplifying the diagram here so that you know what is connected to which pin.

The LM35 has 3 pins:

  • The first one goes to the 5V power pin on Arduino
  • The 3rd one is the GND
  • The middle pin is the VOUT where it emits out the values that we need to capture. We connect this to the Analog Pin (A0) on the Arduino. We can then write our Arduino code to read that value, as is shown next.

Arduino Code

The Arduino Code is straightforward as given below:

float temp;
int tempPin = 0;

void setup()

void loop()
temp = analogRead(tempPin);
temp = temp * 0.48828125;

You will notice in the loop that every 10 seconds, we are printing out the temperature value that was read from the Analog Pin (#0).

If you run the serial port monitor that comes with the Arduino IDE and if the Arduino is powered up and connected as per the diagram shown, then you will find the Temperature value being printed on the serial monitor as given below:

com15Once 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 write those values to the InfluxDB database.

We can now integrate the code that we had used in the previous section to write to the InfluxDB database. The integrated code is shown below:

import serial
import datetime
from influxdb import InfluxDBClient

#Setup some constants with InfluxDB Host and Database name
INFLUXDB_HOST = '<PublicIPInfluxDBHost>'
INFLUXDB_NAME = 'temperature_db'

#Connect to Serial Port for communication
ser = serial.Serial('COM15', 9600, timeout=0)

#Setup a loop to send Temperature values at fixed intervals
#in seconds
fixed_interval = 10
while 1:
 #temperature value obtained from Arduino + LM35 Temp Sensor
 temperature_c = ser.readline()
 timestamp = datetime.datetime.utcnow().isoformat()

 #Station Name that is recording the temperature
 station_name = "S2"

 #Initialize the InfluxDB Client
 client = InfluxDBClient(INFLUXDB_HOST,'8086','','',INFLUXDB_NAME)

 #Write a record
 json_data = [
 "tags": {

 "fields": {

 bResult = client.write_points(json_data)
 print("Result of Write Data : ",bResult)
except ser.SerialTimeoutException:
 print('Error! Could not read the Temperature Value from unit')

That completes the integration. We now have an end-to-end IoT Prototype application that is able to collect a temperature reading every 10 seconds and store that in InfluxDB. This is just one weather station reading this data. We can now provision and deploy multiple such weather stations across the city. Each of the weather stations will be having this setup and code and the only change will be the Station Name, which will be set to the particular Station Name.

Since we have created the Station Name as the tag, our InfluxDB setup can now help us store data as well as query it for all stations, multiple stations and more.

This concludes the tutorial for setting up InfluxDB and integrating it with an IoT project.

What's next?

  • In Part Five, we will investigate other parts of the TICK stack and see how it can further help us to collect and visualize the sensor data that we are going to be collecting. We will also see how to setup alerts depending on certain threshold values we see in the data. Follow us on Twitter @influxdb to catch the next blog in this series.
  • Looking to level up your InfluxDB knowledge? Check out our economically priced virtual and public trainings.