Sending an Image to a Server

Hello all,
We are a senior design group attempting to integrate the electric imp with an existing garage door system. We have connected the imp to a microcontroller which is connected to the Adafruit TTL Serial Camera.

The basic idea is we are trying to get a JPG from the camera to the imp through the microcontroller. So far we have been able to read the bytes from the microcontroller with the imp, via SPI, however after picking it up a couple days later we are unable to read the complete image and recently started receiving the error message:
2014-02-28 15:49:59 UTC-5: [Exit Code] imp restarted, reason: out of memory
2014-02-28 15:49:59 UTC-5: [Status] Device disconnected; 0 bytes sent, 0 received, 0 total
This repeats every 10 seconds. We are new to the squirrel language so it is possible we are missing something.

Is there a way to clear the memory on the imp?
Should we attempt to send the data straight to the server, if so how?
Are we even going to be able to access the image once it is on the imp, or are we going about this completely the wrong way?

Any help would be appreciated!
-OpenSesame

@opensesame - you’re in luck, we’ve actually written some code around camera modules before, you can find it here.

Also, take note of this comment in the device.nut:

Adafruit's VC0706 camera shield is strapped to boot at 38400 baud, while Radioshack's shield defaults to 115200 baud.

Both modules are strapped to boot in SPI Master mode, and require pin 80 on the VC0706 to be pulled to 3.3V in order to enable SPI slave mode, which this class requires in order to read frame buffers via SPI.

@tom is the person to ask about these camera modules!

Thank you for sharing your code with us! We’re looking at it now and making adjustments to our own code.

Do you have any idea what the source of the “out of memory” error could be? We tried a different breakout board and it appears to be functioning again. Could we have “maxed out” the memory on one board? And if so, how can we reset the memory so we can use the board again?

Out of memory is generally… when you’re out of memory. There’s no memory on the breakout board, it’s all within the imp.

There’s likely not enough RAM in the imp to hold a whole image. Our example code sends it in chunks up to the agent where it’s reassembled.

As it turns out we still had the old code loaded onto the imp!
We are still confused as to how it could go into this error state without running the code though.

maybe my FIFO array can be of use here. Then you will never run out of memory again:

http://forums.electricimp.com/discussion/comment/13172#Comment_13172

Thank you all for your help so far! We’ve come quite a ways since we first reached out to you.

At this moment, we are trying to put our image on a web server. We are using the code that beardedinventor shared with us, however we are getting stuck with “upload.php”. (we think) our web server is php enabled, but is there another script we need to include to get this to work?

Is there any other way that you guys know that would put a jpg onto a server if php is not an option?

Thanks in advance for any comments!

Looking at the agent code that was part of the previous post, can anyone give me some insight into the http request part of the function?

http.onrequest(function(req, res) {
device.send(“take_picture”, 1);
requests.push(res);
});

Right now we can create an image on the server however it is empty.
I understand that this part of the code is called when the url is accessed, but what does the “requests.push(res);” part do?

If you look a bit farther up in that agent, you’ll see that “requests” is actually an array. When requests arrive at the http request handler, each request object is pushed into that array. Note that this means they’re still open; the agent hasn’t responded yet.

The requests get pulled out of the array and responses get sent once the device sends the “jpeg done” event:

local req = http.post("http://demo2.electricimp.com/hackathon/upload.php", {"Host":impeeid.tostring()}, s); req.sendsync(); foreach(res in requests) { //res.send(200, "Sent to demo2.electricimp.com/hackathon/photos"); res.header("Location", "http://demo2.electricimp.com/hackathon/camera.jpg" ); res.send(302, "Found\ "); } server.log("Agent: Latest Photo Sent.");

Wo when the photo is done, the agent sends it off to that php script, and then tells everyone who asked for it where it is.

The reason for all this indirection is that the agent can’t hold a whole photo in memory all at once. So instead, the whole process looks like this:

  • receive a request for a photo
  • tell the device to start taking a photo
  • grab the photo in chunks and send each chunk to the php script before asking for the next one
  • when all the chunks have been fetched, tell the requester where you put the photo.

Hope this helps!

The way I have my agent code currently, based on the url accessed (?pic=1, ?status=1, etc) I would like to go to a different function on the device side. I am currently performing the decision making in the http.onrequest, which I am abbreviating here. I attempted to put the requests.push(response) in this http.onrequest at the very end, but if it is only necessary for the picture part of my code, should I move it elsewhere?

`http.onrequest(function(request, response) {
if (“pic” in request.query)
{
device.send(“spy”,request.query[“pic”].tointeger());
}

else if (“door” in request.query)
{
device.send(“open”,1);
}

else if (“status” in request.query) {…
device.send(“doorstat”,1);
}
else {
response.send(200, “for picture add ‘?pic=1’, to open door add ‘?door=1’, to close door add ‘?door=0’, and to check status of door add ‘?status’”);
}

requests.push(response);
});`

Side note: What should we see if the code is working? We changed something and now when we access the url to take a picture it automatically redirects to the location where our photo should be stored. Does this sound right?

One final plea for help before we finish up our project!

We are still stuck getting the image on our server. Is there a possibility that we need to close the php request? php is new to us, and the upload file seems pretty basic so we aren’t sure what could possibly be going wrong.

Any suggestions?