Coder Social home page Coder Social logo

ds18b20.swift's Introduction

DS18B20.swift

A Swift library for the DS18B20 Digital temperature sensor

Linux-only Swift 4 compatible License: MIT

DS18B20

Summary

This is a library for the DS18B20 temperature sensor, available in various forms, from classic ICs to waterproof probes.

The sensor uses the 1-Wire protocol to communicate.

Hardware Details

The sensor can be powered using 3.3V and the signal pin will need a pull-up resistor of 4.7KΩ connected to the same voltage.

The 1-Wire port on the RaspberryPi (pin 4) needs to be enabled via raspi-config before you can use it (restart required), and enabling 1-Wire you'll lose the ability to use the pin as a standard GPIO.

Usage

The temperature returned by the sensor is provided by the only property of the DS18B20 object: Temperature.

The value is in Celsius degrees, if you need a value in Fahrenheit a manual conversion will be necessary.

As for other libraries, to initialize this object you need to retrieve first a OneWireInterface instance from SwiftyGPIO and then initialize the DS18B20 sensor providing a slave id:

let onewires = SwiftyGPIO.hardware1Wires(for:.RaspberryPi2)!
let onewire = onewires[1]

let slaveId = onewire.getSlaves()[0]

let ds = DS18B20(onewire,slaveId: slaveId)

print(ds.Temperature())

The slave id is unique and identifies a specific sensor, since more than one could be connected to the same 1-Wire bus.

In most cases you'll just hardcode the slave id you want, instead of selecting one from the list of available slave devices that could sometime return items in a different order.

Supported Boards

Every board supported by SwiftyGPIO: RaspberryPis, BeagleBones, C.H.I.P., etc...

To use this library, you'll need a Linux ARM board with Swift 3.x/4.x.

The example below will use a RaspberryPi 2 board but you can easily modify the example to use one the other supported boards, a full working demo projects for the RaspberryPi2 is available in the Examples directory.

Installation

Please refer to the SwiftyGPIO readme for Swift installation instructions.

Once your board runs Swift, if your version support the Swift Package Manager, you can simply add this library as a dependency of your project and compile with swift build:

  let package = Package(
      name: "MyProject",
      dependencies: [
        .Package(url: "https://github.com/uraimo/DS18B20.swift.git", majorVersion: 2),
      ]
  ) 

The directory Examples contains sample projects that uses SPM, compile it and run the sample with ./.build/debug/TestDS18B20.

If SPM is not supported, you'll need to manually download the library and its dependencies:

wget https://raw.githubusercontent.com/uraimo/DS18B20.swift/master/Sources/DS18B20.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SwiftyGPIO.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/Presets.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SunXi.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/1Wire.swift 

And once all the files have been downloaded, create an additional file that will contain the code of your application (e.g. main.swift). When your code is ready, compile it with:

swiftc *.swift

The compiler will create a main executable.

ds18b20.swift's People

Contributors

uraimo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ds18b20.swift's Issues

Pi 4 error

Hi,
I tired your library to read two DS18b20 sensors with a Pi 4.
When running the program it throws:
Fatal error: Unexpectedly found nil while unwrapping an Optional value: /.../main.swift
Current stack trace:
Illegal instruction

I made sure that 1wire is enabled. I can read the values using cat in terminal.
It fails here: let onewires = SwiftyGPIO.hardware1Wires(for:.RaspberryPi4)!
When I set it to .RaspberryPi3 it lists the 1wire id but fails as well.
["28-00000cb48019\n", "28-00000cb40e9c\n"]
Couldn't open 1-Wire device: /sys/bus/w1/devices/28-00000cb40e9c
/w1_slave: No such file or directory
Aborted

Anny Ideas?

Cheers

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.