Having trouble storing a float from uart

Hi, I am having trouble with storing a variable read from a uart serial stream. I am able to read the value in and store it locally but when I I try and read the variable outside the callbacks function it is empty. I am unsure if this is a scope issue or somehow its value is being erased. Any help would be appreciated.

Can you post code?

Very, very sorry, @Spellbin but somehow I have deleted your code. Not sure how, but it’s gone. Can you add it back please? My response posted before the code vanished for some reason

`inputString <- "";
ph <- "";

function readback() {
    // Function triggered by receipt of a byte from the connected computer
    // Adds the input byte as an alphanumeric character to a buffer string
    // which is displayed in the log when the remote user hits Enter
    
    local byte = phProbe.read();
    
    // Ignore initial input / no data signal
    
    if (byte == -1) return;
    
    if (byte == 13) {
        // Carriage return received? Output the string and clear it for the next input
    
        //server.log("Sent string: " + inputString);
        ph = inputString.tofloat();
        server.log("p=" + ph);
        inputString = "";

    } else {
        // Add the input character to the buffer

        inputString = inputString + chr(byte);
    }
}

function chr(asciiValue) {
    // Convert passed integer value Ascii code into a character string
    
    if (asciiValue < 32) return "";
    return format("%c", asciiValue);
}
function one_wire_reset()
{
    // Configure UART for 1-Wire RESET timing
    
    ow.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);
    ow.write(0xF0);
    ow.flush();
    if (ow.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 one_wire_write_byte(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
        
        one_wire_bit(byte & 0x01);
    }
} 


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


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


function one_wire_slaves()
{
    id <- [0,0,0,0,0,0,0,0];
    next_device <- 65;
    //server.log("one_wire_slaves");

    while(next_device)
    {
        next_device = one_wire_search(next_device);
        
        // 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 get_temp()
{
    //server.log("getting temps...");
    local temp_LSB = 0; 
    local temp_MSB = 0; 
    local temp_celsius = 0; 

    // We are not doing this imp.wakeup because we're using deep sleep instead with server.sleepfor called by imp.onidle
    //imp.wakeup(5.0, get_temp);
    
    // Reset the 1-Wire bus
    
    one_wire_reset();
    
    // Issue 1-Wire Skip ROM command (0xCC) to select all devices on the bus
    
    one_wire_write_byte(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
    
    one_wire_write_byte(0x44);
    
    // Wait 750ms for the temperature conversion to finish
    
    imp.sleep(0.75);

    local bigdata=[]
    foreach (device, slave_id 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 (slave_id[7] == 0x28)
        {
            one_wire_reset();
            
            // Issue 1-Wire MATCH ROM command (0x55) to select device by ID
            
            one_wire_write_byte(0x55);
            
            // Write out the 64-bit ID from the array's eight bytes
            
            for (local i = 7 ; i >= 0; i--)
            {
                one_wire_write_byte(slave_id[i]);
            }
            
            // Issue the DS18B20's READ SCRATCHPAD command (0xBE) to get temperature
            
            one_wire_write_byte(0xBE);
            
            // Read the temperature value from the sensor's RAM
            
            temp_LSB = one_wire_read_byte();
            temp_MSB = one_wire_read_byte();
            
            // Signal that we don't need any more data by resetting the bus
            
            one_wire_reset();

            // Calculate the temperature from LSB and MSB
            
            temp_celsius = ((temp_MSB * 256) + temp_LSB) / 16.0;
 
            server.log(format("Device: %02d Family: %02x Serial: %02x%02x%02x%02x%02x%02x Temp: %3.2f", (device + 1), slave_id[7], slave_id[1], slave_id[2], slave_id[3], slave_id[4], slave_id[5], slave_id[6], temp_celsius));
            local sensordata = {
                device_num = (device + 1),
                family = slave_id[7],
                serial = format("%02x%02x%02x%02x%02x%02x", slave_id[1], slave_id[2], slave_id[3], slave_id[4], slave_id[5], slave_id[6]),
                temp = temp_celsius,
                time_stamp = getTime()
            }
            bigdata.append(sensordata);
            //agent.send("new_readings", sensordata);
        }
    }
    server.log(format("Supply Voltage: %2.3f", hardware.voltage()));
    bigdata.append({
    device_num = "7",
    family = "ElectricImp",
    serial = "Light Level",
    temp = hardware.lightlevel()/10000.0,
    time_stamp = getTime()    
    })
    server.log(format("Light Level: %2.3f", hardware.lightlevel()/10000.0));
    
    
    server.log("1ph:" + inputString);
    agent.send("bigdata", bigdata);
    
    server.log("Time:"+ getTime());
    server.log("2ph: " + ph);
    
}

// PROGRAM STARTS HERE

// Set our idle function to sleep until one minute from now.
imp.onidle(function() {
   
    //server.log("Time for a nap.");
    //server.sleepfor(60);
    // every minute, on the minute
   // server.sleepfor(1800 - (time() % 1800));
     server.sleepfor(10);
});


ow <- hardware.uart57;
slaves <- [];
phProbe <- hardware.uart12;
phProbe.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, readback);

// Enumerate the slaves on the bus

one_wire_slaves();
// Start sampling temperature data
get_temp();
`

