Reputation: 11
My cousin and I have been experimenting with his new Photon chip, and we decided to be creative and use ComputerCraft (the Minecraft mod) and some Lua code to drive the car he made. Unfortunately, every time we give an input, it indexes a nil value; we're not sure if it is because the http.post code is wrong or something we don't know about... Any ideas?
URL = "https://api.particle.io/v1/devices/456456456456/updateMotors("
token = "access_token=123456789"
robotState = 0
while true do
os.pullEvent("redstone")
if((redstone.getInput("bottom") == true) and (robotState ~= 0)) then
robotState = 0
returnData = http.post(URL .. "\"0\"" .. ")", token)
returnData.close()
.....
end
end
(the rest of the code looks identical to that hence the ellipsis)
We're trying to pass the string to a Photon function that will drive the motors, but every time we create a redstone signal it says "Drive:11: attempt to index ? (a nil value)" - but only when the Photon is connected.
Upvotes: 0
Views: 157
Reputation: 304
I would suggest adding a print( tostring( returnData ) )
and letting us know what that prints.
It is very likely that returnData is nil.
Here is some information on HTTP handles:
All three operations make use of handles, tables that contain functions to read data returned from the HTTP server. These handles act the same as the I/O handles returned by fs.open in read-only text mode, implementing the close, readLine, and readAll methods.
So, with that information we know that close
is a valid function. That along with your error indicate that returnData
is nil which is returned by http.post
on failure.
So, as I mentioned earlier you'll want to add a print statement in that calls tostring
on returnData
. It should return a handle that you can perform returnData.readAll()
on to get the information from the request. As this is not working it is likely because the request is failing (when http.post
fails nil is returned).
To figure out why it's failing I would recommend printing out the URL it is using and visiting it via your browser. I also notice you are using token
as your post data. Perhaps the token is invalid?
The problem maybe also be due to the way the token is passed. Perhaps rather than passing it as post data you should be passing it via the URL bar (PhP GET) instead, like so:
returnData = http.post(URL .. "\"0\"" .. ")?"..token)
which would convert to "https://api.particle.io/v1/devices/456456456456/updateMotors(\"0\")?access_token=123456789"
I have no experience with this API, but is the zero in updateMonitor supposed to be passed as a string like that? If so then its also possible that the URL needs to be made URL compatible. Check if its compatible using this
I hope you figure out the problem.
Upvotes: 0