Go Down

Topic: GET and POST to a webservice on ASP.net (c#) (Read 7670 times) previous topic - next topic

topogigio1979

HI it's me again... well I'm really screwed I cannot make this work... If i run this requests it works fine the first 3 or 4 times but then it crashed sending a LOT of code...

here is the code on arduino
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xEE, 0xDD, 0xBE, 0xEF, 0xFE, 0xED }; // DIRECCION MAC
IPAddress ip(192,168,2,91);  //DIRECCION IP PARA ARDUINO
EthernetServer server(80); //MODO Servidor y Puerto de trabajo
String readString="";
int pin=-1;
int state=-1;

void setup()
{  
 for (int setoutpin = 6; setoutpin<=9; setoutpin++)//defino las salidas 6,7,8,9 como OUT y las pongo en HIGH
 {
   pinMode(setoutpin,OUTPUT);
   digitalWrite(setoutpin,HIGH);
 }
 Serial.begin(9600);//Monitorizacion via serie
 while (!Serial)
 {
   ;
 }//Espera a que se inicie
 Serial.println("Starting network...");
 Ethernet.begin(mac, ip);//Iniciando Ethernet
 Serial.print("My IP address is: ");  
 Serial.println(Ethernet.localIP());
 Serial.println("Waiting for new connections...");
 delay(500);
}
 
//VOID LOOP===================================================================
 
 void loop()
 {
   EthernetClient client = server.available();
   if (client)
   {
     char data;
     readString="";      
     Serial.println("------------------------------------------------------");
     Serial.println("Connection detected");
     while ( client.available())
     {
       //delay(5);
       data = client.read();
       readString += data;
     }
     
     Serial.println("");
     Serial.println("The client sent: ");
     Serial.println(readString);
     Serial.println("");
     //delay(5);
     
     if (data=='\n')
     {  
       client.println("HTTP/1.1 200 OK");
       client.println("Content-Type: text/html");
       client.println("Connection: close");
       client.println();
       client.println("");  
       
       Serial.println("HTTP/1.1 200 OK");
       Serial.println("Content-Type: text/html");
       Serial.println("Connection: close");
       Serial.println();
       Serial.println("");      

       if( readString.indexOf("?p=1")>=0)
       {
         Serial.print("Received pinnumber: 1 ");
         Serial.println("mapping to output: 6");
         pin=6;
       }
       
       if( readString.indexOf("?p=2")>=0)
       {
         Serial.print("Received pinnumber: 2 ");
         Serial.println("mapping to output: 7");
         pin=7;
       }
       
       if( readString.indexOf("?p=3")>=0)
       {
         Serial.print("Received pinnumber: 3 ");
         Serial.println("mapping to output: 8");
         pin=8;
       }  
       
         if( readString.indexOf("?p=4")>=0)
       {
         Serial.print("Received pinnumber: 4 ");
         Serial.println("mapping to output: 9");
         pin=9;
       }  
       
       if( readString.indexOf("&s=0")>=0)
       {
         Serial.println("Received state: 0");
         state=0;
       }
       
       if( readString.indexOf("&s=1")>=0)
       {
         Serial.println("Received state: 1");
         state=1;
       }
       
       if (state==0)
       {
           digitalWrite(pin,HIGH);
           Serial.print("Setting output: " );
           Serial.print(pin);
           Serial.println(" to state: OFF");  
           client.println("ARDUINO_OK");  
           Serial.println("ARDUINO_OK");  
       }
       
       if (state==1)
       {
           digitalWrite(pin,LOW);
           Serial.print("Setting output: " );
           Serial.print(pin);
           Serial.println(" to state: ON");
           client.println("ARDUINO_OK");
           Serial.println("ARDUINO_OK");  
       }
       
       if( readString.indexOf("?QueryDigital")>=0)
       {
         client.print("QUERY_OK");
         Serial.print("QUERY_OK");  
         for (int querypin = 6; querypin <= 9; querypin++)
         {
           int sendpin = digitalRead(querypin);
           client.print(querypin);
           client.print("=");
           if (sendpin >0)
           {
             client.print("ON");
           }
           else
           {
             client.print("OFF");
           }
           client.print(",");            
         }
       }
       
       if (readString.indexOf("?efect1")>=0) //APAGO TODAS
       {
         client.print("EFECTO1_OK");
         Serial.print("EFECTO1_OK");
         for (int efectpin = 6; efectpin <= 9; efectpin++)
         {
           digitalWrite(efectpin,HIGH);          
         }
       }
       
       if (readString.indexOf("?efect2")>=0) //ENCIENDO TODAS
       {
         client.print("EFECTO2_OK");
         Serial.print("EFECTO2_OK");
         for (int efectpin = 6; efectpin <= 9; efectpin++)
         {
           digitalWrite(efectpin,LOW);          
         }
       }
       if (readString.indexOf("?efect3")>=0) //1,2,3,4
       {
         client.print("EFECTO3_OK");
         Serial.print("EFECTO3_OK");
         for (int efectpin = 6; efectpin <= 9; efectpin++)
         {
           digitalWrite(efectpin,HIGH); //apago todas        
         }
         for (int efectpin = 6; efectpin <= 9; efectpin++)
         {
           digitalWrite(efectpin,LOW);
           delay(1000);        
         }        
       }
       if (readString.indexOf("?efect4")>=0) //4,3,2,1
       {
         client.print("EFECTO4_OK");
         Serial.print("EFECTO4_OK");
         for (int efectpin = 6; efectpin <= 9; efectpin++)
         {
           digitalWrite(efectpin,HIGH); //apago todas        
         }
         for (int efectpin = 9; efectpin >=6; efectpin--)
         {
           digitalWrite(efectpin,LOW);
           delay(1000);        
         }        
       }
       if (readString.indexOf("?efect5")>=0) //4,1,3,2
       {
         client.print("EFECTO5_OK");
         Serial.print("EFECTO5_OK");
         for (int efectpin = 6; efectpin <= 9; efectpin++)
         {
           digitalWrite(efectpin,HIGH); //apago todas        
         }
           digitalWrite(6,LOW);
           digitalWrite(9,LOW);
           delay(1000);
           digitalWrite(7,LOW);
           digitalWrite(8,LOW);
       }        
       if (readString.indexOf("?efect6")>=0) //3,2,4,1
       {
         client.print("EFECTO6_OK");
         Serial.print("EFECTO6_OK");
         for (int efectpin = 6; efectpin <= 9; efectpin++)
         {
           digitalWrite(efectpin,HIGH); //apago todas        
         }
           digitalWrite(7,LOW);
           digitalWrite(8,LOW);
           delay(1000);
           digitalWrite(6,LOW);
           digitalWrite(9,LOW);
       }
         delay(1);
         Serial.println("Client.stop");    
         Serial.println("");  
         Serial.println("Conection end");
         Serial.println("");
         Serial.println("waiting for new connections...");    
         client.stop();
       }
     }
   }


so basically I run this... sequence..

send 192.168.2.91/?QueryDigital
receive QUERY_OK6=ON,7=ON,8=ON,9=ON,
send http://192.168.2.91/?efect1

receive

Code: [Select]
K DUINO_OK 0 OK Content-Type: text/html Connection: close ARDUINO_OK QUERY_OK6=OFF,7=ON,8=ON,9=OHTTP/1.1 200 OK ConHTTP/1.1 200 OK Content-Type: text/html Connection: close ARDUINO_OK EFECTO5_OK 200 OK Content-Type: text/html Connection: close ARDUINO_OK EFECTO3_OKent-Type: text/html Connection: closeHTTP/HTTP/1.1 200 OK Content-Type: text/html Connnection: close Jack's Arudino Control Jack's LED OffOn on: close Jack's Arudino Control Jack's LED OffOn ConHTTP/1.1 200 OK Content-Type: text/html Connnection: cHTHTTP/1.1 200 OK Content-Type: text/html Connection: close ARDUINO_OK Type:HTTP/1.1 200 OK Content-Type: text/html Connection: clONe ARDUINO_OK ext/html ConnectiHTTP/1.1 200 OK ContHTTP/1.1 200 OK Content-Type: text/html Connection: close QUERY_OK6=ON,7=ON,8=ON,9=ON,Type: text/html Connnection: close Jack's Arudino Control Jack's LED OffOn tml Connect OffOn e ARDUINO_OK ontent-Type: text/html Connection: close ARHTTP/1.1 200 OK Content-TypeHTTP/1.1 200 OK Content-Type: text/html Connection: close ARDUINO_OK QUERY_OK6=OFF,7=ON,8=ON,9=ON,O_OK QUERY_OK6=OFF,7=ON,8=ON,9=ON, ARDUINO_OK HTTP/1.1 200 OK Content-Type: text/html CoHTTP/1.1HTTP/1.1 200 OK Content-Type: text/html Connnection: close Jack's Arudino ControHTTP/1.1 200 OK Content-Type: text/html Connnection: close HTTP/1.1 200 OK Content-Type: text/html Connection: close ARDUINO_OK e ARDUINO_OK HTTP/1.1 200 OK Content-Type: text/html ConnHTTP/1.1 200 OK Content-Type: text/html ConHTTP/1.1 200 OK HTTP/1.1 200 OK ContHTTP/1.1 200 OK Content-Type: text/html HTTP/1.1 200 OK Content-Type: text/HTTP/1.1 200 OK Content-Type: text/html Connection: close ARDUINO_OK QUERY_OK6=OFF,7=OFF,8=OFF,9=OFF,EFECTO1_OKEFECTO2_OKEFECTO3_OKEFECTO4_OKEFECTO5_OKEFECTO6_OK this repeats but I exeeded the post limit


