Announcement

Collapse
No announcement yet.

sending data to the web

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • sending data to the web

    I have implemented a code in the xport that takes values from the inverters and now I would like that the code could send the chosen values into a web. I have been proving with the http.c and dgi.c examples but it didn?t work.

    Could you send me an example or guide explaining how I may implement the code, please? I have already seen the evolution_http.h library and its functions but I want to know how could I implement the code for sending data to the web.

    Thanks
    XPort Pro newbie

  • #2
    Can you post some more info? You should be able to do exactly what you are describing (what I can understand) with very little modification to cgi.c .
    To better understand how to do things you should follow the instructions to compile and load the cgi.c example. Load the cgi webpage as directed in the source code. Once you have seen and understand these things I think you will be able to accomplish what you want.

    Comment


    • #3
      Hi,
      I add my code.
      I want to read from serial port and write to the network.
      I have done the tunneler.c example, but I can't adapt it to my code.
      Attached Files
      XPort Pro newbie

      Comment


      • #4
        I am happy to look at your code but until we know what it is you are trying to do I can't really help. In your first post you said "send to web" in your last post you said "write to the network." So which one is it?

        Please explain the flow of your desired program, what comes in, what goes out (and where it goes), etc.

        Comment


        • #5
          Hi,

          Sorry, but my english is very poor.

          What we really need is to send the info from a certain number of variables collected by serial polling to different devices to the IP xx.xx.xx.xxx port xxxx. Could you please help us with the values we need to set up in the connect and socket functions.

          My function of send to web is "sdkTcpTxClient" but it ins?t correct.

          Thanks
          Attached Files
          XPort Pro newbie

          Comment


          • #6
            Ok, tunneler.c is a transparent tunnel that passes incoming TCP data to the serial port and passes serial data back to the TCP port.

            That is a very good spot to start I think. But I think you need to disconnect the tunnel. On the serial side you need to write your own thread that polls for the "variables." Store the responses somewhere in the Lantronix.

            On the TCP side you need to decide your protocol for transmitting data. Do you need a request message from the client? Do you want to send it every X seconds if there is a connection? Etc. Once you figure that out you can write a simple function that transmits the variable.

            J

            Comment


            • #7
              I'm still trying to connect the socket to send to the server. I have written elsewhere in the forum, but like you helped me at first I put my code here if you have any idea:

              Code:
              #includes...
              
              #define host "192.168.1.156"
              #define loc "192.168.1.241"
              #define PORT		77
              #define PORT2		1234
              #define MESSAGE		"Hello world"
              FILE *fp;
              
              void write_to_server (int socket_id)
              {
                char* str = sprintf_malloc(MESSAGE);
                int result = send(socket_id, str, strlen (str), MSG_OOB);		//socket de envio en modo no bloqueante, no voy a recibir datos del servidor
                
                if(result > 0)
                {
                		char* str_message = sprintf_malloc("SEND OK %d \n", strlen(str));
              		fwrite(str_message, 1, strlen(str_message), fp);
              		Free(str_message);
                }
                else
                {
              		char* str_message = sprintf_malloc("ERROR SEND %d %d\n", errno, strlen(str));
              		fwrite(str_message, 1, strlen(str_message), fp);
              		Free(str_message);
                }
                
                Free(str);
              }
              
               
              unsigned int inet_addr2(char *str){ 
                unsigned int ip, b1, b2, b3, b4;
                ip=b1=b2=b3=b4=0;
                sscanf(str,"%d.%d.%d.%d", &b1, &b2, &b3, &b4); 
                //ip = htonl((b1<<24)|(b2<<16)|(b3<<8)|b4 );//si lo pon?a as? estaba cogiendo la ip al rev?s 156.1.168.192
                ip = htonl(b1|(b2<<8)|(b3<<16)|(b4<<24) );
                char* str_message = sprintf_malloc("\ninetaddr2 %u str%s\n", ip, str);
              		fwrite(str_message, 1, strlen(str_message), fp);
              		Free(str_message);
                return ip; 
              } 
              
              
              
              int SDKMain(struct command_line *commandLine, struct http_server *httpServer)
              {
                int sock=0, resulbind=0,valorbind=0, valorconnect=0;
                struct sockaddr_in servername, localname;
                
                (void)commandLine; /* To avoid compiler warning for unused variables */
                (void)httpServer;  /* To avoid compiler warning for unused variables */
                
                fp = fopen ( "/sd/serialread.txt", "w" );
              	
              	  /* Connect to the server.   */
               
                memset(&servername, 0, sizeof(servername));
                servername.sin_family = AF_INET;
                servername.sin_port = htons (PORT);
                servername.sin_addr.s_addr = NetDottedTo32BitHostByteOrder(host);  // IP de la interface por donde escuchara el servidor.
                //servername.sin_addr.s_addr = htonl(host);
                //servername.sin_addr.s_addr = inet_addr2(host);
                char* str_message = sprintf_malloc("\nservername.sin_addr.s_addr %u, htons %u\n", servername.sin_addr.s_addr,servername.sin_port);
              		fwrite(str_message, 1, strlen(str_message), fp);
              		Free(str_message);
              	
              	
                /* Create the socket.   */
                sock = socket (AF_INET, SOCK_STREAM, IPPROTO_IP);
                if (sock < 0)
                {
              	uint8_t message[16];
              	  snprintf(message, sizeof(message), "ERROR 1\n");
              	  fwrite(&message,1,sizeof(message), fp);
                    //exit (EXIT_FAILURE);
                }
              
                
                
                
                //localname
                memzero(&localname, sizeof(localname));//igual a memset
                localname.sin_family = AF_INET;
                localname.sin_addr.s_addr = htonl(INADDR_ANY);  // IP local.
                localname.sin_port = htons (PORT2);
                
                char* str_message2 = sprintf_malloc("\nlocalname.sin_addr.s_addr %u, htons %u\n", localname.sin_addr.s_addr, localname.sin_port );
              		fwrite(str_message2, 1, strlen(str_message2), fp);
              		Free(str_message2);
                
                 resulbind=bind(sock, (struct sockaddr *) &localname, sizeof(localname));
              	if (resulbind==-1)
              	{ 
              	  uint8_t message[16];
              	  snprintf(message, sizeof(message), "ERROR BIND\n");
              	  fwrite(&message,1,sizeof(message), fp);
              	  closesocket(sock);
              	  valorbind=errno;
              	}
                
              	
              	// errno connect -->-128 : host inalcanzable
                if (connect (sock, (struct sockaddr *) &servername, sizeof (servername)) == 0)
                  {	  
              	    write_to_server (sock);
              		closesocket(sock);
                  }
              	else
              	{
              		valorconnect=errno;
              		char* str_message = sprintf_malloc("ERROR 2 valorbind=%d valorconnect %d sock=%d resulbind %d \n", valorbind,valorconnect, sock,resulbind);
              		fwrite(str_message, 1, strlen(str_message), fp);
              		Free(str_message);
              	}
              	
              	fclose(fp);
                
              	return 0;
              }
              Thanks!
              XPort Pro newbie

              Comment


              • #8
                Can you tell me what problem you're having with this code?

                Comment


                • #9
                  I can?t connect to socket and I can?t send a message to a server.

                  Thanks for your answer
                  XPort Pro newbie

                  Comment

                  Working...
                  X