I just picked up linksprite infrared serial camera and i am using it with particle.I was able to take a picture and print the bytes/hex in the serial monitor(FF D8-FF D9).Now my struggle is to send the jpeg to the webserver.I have already set up the server, and i tested it with simple HTTP POST from the particle and data is sent fine.Now i need help writing these series bytes to the server.I have tried to write one byte at a time in for loop (as shown in the code below) but nothing is written to the server.The particle does connect to the server but nothing happens.I have looked into other people’s code, but didn’t work for me.Since i already got the bytes can anyone show me how to send them to the server, i am sure i am not playing with these bytes right.Thank you in advance.
If you found a solution that sends some arbitrary bytes and it works for them, the same solution will work for you too if you just replace their arbitrary bytes with yours, I'd guess
client.write() returns -1 . I used the same code as in the link, (i just had to plug in my server name ) but client.write() returns -1 constantly (falls in this block "ERROR write failed %d"). I am not sure whats wrong.Here is my code
#define HOST "myapp.herokuapp.com"
byte buffer[1024];
int bufOffset = 0;
long dataSize = 1024 * 1024;
long dataSent = 0;
byte startByte = 0;
byte nextByte = 0;
TCPClient client;
int port = 80;
enum { STATE_CONNECT, STATE_FILL_BUFFER, STATE_WRITE };
int state = STATE_CONNECT;
void setup() {
Serial.begin(9600);
}
void loop() {
switch(state) {
case STATE_CONNECT:
if (client.connect(HOST, port)) {
dataSent = 0;
nextByte = startByte++;
state = STATE_FILL_BUFFER;
}
else {
Serial.println("ERROR failed to connect");
delay(5000);
}
break;
case STATE_FILL_BUFFER:
for(int ii = 0; ii < sizeof(buffer); ii++) {
buffer[ii] = nextByte++;
}
bufOffset = 0;
state = STATE_WRITE;
// Fall through
case STATE_WRITE:
int requestSize = sizeof(buffer) - bufOffset;
int count = client.write(&buffer[bufOffset], requestSize);
if (count == -16) {
delay(10);
}
else
if (count < 0) {
Serial.printlnf("ERROR write failed %d", count);
client.stop();
state = STATE_CONNECT;
delay(1000);
}
else {
bufOffset += count;
if (bufOffset >= sizeof(buffer)) {
// Done sending this buffer
dataSent += sizeof(buffer);
if (dataSent >= dataSize) {
// Sent all of the buffers for this connection
Serial.printlnf("SUCCESS %ld bytes sent", dataSent);
client.stop();
state = STATE_CONNECT;
}
else {
// Load the buffer with data again
state = STATE_FILL_BUFFER;
}
}
else {
//Serial.printlnf("sent count=%d", count);
}
}
break;
}
}
I have been playing with Linksprite serial camera and electron particle. I can take a picture but i am still struggling sending that jpeg to the server.I built a simple Node js web app/server( deployed to heroku) to receive the image.When i upload the image with Curl the file get uploaded fine but when i upload from an electron i get an error at=error code=H13 desc="Connection closed without response" which the error is explained here. In short it seems like the socket closes before it receives anything.int data_sent = client.write(&buffer[256], sizeof(buffer)); returns/prints 256 for about 5 seconds and then starts spitting out -1 .So right after the server throws a H13 error i start getting -1 on serial Monitor.Since Curl can upload the file just fine i know the problem is on Particle side.Anybody that can guide me in the right direction i would appreciate.Thanks.Here is my code
From the code above…i have improved ,now i get a clean server log statement at=info method=POST path="/api/binary" host=myapp.herokuapp.com request_id=d9aa4bc9-e53a-4644-9f1e-b1e806a247fb fwd="176.83.209.31" dyno=web.1 connect=0ms service=22ms status=200 bytes=229 No errors.But in the route i am debugging the request body like so (console.log(request.body)); and this returns undefined which is a sign a request was not understood or formated properly. Looking in my code above i don’t see anything wrong maybe somebody can point it out something?
I think the empty client.println() should be before the client.println(start_request). The empty println marks the end of the HTTP header, and the start_request should actually be part of the body data, because it’s a multipart/form-data body.
Yes you are right and i had like that before and just tried again.When i put it before client.println(start_request). the request doesn’t hit the server at all.
Follow up on my previous comment…Now it does hit the server but the request object is still undefined which still means there is something wrong with my Http request.
I finally got it to work and sent my first jpeg file to my web app. I just needed to re-format my http headers and pass the right content length length = start_request.length +end_request.length + (jpeg_length * byte_array_size) In my case byte array size was 32.
Hi @sparkz19
I m going to buy the same cam you use for your projet and I am totally a newbie with Photon.
Could you please publish your updated code?
Thanks in advance
Big thanks for sharing the codes. I am quite new to this community and have some simple questions regarding the codes:
It seems that the setup() method alone can finish uploading the jpg file to the server. If I want to periodically take snaps (like once per hour), can you give me some suggestions on how to construct the loop() and modify the setup()?
I did find a few ways to upload images from an SD card that worked so all we need to do have this camera save the pictures to an SD card and then push that image file to an FTP server using the code we already have.
You should buy the camera and do some testing for us
Also, 4D Systems has a good forum with product engineers on there to help with questions or issues should they come up which is always a good thing.