1WIRE DS18B20 Multiple-device Code - Help!

Hi!!!
I am working with the code page of electric imp, Implementing a 1-Wire bus on the imp, Multiple Devices and works very well.
https://electricimp.com/docs/resources/onewire/

Temperature values can only see through server.log ()

I tried to create an array or table to work separately with each sensor, ID number to send the agent independently, but I failed for weeks,

someone worked with this?
Thanks!!

Just put the data straight into a table:

Toward the top of the program add:

results = [0,0,0,0]; // Assume you have four 1-Wire devices

then in place of the long server.log() statement, add:

`local deviceData = {};
deviceData.familyID <- format("%2x", slaveId[7]);
deviceData.deviceID <- format("%02x%02x%02x%02x%02x%02x", slaveId[1], slaveId[2], slaveId[3], slaveId[4], slaveId[5], slaveId[6]);
deviceData.currentTemp <- tempCelsius;`

and add/update the results table:

results[device] = deviceData;

You can dump out the contents of results or forward it via the agent to a data storage or charting service later.

PS. You may want to explore the OneWire library, which will help you simplify a lot of this code/

Thanks Smittytone, I’ll tests. this tables and arrays is difficult for me, but I must learn.!

help, I still can not achieve the values stored separately,
Or at least I think so.
on the line
tempCelsius = ((tempMSB * 256) + tempLSB) / 16.0;

I have my temperatures as can be seen in the logs.
but how can I save these values in independent variables, for example Temp0, Temp1, Temp …

I do not speak English and is very hard for me this language, is there electric imp support in the Latin American market? Are there blogs, groups or information in Spanish?

Thank you!

`

function onewireReset() {
// Configure UART for 1-Wire RESET timing
ow.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);
ow.write(0xF0);
ow.flush();
local read = ow.read();
if (read == -1) {
// No UART data at all
server.log(“No circuit connected to UART.”);
return false;
} else if (read == 0xF0) {
// UART RX will read TX if there’s no device connected
server.log(“No 1-Wire devices are present.”);
return false;
} else {
// Switch UART to 1-Wire data speed timing
ow.configure(115200, 8, PARITY_NONE, 1, NO_CTSRTS);
return true;
}
}

function onewireWriteByte(byte) {
for (local i = 0 ; i < 8 ; i++, byte = byte >> 1) {
// Run through the bits in the byte, extracting the
// LSB (bit 0) and sending it to the bus
onewireBit(byte & 0x01);
}
}

function onewireReadByte() {
local byte = 0;
for (local i = 0 ; i < 8 ; i++) {
// Build up byte bit by bit, LSB first
byte = (byte >> 1) + 0x80 * onewireBit(1);
}
return byte;
}

function onewireBit(bit) {
bit = bit ? 0xFF : 0x00;
ow.write(bit);
ow.flush();
local returnVal = ow.read() == 0xFF ? 1 : 0;
return returnVal;
}

function onewireSearch(nextNode) {
local lastForkPoint = 0;

// Reset the bus and exit if no device found
if (onewireReset()) {
    // There are 1-Wire device(s) on the bus, so issue the 1-Wire SEARCH command (0xF0)
    onewireWriteByte(0xF0);

    // Work along the 64-bit ROM code, bit by bit, from LSB to MSB
    for (local i = 64 ; i > 0 ; i--) {
        local byte = (i - 1) / 8;

        // Read bit from bus
        local bit = onewireBit(1);
        
        // Read the next bit
        if (onewireBit(1)) {
            if (bit) {
                // Both bits are 1 which indicates that there are no further devices
                // on the bus, so put pointer back to the start and break out of the loop
                lastForkPoint = 0;
                break;
            }
        } else if (!bit) {
            // First and second bits are both 0: we're at a node
            if (nextNode > i || (nextNode != i && (id[byte] & 1))) {
                // Take the '1' direction on this point
                bit = 1;
                lastForkPoint = i;
            }                
        }

        // Write the 'direction' bit. For example, if it's 1 then all further
        // devices with a 0 at the current ID bit location will go offline
        onewireBit(bit);
        
        // Write the bit to the current ID record
        id[byte] = (id[byte] >> 1) + 0x80 * bit;
    }
}

// Return the last fork point so it can form the start of the next search
return lastForkPoint

}

function onewireSlaves() {
id <- [0,0,0,0,0,0,0,0];
nextDevice <- 65;

while (nextDevice) {
    nextDevice = onewireSearch(nextDevice);
    
    // Store the device ID discovered by one_wire_search() in an array
    // Nb. We need to clone the array, id, so that we correctly save 
    // each one rather than the address of a single array
    slaves.push(clone(id));
}

}

function getTemp() {
local tempLSB = 0;
local tempMSB = 0;
local tempCelsius = 0;

// Wake up in five seconds for the next reading 
imp.wakeup(10.0, getTemp);

// Reset the 1-Wire bus
local result = onewireReset();

if (result) {
    // Issue 1-Wire Skip ROM command (0xCC) to select all devices on the bus
    onewireWriteByte(0xCC);

    // Issue DS18B20 Convert command (0x44) to tell all DS18B20s to get the temperature
    // Even if other devices don't ignore this, we will not read them
    onewireWriteByte(0x44);

    // Wait 750ms for the temperature conversion to finish
    imp.sleep(0.75);

    foreach (device, slaveId in slaves) {
        // Run through the list of discovered slave devices, getting the temperature
        // if a given device is of the correct family number: 0x28 for BS18B20
        if (slaveId[7] == 0x28) {
            onewireReset();
        
            // Issue 1-Wire MATCH ROM command (0x55) to select device by ID
            onewireWriteByte(0x55);
        
            // Write out the 64-bit ID from the array's eight bytes
            for (local i = 7 ; i >= 0 ; i--) {
                onewireWriteByte(slaveId[i]);
            }
        
            // Issue the DS18B20's READ SCRATCHPAD command (0xBE) to get temperature
            onewireWriteByte(0xBE);
        
            // Read the temperature value from the sensor's RAM
            tempLSB = onewireReadByte();
            tempMSB = onewireReadByte();
        
            // Signal that we don't need any more data by resetting the bus
            onewireReset();

            // Calculate the temperature from LSB and MSB
            tempCelsius = ((tempMSB * 256) + tempLSB) / 16.0;
           
            server.log(format("Device: %02d Family: %02x Serial: %02x%02x%02x%02x%02x%02x Temp: %3.2f", (device + 1), slaveId[7], slaveId[1], slaveId[2], slaveId[3], slaveId[4], slaveId[5], slaveId[6], tempCelsius));
           local deviceData = {};
           deviceData.deviceID <- format("%02x%02x",slaveId[5], slaveId[6]);
           deviceData.currentTemp <- tempCelsius;
           results[device] = deviceData;
           
            }
    
    }

}

}

// PROGRAM STARTS HERE
ow <- hardware.uart57
results <- [0,0]
slaves <- []
onewireSlaves()
getTemp()
`

You can’t really make each temperature reading an independent variable. But you can add each one to an array:

`// Create array global at the start of the program
data <- [];

. . . 

// Get Celsius value
tempCelsius = ((tempMSB * 256) + tempLSB) / 16.0;

// Add value to array
data.append(tempCelsius);`

You can then access readings as data[0], data[1], data[2], etc. However, you need to watch out that you don’t just full up the imp’s memory with temperature readings.

Why do you want to keep all the readings? If it’s to make a graph, you should think about sending each reading to the agent, which can forward the data to a graphing web services like plotly or (more simple) Dweet.io.

thank you very much Smittytone, works!!. I mixed two programs, one of grovestrams page and other page 1wire_multiple (we have here in the forum). but could only one temperature graph, now I can send so many temps to plot.

when I get ready I will put a picture of the results.
Thanks again,!