mirror of
https://github.com/steinhobelgruen/netatmo-api-go.git
synced 2024-11-23 06:53:56 +00:00
Add code example
This commit is contained in:
parent
fb0d296b5f
commit
41589231f4
71
README.md
71
README.md
|
@ -1,75 +1,12 @@
|
||||||
# netatmo-api-go
|
# netatmo-api-go
|
||||||
Simple API to access Netatmo weather station data written in 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
|
## Quickstart
|
||||||
|
|
||||||
- [Create a new netatmo app](https://dev.netatmo.com/dev/createapp)
|
- [Create a new netatmo app](https://dev.netatmo.com/dev/createapp)
|
||||||
- Download module ```go get github.com/exzz/netatmo-api-go```
|
- Edit ```test/sample.conf```with your credentials
|
||||||
- Try below example (do not forget to edit auth credentials)
|
- run ```go run test/netatmo-api-test.go -f test/sample.conf```
|
||||||
|
- Output shall look like :
|
||||||
## 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 :
|
|
||||||
```
|
```
|
||||||
Station : Home
|
Station : Home
|
||||||
Module : Chambre Elsa
|
Module : Chambre Elsa
|
||||||
|
@ -108,6 +45,8 @@ Station : Home
|
||||||
Pressure : 1028.1 (updated 278s ago)
|
Pressure : 1028.1 (updated 278s ago)
|
||||||
AbsolutePressure : 1008.4 (updated 278s ago)
|
AbsolutePressure : 1008.4 (updated 278s ago)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Tips
|
## Tips
|
||||||
- Only Read() method actually do an API call and refresh all data at once
|
- 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.
|
- 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
80
test/netatmo-api-test.go
Normal 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
4
test/sample.conf
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
clientID = "NETATMO_CLIENTID"
|
||||||
|
clientSecret = "NETATMO_CLIENTSECRET"
|
||||||
|
username = "NETATMO_USERNAME"
|
||||||
|
password = "NETATMO_PASSWORD"
|
Loading…
Reference in a new issue