Adding character to string

I apologizing if my question is simple, I am not very good at programming. First my overall goal, I want to control a servo with a arduino ethernet, my plan was to send a telnet command with an angle an an escape character to the board. For instance “telnet 130a”, (the escape character is a), the board will then set the servo to 130 degrees, clear all variables and disconnect the client.

Surprisingly controlling the servo seems pretty fine, and even setting up the telnet server, my main problem is actually transmitting the command. My problem is adding characters to a string, or eventually a character array.

My original plan was something like this.

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

byte mac[] = {
0x90, 0xA2, 0xDA, 0x0D, 0x58, 0x14 };
byte ip[] = { 192, 168, 1, 210 };


//setting up the server
EthernetServer server(23);
EthernetClient client;
boolean connected = false;
//the commad that is going to be sent to the servo
String kommando;

void setup() {

//starting server
Ethernet.begin(mac, ip);
server.begin();

}

void loop() {
// check if client connects
EthernetClient client = server.available();

//if client is connected add what the client sends to "bokstav" and then append "bokstav" to the string "kommando"
if (client) {


char bokstav = client.read();
kommando += bokstav;
client.println(kommando);

}
}

Hovever this doesnt work, when i telnet into the server it justs prints bak the empty string “kommando” again and again. I also tried “kommando += string(bokstav);” but that didn’t seem to help, I also dabbelde with using

stringstream myStream;
string myString;
myStream << bokstav;
myStream >> myString;

But stringstream seems to be unsupported on the Arduino, I also tried a character array, but I am not able to add more than one character into the array, and it then resets itself. Any tips? Its only the part in the loop that gives me problems.

Bear in mind, I am a total newbie.

if (client) {
char bokstav = client.read();
kommando += bokstav;
client.println(kommando);
}

  • kommando MÅ være definert som string for at dette skal virke.
  • du vil ta mot mer enn ett tegn, da må du lage en løkke (while?) som tar mot tegn splenge 'client' er tilgjengelig

Thanks for your tip knut, My code is a bit messy but the string "kommando" is defined earlier in the code.

I have followed your advise and tried to make a while() in the if(), and it seems to help, my new code is:

void loop() {
   EthernetClient client = server.available();

   if (client) {    
   int teller = 0;
   
 while ((bokstav = client.read()) != 'a')
    {     
      kommando += bokstav;
    }
     client.println(kommando);
     
   }
}

It works in the sense that if i connect via telnet and write "182a", it jumps out of the while(), and also it doesn't print the the "kommando"-variable again and again, however the String "kommando" seems to not be filled in right. So it seems like I am adding the character to the string the wrong way still.

..wrong way ?? like backwords?

what abuot char array then?

char kommando[10]; // stor kommando
byte ptr; 

void loop() 
{
   EthernetClient client = server.available();
   if (client) 
  {    
     int teller = 0; // ???
     ptr=0;
     while ((bokstav = client.read()) != 'a')    kommando[ptr++]= bokstav;
     kommando[ptr]= '\0';
     client.println(kommando);
   }
}

Why don't you try this code:

char buffer[number of characters];
int  bufferIndex = 0;

void setup()
{
  Serial.begin(38400);
}

void loop()
{
  if( Serial.available())
  {
    char ch = (char)Serial.read();
    if( ch == '\n')  // is this the terminating carriage return
    {
      buffer[ bufferIndex ] = 0; // terminate the string with a 0      
      bufferIndex = 0;  // reset the index ready for another string
      // do something with the string
      Serial.println(buffer);
    }
    else
      buffer[ bufferIndex++ ] = ch; // add the character into the buffer
  }
}

Hello, I was a bit annoyed with this problem, so I gave up for a while, but it still haven't been solved. I tried Vmateos suggestion (modified to work over telnet instead of serial), but that still doesn't seem to fix it, I never reach the printline part of the code, so I am not sure where it goes wrong, any tips are greatly appreciated.

If you know that the client is only going to send one command each time it connects then you can keep reading and buffering the incoming characters until the client disconnects or you receive the terminating character ‘a’.

// incomplete, untested

const byte BUFFER_LENGTH = 32; // make this long enough to hold your longest command
char buffer[BUFFER_LENGTH+1]; // +1 allows space for the null terminator
byte bufferCount;

