0

I'm working with esp8266.i am able to send text from webserver to arduino which is displayed on my 16*2 lcd display.but the problem is that when i send text including space i.e:john smith, the text show on my serial monitor and also in lcd like that (john+smith)..the spaces i gave is converted into "+" sign..Can anyone suggest me how to overcome from that problem?? my code is given below:

  #include <SoftwareSerial.h>
  #include <LiquidCrystal.h>

 SoftwareSerial espSerial(2,3);

  LiquidCrystal lcd(12,11, 7, 6, 5, 4);

const bool printReply = true;
const char line[] = "-----\n\r";
int loopCount=0;

char html[50];
char command[20];
char reply[500]; 

char ipAddress [20];
char name[30];
int lenHtml = 0;
char temp[5];


void setup()
{
      Serial.begin(9600);
        lcd.begin(16,2);

      Serial.println("Start\r\n\r\n");

      lcd.print("WELCOME ");

      espSerial.begin(9600); 



      Serial.println("reset the module"); 
      espSerial.print("AT+RST\r\n");

      getReply( 2000 );



      Serial.println("Change to station mode"); 
      lcd.clear();
      lcd.print("station mode");
      espSerial.print("AT+CWMODE=1\r\n");
      getReply( 1500 );



      Serial.println("Connect to a network ");
      lcd.clear();
     lcd.print("connecting...");


      espSerial.print("AT+CWJAP=\"ESP_E61A83\"\r\n");
      getReply( 6000 );


      Serial.println("Get the ip address assigned by the router"); 
      espSerial.print("AT+CIFSR\r\n");
      getReply( 1000 );


      int len = strlen( reply ); 
      bool done=false;
      bool error = false;
      int pos = 0;
      while (!done)
      {
           if ( reply[pos] == 10) { done = true;} 
           pos++;
           if (pos > len) { done = true;  error = true;}
      }

      if (!error)
      {
            int buffpos = 0;
            done = false;
            while (!done)
            {
               if ( reply[pos] == 13 ) { done = true; }
               else { ipAddress[buffpos] = reply[pos];    buffpos++; pos++;   }
            }
            ipAddress[buffpos] = 0;
      }
      else { strcpy(ipAddress,"ERROR"); }




      Serial.println("Set for multiple connections"); 
      espSerial.print("AT+CIPMUX=1\r\n");
      getReply( 1500 );



      Serial.println("Start the server"); 
        lcd.clear();
      lcd.print("Server starts");
      espSerial.print("AT+CIPSERVER=1,80\r\n");
      getReply( 1500 );

      Serial.println("");



   lcd.clear();
      Serial.println("Waiting for page request");
      Serial.print("Connect to "); Serial.println(ipAddress);
      Serial.println("");
lcd.print("feed me more");
delay(1500);
        lcd.clear();

}



void loop()
{
      if(espSerial.available()) 



          bool foundIPD = false;
          for (int i=0; i<strlen(reply); i++)
          {
               if (  (reply[i]=='I') && (reply[i+1]=='P') && (reply[i+2]=='D')   ) { foundIPD = true;    }
          }


          if ( foundIPD  )  
          {

              loopCount++;

              int nameStartPos = 0;
              for (int i=0; i<strlen(reply); i++)
              {
                   if (!haveName)
                   {
                         if (  (reply[i]=='n') && (reply[i+1]=='a') && (reply[i+2]=='m') && (reply[i+3]=='e')  && (reply[i+4]=='=') ) 
                         { 
                             haveName = true;
                             nameStartPos = i+5;
                         }
                   }     
              }


              if (haveName)
              {
                    int tempPos = 0;
                    bool finishedCopying = false;
                    for (int i=nameStartPos; i<strlen(reply); i++)
                    {
                         if ( (reply[i]==' ') && !finishedCopying )  { finishedCopying = true;   } 
                         if ( !finishedCopying )                     { name[tempPos] = reply[i];   tempPos++; }
                    }              
                    name[tempPos] = 0;
              }

              if (haveName) { lcd.clear(); lcd.print("TEACHERS INFO..");Serial.print( "name = ");lcd.setCursor(0,1);Serial.println(name);lcd.print(name);Serial.println("");lcd.print(""); }



              else          { Serial.println( "no name entered");   Serial.println("");lcd.print(""); }






              strcpy(html,"<html><head></head><body>");
              strcpy(command,"AT+CIPSEND=0,25\r\n");
              espSerial.print(command);
              getReply( 2000 );          
              espSerial.print(html);
              getReply( 2000 );                      

              strcpy(html,"<h1 >TEACHERS INFORMATION WEBSERVER</h1>");
              strcpy(command,"AT+CIPSEND=0,40\r\n");
              espSerial.print(command);
              getReply( 2000 );         
              espSerial.print(html);
              getReply( 2000 );          

              strcpy(html,"<p>CREATED by jack</p>");
              strcpy(command,"AT+CIPSEND=0,31\r\n");
              espSerial.print(command);
              getReply( 2000 );          
              espSerial.print(html);
              getReply( 2000 );           

              strcpy(html,"<p> Teachers Request number ");
              itoa( loopCount, temp, 10);
              strcat(html,temp);
              strcat(html,"</p>");

              // need the length of html
              int lenHtml = strlen( html );

              strcpy(command,"AT+CIPSEND=0,");
              itoa( lenHtml, temp, 10);
              strcat(command, temp);
              strcat(command, "\r\n");
              espSerial.print(command);
              getReply( 2000 );          
              espSerial.print(html);
              getReply( 2000 );                       



             if (haveName)
             {

                  strcpy(html,"<p >TEACHER name is "); strcat(html, name ); strcat(html,"</p>");


                  lenHtml = strlen( html );
                  strcpy(command,"AT+CIPSEND=0,"); itoa( lenHtml, temp, 10); strcat(command, temp); strcat(command, "\r\n");
                  espSerial.print(command);
                  getReply( 2000 );          
                  espSerial.print(html);
                  getReply( 2000 );                           
             }


              strcpy(html,"<form action=\""); strcat(html, ipAddress); strcat(html, "\" method=\"GET\">"); strcat(command, "\r\n");

              lenHtml = strlen( html );
              itoa( lenHtml, temp, 10);
              strcpy(command,"AT+CIPSEND=0,"); 
              itoa( lenHtml, temp, 10); 
              strcat(command, temp);  
              strcat(command, "\r\n");

              espSerial.print(command);
              getReply( 2000 );          
              espSerial.print(html);
              getReply( 2000 );          

              strcpy(html,"Name:<br><input type=\"text\" name=\"name\">");
              strcpy(command,"AT+CIPSEND=0,40\r\n");
              espSerial.print(command);
              getReply( 2000 );         
              espSerial.print(html);
              getReply( 2000 );         


              strcpy(html,"<input type=\"submit\" value=\"Submit\"></form>");
              strcpy(command,"AT+CIPSEND=0,43\r\n");
              espSerial.print(command);
              getReply( 2000 );       
              espSerial.print(html);
              getReply( 2000 );

              strcpy(html,"</body></html>");
              strcpy(command,"AT+CIPSEND=0,14\r\n");
              espSerial.print(command);
              getReply( 2000 ); 
              espSerial.print(html);
              getReply( 2000 ); 


              espSerial.print( "AT+CIPCLOSE=0\r\n" );
              getReply( 1500 );            


              Serial.println("last getReply 1 ");
              getReply( 2500 );  

              Serial.println("last getReply 2 ");
              getReply( 2500 ); 



          } 
      } 


      delay (100);



}


void getReply(int wait)
{
    int tempPos = 0;
    long int time = millis();
    while( (time + wait) > millis())
    {
        while(espSerial.available())
        {
            char c = espSerial.read(); 
            if (tempPos < 500) { reply[tempPos] = c; tempPos++;   }
        }
        reply[tempPos] = 0;
    } 

    if (printReply) { Serial.println( reply );  Serial.println(line);     }
}
1

It is hard to understand what your code does. You said that arduino recives text from the server, but I see in the code is that the arduino is the http server, serving an html page with a form in it.

That is also the reason you recieve spaces replaced with "+", the http request is url encoded.

The problem with a search and replace solution of a "+" sign is that url encoding is more than a "+" sign. A space can also be encoded into %20 and there are several other characters that are being encoded as well.

What you will have to do is to implement a decoding function. It is not that hard but if you are lazy, there are plenty of C implementations available on the net. Here is a quick google search hit: https://gist.github.com/jmsaavedra/7964251

| improve this answer | |
0

If you have your text in a character array, which it seems you have, then it is simple enough to iterate through that array changing whatever characters you want.

For instance, if the text is in reply then:

for (char *p = reply; *p; p++) {
    if (*p == '+') *p = ' ';
}

That is, starting at the beginning of reply, by creating a pointer p and pointing it at the start of reply, check to see if there is anything except the NULL character, run the code inside the brackets, and move to the next character (p++).

The code inside the brackets looks to see if the current character is a + and if so then set the current character to be a space.

Another way is to use an integer counter:

for (int i = 0; reply[i] != 0; i++) {
    if (reply[i] == '+') reply[i] = ' ';
}
| improve this answer | |
  • i have already used ur second way (integer counter)..but it's only show the first word of the text...i.e:whether i write (my text) ,it shows only (my).. – razu Nov 2 '15 at 19:22
0

Two things: +If you are using strings, try using the String built in object, it is very handy and useful +when using String, you can use replace function

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.