Parsing received sms.... problems converting to strings.

Hello everyone.

I have trouble converting parsed received sms message.

Have used strtok and when executing Serial.println(pch) (// char * pch) it seems fine.

count=0;

  • pch = strtok (buffer,","\r");*
  • while (pch != NULL)*
  • {*
  • pch = strtok (NULL, ","\r");*
  • count++;*
  • }*
  • Serial.print("count: ");*
  • Serial.println(count);*
  • Serial.print("pch: ");*
  • Serial.println(pch);*

But I want to convert "char * pch" to string to be able to do som work with it.
I have search the net for a while and have tested a lot of "solutions".

This code I have used before wont even work:

String str(char[])

Im a little confused :slightly_frowning_face:

Help needed.

Regards

Kjartan

What do you think that String (uppercase S) can do that normal c string (lowercase s) functions can’t do.

Im buliding a system for my cabin to be able to remotely control heat, lights; to check solar charging of by batteries; checking temperature aso.....

So with the sms I want to use the Arduino String operators; to ex. check the mobilenumber, analyse the message to check what operations I want to start or stop.

So do you mean I can use c or c++ function to cover my needs?

Should I include some more libraries to do this?

Regards

Kjartan

The variable pch is a pointer to a NULL terminated array of chars. In other words, is IS a string. Why do you think you need to “convert” a string to a string?

kjartani:
So do you mean I can use c or c++ function to cover my needs?

Should I include some more libraries to do this?

C/C++ can do. And in the Arduino world you don 't need to include something extra for this; did you include something when you used strtok? Maybe you did but it was not necessary.

You did not give an example of the data that you received. Below some code that parses received sms data (just a thumb suck) containing a few commands to execute and some queries. I kept it simple.

The code demonstrates
splitting a null terminated character array (smsData, using strtok)
splitting a null terminated character array (cmd, using strchr)
checking a single character (! or ? at the first position)
comparing a null terminated character array to a fixed value (using strcmp)
converting a null terminated character array to an integer (using atoi)

I kept it simple, the commands are single letter commands and not words, the values can be anything. A command starting with a '!' is used to set something and a command starting with a '?' is used to query a status.

// received data from SMS
char smsData[] = "!L=127,!H=ON,?T,!T=23,?B";

int temperature = 20;
int lightlevel = 0;


void setup()
{
  Serial.begin(250000);


  char *token;

  token = strtok(smsData, ",");
  while ( token != NULL )
  {
    Serial.println(token);

    if (token[0] == '!')
    {
      execute(&token[1]);
    }
    else if (token[0] == '?')
    {
      query(&token[1]);
    }
    else
    {
      // invalid
      Serial.println("Unknown");
    }

    token = strtok(NULL, ",");
  }
}

void execute(char *cmd)
{
  // parse command into command and value
  char *argument = strchr(cmd, '=');
  // no '=' in command
  if (argument == NULL)
  {
    Serial.println("Command structure invalid");
    return;
  }

  // replace comma by null terminator
  *argument = '\0';
  // increment pointer
  argument++;
  // cmd now only contains the command
  // ptr now points to command argument

  switch (cmd[0])
  {
    // dim light
    case 'L':
      lightlevel = atoi(argument);
      analogWrite(9, lightlevel);
      Serial.print("light level set to "); Serial.println(lightlevel);
      break;
    // control heater on/off
    case 'H':
      if (strcmp(argument, "ON") == 0)
      {
        digitalWrite(13, HIGH);
        Serial.println("heater on");
      }
      else
      {
        digitalWrite(13, LOW);
        Serial.println("heater off");
      }
      break;
    //set temperature
    case 'T':
      temperature = atoi(argument);
      Serial.print("temperature set to "); Serial.println(temperature);
      break;
  }
}

void query(char *key)
{
  switch (key[0])
  {
    //query battery status
    case 'B':
      Serial.print("battery level = "); Serial.println(analogRead(A0));
      break;
    //query current temperature
    case 'T':
      Serial.print("temperature reading = "); Serial.println(analogRead(A2));
      break;
  }
}

void loop()
{
}

I hope this helps to show you some of the options.

Be aware that the switch/case can't be used if the commands are words.

Wow.

PaulS. I have not thought of it the way you explained. Thanks.

sterretje. That was an explanation that I think I come to understand. Thanks a lot.

I will be back with my progress and code solution.

:slight_smile:

Kjartan