Building Go Applications Using InfluxDB

Ajiyba Nesij Korkmaz
Published in
3 min readAug 16, 2021


This tutorial explains how to save data to InfluxDB using a Golang application.

There is an environment, and its temperature is variable, meaning it can fluctuate and change over time. When the temperature of the environment reaches the upper limit, the air conditioner activates to cool down the environment. Similarly, when the temperature falls below the lower limit, the air conditioner deactivates. This scenario can be likened to the operation of a car’s air conditioner.

Throughout the process, both the temperature of the environment and the status of the air conditioner are recorded instantaneously in InfluxDB. This allows for real-time monitoring and analysis of the temperature changes and the corresponding actions taken by the air conditioner.

NOTE : I used Newton’s Law to make the temperature change realistic.

Optionally, the data recorded in InfluxDB can be visualized and analyzed using Grafana.



Defining Constants

We need to define global constant values.

  • databaseaddr, database, username and password are required for InfluxDB.
  • hotlimit is the maximum value that the environment’s temperature can take.
  • coldlimit is the minimum value that the environment’s temperature can take.
  • deltatemp is used for temperature changing.
  • threshold is the threshold for limits.
const (
databaseaddr = "http://localhost:8086"
database = "example"
username = "root"
password = "root"
hotlimit = 45.0
coldlimit = 10.0
deltatemp = 0.1
threshold = 0.05

Defining InfluxDB Client

We create a function to initialize the InfluxDB client.

func influxDBClient() client.Client {
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr:databaseaddr ,
Username: username,
Password: password,
if err != nil {
log.Fatalln("Error: ", err)
return c

Changing Environment Temperature

This function calculates the temperature change. The temperature change is calculated with a function inspired by Newton’s Law of Cooling.

func changeEnvironmentDegree(currentTemp float64, deltaTemp float64,limit float64) float64  {
return limit + (currentTemp-limit)*math.Exp(-0.300*deltaTemp)

Inserting to InfluxDB

This function takes InfluxDB client, temperature and climate status as parameters. It also creates a point with these values and saves this point.

func insertMetrics(c client.Client,temp float64,airConditionerStatus int) {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: database,
Precision: "s",

if err != nil {
log.Fatalln("Error: ", err)

tags := map[string]string{
"airConditioner": "Air Conditioner 1",
fields := map[string]interface{}{
"degree": temp,
point, err := client.NewPoint(
if err != nil {
log.Fatalln("Error: ", err)


err = c.Write(bp)
if err != nil {


A client named c is created in the main method. In addition to this, the starting temperature and air conditioning status are set.

The general algorithm is as follows; realtime temperature is calculated.

  • If the temperature is above the hot limit, the air conditioner will turn on and the limit will be Coldlimit.
  • If the temperature is below the cold limit value, the air conditioner is turned off and the limit becomes hotlimit.
  • Climate status and realtime temperature are recorded in the database.

After a delay of 1 second, all operations are performed again.

func main() {
c := influxDBClient()

limit := coldlimit
var airConditionerStatus =1
currentTemp := 28.0

for {

if currentTemp >= hotlimit-threshold {
} else if currentTemp <= coldlimit+threshold {
time.Sleep(100 * time.Millisecond)


If you connect Grafana to InfluxDB and import dashboard.json , you can get the graph shown below.

You can also find all the codes on my Github account.