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

Navigate to:

Part 3 : Using InfluxDB Client Libraries

InfluxDB provides support for Client Libraries in multiple programming languages. A client library goes a long way in wrapping the core HTTP API with a high level wrapper, so that you can work directly with the language and not worry about the low level mechanics of the API.

Hop over to HTTP API client libraries documentation and check out the list of programming languages that InfluxDB supports.

In part 3, we are going to look at the Python client library to perform the same operations that we have been doing so far i.e. query for records and insert some records.

The first step for the Python client library should be installed the InfluxDB Python library in your local setup and the documentation provides information on that. You can do that via the pip install mechanism.

$ pip install influxdb

The steps to using the Python client library is no different when it comes to conceptually thinking of what we need to do:

  • Import the python library
  • Establish a client connection to the InfluxDB host. In our case, this is the host running on Google Compute Engine.
  • Use the client object to query for records. You can use the queries similar to the ones that we saw in the previous part.
  • Use the client object to write a record or two to the InfluxDB database.

The inluxdbclient.py Python program is shown below:

import datetime

from influxdb import InfluxDBClient

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

#Sample values -- these will be read from sensor
temperature_c = 27.8

#Timestamp
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)

#Query Existing Values
result = client.query('SELECT * FROM temperature')
points = list(result.get_points(measurement='temperature'))
for point in points:
   print('Station = ',point['Station'],'Value = ',point['value'])

#Write a record
json_data = [
   {
       "measurement":"temperature",
"time":timestamp,
"tags": {
   "Station":station_name
},
"fields": {
   "value":temperature_c
}
   }
]
bResult = client.write_points(json_data)
print("Result of Write Data : ",bResult)

Let us understand key points of the code listing above:

  • We import the InfluxDBClient class at the start of the program code.
  • We define two constants, one for the InfluxDB Host, which is the Public IP of your Compute Engine instance. Remember to replace it in the code. The other one is for the database name, which is `temperature_db`.
  • The next statement is important. We initialize the InfluxDBClient with the hostname, port (8086 for the API) and provide it with the database name.
  • Now that we have the client object, we can then either query or insert records.
  • The `query` method takes in the InfluxQL query that you want to execute against the database. This returns you a `ResultSet` object. One of the methods on the `ResultSet` object is `get_points` , which we use to get the measurement points for the `temperature` measure. We convert that into a list. We then iterate and print out the values.
  • To write data to InfluxDB, we use the `write_points` method that takes in a list of measurement points that we want to write to the database. Each measurement point, and which you should be familiar with now, specifies the measure, one or more tags (Station), and a value (temperature). Also notice that this time we are passing in the timestamp so that we record the timestamp from the station that it was recorded at. We use the `datetime` package in python to help us get the Unix timestamp that we need to send to InfluxDB.
  • The result of the `write_points` method is of boolean type. If the operation is successful, it returns back a `true`.

We can run this program via the following command and it presents the output as shown below:

$ python influxdbclient.py
Station =  S2 Value =  27.8
Station =  S1 Value =  29.8
Station =  S1 Value =  29.9
Station =  S1 Value =  29.8
Station =  S2 Value =  29.9
Station =  S1 Value =  29.8
Station =  S1 Value =  29.85
Station =  S1 Value =  29.9
Station =  S2 Value =  27.8
Station =  S2 Value =  27.8
Station =  S2 Value =  27.8
Result of Write Data :  True

I had run this application a few times and hence you see a lot more records in the query result. Alternately, you could use the InfluxDB web admin app to query the results too.

In this post we looked at how to use the InfluxDB Python client library to insert records into the InfluxDB host that we set up in previous posts. The code in this part used a dummy value for the temperature reading but that is fine since we wanted to first setup it up and ensure that this code works.

What's next?

  • This concludes part three of this tutorial. In part four, we will integrate this client code into an actual IoT sensor application that uses the Arduino and LM35 Temperature Sensor to read temperature values at regular intervals. The live temperature data will then be fed into the InfluxDB database. Follow us on Twitter @influxdb to catch the next blog in this series.
  • Looking to level up your InfluxDB knowledge? Check out our economically pricedvirtual and public trainings.