Old code stopped working

Hi guys.

I'm basically a noob in programming and I made this program like 4-5 years ago and it worked flawlessly and thus I never updated the code until when I tried to update the IP for it today due to moving flats, but now my code suddenly stopped wont work/compile without errors and I do not know what to do to fix it, and wondering if anyone is able to help. Maybe the library it is based on is outdated?

Premise:

It's a simple Webserver arduino with a ethernet shield. It hosts a basic HTTP website with 3 clickable buttons, each clickable buttons are linked to a radio transmitter, so when they are clicked a radio transmitter linked up to the arduino gets broadcasted and controls some wireless plugs around the house.

Code:

#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);
  } else if (strcmp(command, "4-on") == 0) {
    transmitter.sendUnit(2, true);
  } else if (strcmp(command, "4-off") == 0) {
    transmitter.sendUnit(2, 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 power</a></li>");
  c.println(        "<li><a href=\"./?1-off\">Turn off power</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?2-on\">Turn on power 1</a></li>");
  c.println(        "<li><a href=\"./?2-off\">turn off power 1</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?3-on\">Turn on power 2</a></li>");
  c.println(        "<li><a href=\"./?3-off\">turn off power 2</a></li>");
  c.println(    "</ul>");
  c.println(    "<ul>");
  c.println(        "<li><a href=\"./?4-on\">Turn on power 3</a></li>");
  c.println(        "<li><a href=\"./?4-off\">turn off power 3</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';
}

Errors I get while compiling:

G:\Dropbox\Dropbox\Arduino\RemoteControllFans\RemoteControllFans.ino: In function 'char* httpServer()':
G:\Dropbox\Dropbox\Arduino\RemoteControllFans\RemoteControllFans.ino:132:10: warning: address of local variable 'sReturnCommand' returned [-Wreturn-local-addr]
     char sReturnCommand[32];
          ^~~~~~~~~~~~~~
G:\Dropbox\Dropbox\Arduino\RemoteControllFans\RemoteControllFans.ino:171:10: warning: invalid conversion from 'char' to 'char*' [-fpermissive]
   return '\0';
          ^~~~
In file included from C:\Program Files (x86)\Arduino\libraries\Ethernet\src\Dns.cpp:8:0:
C:\Program Files (x86)\Arduino\libraries\Ethernet\src\Dns.cpp: In member function 'uint16_t DNSClient::BuildRequest(const char*)':
C:\Program Files (x86)\Arduino\libraries\Ethernet\src\utility/w5100.h:457:25: warning: result of '(256 << 8)' requires 18 bits to represent, but 'int' only has 16 bits [-Wshift-overflow=]
 #define htons(x) ( (((x)<<8)&0xFF00) | (((x)>>8)&0xFF) )
                      ~~~^~~
C:\Program Files (x86)\Arduino\libraries\Ethernet\src\Dns.cpp:164:18: note: in expansion of macro 'htons'
  twoByteBuffer = htons(QUERY_FLAG | OPCODE_STANDARD_QUERY | RECURSION_DESIRED_FLAG);
                  ^~~~~
Sketch uses 14004 bytes (43%) of program storage space. Maximum is 32256 bytes.
Global variables use 1211 bytes (59%) of dynamic memory, leaving 837 bytes for local variables. Maximum is 2048 bytes.

Those are repairable problems, and should be fixed. The compiler error messages make it very clear what each problem is.

Attempting to return a pointer to a locally declared array is a very serious error, although it is possible that it may have worked.

   return sReturnCommand;

Do your research and if you get stuck, post a specific question.

1 Like

The value returned by 'httpServer()' is never used so it might as well not return a value:

/*
   Process HTTP requests, parse first request header line and
   call processCommand with GET query string (everything after
   the ? question mark in the URL).
*/
void 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;
}

Those are warnings so your sketch should still work. I've never seen a "Note:" before.

I think you mean "your sketch should still compile."

The compiler warnings point out code that is syntactically legal but semantically dubious. Many times they are warning you of a problem that will cause your sketch to not work correctly. Ignore compiler warnings at your peril! :slight_smile:

You are correct. My error.
I have recently gotten into the habit of fixing all warnings even if the code works. I think it makes me a better programmer. Frustratingly, a lot of the warnings come from installed libraries.

Hi guys.

You were all correct... I just did a stupid mistake. The program still compiled properly and worked. The reason why I thought it stopped working was because I got no internet access to the arduino, but it was because the switch I connected it to died.... :stuck_out_tongue: but anyway due to the errors that I have in my code, it still has problems, like it freezes often (so I need to reset it once or twice a week) so instead of trying to correct the code I made a new program since that was easier for me...