Skip to content

After using the String class for a few iteration, it starts malfunctioning #3004

@joviol

Description

@joviol

I have entered the following program to get commands through an ethernet shield.
The commands seem to get to the arduino correctly as it prints it correctly in the println call placed in the loop() function.
But once I call the processCommand function, it only works the first few times, after which it starts printing random strings (mostly "?"). If I try to use a reference to String instead as a parameter to processCommand, the indexOf function works only the first few times after which it will not recognise the string event if it prints the one I entered, which is one of those I expect as a command.

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

byte mac[] = {
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
IPAddress ip(192, 168, 1, 177);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false;

const int ledPin = 2;
String commandString = "";

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);

   // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection:
  Serial.println("Trying to get an IP address using DHCP");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // initialize the ethernet device not using DHCP:
    Ethernet.begin(mac, ip, gateway, subnet);
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  ip = Ethernet.localIP();
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(ip[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
  // start listening for clients
  server.begin();

}

void loop() {
  // wait for a new client:
  EthernetClient client = server.available();

  if(client) {
    if(!alreadyConnected) {
      // Clear out the input buffer
      client.flush();
      commandString = "";

      server.println("--> Please type your command and hit Return...");
      alreadyConnected = true;
    }

    while(client.available()) {
      // Read bytes incoming from the client:
      char newChar = client.read();
      if(newChar == 0x0D) { // 0x0D = Carriage Return ASCII code
        server.print("Received the following command: ");
        server.println(commandString);
        processCommand(commandString);
      }
      else {
        Serial.println(newChar);
        commandString += newChar;
      }
    }
  }

}

void processCommand(String cmd) {
  server.print("Processing Command: ");
  server.println(cmd);

  if(cmd.indexOf("photo") > -1) {
    server.println("Photo command Received");
    server.print("Reading from the photoresistor: ... ");
    server.println(analogRead(A0));
    commandString = "";
    return;
  }

  if(cmd.indexOf("ledon") > -1){
    server.println("LED On command received");
    digitalWrite(ledPin, HIGH);
    server.println("LED was turned on");
    commandString = "";
    return;
  }

  if(cmd.indexOf("ledoff") > -1){
    server.println("LED Off command received");
    digitalWrite(ledPin, LOW);
    server.println("LED was turned off");
    commandString = "";
    return;
  }
  commandString = "";
  instructions();
}

void instructions() {
  server.println("I don't understand");
  server.println("Please use one of the following commands:");
  server.println("> photo, to get a reading from the photoresistor");
  server.println("> ledon, to turn on the LED");
  server.println("> ledoff, to turn off the LED");
}

I am using and Arduino Uno Rev3 and an ethernet shield.
The version of the IDE is 1.6.3

Metadata

Metadata

Assignees

Labels

Component: CoreRelated to the code for the standard Arduino API

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions