Send captured image to socket server

@ dobova -
Hi,
I now take the port as a parameter in the JoinWirelessNetwork method


 #region JoinWirelessNetwork

        /// <summary>
        /// Attempt to join a wireless with static IP.
        /// Function does not return until and IP address has been granted,
        /// or a time-out has occured.
        /// </summary>
        /// <param name="SSID"></param>
        /// <param name="Passphrase"></param>
        /// <param name="channel"></param>
        /// <param name="Authentication"></param>
        /// <param name="enableDHCP"></param>
        /// <param name="IP"></param>
        /// <param name="Gateway"></param>
        /// <param name="SubnetMask"></param>
        /// <param name="DNS"></param>
        /// <returns>Whether or not it was successful</returns>
        /// 
        public bool JoinWirelessNetwork(string SSID, string Passphrase, int channel = 0, WirelessEncryptionMode Authentication = WirelessEncryptionMode.Open,
                                          bool enableDHCP = false,
                                          string listenPort = "2000",
                                          string IP = "192.168.1.5",
                                          string Gateway = "192.168.1.1",
                                          string SubnetMask = "255.255.255.0",
                                          string DNS = "192.168.1.1")
        {
            return DoJoinToWirelessNetwork(SSID, Passphrase, channel, Authentication, enableDHCP, listenPort, IP, Gateway,SubnetMask, DNS);
        }

        /// <summary>
        /// Attempt to join a wireless network with given parameters using DHCP.
        /// Function does not return until and IP address has been granted,
        /// or a time-out has occured.
        /// </summary>
        /// <param name="SSID"></param>
        /// <param name="Passphrase"></param>
        /// <param name="channel"></param>
        /// <param name="Authentication"></param>
        /// <returns>Whether or not it was successful</returns>
        /// 
        public bool JoinWirelessNetwork(string SSID, string Passphrase, int channel = 0, WirelessEncryptionMode Authentication = WirelessEncryptionMode.Open, string listenPort = "2000")
        {
            return DoJoinToWirelessNetwork(SSID, Passphrase, channel, Authentication, true, listenPort, "", "", "", "");
        }


        private bool DoJoinToWirelessNetwork(string SSID, string Passphrase, int channel, WirelessEncryptionMode Authentication,
                                            bool enableDHCP,
                                            string listenPort = "2000",
                                            string IP = "",
                                            string Gateway = "",
                                            string SubnetMask = "",
                                            string DNS = "")
        {
            _DHCP = enableDHCP;

            _device_ready = false;

            //Reset the module
            Reset.Write(false);
            Thread.Sleep(100);
            Reset.Write(true);
            Thread.Sleep(250);

            //Wait for the device to be ready
            _TimeOutDate = DateTime.Now.AddMilliseconds((double)_Timeout);
            while (!_device_ready)
            {
                if (_TimeOutDate <= DateTime.Now)
                    return false;
                Thread.Sleep(1);
            }

            //Exit command mode..just in case
            if (!_Command_Mode_Exit())
                return false;

            //Enter command mode
            if (!_Command_Mode_Start())
                return false;

            if (!enableDHCP)
            {
                //Set DHCP off (use static IP address)
                if (!_Command_Execute("set ip dhcp 0"))
                    return false;

                //Set requested IP address
                if (!_Command_Execute("set ip address " + IP))
                    return false;

                //Set requested gateway
                if (!_Command_Execute("set ip gateway " + Gateway))
                    return false;

                //Set requested subnetmask
                if (!_Command_Execute("set ip netmask " + SubnetMask))
                    return false;

                //Set requested DNS address
                if (!_Command_Execute("set dns address " + DNS))
                    return false;
            }
            else
            {
                //Set DHCP on
                if (!_Command_Execute("set ip dhcp 1"))
                    return false;
            }


            //if (!_Command_Execute("scan"))
            //   return false;

                if (!_Command_Execute("set ip localport " + listenPort))
                return false;

            if (!_Command_Execute("set ip protocol 2"))
                //if (!_Command_Execute("set ip protocol 3"))
                return false;



            if (!_Command_Execute("set wlan join 0"))
                return false;

            if (!_Command_Execute("set wlan ssid " + SSID))
                return false;
            
            if (!_Command_Execute("set wlan channel " + channel.ToString()))
                return false;

            
           if (!_Command_Execute("set wlan auth " + Authentication.ToString()))
                return false;

            if (Authentication == WirelessEncryptionMode.Open)
            {
                Debug.Print("I set wlan key to - No");
            }
            else
            {
                if (Authentication == WirelessEncryptionMode.WEP_128)
                {
                    if (!_Command_Execute("set wlan key " + Passphrase))
                        return false;
                }
                else
                {
                    if (!_Command_Execute("set wlan phrase " + Passphrase))
                        return false;
                }
            }

            if (!_Command_Execute("set wlan hide 1"))       // Hide Wlan password / -phrase
                        return false;
            
            if (!_Command_Execute("join"))
                return false;

           
            if (!_Command_Execute("get ip"))
                return false;

            //Exit command mode
            if (!_Command_Mode_Exit())
                return false;

            return true;
        }

        #endregion


But I have now issues with a favicon.ico request of Internet Explorer that Comes after the request for /index.html

Hi Roshmi,
thanks for your update.
The problem I was talking in previous post, it’s not in your driver. I couldn’t find a solution.To change the default port to an other you need a save/reboot.

Modern browsers try to get favicon.ico from the server. I’ve have solved on netmf creating a resource with an ico that is sent to the browser at request. If you don’t want to manage it, send status 404 back with empty stream.

Thank you for reply, i was on my vacation these days… I will try it soon and let you know how it goes…

Hi dobova, did you try to send a picture? How it goes?

@ Alex Bilityuk -
Hi Alex, this afternoon I will make a try, but you can test very easy with Roshmi driver, managing to send byte array of your bitmap:


       void wifi_RN171_HttpRequestReceived(HttpStream request)
        {
            Debug.Print("HttpRequest");

            string requestedURL = request.Request.URL;
            Debug.Print("Selected URL = " + requestedURL);
            if (requestedURL == "/index.html")
            {
                request.Response.HeaderData["Content-type"] = "text/html";
                request.Response.HeaderData["Connection"] = "close";
                request.Response.HeaderData["Cache-Control"] = "no-cache";
                request.Response.StatusCode = GTM.GHIElectronics.HttpResponse.ResponseStatus.OK;

                byte[] document = System.Text.Encoding.UTF8.GetBytes("<html><head></head><body>HELLO world littlething</body></html>");

                request.Response.HeaderData["Content-Length"] = document.Length.ToString();

                //All you have to do is send the document data through the response object.
                //Header data is automatically applied for you when you chose to send.

                request.Response.Send(document);

                Debug.Print("Response sent -> length: " + document.Length.ToString());
            }
            else if (requestedURL == "/mybitmap.jpg" )
            {
                request.Response.HeaderData["Content-type"] = "image/jpg";
                request.Response.HeaderData["Connection"] = "close";
                request.Response.HeaderData["Cache-Control"] = "no-cache";
                request.Response.StatusCode = GTM.GHIElectronics.HttpResponse.ResponseStatus.OK;

                request.Response.Send(mybitmap); // where mybitmap[] is you image byte array
                 // here may be good idea to loop for small chunks (512byte e,g.)          

                request.Response.HeaderData["Content-Length"] = mybitmap.Length.ToString();
     
            }
            else
            {
                // Debug.Print("Hello not connected");
            }



just call from your browser : http://yourcerb_address:2000/mybitmap.jpg

1 Like

Ok favicon.ico is creating problem, it doesn’t clear the stream from browser … mhhh



                request.Response.StatusCode = GTM.GHIElectronics.HttpResponse.ResponseStatus.NotFound;



it’s not enough the NotFound …
As a side note, with Cerberus is practically a mess working with bitmap … I have GC acting as demolition man video game !!

@ Alex Bilityuk - @ dobova
Just posted a revison of my driver and utility

2 Likes

Hi everyone,
Does anybody know an open source C# simple http client Windows Forms Application that can be used to look directly on the transferred bytes when searching for eventually corrupted data transmission?

@ RoSchmi - My go to tool for any HTTP/HTTPS debugging is Fiddler, it is free and very versatile.

@ RoSchmi - Fiddler good choice, but you can use development feature just builtin Mozilla Firefox…

Ok dobova, thank you for the code sample… i will try

Good news Roschmi. What are the differences from version 1?

@ taylorza - @ dobova
Thanks, orignally I thought of a http Client in C# that I can run in Visual Studio to set e.g. set breakpoints. I had Fiddler already installed but did not look deeper into its features. Perhaps it is easier to use Fiddler. Helped me already to find a bug.

Hi RoSchmi, just tried your suggestions. Here is a result:

This works for me too…

This command doesn’t work… the port is still 2000

Doesn’t work for me too. Still recieve hello messages on the client side…

Also doesn’t work for me…

[quote=“Alex Bilityuk”]
Hi RoSchmi, just tried your suggestions. Here is a result:

Yes, please set the port in the “JoinWirelessNetwork” method

bool ActionResult = wifi_RN171.JoinWirelessNetwork(“mySSID”, “myPassword”, 0, Gadgeteer.Modules.RoSchmi.WiFi_RN171.WirelessEncryptionMode.WPA2_PSK, “80”);

Right: This is now in a corrected Version 3 achieved by the commands:

wifi_RN171.SetProtocol(WiFi_RN171.SocketProtocol.TCP_Server);
wifi_RN171.EnableHttpServer(); //Enable HTTP Parsing

Please download latest Version.

Thank you, I will have a look on this later. For the OPEN and CLOS Messages— you can include the commands in the Driver Code, then it should work.

@ Alex Bilityuk - Download last revision of the driver from Roshmi, it works for listening port setting.

Make sure of the following order to execute commands:


        void ProgramStarted()
        {

            wifi_RN171 = new WiFi_RN171(6, 115200);
            wifi_RN171.SetDebugLevel(WiFi_RN171.DebugLevel.DebugErrors);


            wifi_RN171.EnableDHCP("192.168.20.1", "255.255.255.0", "192.168.20.1");


            bool ActionResult = wifi_RN171.JoinWirelessNetwork("MYSSID", "MyPasswd", 0, Gadgeteer.Modules.RoSchmi.WiFi_RN171.WirelessEncryptionMode.WPA2_PSK, true, "80");

            wifi_RN171.HttpRequestReceived += wifi_RN171_HttpRequestReceived;

            executeCommand(0); // start command
                        wifi_RN171._Command_Execute("set comm open 0");
                        wifi_RN171._Command_Execute("set comm close 0");
                        wifi_RN171._Command_Execute("set comm remote 0");
            executeCommand(1); // close command mode


            if (wifi_RN171.EnableHttpServer())
                Debug.Print("Ok ::: WebServer Started!"); //Enable HTTP Parsing

            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
        }



For me it’s working fine on port 80 with OPEN/CLOSE/HELLO disabled

@ RoSchmi -
I suggest to change the property setting in this way, in the HttpResponse.cs file:



        public ResponseStatus StatusCode
        {
            get
            {
                return _status;
            }
            set
            {
                _status = value;
                HeaderData["Status"] = "HTTP/1.1 " + GetResponseText(_status) + "\r\n";
            }
        }


In the last rev driver, the property is fixed to “200 OK”.

In the image, Just to give you an idea, I used the USB camera with FEZ Spider. You can see times required.
I made very simple test. When GET for /mybitmap.bmp URL, the function start a TakePicture and then when ready get byte array. After that Send() bytes to client all in one, NOT chunked.
Spider takes 1688ms to take image and to fill the bytearray and pure trasmission time is 26855ms for 230454bytes (320x240x24bit bitmap) at baudrate 230400.


            else if (requestedURL == "/mybitmap.bmp")
            {
                const int CHUNKLEN = 512;

                request.Response.HeaderData["Content-type"] = "image/bmp";
                request.Response.HeaderData["Connection"] = "close";
                request.Response.HeaderData["Cache-Control"] = "no-cache";
                request.Response.StatusCode = GTM.GHIElectronics.HttpResponse.ResponseStatus.OK;

                //CreatBitmap();
                camera.TakePicture();
                while (!fl_pictready) Thread.Sleep(10);
                fl_pictready = false;
                int numchunks = mybitmap.Length / CHUNKLEN;
                request.Response.HeaderData["Content-Length"] = mybitmap.Length.ToString();

                //for (int i = 0; i <= numchunks; i++)
                //{
                    request.Response.Send(mybitmap); // where mybitmap[] is you image byte array
                //}
                    //request.Response.Send(new byte[] { 0x13, 0x10 });

                //mybitmap = null;
            }


1 Like

Wow it looks really fast… thank you for the sample… i can’t try it now cuz i am on my bysiness trip… but i am impatient to try it with the last REVISION 3 of RoSchmi’s driver… let you know as soon as i can

dobova, RoSchmi… your are really smart and the best guys for ever!!!
Thanks for all who give a help and post advices in this thread
My project continues to go on because of you guys ;D

I was able to send pictured image using http stream on port 80
No problem with HELLO OPEN CLOS in Http stream in Revision 3

Here is my results (attached).
Picture was 12444 bytes…

And finally if start to compare http stream and chunked bytes method… http is a leader without saying…

All i have to do now is to rewrite my code to use httprecieve method…

Can you advice guys wich approach to use to make a simple GUI on the browser side to hold a picture on a accurate rectangle and put some buttons to send a commands to robot?
Maybe i should use ASP.Net or something?