Skip to content

Instantly share code, notes, and snippets.

@akash-gautam
Created September 12, 2018 06:29
Show Gist options
  • Save akash-gautam/7c044d08ea06d9a2a16c338a89093b3f to your computer and use it in GitHub Desktop.
Save akash-gautam/7c044d08ea06d9a2a16c338a89093b3f to your computer and use it in GitHub Desktop.
package dao
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
)
type SensorDataFiltered struct {
Temperature float64 `json:"temperature"`
Humidity float64 `json:"humidity"`
City string `json:"city"`
}
var dbName = "iotdata"
var dbHost = "184.73.62.30"
var dbPort = "8086"
func (sdf *SensorDataFiltered) PostDataToInfluxDB(Data []byte) {
err := json.Unmarshal(Data, &sdf)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(sdf.Temperature, sdf.Humidity)
}
url := "http://" + dbHost + ":" + dbPort + "/write?db=" + dbName
humidity := fmt.Sprintf("%.2f", sdf.Humidity)
temperature := fmt.Sprintf("%.2f", sdf.Temperature)
city := sdf.City
requestBody := "sensordata,city=" + city + " humidity=" + humidity + ",temperature=" + temperature
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(requestBody)))
httpclient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
resp, err := httpclient.Do(req)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Status code for influxdb data port request = ", resp.StatusCode)
}
defer resp.Body.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment