akrv
akrv

Reputation: 55

Using DS2408 with Raspberry Pi GPIO 4 1wire linux driver

I have a Raspberry Pi B+ with the latest raspbian. I activated 1wire support through interface options in raspi-config command and I connected my DS2408 chip on to the GPIO 4.

When I try to check for the list of devices, they all show up

ls -la /sys/bus/w1/devices/

when I try to cat their output or status I receive ?. when I try to write on the output file, the nothing happens.

I have two questions:

  1. Is there an easier software interface to communicate with the DS2408? There is a lot of documentation for reading temperature sensor data but DS2408 does not work like that.
    1. I tried with a 1wire USB dongle and OWFS which works, but is there a possibility to make the GPIO 4 as the bus master for OWFS and communicate using the OWFS software interface?

answer to any one of these questions would solve my problem of talking to DS2408 connected to GPIO 4 of a RPI B+

Upvotes: 0

Views: 660

Answers (3)

Jeff
Jeff

Reputation: 1

The DS2408 works well with the Raspberry Pi using 1-wire, I am using a 8 port relay from http://denkovi.com.

To turn a relay on all you have to do is set the correct bit in the "/sys/bus/w1/devices/29-xxxxx/output" file, however to ensure you don't change any of the other relays it is best to read "/sys/bus/w1/devices/29-xxxx/state" and OR or XOR to turn a relay on or off.

Here is a VERY basic python script (I am by no means a programmer):

#!/usr/bin/python
import sys
from time import sleep

readfile = "/sys/bus/w1/devices/29-xxxxx/state"
writefile = "/sys/bus/w1/devices/29-xxxxx/output"

def getTheState():
    try:
        f = open(readfile, 'rb')
        while True:
            binarycontent = f.read(-1)
            if not binarycontent:
                break
            mynum = int.from_bytes(binarycontent,byteorder=sys.byteorder)
        f.close()
    except IOError:
        print('Error While Opening the file!')
    
    return(mynum)

def turnOnRelay(theRelay,theState):
    theTag = theRelay
    # Turn On Relay
    if theTag == 1 :
        tagMask = 0b11111110
    elif theTag == 2 :
        tagMask = 0b11111101
    elif theTag == 3 :
        tagMask = 0b11111011
    elif theTag == 4 :
        tagMask = 0b11110111
    elif theTag == 5 :
        tagMask = 0b11101111
    elif theTag == 6 :
        tagMask = 0b11011111
    elif theTag == 7 :
        tagMask = 0b10111111
    elif theTag == 8 :
        tagMask = 0b01111111
    else:
        print("Error")

    theNewState = theState & tagMask
    print("theNewState",theNewState)
    return theNewState

def turnOffRelay(theRelay,theState):
    theTag = theRelay
    # Turn Off Relay
    if theTag == 1 :
        tagMask = 0b00000001
    elif theTag == 2 :
        tagMask = 0b00000010
    elif theTag == 3 :
        tagMask = 0b00000100
    elif theTag == 4 :
        tagMask = 0b00001000
    elif theTag == 5 :
        tagMask = 0b00010000
    elif theTag == 6 :
        tagMask = 0b00100000
    elif theTag == 7 :
        tagMask = 0b01000000
    elif theTag == 8 :
        tagMask = 0b10000000
    else:
        print("Error")

    theNewState = theState ^ tagMask
    print("theNewState",theNewState)
    return theNewState

def updateTheState(theNewState):
    try:
        newFileBytes = [theNewState]
        newFile = open(writefile, "wb")
        # write to file
        for byte in newFileBytes:
            newFile.write(byte.to_bytes(1, byteorder='big'))
    except:
        print('Error While Opening the file!')

def main():
 
    print("turn relay 1 on")
    x = getTheState()
    y = turnOnRelay(1,x)
    updateTheState(y)

    sleep(5)
    print("turn relay 1 off")
    x = getTheState()
    y = turnOffRelay(1,x)
    updateTheState(y)
    

if __name__ == '__main__':
    main()

Good luck hope this helps.

j.

Upvotes: 0

Paul
Paul

Reputation: 16

Actually I have managed to operate DS2408 with Raspberry Pi 4 but I had really tough time getting it to work. First of all it had to be connected properly: RSTZ through 10k resistor to Vcc, I/O pin to GPIO4 and GPIO4 to 3V3 through 4,7k resistor. After that I was able to read and write using "echo -n -e '\x02' > output", where output was in /sys/bus/w1/devices/29-...

Upvotes: 0

akrv
akrv

Reputation: 55

I implemented my 345 nodes with a 23 1wire bus. The idea was to simplify cost by using the 1wire GPIO on the RPi but the implementation was limited or I wasn't able to easily find the DS2408 driver implementation on the 1wire interface from RPi.

So went down this path of having a dongle per RPi to drive the 1wire using OWFS.

to answer my own question:

  1. the 1wire implementation in RPi is limited.
  2. USB dongle with OWFS is the only way to drive DS2408. If it is not possible, you would have to write your own extension.

Upvotes: 0

Related Questions