I am using the imp as an I2C to Web interface so I can talk to a device through a browser. Right now I am struggling with the second argument of the i2c.read method. If I hard code a value for the register address like “\x00” it works fine. However, I cannot find any way to use a variable which is passed in from the agent. Can anybody help with this?
function requestHandler(request, response)
if (“read” in request.query)
response.send(500, "Internal Server Error: " + ex);
local read_data = hardware.i2c12.read(0x10, i2c, 6);
The second parameter is a string, but it’s actually slightly more complicated than that sounds.
If your incoming value is an integer between 0 (0x00) and 255 (0xFF), I would change the first line of your device read_i2c() function to:
local read_data = hardware.i2c12.read(0x10, i2c.tochar(), 6);
So if i2c is 0xFF, this will implicitly send “\xFF” out on the I2C bus.
We don’t use .tostring() in place of .tochar() because this converts the integer to a numeric string, ie. it would send “255” down the bus, which is actually (converting the digits in the string to their Ascii values, as these are what the string stores), or “\x32\x34\x34”.
I also had to add .tointeger() on the agent side because it was saying the .tochar() does not exist. Is there a defined data type for the request.query table, or does it change based on what data is entered?
Yeah. Should have spotted that – the HTTP request data is a string not an integer and Squirrel’s string object has no .tochar() method.