At what point do you expect data on the phProbe UART to arrive? You set the port up and install a handler for it, but there doesn’t seem to be any opportunity allowed for your code to receive the bytes. I could be wrong, but I assume that readback will not be called during the imp.sleep(0.75) pause, as it blocks everything. This means that any pending serial data will be buffered until after get_temp() completes execution. If there is serial data pending, readback will be called successive times until the receive buffer is clear. As soon as the last byte is read, the imp will be idle and it will sleep for 10 seconds. At that point you will get a warm reset, clearing inputString and ph. This means any data you got will be lost.

imp.idle and imp.sleep need to be used with caution. I suggest that you use the receipt of a carriage return as your cue to send data to the agent, then sleep. I also suggest that you look at some of the examples that take advantage of the asynchronous nature of squirrel. If you have to put long pauses (0.75s is a long one) in your code, there is probably a better way of doing it.

I am wondering if maybe I need to have the imp sleep for a period before polling the data, or maybe poll the data through a function other than the .configure() callback function. As it stands, the ph variable is populated within the readback() function but the data fails to reach the global scope outside when I attempt to read the data from another function. Any help would be greatly appreciated!

A thought: server.sleepfor() puts the imp into deep sleep. When the imp wakes, it performs a warm boot, ie. all pre-existing data is lost, and the program starts afresh. Is your device sleeping between ph being set in readback(), and the imp going idle and deep sleeping – after which, any attempt to read ph will return an empty string (you initialise it as

""

).

You might change line 2 to ph <- 99.99; to confirm this.

Would the initial type casting have anything to do with it.
You state ph <- “”; maybe ph <- 0.0; or ph <- null; would do the trick so that ph = inputString.tofloat(); works as intended.

Coverdriven, okay that makes sense. It would appear that the data is pulled from the probe after a slight delay. The callback function is actually called 4 times for each cycle of the imp. Asynchronous programming is fairly new to me. Could you provide an example of how to poll this information? Also, rather than sleeping during the temperature probe function what other options could I implement? Thanks.

Sure, which do you expect to happen first? The reception of the full inputString on the uart or the completion the temperature conversion? It’s a little bit harder if this varies, as you’d have to use a semaphore. If one always finishes after the other, we can tie the agent.send to the latter event.

It shouldn’t matter which is polled first so long as both sets of data are sent to the agent. Since it appears the ph probe data is populated after a slight delay perhaps we can poll the temperature data first, however, if it’s easier one way than another that will work for me.

I’ve made a few edits, namely:

  • split get_temp() into 3 functions, start_temp() to trigger temp sampling, get_temp() to acquire temp values at end of conversion and send_and_sleep(string) to post data to agent.
  • added server.settimeoutpolicy() at beginning of code. You need to read the documentation on this and decide how much control you want over imp connectivity and error handling.
  • removed imp.onidle(), you don’t need it.

