Arduino NetServer - Upload An Image

Here demonstrates a way to upload an image to internet user from a custom made Web Server.      It can be triggered by loading a page or click on a button.

In this section , the program can exchange data for monitor and control electronics purpose , and also transfer images to internet user.    But it does not turn on your webcam like the first one you read.     In a short , this is a simplified version of Arduino NetServer.
I separate this section from Arduino NetServer , since its not necessary and will increase the complexity of the program code.    However , it's a good to have function.     So I put here for your reference.

The application can be , say upload the images from a security webcam , traffic monitor camera , weather report camera , or city view camera on the web server to the internet user. 921k Download

Web Server Source Code :
In javascript tag insert :
     function fnAutoGetImage() {
      document.images["Image"].src="WebcamImage"+"?"+new Date();
      document.getElementById("Image").src = document.images["Image"].src;
    function fnGetImage() {
      document.images["Image"].src="WebcamImage"+"?"+new Date();
      document.getElementById("Image").src = document.images["Image"].src;

In html body tag insert :
<img src="WebcamImage" width="240" id="Image">

<input type="button" value="Update Image" onclick="fnGetImage()">

<input type="button" value="Auto Update Image" onclick="fnAutoGetImage()">

This code here only works for FireFox.

Arduino NetServer Source Code :
 bool Upload_Image( _SDLNET & net , char *pStr , ... ) {
    va_list valist; // Type to hold information about variable arguments
    va_start(valist, pStr); // Initialize a variable argument list
    vsprintf( net.msg.string , pStr , valist ); // Print formatted variable argument list to string
    va_end(valist); // This macro should be executed before the function
    if( strstr( net.msg.buffer , net.msg.string ) ) {
        net.fWebcamImg = fopen("./WebServer/webcamImage.jpg" , "rb" );
        if( net.fWebcamImg == NULL) {
            fprintf(net.fNetLog,"*** Fail to open ./WebServer/webcamImage.jpg ***\n" );
            SDLNet_TCP_Send( net.ANClientSocket , "./WebServer/webcamImage.jpg File Missing." , 26 );
            return 0;
        long lSize;
        char * buffer;
        // obtain file size:
        fseek (net.fWebcamImg , 0 , SEEK_END);
        lSize = ftell (net.fWebcamImg);
        rewind (net.fWebcamImg);
        // allocate memory to contain the whole file:
        buffer = (char*) malloc (sizeof(char)*lSize);
        fread (buffer,1,lSize,net.fWebcamImg);
        fclose( net.fWebcamImg );
        txtBuffer( net.msg.buffer , "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
        SDLNet_TCP_Send( net.ANClientSocket , net.msg.buffer , strlen(net.msg.buffer) );
        SDLNet_TCP_Send( net.ANClientSocket , buffer , lSize );
        free (buffer);
    return true;
    return false;
// bool Upload_Image( _SDLNET & net , char *pStr , ... ) { END

The source code here is the core part of a custom made Web Server(click for detail information) for receiving request and uploading the image to a web browser.

Update the file "webcamImage.jpg" so the Web Browser will get a new image.

Copyright © 2010 bdragon All rights reserved.
Overall rating
Bdragon Ho,
Jun 6, 2010, 11:17 AM