Web Server Long Waits

Hello! Try to remote by IR for my TV.

All working good, but after 5-6 pressed Web Server stopped, and waiting very long time.
I don't understand what happens?

#include <etherShield.h>
#include <ETHER_28J60.h>
#include <IRremote.h>
#include <wrun_jvc.h>

IRsend irsend;
ETHER_28J60 e;

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};            
static uint8_t ip[4] = {192, 168, 1, 15};
static uint16_t port = 80;  


void RemoteJVC(unsigned long data) {
  irsend.sendJVC(data, 16, 0);
  delayMicroseconds(50);
  irsend.sendJVC(data, 16, 1);
  delay(50);
}

void setup() {
  Serial.begin(9600);
  e.setup(mac, ip, port);
}

void loop() {
  // WebServer
  String params;
  if (params = e.serviceRequest()) {
    params = params.substring(1,99);

//    Serial.println(params);
    
    if (params=="JvcPower") RemoteJVC(JVC_Power);
    if (params=="JvcVolUp") RemoteJVC(JVC_Vol_Up);
    if (params=="JvcVolDn") RemoteJVC(JVC_Vol_Dn);
    if (params=="JvcChNext") RemoteJVC(JVC_CH_Next);
    if (params=="JvcChPrev") RemoteJVC(JVC_CH_Prev);
    if (params=="031A") RemoteJVC(0xFFFF031A);

    e.print("<html><head><title>wArd</title>");
    e.print("<meta http-equiv='content-type' content='text/html; charset=utf-8' />");
    e.print("<style>a{background:#ccc;display:block;font-size:80px;height:100px;text-align:center;margin:10px;}.temp{width:100px;display:inline-block;margin:10px 5px;}</style>");
    e.print("</head><body> <H1>Web Remote</H1>");
    e.print("<a href='?JvcPower'>Power</a>");
    e.print("<a href='?JvcVolUp'>Volume Up</a>");
    e.print("<a href='?JvcVolDn'>Volume Down</a>");
    e.print("<a href='?JvcChNext'>CH+</a>");
    e.print("<a href='?JvcChPrev'>CH-</a>");
    e.print("<a href='?031A'>031A</a>");
    e.print("</body></html>");

    e.respond();
  }
}
  String params;
  if (params = e.serviceRequest()) {
    params = params.substring(1,99);

e.serviveRequest() returns a nice pointer to char. Why do you insist on creating a new String every pass through loop? What do you think happens to them at the end of loop?

I'd suggest that you put a Serial.begin() and Serial.print() call in setup(), to see if the long wait is because the Arduino is resetting (as I suspect it is).

Sorry, I am begginer... I took example I try to use it... I can to put declaration String params; into Setup block, but I don`t understand about looking by Serial.print, looking for what?

I understand your recommendation about Serial... I dont think about Arduino restart, because when I press Reset button on board - Its help!

I can to put declaration String params; into Setup block

That won't do any good. You can leave the declaration of params where it is, but change the type to char * (may actually need to be const char *).

Get rid of the substring method call.

Change all the statements like:

    if (params=="JvcPower") RemoteJVC(JVC_Power);

to

    if (strcmp(params, "JvcPower") == 0) RemoteJVC(JVC_Power);

but I don`t understand about looking by Serial.print, looking for what?

If you put:

Serial.begin(115200);
Serial.println("Hey, I just reset...");

in setup(), and open the Serial Monitor, you should see:

Hey, I just reset...

If you see;

Hey, I just reset...
Hey, I just reset...
Hey, I just reset...
Hey, I just reset...
Hey, I just reset...
Hey, I just reset...
Hey, I just reset...
Hey, I just reset...

that is not good.

I do it all, so:

If I use char* - I can`t use params = params.substring(1,99);, but I put ? into IF:
if (strcmp(params, "**?**JvcPower") == 0) RemoteJVC(JVC_Power);

After 6 press Web Page try to loading too long, and said TimeOut

I don`t see restarting in Serial Monitor

After 6 press Web Page try to loading too long, and said TimeOut

I don`t see restarting in Serial Monitor

And I don't see your modified code.

Sorry

#include <etherShield.h>
#include <ETHER_28J60.h>
#include <IRremote.h>
#include <wrun_jvc.h>

IRsend irsend;
ETHER_28J60 e;

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};            
static uint8_t ip[4] = {192, 168, 1, 15};
static uint16_t port = 80;  

void RemoteJVC(unsigned long data) {
  irsend.sendJVC(data, 16, 0);
  delayMicroseconds(50);
  irsend.sendJVC(data, 16, 1);
  delay(50);
}

void setup() {
  Serial.begin(115200);
  Serial.println('Start 0');
  e.setup(mac, ip, port);
  Serial.println('Start 1');
}

void loop() {
  // WebServer
  char* params;
  if (params = e.serviceRequest()) {
    //params = params.substring(1,99);

//    Serial.println(params);
    
    if (strcmp(params, "?JvcPower") == 0) RemoteJVC(JVC_Power);
    if (strcmp(params, "?JvcVolUp") == 0) RemoteJVC(JVC_Vol_Up);
    if (strcmp(params, "?JvcVolDn") == 0) RemoteJVC(JVC_Vol_Dn);
    if (strcmp(params, "?JvcChNext") == 0) RemoteJVC(JVC_CH_Next);
    if (strcmp(params, "?JvcChPrev") == 0) RemoteJVC(JVC_CH_Prev);
    if (strcmp(params, "?031A") == 0) RemoteJVC(0xFFFF031A);

    e.print("<html><head><title>wArd</title>");
    e.print("<meta http-equiv='content-type' content='text/html; charset=utf-8' />");
    e.print("<style>a{background:#ccc;display:block;font-size:80px;height:100px;text-align:center;margin:10px;}.temp{width:100px;display:inline-block;margin:10px 5px;}</style>");
    e.print("</head><body> <H1>Wrun Web Remote</H1>");
    e.print("<a href='?JvcPower'>Power</a>");
    e.print("<a href='?JvcVolUp'>Volume Up</a>");
    e.print("<a href='?JvcVolDn'>Volume Down</a>");
    e.print("<a href='?JvcChNext'>CH+</a>");
    e.print("<a href='?JvcChPrev'>CH-</a>");
    e.print("<a href='?031A'>031A</a>");
    e.print("</body></html>");

    e.respond();
  }
}
  Serial.println('Start 0');

Which single key did you press to get "Start 0"? Single characters are enclosed in single quotes. Multiple characters are enclosed in double quotes.

I don't know if this is your problem, but maybe a response header would help? Web browsers like that kinda stuff.

    // add this before the document body
    e.print("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n");
    // now the rest of your document
    e.print("<html><head><title>wArd</title>");

Otherwise, it appears to be running out of sockets. That happens in the w5100 if the connections are not closed properly, but I know little about the ENC28J60.

Tnx! But it`s not big probleme

I try next:

#include <etherShield.h>
#include <ETHER_28J60.h>
#include <IRremote.h>
#include <wrun_jvc.h>

IRsend irsend;
ETHER_28J60 e;

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};            
static uint8_t ip[4] = {192, 168, 1, 15};
static uint16_t port = 80;  

