Webserver with buttons linked to Radio Commands works only once

Hi guys.

I made an arduino with a webserver that hosts buttons with an on and off switch. Each button corresponds with signal that gets outputted to my nexa radio receiver. So when I press “Button 1 on” on my Web Server, it tells the arduino to transmit a radio signal to my Nexa Unit which then tells it to turn on.

The problem I am having is that my program only seems to work once, then it stops. Like I can press every button once in the webserver but I cannot press it again until I restart the device. Sometimes after restarting it will still not work, unless I open a private tab in my browser, but then after a while that will also stop working.

I got everything in a loop, and the loop seems to be working so I am not sure why my radio signals only get sent once, then stops.

I am wondering if you guys could have a look at my code and see if you see anything obvious. I’m the farthest you can be from an expert, and my code is based on many examples.

I am using the Ethernet Library, and the NewRemoteSwitch library from: fuzzillogic / 433mhzforarduino / wiki / Home — Bitbucket (Located inside the NewRemoteSwitch folder)

#include <SPI.h>
#include <Ethernet.h>
#include <NewRemoteTransmitter.h>


const unsigned long TRANSMITTER_ADDRESS = 27612394; //My personal transmitter address
const int PIN_TRANSMITTER = 8;

// Create a transmitter on address TRANSMITTER_ADDRESS, using digital pin PIN_TRANSMITTER to transmit, 
// with a period duration of 260ms (default), repeating the transmitted
// code 2^3=8 times.
NewRemoteTransmitter transmitter(TRANSMITTER_ADDRESS, PIN_TRANSMITTER, 260, 3);

// Ethernet configuration
uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
uint8_t ip[] = { 192,168,1,230 };                        // IP Address
EthernetServer server(80);                           // Server Port 80

// RemoteTransmitter configuration
// Connect Transmitter to Pin 11 and receiver to pin 2 (all digital)
// FAN 1 ON/OFF:
// Addr 27612394 unit 0 on, period: 267us.
// Addr 27612394 unit 0 off, period: 267us.
// FAN 2 ON/OFF:
// Addr 27612394 unit 1 on, period: 266us.
// Addr 27612394 unit 2 off, period: 267us.
// FAN 3 ON/OFF:
// Addr 27612394 unit 2 on, period: 267us.
// Addr 27612394 unit 1 off, period: 266us.
// ALL POWER OFF:
// Addr 27612394 group off, period: 267us.


void setup() {
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(PIN_TRANSMITTER, OUTPUT);
  Serial.begin(9600);
  }

  
void loop() {
  char* command = httpServer();
}


void processCommand(char* command) {
  if        (strcmp(command, "1-on") == 0) {
    transmitter.sendGroup(true); //All fans on
  } else if (strcmp(command, "1-off") == 0) {
    transmitter.sendGroup(false); //All fans off
  } else if (strcmp(command, "2-on") == 0) {
    transmitter.sendUnit(0, true);
  } else if (strcmp(command, "2-off") == 0) {
    transmitter.sendUnit(0, false);
  } else if (strcmp(command, "3-on") == 0) {
    transmitter.sendUnit(1, true);
  } else if (strcmp(command, "3-off") == 0) {
    transmitter.sendUnit(1, false);
  }
}

/**
 * HTTP Response with homepage
 */
void httpResponseHome(EthernetClient c) {
  c.println("HTTP/1.1 200 OK");
  c.println("Content-Type: text/html");
  c.println();
  c.println("<html>");
  c.println("<head>");
  c.println(    "<title>MPC NEXA Webserver</title>");
  c.println(    "<style>");
  c.println(        "body { font-family: Arial, sans-serif; font-size:12px; }");
  c.println(    "</style>");
  c.println("</head>");
  c.println("<body>");
  c.println(    "<h1>MPC NEXA Webserver</h1>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?1-on\">Turn on all fans</a></li>");
  c.println(        "<li><a href=\"./?1-off\">Turn off all fans</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?2-on\">Turn on fan 1</a></li>");
  c.println(        "<li><a href=\"./?2-off\">turn off fan 1</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?3-on\">Turn on fan 2</a></li>");
  c.println(        "<li><a href=\"./?3-off\">turn off fan 2</a></li>");
  c.println(    "</ul>");
  c.println(    "<hr>");
  c.println("</body>");
  c.println("</html>");
}

/**
 * HTTP Redirect to homepage
 */
void httpResponseRedirect(EthernetClient c) {
  c.println("HTTP/1.1 301 Found");
  c.println("Location: /");
  c.println();
}

/**
 * HTTP Response 414 error
 * Command must not be longer than 30 characters
 **/
void httpResponse414(EthernetClient c) {
  c.println("HTTP/1.1 414 Request URI too long");
  c.println("Content-Type: text/plain");
  c.println();
  c.println("414 Request URI too long");
}

/**
 * Process HTTP requests, parse first request header line and 
 * call processCommand with GET query string (everything after
 * the ? question mark in the URL).
 */
char*  httpServer() {
  EthernetClient client = server.available();
  if (client) {
    char sReturnCommand[32];
    int nCommandPos=-1;
    sReturnCommand[0] = '\0';
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if ((c == '\n') || (c == ' ' && nCommandPos>-1)) {
          sReturnCommand[nCommandPos] = '\0';
          if (strcmp(sReturnCommand, "\0") == 0) {
            httpResponseHome(client);
          } else {
            processCommand(sReturnCommand);
            httpResponseRedirect(client);
          }
          break;
        }
        if (nCommandPos>-1) {
          sReturnCommand[nCommandPos++] = c;
        }
        if (c == '?' && nCommandPos == -1) {
          nCommandPos = 0;
        }
      }
      if (nCommandPos > 30) {
        httpResponse414(client);
        sReturnCommand[0] = '\0';
        break;
      }
    }
    if (nCommandPos!=-1) {
      sReturnCommand[nCommandPos] = '\0';
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
    Serial.print("Restarting Web Server");
    
    return sReturnCommand;
  }
  return '\0';
}

Edit:

I am wondering if it could be an issue with the browser. Chrome stopped working, but works on IE :stuck_out_tongue:

Edit2: Yeah, works fine in IE. Just Chrome that does not like it. I also made an application on android, and seems to work fine there.

MPC:
Edit2: Yeah, works fine in IE. Just Chrome that does not like it. I also made an application on android, and seems to work fine there.

Then it sounds like the Arduino code is working correctly, which is great.
It sounds like you'll have to start digging into the html/js code and find out which part hangs in Chrome - and that's out of the scope of this forum.

Jepp :slight_smile: