Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

Xtrophic

Member Since 17 Dec 2012
Offline Last Active Jun 12 2014 08:26 PM
-----

#44732 Dallas 1-wire support library

Posted by Xtrophic on 01 February 2013 - 08:27 PM

Hi,

 

I just wanted to share the little support library I created for my temperature sensor application that deals with 1-wire operations. 

 

The library is currently only supporting DS18S20 and DS18B20 temp sensors, but I created the library to be extendable in the future. 

 

I had fun creating it and it seems to work pretty good. Been logging temperature for the past 2 weeks every 5 mins from 3 probes on the same line. (Although now I rewired the NP2 to attempt to get an LCD display working so it's currently offline hehe).

 

Also here is the code I am using to probe the temp:

 

OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
OutputPort oneWirePort = new OutputPort(Pins.GPIO_PIN_D7, false);


try {               
    Microsoft.SPOT.Hardware.OneWire wire = new OneWire(oneWirePort);
    var devices = wire.FindAllDevices();
    DallasOneWireDevices collection = new DallasOneWireDevices();   // Contains type defined devices with helper commands     

    foreach (byte[] device in devices) {         
        collection.AddDevice(device);   // If supported, it will add as the correct device type it identifies it as.          
    }        

    led.Write(true); // Just lights up the led while conducting 1-wire business :)  

    // Loop through all devices, and if we find any DS18B20, then we set the resolution to 12-bit.     
    foreach (OneWireTemperatureProbe probe in collection) {      
        DeviceDS18B20 probe2 = probe as DeviceDS18B20;           
        if (probe2 != null)                     
            probe2.SetResolution(12, wire);   
    }                
    collection.SendCommandToAll(OneWireDs18b20Reference.CmdConvert, 1050, wire);    // Sends the convert command to all devices connected, and waits 1050ms   
    foreach (OneWireTemperatureProbe probe in collection) {          
        probe.ReadScratchPad(wire); // Reads the scratchpad and updates the last temperature property (which is used below)                   
        Debug.Print(probe.ToString());          
    }         
}        
catch (Exception ex)       
{      
    Debug.Print(ex.ToString());          
}        
led.Write(false);

 

 

Attached Files




home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.