This code will (probably) work if you do indeed receive a terminated value on your phProbe uart. I don’t see from the code how it will be triggered, though. If you don’t receive a , it will not sleep, ever. You could define a maximum timeout to wait and send a message to the agent to say you didn’t get anything on phProbe, which is probably a more fault tolerant approach. If you get a BEFORE the temperature conversion is complete, then bigdata will not have the data you expect.

Perhaps I’ve missed something but it’s still not clear to me from the code when you expect to receive your inputString.

`// you should consider what settings you want in this call…
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_FOR_ACK, 10);

inputString <- “”;
ph <- “”;
bigdata <- [];

function readback() {
// Function triggered by receipt of a byte from the connected computer
// Adds the input byte as an alphanumeric character to a buffer string
// which is displayed in the log when the remote user hits Enter

local byte = phProbe.read();

// Ignore initial input / no data signal

if (byte == -1) return;

if (byte == 13) {
    // Carriage return received? Output the string and clear it for the next input

    //server.log("Sent string: " + inputString);
    //ph = inputString.tofloat();
    //server.log("p=" + ph);
    //inputString = "";
    
    send_and_sleep(inputString);

} else {
    // Add the input character to the buffer

    inputString = inputString + chr(byte);
}

}

function chr(asciiValue) {
// Convert passed integer value Ascii code into a character string

if (asciiValue < 32) return "";
return format("%c", asciiValue);

}
function one_wire_reset()
{
// Configure UART for 1-Wire RESET timing

ow.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);
ow.write(0xF0);
ow.flush();
if (ow.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 one_wire_write_byte(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

    one_wire_bit(byte & 0x01);
}

}

function one_wire_read_byte()
{
local byte = 0;
for (local i = 0; i < 8; i++)
{
// Build up byte bit by bit, LSB first

    byte = (byte >> 1) + 0x80 * one_wire_bit(1);
}

return byte;

}

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

function one_wire_slaves()
{
id <- [0,0,0,0,0,0,0,0];
next_device <- 65;
//server.log(“one_wire_slaves”);

while(next_device)
{
    next_device = one_wire_search(next_device);
    
    // 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 start_temp()
{

// We are not doing this imp.wakeup because we're using deep sleep instead with server.sleepfor called by imp.onidle
//imp.wakeup(5.0, get_temp);

// Reset the 1-Wire bus

one_wire_reset();

// Issue 1-Wire Skip ROM command (0xCC) to select all devices on the bus

one_wire_write_byte(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

one_wire_write_byte(0x44);

}

function get_temp()
{
//server.log(“getting temps…”);
local temp_LSB = 0;
local temp_MSB = 0;
local temp_celsius = 0;

//local bigdata=[]
foreach (device, slave_id 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 (slave_id[7] == 0x28)
    {
        one_wire_reset();
        
        // Issue 1-Wire MATCH ROM command (0x55) to select device by ID
        
        one_wire_write_byte(0x55);
        
        // Write out the 64-bit ID from the array's eight bytes
        
        for (local i = 7 ; i >= 0; i--)
        {
            one_wire_write_byte(slave_id[i]);
        }
        
        // Issue the DS18B20's READ SCRATCHPAD command (0xBE) to get temperature
        
        one_wire_write_byte(0xBE);
        
        // Read the temperature value from the sensor's RAM
        
        temp_LSB = one_wire_read_byte();
        temp_MSB = one_wire_read_byte();
        
        // Signal that we don't need any more data by resetting the bus
        
        one_wire_reset();

        // Calculate the temperature from LSB and MSB
        
        temp_celsius = ((temp_MSB * 256) + temp_LSB) / 16.0;

        server.log(format("Device: %02d Family: %02x Serial: %02x%02x%02x%02x%02x%02x Temp: %3.2f", (device + 1), slave_id[7], slave_id[1], slave_id[2], slave_id[3], slave_id[4], slave_id[5], slave_id[6], temp_celsius));
        local sensordata = {
            device_num = (device + 1),
            family = slave_id[7],
            serial = format("%02x%02x%02x%02x%02x%02x", slave_id[1], slave_id[2], slave_id[3], slave_id[4], slave_id[5], slave_id[6]),
            temp = temp_celsius,
            time_stamp = getTime()
        }
        bigdata.append(sensordata);
        //agent.send("new_readings", sensordata);
    }
}
server.log(format("Supply Voltage: %2.3f", hardware.voltage()));
bigdata.append({
device_num = "7",
family = "ElectricImp",
serial = "Light Level",
temp = hardware.lightlevel()/10000.0,
time_stamp = getTime()    
})
server.log(format("Light Level: %2.3f", hardware.lightlevel()/10000.0));

}

// post data to agent and go to sleep for 10 seconds, imp will resume with warm boot
function send_and_sleep(floatStr)
{
local ph = floatStr.tofloat();
server.log(“1ph:” + floatStr);
server.log(“Time:”+ getTime());
server.log("2ph: " + ph);

agent.send("bigdata", bigdata);
server.flush(2);
server.sleepfor(10);

}

// PROGRAM STARTS HERE

ow <- hardware.uart57;
slaves <- [];
phProbe <- hardware.uart12;
phProbe.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, readback);

// Enumerate the slaves on the bus
one_wire_slaves();

// Start sampling temperature data
start_temp();

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

`