void loop()
{
    // Create a client connection
    EthernetClient client = server.available();
    if (client)
    {
        Serial.println("Client connected");
        while (client.connected())
        {
            if (client.available())
            {
                char c = client.read();
                if(c == 'a')
                {
                    handleMessage(buffer);
                    bufferCount = 0;
                    buffer[bufferCount] = 0;
                    // if you want the server to disconnect the client after receiving the first command, call client.stop(); here
                }
                else
                {
                    if(bufferCount < BUFFER_LENGTH)
                    {
                        buffer[bufferCount++] = c;
                        buffer[bufferCount] = 0; // append null terminator
                    }
                    else
                    {
                        // buffer full - discard received character
                    }
                }
            }
        }
    }
}

void handleMessage(char *msg)
{
    your code here
}

Thank you peter, I tried to implement you changes as suggested. But it doesn’t quite work, this is what happes:

  1. I telnet into the board.

  2. It immediately jumps into to this if-loop:

if(bufferCount < BUFFER_LENGTH)
                    {
                        buffer[bufferCount++] = c;
                        buffer[bufferCount] = 0; // append null terminator
                    }

And runs through it 3 times (probably because i set the length to 3) (this is without me writing anything with the telnet client)

  1. If i press “a” ont he keyboard, and then enter it goes into
   if(c == 'a')
                {
                    handleMessage(buffer);
                    bufferCount = 0;
                    buffer[bufferCount] = 0;
                    // if you want the server to disconnect the client after receiving the first command, call client.stop(); here
                }

So in a way it works, but it seems like it runs through the loop even though i don’t write anything, and for some reason i have to press return to activate the escape character.

any tips?

Seems like i got it to work , the main problem was that putty sent some commands upon connecting with thelnet, after setting it in passive mode it worked a lot better, now i can connect, write 90a and return, and it will send 90 degrees to the servo (it still sends a few characters afterwards, maybe the return?)

THe code is underneath, I have a question though,can somebody tell me what theese two lines does:

buffer[bufferCount++] = c;
buffer[bufferCount] = 0; // append null terminator

Why does one add a 0? And won’t that overwrite the char added in the line over?

Thank you to everyone who helped, I am very grateful

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

byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0D, 0x58, 0x14 };
byte ip[] = { 192, 168, 1, 210 };


//setter opp server (telnet, port 23)
EthernetServer server(23);
EthernetClient client;
boolean connected = false; 
//kommandoen som skal tas imot av kliten
Servo myservo;

const byte BUFFER_LENGTH = 4; // make this long enough to hold your longest command
char buffer[BUFFER_LENGTH+1]; // +1 allows space for the null terminator
byte bufferCount;
                         
void setup() {
   
//starter serveren
   Ethernet.begin(mac, ip);
   server.begin();
   myservo.attach(3);
  
}

void loop() {
   // sjekker om klintt kobler seg til
   EthernetClient client = server.available();

   //om klienten er på, mota tegn som klienten skriver inn, legg inn tegnene i "kommando", og print kommando
   if (client) {
       
   // client.println("Tilkoblet via Telnet");
     while (client.connected())
        {
            if (client.available())
            {
                char c = client.read();
                if(c == 'a')
                {
                  //client.println("char er lik a");  
                 // client.println(buffer);  
                  handleMessage();
                    bufferCount = 0;
                    buffer[bufferCount] = 0;
                    client.stop();
                }
                else
                {
                    if(bufferCount < BUFFER_LENGTH)
                    {
                        buffer[bufferCount++] = c;
                        buffer[bufferCount] = 0; // append null terminator
                       // client.println("char er IKKE lik a"); 
                        //client.println(buffer); 
                    }
                    else
                    {
                        client.stop();
                    }
                }
            }
        }
    }
}

    
void handleMessage()
{
int pos;
pos = atoi(buffer);
  client.println(buffer);
  myservo.write(pos);
}

buffer[bufferCount++] = c;
buffer[bufferCount] = 0; // append null terminator

Why does one add a 0? And won't that overwrite the char added in the line over?

Suppose bufferCount is already 2. This code will put c into element [2] ( note, this is the third element of the array ), and then increment bufferCount to 3, and put a zero byte into element [3].

The next character you get will put the next c into element [3], and yet another zero byte into element [4].

This method is intended to ensure that, whenever your message ends, your character array will always have a zero byte at the end of it.