I2c data type

I am trying to understand data coming via I2C from a Honeywell flow sensor. Here is my code:
hardware.i2c89.configure(CLOCK_SPEED_400_KHZ); local given_i2c_address = 0x49; local impified_i2c_address = given_i2c_address << 1; local data = hardware.i2c89.read(impified_i2c_address, "\\x00", 2); function loop() { //server.log(hardware.i2c89.readerror()); server.log(data); imp.wakeup(10, loop); } loop();
I admit I don’t understand much of what is going on, but I am confident the circuit is wired correctly and can get varying data, but it comes back “b” or “d” or something I would describe as a single ascii character. Any help is appreciated.

I have tried converting what the i2c.read function gave into a hex value, but that returned a negative value that was quite a way off what I expected. The documentation for the function would lead me to believe it kicks out a string, so it makes sense that I am not getting a value I would expect. Any suggestions would be greatly appreciated.
`function hexStringToInt(hexString)
{
// Does the string start with ‘0x’? If so, remove it

if (hexString.slice(0, 2) == "0x") hexString = hexString.slice(2)

// Get the integer value of the remaining string

local intValue = 0

foreach (character in hexString)
{
    local nibble = character - '0'
    if (nibble > 9) nibble = ((nibble & 0x1F) - 7)
    intValue = (intValue << 4) + nibble
}

return intValue

}

function loop()
{
// Only do this if the pin is low (pressed)
//if (buttonPin.read() == 0)
//{
// Collect Data
//local lightValue = lightPin.read();
//local switchValue = switchPin.read();
local flowRead = hardware.i2c89.read(impified_i2c_address, “\x00”, 2);
// Populate fieldData array with respective data values
local flowVal = hexStringToInt(flowRead);
local fieldData = [flowVal, impName];

    // Create a data string to send to the agent. Should be of the form:
    // "fieldName0=fieldData0&fieldName1=fieldData1&fieldName2=fieldData2"
    local data = "";
    for (local i=0; i<FIELD_COUNT; i++)
    {
        data += fieldNames[i] + "=" + fieldData[i];
        if (i < FIELD_COUNT - 1)
            data += "&";
    }
    server.log("Sending Data!");
    agent.send("postData", data);

    imp.wakeup(10,loop); // Wait for a second (lazy debounce, update rate control)

// }
}
loop();`

The string it returns is binary, not ascii or hex encoded. You are reading raw 8 bit values from the peripheral - they likely will require some processing to turn them into numbers you can relate to the measured quantity - the datasheet will say how.

Try this to print the raw data in human-readable hex:

local str=""; foreach(a in data) str+=format("0x%02x ", a); server.log("Received data: "+str);

Hugo, thank you for your help. I have tried several ways to incorporate the code you included, but have gotten back the error message that follows

[Device] ERROR: integer expected for the specified format

If it helps, I am using an zephyr series Honeywell flow sensor.
here is the datasheet: http://sensing.honeywell.com/honeywell-sensing-i2c-communications-digital-airflow-sensors-technical-note-008210-5-en.pdf

The error without the code generating the error isn’t awfully helpful, as I can’t tell what you’re doing…

Just to give you the complete example, then:

local flowRead = hardware.i2c89.read(impified_i2c_address, "\\x00", 2); local str=""; foreach(a in flowRead) str+=format("0x%02x ", a); server.log("Received data: "+str);

While the code successfully output hex bytes, I am looking for an int or a double that I can perform a transform function on. I do appreciate all the help.

Well, what does it output? The datasheet says the first 3 reads are 0x0000, then some versions, then the data.

The datasheet doesn’t make it obvious whether the data is sent MSB first or LSB first.

If it’s MSB then:

local airflow = (flowRead[0] << 8) | flowRead[1];

otherwise…

local airflow = (flowRead[1] << 8) | flowRead[0];

…then just print the airflow. If you’re not discarding the initial values in your code then the first few will look screwy before you get real data.

Thank you Hugo. It was MSB. Here is the code I will implement, in case it of use to anybody else. Most of it is from the Sparkfun tutorial working to datalog from the Phant server.
`function loop()
{
// Only do this if the pin is low (pressed)
//if (buttonPin.read() == 0)
//{
// Collect Data
//local lightValue = lightPin.read();
//local switchValue = switchPin.read();
local flowRead = hardware.i2c89.read(impified_i2c_address, “\x00”, 2);//\x00
local airflow = (flowRead[0] << 8) | flowRead[1];//Thanks Hugo!
// Populate fieldData array with respective data values
//local flowVal = hexStringToInt(flowRead);
//300*((flowVal/16384)-0.1)/0.8;
//foreach(a in data)
//local str="";
//str = format("0x%02x ", flowRead);
//server.log("Received data: "+str);
//local flowVal = hexStringToInt(flowRead);
local flex1 = airflow.tofloat();
local flowXfer = 300*((flex1/16384)-0.1)/0.8;
local fieldData = [flowXfer, impName];

    // Create a data string to send to the agent. Should be of the form:
    // "fieldName0=fieldData0&fieldName1=fieldData1&fieldName2=fieldData2"
    local data = "";
    for (local i=0; i<FIELD_COUNT; i++)
    {
        data += fieldNames[i] + "=" + fieldData[i];
        if (i < FIELD_COUNT - 1)
            data += "&";
    }
    /*local str="";
    str+=format("0x%02x ", a);
    server.log("Received data: "+str);*/
    server.log("Sending Data!");
    agent.send("postData", data);

    imp.wakeup(10,loop); // Wait for a ten seconds (lazy debounce, update rate control)

//}
}
loop();`

Glad you got it working!

One note here is that you do not need to format the data for web transmission on the device; the power of agents means you could just do:

agent.send("fieldData", { field1=flowXfer, field2=impName });

…and send the array you built to the agent, where it can then use http.urlencode to format it for transmission to a remote server. Much neater that way!