Add code example

This commit is contained in:
Nicolas Leclercq 2017-10-26 17:27:54 +02:00
parent fb0d296b5f
commit 41589231f4
3 changed files with 89 additions and 66 deletions

View File

@ -1,75 +1,12 @@
# netatmo-api-go
Simple API to access Netatmo weather station data written in Go.
Currently tested only with one weather station, outdoor and indoor modules and rain gauge. Let me know if it works with wind gauge.
## Quickstart
- [Create a new netatmo app](https://dev.netatmo.com/dev/createapp)
- Download module ```go get github.com/exzz/netatmo-api-go```
- Try below example (do not forget to edit auth credentials)
## Example
```
go
package main
import (
"fmt"
"os"
"time"
netatmo "github.com/exzz/netatmo-api-go"
)
func main() {
n, err := netatmo.NewClient(netatmo.Config{
ClientID: "YOUR_APP_ID",
ClientSecret: "YOUR_APP_SECRET",
Username: "YOUR_CREDENTIAL",
Password: "YOUR_PASSWORD",
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
dc, err := n.Read()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ct := time.Now().UTC().Unix()
for _, station := range dc.Stations() {
fmt.Printf("Station : %s\n", station.StationName)
for _, module := range station.Modules() {
fmt.Printf("\tModule : %s\n", module.ModuleName)
{
ts, data := module.Info()
for dataName, value := range data {
fmt.Printf("\t\t%s : %v (updated %ds ago)\n", dataName, value, ct-ts)
}
}
{
ts, data := module.Data()
for dataName, value := range data {
fmt.Printf("\t\t%s : %v (updated %ds ago)\n", dataName, value, ct-ts)
}
}
}
}
}
```
Output should look like this :
- Edit ```test/sample.conf```with your credentials
- run ```go run test/netatmo-api-test.go -f test/sample.conf```
- Output shall look like :
```
Station : Home
Module : Chambre Elsa
@ -108,6 +45,8 @@ Station : Home
Pressure : 1028.1 (updated 278s ago)
AbsolutePressure : 1008.4 (updated 278s ago)
```
## Tips
- Only Read() method actually do an API call and refresh all data at once
- Main station is handle as a module, it means that Modules() method returns list of additional modules and station itself.
- Data() returns sensors values (such as temperature) whereas Info() returns module status (such as battery level)

80
test/netatmo-api-test.go Normal file
View File

@ -0,0 +1,80 @@
package main
import (
"flag"
"fmt"
"os"
"time"
toml "github.com/BurntSushi/toml"
netatmo "github.com/exzz/netatmo-api-go"
)
// Command line flag
var fConfig = flag.String("f", "", "Configuration file")
// API credentials
type NetatmoConfig struct {
ClientID string
ClientSecret string
Username string
Password string
}
var config NetatmoConfig
func main() {
// Parse command line flags
flag.Parse()
if *fConfig == "" {
fmt.Printf("Missing required argument -f\n")
os.Exit(0)
}
if _, err := toml.DecodeFile(*fConfig, &config); err != nil {
fmt.Printf("Cannot parse config file: %s\n", err)
os.Exit(1)
}
n, err := netatmo.NewClient(netatmo.Config{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
Username: config.Username,
Password: config.Password,
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
dc, err := n.Read()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ct := time.Now().UTC().Unix()
for _, station := range dc.Stations() {
fmt.Printf("Station : %s\n", station.StationName)
for _, module := range station.Modules() {
fmt.Printf("\tModule : %s\n", module.ModuleName)
{
ts, data := module.Info()
for dataName, value := range data {
fmt.Printf("\t\t%s : %v (updated %ds ago)\n", dataName, value, ct-ts)
}
}
{
ts, data := module.Data()
for dataName, value := range data {
fmt.Printf("\t\t%s : %v (updated %ds ago)\n", dataName, value, ct-ts)
}
}
}
}
}

4
test/sample.conf Normal file
View File

@ -0,0 +1,4 @@
clientID = "NETATMO_CLIENTID"
clientSecret = "NETATMO_CLIENTSECRET"
username = "NETATMO_USERNAME"
password = "NETATMO_PASSWORD"