void RemoteJVC(unsigned long data) {
  irsend.sendJVC(data, 16, 0);
  delayMicroseconds(50);
  irsend.sendJVC(data, 16, 1);
  delay(50);
}

void setup() {
  Serial.begin(115200);
  Serial.println("Start 0");
  e.setup(mac, ip, port);
  Serial.println("Start 1");
}

void loop() {
  // WebServer
  char* params;
  if (params = e.serviceRequest()) {
    //params = params.substring(1,99);

    Serial.println("in");
    
    if (strcmp(params, "?JvcPower") == 0) RemoteJVC(JVC_Power);
    if (strcmp(params, "?JvcVolUp") == 0) RemoteJVC(JVC_Vol_Up);
    if (strcmp(params, "?JvcVolDn") == 0) RemoteJVC(JVC_Vol_Dn);
    if (strcmp(params, "?JvcChNext") == 0) RemoteJVC(JVC_CH_Next);
    if (strcmp(params, "?JvcChPrev") == 0) RemoteJVC(JVC_CH_Prev);
    if (strcmp(params, "?031A") == 0) RemoteJVC(0xFFFF031A);

    e.print("<html><head><title>wArd</title>");
    e.print("<meta http-equiv='content-type' content='text/html; charset=utf-8' />");
    e.print("<style>a{background:#ccc;display:block;font-size:80px;height:100px;text-align:center;margin:10px;}.temp{width:100px;display:inline-block;margin:10px 5px;}</style>");
    e.print("</head><body> <H1>Wrun Web Remote</H1>");
    e.print("<a href='?JvcPower'>Power</a>");
    [b]e.print("<a href='?JvcVolUp&");
      e.print(random(3000));
      e.print("'>Volume Up</a>");
    e.print("<a href='?JvcVolDn&");
      e.print(random(3000));
      e.print("'>Volume Down</a>");[/b]
    e.print("<a href='?JvcChNext'>CH+</a>");
    e.print("<a href='?JvcChPrev'>CH-</a>");
    e.print("<a href='?031A'>031A</a>");
    e.print("</body></html>");

    e.respond();
  }
}

