This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mcp9808: add experimental support for digital temperature sensor (#356)
* mcp9808: add support for digital temperature sensor Add support for microchip i2c MCP9808 temperature sensor.
- Loading branch information
1 parent
1bed8c9
commit e8fdbe0
Showing
6 changed files
with
1,622 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2018 The Periph Authors. All rights reserved. | ||
// Use of this source code is governed under the Apache License, Version 2.0 | ||
// that can be found in the LICENSE file. | ||
|
||
// mcp9808 communicates with an mcp9808 sensor reading ambient temperature. | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"periph.io/x/periph/conn/i2c/i2creg" | ||
"periph.io/x/periph/experimental/devices/mcp9808" | ||
"periph.io/x/periph/host" | ||
) | ||
|
||
func mainImpl() error { | ||
if _, err := host.Init(); err != nil { | ||
return err | ||
} | ||
address := flag.Int("address", 0x18, "I²C address") | ||
i2cbus := flag.String("bus", "", "I²C bus (/dev/i2c-1)") | ||
|
||
flag.Parse() | ||
|
||
fmt.Println("Starting MCP9808 Temperature Sensor") | ||
if _, err := host.Init(); err != nil { | ||
return err | ||
} | ||
|
||
// Open default I²C bus. | ||
bus, err := i2creg.Open(*i2cbus) | ||
if err != nil { | ||
return fmt.Errorf("failed to open I²C: %v", err) | ||
} | ||
defer bus.Close() | ||
|
||
// Create a new temperature sensor a sense with default options. | ||
sensor, err := mcp9808.New(bus, &mcp9808.Opts{Addr: *address}) | ||
if err != nil { | ||
return fmt.Errorf("failed to open new sensor: %v", err) | ||
} | ||
|
||
// Read values from sensor every second. | ||
everySecond := time.Tick(time.Second) | ||
var halt = make(chan os.Signal) | ||
signal.Notify(halt, syscall.SIGTERM) | ||
signal.Notify(halt, syscall.SIGINT) | ||
|
||
fmt.Println("ctrl+c to exit") | ||
for { | ||
select { | ||
case <-everySecond: | ||
t, err := sensor.SenseTemp() | ||
if err != nil { | ||
return fmt.Errorf("sensor reading error: %v", err) | ||
} | ||
fmt.Println(t) | ||
case <-halt: | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
if err := mainImpl(); err != nil { | ||
fmt.Fprintf(os.Stderr, "mcp9808: %s.\n", err) | ||
return | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2018 The Periph Authors. All rights reserved. | ||
// Use of this source code is governed under the Apache License, Version 2.0 | ||
// that can be found in the LICENSE file. | ||
|
||
// Package mcp9808 controls a Microchip MCP9808 digital I²C temperature sensor. | ||
// | ||
// Features | ||
// | ||
// -40°C and +125°C Operating Range. | ||
// | ||
// User-Selectable Measurement Resolution: +0.5°C, +0.25°C, +0.125°C, +0.0625°C. | ||
// | ||
// User-Programmable Temperature Alerts. | ||
// | ||
// Datasheet | ||
// | ||
// http://ww1.microchip.com/downloads/en/DeviceDoc/25095A.pdf | ||
package mcp9808 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2018 The Periph Authors. All rights reserved. | ||
// Use of this source code is governed under the Apache License, Version 2.0 | ||
// that can be found in the LICENSE file. | ||
|
||
package mcp9808_test | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"periph.io/x/periph/conn/i2c/i2creg" | ||
"periph.io/x/periph/conn/physic" | ||
"periph.io/x/periph/experimental/devices/mcp9808" | ||
"periph.io/x/periph/host" | ||
) | ||
|
||
func ExampleDev_SenseTemp() { | ||
// Make sure periph is initialized. | ||
if _, err := host.Init(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Open default I²C bus. | ||
bus, err := i2creg.Open("") | ||
if err != nil { | ||
log.Fatalf("failed to open I²C: %v", err) | ||
} | ||
defer bus.Close() | ||
|
||
// Create a new temperature sensor. | ||
sensor, err := mcp9808.New(bus, &mcp9808.DefaultOpts) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
// Read values from sensor. | ||
measurement, err := sensor.SenseTemp() | ||
|
||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
fmt.Println(measurement) | ||
} | ||
|
||
func ExampleDev_SenseWithAlerts() { | ||
// Make sure periph is initialized. | ||
if _, err := host.Init(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Open default I²C bus. | ||
bus, err := i2creg.Open("") | ||
if err != nil { | ||
log.Fatalf("failed to open I²C: %v", err) | ||
} | ||
defer bus.Close() | ||
|
||
// Create a new temperature sensor. | ||
sensor, err := mcp9808.New(bus, &mcp9808.DefaultOpts) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
lower := physic.ZeroCelsius | ||
upper := physic.ZeroCelsius + 25*physic.Celsius | ||
critical := physic.ZeroCelsius + 32*physic.Celsius | ||
|
||
// Read values from sensor. | ||
temperature, alerts, err := sensor.SenseWithAlerts(lower, upper, critical) | ||
|
||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
for _, alert := range alerts { | ||
fmt.Println(alert) | ||
} | ||
|
||
fmt.Println(temperature) | ||
} |
Oops, something went wrong.