Hello all,
I am writing a piece of code so I can interact with the Grid Eye. I am porting the code from the Arduino on to the Imp. I am having issue though when I read upperLevel and lowerLevel.
The error is here
2015-07-20 15:17:38 UTC-7 [Status] Device connected
2015-07-20 15:17:38 UTC-7 [Device] Beginning Program
2015-07-20 15:17:38 UTC-7 [Device] binary: 55 70 70 65 72 20 4c 65 76 65 6c 20 00
2015-07-20 15:17:38 UTC-7 [Device] binary: 4c 6f 77 65 72 20 4c 65 76 65 6c 20 00
2015-07-20 15:17:38 UTC-7 [Device] ERROR: bitwise op between ‘string’ and 'integer’
2015-07-20 15:17:38 UTC-7 [Device] ERROR: at GridEye:53
2015-07-20 15:17:38 UTC-7 [Device] ERROR: from main:90
I am only supposed to read one byte for each upperLevel and lowerLevel.
For the arduino, it works fine and I notice that the lowerLevel is almost always 0 for it. Here is the Arduino Code for reference.
I have read the i2c reference and tried a lot of things. I remember that at one point it worked ok but I can’t find the build and don’t want to go through them all.
To clarify, when I have this:
//upperLevel = upperLevel[0]
//lowerLevel = lowerLevel[0]
I get values of 0.
Here is the code for the imp (device). The i2c reading will be on the function GridEye.
`i2c <- hardware.i2c12;
hardware.i2c12.configure(CLOCK_SPEED_100_KHZ);
//Original I2C address is 0x068. Shifted to the right one bit is 0xD0
const i2c_grideye = 0xD0;
Threshold <- 0;
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 + 2
}
}
function GridEye(){
server.log(“Beginning Program”)
local pixelTempL = “0x80”;
local aveTemp = 0;
local celsius = 0;
for(local pixel = 0; pixel < 64; pixel++)
{
//Must pass pixelTempL as a string
i2c.write(i2c_grideye, pixelTempL);
local upperLevel = i2c.read(i2c_grideye, “”, 1);
//local lowerLevel = i2c.read(i2c_grideye, “”, 1);
//server.log("upperLevel " + upperLevel)
local lowerLevel = i2c.read(i2c_grideye, “”, 1);
//local upperLevel = i2c.read(i2c_grideye, “”, 1);
//server.log("lowerLevel " + lowerLevel)
//upperLevel = upperLevel[0]
//lowerLevel = lowerLevel[0]
server.log("Upper Level " + upperLevel)
server.log("Lower Level " + lowerLevel)
local temperature = ((upperLevel << 8) | lowerLevel);
server.log("Temperature " + temperature)
if (temperature > 2047){
temperature = temperature - 4096;
}
celsius = temperature * 0.25;
aveTemp += celsius;
pixelTempL = hexStringToInt(pixelTempL);
pixelTempL = format("0x%02X", pixelTempL)
server.log("Next Address" + pixelTempL)
}
/*i2c.write(i2c_grideye, "\\x0E")
local upperLevelTherm = i2c.read(i2c_grideye, "", 1);
local lowerLevelTherm = i2c.read(i2c_grideye, "", 1);
upperLevelTherm = upperLevelTherm[0]
lowerLevelTherm = lowerLevelTherm[0]
local temperatureTherm = ((lowerLevelTherm << 8) | upperLevelTherm);
local celsiusTherm = temperatureTherm * 0.0625;
*/
aveTemp *= 0.015625;
if (aveTemp > Threshold)
{
//send occupancy signal
}
else
{
//send vacancy signal
}
server.log("Average Temperature: " + aveTemp)
imp.wakeup(10, GridEye)
}
GridEye();`
For reference, here is the what the GridEye is
Ultimately I want to get the values for all 64 pixels and average them.
Thanks for any assistance.
Update 1: So now for upper level, I get a random char. I decided to change the code above to read in 4 bytes for upperLevel and I get random chars (i.e z, t, }, u, ~, |, etc). I changed it back to read 1 byte but it still gets me the random chars.
Update 2: Now its back to normal reading in a bunch of bytes like my output above.
Update 3: It seems to be alternating between random character and the string of bytes. The code is still the same as above.
Update 4: Fixed something in the hexStringToInt function.