Created
June 11, 2020 07:44
-
-
Save velotiotech/9bb20ce2e4ca1a47099f02086dc07984 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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