Capture impee id and time stamp

is there a way for me to capture the timestamp from the planner and the impee id so that I can track which of 4 imp connected sensors I am recording in the db?

hardware.getimpeeid()
for timestamps you have a lot of options, but why should you?
Storing a value in a db generates normaly a timestamp automaticly

@DolfTraanberg - there are some cases where you won’t be writing to the database at the same time as you collect the information. Also, depending on the application, you might want it to be a bit more precise (when the sensor was read vs when the sensor was read + time to send to server + time to send to database)

The imp actually has an RTC inside of it, so no need to add an external one :slight_smile: You can access it with the date() function. Here’s the documentation from the Squirrel page about date:

date([time], [format]);

returns a table containing a date/time splitted in the slots:

sec - Seconds after minute (0 - 59).
min - Minutes after hour (0 - 59).
hour - Hours since midnight (0 - 23).
day - Day of month (1 - 31).
month - Month (0 - 11; January = 0).
year - Year (current year).
wday - Day of week (0 - 6; Sunday = 0).
yday - Day of year (0 - 365; January 1 = 0).
if time is omitted the current time is used.
if format can be ‘l’ local time or ‘u’ UTC time, if omitted is defaulted as ‘l’(local time).

@beardedinventor
That’s why I said there were a lot of options.
date() function alone is often confusing, because it has no daylight savings correction

Once you get the correct hour taking into account DST and time zones… I have written code to get you the rest to within Milliseconds.

So what are the alternatives to the date function for grabbing time stamps. I am also working on a project with multiple imps communicating with a db. I need to send a timestamp with each data point. I will be maxing out the sampling rates of the imps (~8KHz) so timestamps are essential to data integrity. Does anyone know how to attach timestamps to items in a blob?

You would likely be making a table / array of some kind… it suspect it would look something like this:

`data <- [];

function addData() {
local dataPoint = getDataPoint(); // read data
local timestamp = time();
data.push({ t = timestamp, d = dataPoint });
}`

I believe we ended up with this code.

// New Clean Temperature code, using array to pass all values.

// Temperature logging example for Ogle Window Systems.
// Temperature Sensor Class for tc74
class TemperatureSensor
{
i2cPort = null;
i2cAddress = null;

constructor(port, address)
{
    if(port == I2C_12)
    {
        // Configure I2C bus on pins 1 & 2
        hardware.configure(I2C_12);
        i2cPort = hardware.i2c12;
    }
    else if(port == I2C_89)
    {
        // Configure I2C bus on pins 8 & 9
        hardware.configure(I2C_89);
        i2cPort = hardware.i2c89;
    }
    else
    {
        server.log("Invalid I2C port specified.");
    }
    // take the address passed and divide by 10
    i2cAddress = address << 1;
}
// Read and return the first byte
function read(register)
{
    //local data = i2cPort.read(0x9B,"",1);//[5,6,7];
    local data = i2cPort.read(i2cAddress, format("%c", register), 1);
    if(data == null)
    {
        server.error("i2c::read method returned NO data");
        return -1;
    }
    return data[0];
}

// Write a byte
function write(register, data)
{
    i2cPort.write(i2cAddress, format("%c%c", register, data));
}

// Stop continuous conversion
function stop()
{
    write(0x09, 0xD5);
}

// Start conversion, continuous or single shot
function start(continuous)
{
    if(continuous == true)
    {
        write(0x09, 0x55);
    }
    else
    {
        write(0x0f, 0x00);
    }
}

// Check if conversion is completed
function isReady()
{
    return (read(0x00) & 0x80)?false:true;
}

// Retrieve temperature (from local sensor) in deg C
function getTemperature()
{
    // Get 11-bit signed temperature value in 0.125C steps
    local temp = (read(0x00) << 3) | (read(0x22) >> 5);

    if(temp & 0x400)
    {
        // Negative two's complement value
        return -((~temp & 0x7FF) + 1) / 8.0;
    }
    else
    {
        // Positive value
        return temp / 8.0;
    }
}

}

// Register with the server
local all = OutputPort(“tempout”,“string”); // impee,channel, time, temp other side

imp.configure(“Temperature Logger”, [], [all]);

function capture()
{
local sensor = TemperatureSensor(I2C_12, 0x4f);

// Set timer for the next capture
imp.wakeup(10.0, capture);

// Start a single shot conversion
sensor.start(false);

// Wait for conversion to complete
while(!sensor.isReady()) imp.sleep(5.5);

// Output the temperature


local temp = sensor.getTemperature();

local sensor2 = hardware.pin8.read();

local sensor3 = hardware.pin9.read();


local tempout = (temp+","+sensor2+","+sensor3);

all.set(tempout);

server.show(tempout);
server.log(tempout);

}

capture();
// End of code.

Not sure if you are using a website/server, or if you are using any server-side scripting such as PHP. With PHP, you can specify your timezone and it automatically handles daylight savings time. Maybe create your own simple API and have the agent go out to your PHP script and get the correct official time?