I think its help, but I must to parse getting string...

SurferTim:
I don't know if this is your problem, but maybe a response header would help? Web browsers like that kinda stuff.

    // add this before the document body

e.print("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n");
    // now the rest of your document
    e.print("wArd");



Otherwise, it appears to be running out of sockets. That happens in the w5100 if the connections are not closed properly, but I know little about the ENC28J60.

Its working like text, not like header (((

As text? Is it printing the "\r\n" or inserting a carriage return/line feed? That e.print() function may not work like I thought.

Of course its print like html text, I think to send header i must to use something different like e.print...
But I think I need to randomize url - it`s wrong

But I cant to write IF section
I try to if(params.contains("?JvcPower&")) RemoteJVC(JVC_Power);
But:
error: 'class String' has no member named 'contains'

Why are you back to the damned String class?

to try contains, I dont understand how t work with char* like String

No "contains" here. Use strstr instead.

char *strstr( const char *s1, const char *s2)

returns a pointer to the first instance of string s2 in s1. Returns a NULL pointer if s2 is not encountered in s1.

Great! Its working! And when I add Randomize to URL - its stop to waiting!!! Problem done!

Thanks!!!

And small question:

    e.print("<a href='?JvcVolUp&");
      e.print(random(9999));
      e.print("'>Volume Up</a>");

I insert int into text with different string (e.print), can I do it in one, like:
e.print("Volume Up"); // ???

If earlier it displayed this in the web browser

HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n

then probably not. If it didn't know to use the escape characters for "\r\n", then it probably won't be able to insert anything into the text.

And some more )))

Can I use without text string (put IR code to URL):
e.print("CH-");

and get like:
RemoteJVC(params);

Its possible?

My last code:

#include <WString.h>
#include <etherShield.h>
#include <ETHER_28J60.h>
#include <IRremote.h>
#include <wrun_jvc.h>

IRsend irsend;
ETHER_28J60 e;

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};            
static uint8_t ip[4] = {192, 168, 1, 15};
static uint16_t port = 80;  

void RemoteJVC(unsigned long data) {
  irsend.sendJVC(data, 16, 0);
  delayMicroseconds(50);
  irsend.sendJVC(data, 16, 1);
  delay(50);
}

void setup() {
  Serial.begin(115200);
  Serial.println("Start 0");
  e.setup(mac, ip, port);
  Serial.println("Start 1");
}

void loop() {
  // WebServer
  char* params;
  if (params = e.serviceRequest()) {
    //params = params.substring(1,99);

    Serial.println(params);
    
    if(strstr(params,"JvcPower")) RemoteJVC(JVC_Power);
    if(strstr(params,"JvcVolUp")) RemoteJVC(JVC_Vol_Up);
    if(strstr(params,"JvcVolDn")) RemoteJVC(JVC_Vol_Dn);
    if(strstr(params,"JvcChNext")) RemoteJVC(JVC_CH_Next);
    if(strstr(params,"JvcChPrev")) RemoteJVC(JVC_CH_Prev);

    int rnd = random(99999);
    e.print("<html><head><title>wArd</title>");
    e.print("<meta http-equiv='content-type' content='text/html; charset=utf-8' />");
    e.print("<style>a{background:#ccc;display:block;font-size:80px;height:100px;text-align:center;margin:10px;}.temp{width:100px;display:inline-block;margin:10px 5px;}</style>");
    e.print("</head><body> <H1>Wrun Web Remote</H1>");
    e.print("<a href='?JvcPower&");
      e.print(rnd);
      e.print("'>Power</a>");
    e.print("<a href='?JvcVolUp&");
      e.print(rnd);
      e.print("'>Volume Up</a>");
    e.print("<a href='?JvcVolDn&");
      e.print(rnd);
      e.print("'>Volume Down</a>");
    e.print("<a href='?JvcChNext&");
      e.print(rnd);
      e.print("'>CH+</a>");
    e.print("<a href='?JvcChPrev&");
      e.print(rnd);
      e.print("'>CH-</a>");
    e.print("</body></html>");

    e.respond();
  }
}