Awesome! Thanks coverdriven, I appreciate your help. I’ll implement the changes this weekend and let you know how it goes.

Just a couple of amendments to my above post, the forum took out my Carriage Return symbols when I posted it.
It should read:

If you don't receive a CR, it will not sleep, ever. You could define a maximum timeout to wait and send a message to the agent to say you didn't get anything on phProbe, which is probably a more fault tolerant approach. If you get a CR BEFORE the temperature conversion is complete, then bigdata will not have the data you expect.

Additionally, if you’re not sure when the phProbe returns the string, I can show you how to add a semaphore to trigger sending data to the agent.

Ah. Yes, that would be great. Even just to learn how the principal works. I am a disabled programmer and require assistance to type so I will have to wait until tomorrow at the earliest to try these changes out. Thanks again.

Here’s updated code where it checks that both the reading of the probe Value and reading of the one wire probes are complete before sending. There are still a lot of assumptions here, but it’s a reasonable starting point.

`// you should consider what settings you want in this call…
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_FOR_ACK, 10);

inputString <- “”;
ph <- 0.0;
bigdata <- [];
probeComplete <- false;
tempComplete <- false;

function readback() {
// Function triggered by receipt of a byte from the connected computer
// Adds the input byte as an alphanumeric character to a buffer string
// which is displayed in the log when the remote user hits Enter

local byte = phProbe.read();

// Ignore initial input / no data signal

if (byte == -1) return;

if (byte == 13) {
    // Carriage return received? Output the string and clear it for the next input

    server.log("Sent string: " + inputString);
    ph = inputString.tofloat();
    server.log("p=" + ph);
    inputString = "";
    
    probeComplete = true;
    if (tempComplete)
        send_and_sleep();
        
} else {
    // Add the input character to the buffer

    inputString = inputString + chr(byte);
}

}

function chr(asciiValue) {
// Convert passed integer value Ascii code into a character string

if (asciiValue < 32) return "";
return format("%c", asciiValue);

}
function one_wire_reset()
{
// Configure UART for 1-Wire RESET timing

ow.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);
ow.write(0xF0);
ow.flush();
if (ow.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 one_wire_write_byte(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

    one_wire_bit(byte & 0x01);
}

}

function one_wire_read_byte()
{
local byte = 0;
for (local i = 0; i < 8; i++)
{
// Build up byte bit by bit, LSB first

    byte = (byte >> 1) + 0x80 * one_wire_bit(1);
}

return byte;

}

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

function one_wire_slaves()
{
id <- [0,0,0,0,0,0,0,0];
next_device <- 65;
//server.log(“one_wire_slaves”);

while(next_device)
{
    next_device = one_wire_search(next_device);
    
    // 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 start_temp()
{

// We are not doing this imp.wakeup because we're using deep sleep instead with server.sleepfor called by imp.onidle
//imp.wakeup(5.0, get_temp);

// Reset the 1-Wire bus

one_wire_reset();

// Issue 1-Wire Skip ROM command (0xCC) to select all devices on the bus

one_wire_write_byte(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

one_wire_write_byte(0x44);

}

function get_temp()
{
//server.log(“getting temps…”);
local temp_LSB = 0;
local temp_MSB = 0;
local temp_celsius = 0;

//local bigdata=[]
foreach (device, slave_id 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 (slave_id[7] == 0x28)
    {
        one_wire_reset();
        
        // Issue 1-Wire MATCH ROM command (0x55) to select device by ID
        
        one_wire_write_byte(0x55);
        
        // Write out the 64-bit ID from the array's eight bytes
        
        for (local i = 7 ; i >= 0; i--)
        {
            one_wire_write_byte(slave_id[i]);
        }
        
        // Issue the DS18B20's READ SCRATCHPAD command (0xBE) to get temperature
        
        one_wire_write_byte(0xBE);
        
        // Read the temperature value from the sensor's RAM
        
        temp_LSB = one_wire_read_byte();
        temp_MSB = one_wire_read_byte();
        
        // Signal that we don't need any more data by resetting the bus
        
        one_wire_reset();

        // Calculate the temperature from LSB and MSB
        
        temp_celsius = ((temp_MSB * 256) + temp_LSB) / 16.0;

        server.log(format("Device: %02d Family: %02x Serial: %02x%02x%02x%02x%02x%02x Temp: %3.2f", (device + 1), slave_id[7], slave_id[1], slave_id[2], slave_id[3], slave_id[4], slave_id[5], slave_id[6], temp_celsius));
        local sensordata = {
            device_num = (device + 1),
            family = slave_id[7],
            serial = format("%02x%02x%02x%02x%02x%02x", slave_id[1], slave_id[2], slave_id[3], slave_id[4], slave_id[5], slave_id[6]),
            temp = temp_celsius,
            time_stamp = getTime()
        }
        bigdata.append(sensordata);
        //agent.send("new_readings", sensordata);
    }
}
server.log(format("Supply Voltage: %2.3f", hardware.voltage()));
bigdata.append({
device_num = "7",
family = "ElectricImp",
serial = "Light Level",
temp = hardware.lightlevel()/10000.0,
time_stamp = getTime()    
})
server.log(format("Light Level: %2.3f", hardware.lightlevel()/10000.0));

tempComplete = true;
if (probeComplete)
    send_and_sleep();

}

// post data to agent and go to sleep for 10 seconds, imp will resume with warm boot
function send_and_sleep()
{
server.log(“1ph:” + inputStr);
server.log(“Time:”+ getTime());
server.log("2ph: " + ph);

agent.send("bigdata", bigdata);
server.flush(2);
server.sleepfor(10);

}

// PROGRAM STARTS HERE

ow <- hardware.uart57;
slaves <- [];
phProbe <- hardware.uart12;
phProbe.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, readback);

// Enumerate the slaves on the bus
one_wire_slaves();

// Start sampling temperature data
start_temp();

// Wait 750ms for the temperature conversion to finish
imp.wakeup(0.75,get_temp);
`

Hurray! I was able to implement the changes and have got it working as intended with a few changes. I turned off the continuous reading mode on the ph probe and now manually request the data. Thanks again for all your help coverdriven, its working great now. I will post my latest code.

// you should consider what settings you want in this call…
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_FOR_ACK, 10);

inputString <- “”;
ph <- 0.0;
bigdata <- [];
probeComplete <- false;
tempComplete <- false;

function readback() {
// Function triggered by receipt of a byte from the connected computer
// Adds the input byte as an alphanumeric character to a buffer string
// which is displayed in the log when the remote user hits Enter

local byte = phProbe.read();

// Ignore initial input / no data signal

if (byte == -1) return;

if (byte == 13) {
    // Carriage return received? Output the string and clear it for the next input

    server.log("Sent string: " + inputString);
    ph = inputString.tofloat();
    server.log("p=" + ph);
    inputString = "";
    
    probeComplete = true;
    if (tempComplete)
        send_and_sleep();
        
} else {
    // Add the input character to the buffer

    inputString = inputString + chr(byte);
}

}

function chr(asciiValue) {
// Convert passed integer value Ascii code into a character string

if (asciiValue < 32) return "";
return format("%c", asciiValue);

}

function start_temp()
{

// We are not doing this imp.wakeup because we're using deep sleep instead with server.sleepfor called by imp.onidle
//imp.wakeup(5.0, get_temp);

// Reset the 1-Wire bus

one_wire_reset();

// Issue 1-Wire Skip ROM command (0xCC) to select all devices on the bus

one_wire_write_byte(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

one_wire_write_byte(0x44);

}

function get_temp()
{
//server.log(“getting temps…”);
local temp_LSB = 0;
local temp_MSB = 0;
local temp_celsius = 0;
bigdata=[];

foreach (device, slave_id 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 (slave_id[7] == 0x28)
    {
        one_wire_reset();
        
        // Issue 1-Wire MATCH ROM command (0x55) to select device by ID
        
        one_wire_write_byte(0x55);
        
        // Write out the 64-bit ID from the array's eight bytes
        
        for (local i = 7 ; i >= 0; i--)
        {
            one_wire_write_byte(slave_id[i]);
        }
        
        // Issue the DS18B20's READ SCRATCHPAD command (0xBE) to get temperature
        
        one_wire_write_byte(0xBE);
        
        // Read the temperature value from the sensor's RAM
        
        temp_LSB = one_wire_read_byte();
        temp_MSB = one_wire_read_byte();
        
        // Signal that we don't need any more data by resetting the bus
        
        one_wire_reset();

        // Calculate the temperature from LSB and MSB
        
        temp_celsius = ((temp_MSB * 256) + temp_LSB) / 16.0;

        server.log(format("Device: %02d Family: %02x Serial: %02x%02x%02x%02x%02x%02x Temp: %3.2f", (device + 1), slave_id[7], slave_id[1], slave_id[2], slave_id[3], slave_id[4], slave_id[5], slave_id[6], temp_celsius));
        local sensordata = {
            device_num = (device + 1),
            family = slave_id[7],
            serial = format("%02x%02x%02x%02x%02x%02x", slave_id[1], slave_id[2], slave_id[3], slave_id[4], slave_id[5], slave_id[6]),
            temp = temp_celsius,
            time_stamp = getTime()
        }
        bigdata.append(sensordata);
        //agent.send("new_readings", sensordata);
    }
}
server.log(format("Supply Voltage: %2.3f", hardware.voltage()));
bigdata.append({
device_num = "7",
family = "ElectricImp",
serial = "Light Level",
temp = hardware.lightlevel()/10000.0,
time_stamp = getTime()    
})
server.log(format("Light Level: %2.3f", hardware.lightlevel()/10000.0));

tempComplete = true;

//if (probeComplete) {
    bigdata.append({
    device_num = "8",
    family = "ElectricImp",
    serial = "ph",
    temp = ph,
    time_stamp = getTime()    
})

send_and_sleep();

    
//}

}

// post data to agent and go to sleep for 10 seconds, imp will resume with warm boot
function send_and_sleep()
{
server.log(“Time:”+ getTime());
server.log("ph: " + ph);

agent.send("bigdata", bigdata);
server.flush(2);
//server.sleepfor(10);
imp.sleep(10);

}

// PROGRAM STARTS HERE

ow <- hardware.uart57;
slaves <- [];
phProbe <- hardware.uart12;
phProbe.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, readback);

// Enumerate the slaves on the bus

one_wire_slaves();

function poll() {

server.log("Time for a nap.");
start_temp();
// Wait 750ms for the temperature conversion to finish
imp.wakeup(0.75,get_temp);

// server.sleepfor(1800 - (time() % 1800));

// request ph reading from probe

 phProbe.write("R\\r");
 imp.wakeup(10, poll);

}
start_temp();
imp.wakeup(0.75, get_temp);
phProbe.write(“R\r”);
poll();