some times this just works fine, but some times, it crashed... any idea?
any clue?

pylon

You probably ran out of memory. You use a lot of print/println calls without using the F() macro (holds the strings in the flash without using the same amount of RAM). And you're using the String class. Although the worst bug in it is fixed in version 1.0.5 of the IDE, it's still a bad choice for sketches that have to run reliably for a longer time because it uses dynamic memory allocation. That way the memory gets fragmented until there isn't a memory area of the requested size. At that point the results get erratic and your sketch isn't working in a predictable manner anymore.

topogigio1979

Hi could you please explain me more about the F() macro? also how do I search for a substring on a string without using the string class.. any idea? please expand a little bit on that. THANKS!!! a lot

pylon

Quote
could you please explain me more about the F() macro?


You could use the F() macro with an class that inherits from the Print class, so almost any library that offers print() and println() methods is a candidate. Just add the macro around a string literal and you save the RAM:

Code: [Select]
client.print("This is just an example string literal");

gets

Code: [Select]
client.print(F("This is just an example string literal"));

Quote
also how do I search for a substring on a string without using the string class.


If you work with C strings (character arrays) you can use the strstr() function to search for substrings (http://www.cplusplus.com/reference/cstring/strstr/).

In an embedded platform with that limited memory it's even better to not store that string but parse it while it drops in. Usually that results in a bit more code but saves relevant amounts of memory, depending on the area of use, of course.

topogigio1979

pylon really you rock ,.... I mostly solve it... basically I got a more clear working, but it seems that it is not flushing the string on after each reply... I do not know why... or how to flush an string rather than ="" so... basically I do not know how to do it...


in the other hand  I will investigate the str function that you sent... and I will try to replace it.. but if you could please help me on the flushing stuff first? THANKS!

pylon

If flushing means setting it to be empty, use this:

Code: [Select]
readString = (char *)NULL;

That's the easiest way to call the (protected) method invalidate() which frees the occupied memory and sets the object to be empty.

cinfocepts

 Serial.println(F("connected"));
   String DatatobeSent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\r";
   DatatobeSent += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n\r";
   DatatobeSent += "<soap:Body>\n\r";
   DatatobeSent += "<SampleData  xmlns=\"http://tempuri.org\">\n\r";
   DatatobeSent += "<Data>34</Data>\n\r";
   DatatobeSent += "</SampleData>\n\r";
   DatatobeSent += "</soap:Body>\n\r";
   DatatobeSent += "</soap:Envelope>\n\r";

Please help with above Code !! I am using Webservice developed in ASP.NET !! On Invoking SOAP  Request from C# i am getting the same value which i sent using <Data> Parameter.

But in case of Arduino, it is always Blank !! i printed the string on serial before sending it to client, it shows data at the serial terminal. But on Repsonse i always get like this--->


HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Sun, 22 Jan 2017 09:19:20 GMT
Connection: close
Content-Length: 296

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><SampleDataResponse xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>
disconnecting.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy