Skip to content

Instantly share code, notes, and snippets.

@craftzdog
Created September 10, 2018 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craftzdog/cbd7e6ec2b187b44a0656a45b2a44d0f to your computer and use it in GitHub Desktop.
Save craftzdog/cbd7e6ec2b187b44a0656a45b2a44d0f to your computer and use it in GitHub Desktop.
package main
import (
"./sensors"
"log"
"time"
"context"
firebase "firebase.google.com/go"
"google.golang.org/api/option"
)
type RoomData struct {
temperature float64
pressure float64
humidity float64
light float64
}
func recordData (roomData *RoomData) {
ctx := context.Background()
opt := option.WithCredentialsFile("<YOUR-SERVICE-ACCOUNT-KEY.json>")
app, err := firebase.NewApp(ctx, nil, opt)
client, err := app.Firestore(ctx)
if err != nil {
log.Fatal(err)
}
collection := client.Collection("conditions")
_, _, err = collection.Add(ctx, map[string]interface{}{
"createdAt": time.Now(),
"humidity": roomData.humidity,
"light": roomData.light,
"pressure": roomData.pressure,
"temperature": roomData.temperature,
})
if err != nil {
log.Fatalf("Failed adding alovelace: %v", err)
}
}
func main () {
log.Println("Start capturing my room confitions")
temperature1, humidity, _ := sensors.GetTempAndHumid()
log.Printf("Temperature: %f C\n", temperature1)
log.Printf("Humidity: %f %%rh\n", humidity)
light, _ := sensors.GetLight()
log.Printf("Light: %f Lux\n", light)
temperature2, pressure, _ := sensors.GetTempAndPressure()
log.Printf("Temperature: %f C\n", temperature2)
log.Printf("Pressure: %f %%rh\n", pressure)
data := RoomData{
temperature: temperature1,
pressure: pressure,
humidity: humidity,
light: light,
}
recordData(&data)
log.Println("Finished recording data!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment