inserting values to a function with Serial read?

More specifically, I want to set the date of a RTC whenever I want via a Serial text, SMS or Internet or w/e (the doubt is for the whole programming language not just for rtc)

But I’m lost of how I’d make it read the message as the date I’m trying to set…

my idea is something like this:

#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeLib.h>

RTC_DS1307 rtc;

#define _sda A4 //rtc
#define _scl A5 //rtc

String message = "";

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


void loop()
{
  message = ReadMSG();

  if (message == "set xxxx, xx, xx, xx, xx, xx") // in this case: 2019, 02, 05, 16, 30, 00
{  
     // after "set", these would be the input for the date to make rtc.adjust like this:
     //rtc.adjust(DateTime(2019, 02, 05, 16, 30, 00));
 

    rtc.adjust(DateTime(xxxx, xx, xx, xx, xx, xx));
    Serial.println("fixed!");
  }
}


String ReadMSG()
{
  while (Serial.available() > 0)
  {
    return Serial.readString();
  }
}

how would i do something like this?

–edit

I modified it to a HEX code via Serial check post #9 for more

how would i do something like this?

A lot more intelligently than you are trying.

It is pointless to have a while loop that contains a return statement as the only statement in the body.

That will statement will execute at most once. If it does not execute one, you do not return anything, so you lied when you said the function would always return a String.

If you KNOW that the message is going to be "set 2019, 02, 05, 12, 34, 56", then you would just call rtc.adjust() with those hard-coded values.

Testing that message STARTS WITH "set", and then parsing the rest of the message to get an int and 5 bytes WOULD make sense. Using strtok() and a string would make sense. Pissing away resources using indexOf() and substring() on a String does not.

It wasn't an attempt this is just a code I have to modify the date, I just showed how I thought it would be...

PaulS:
If you KNOW that the message is going to be "set 2019, 02, 05, 12, 34, 56", then you would just call rtc.adjust() with those hard-coded values.

Testing that message STARTS WITH "set", and then parsing the rest of the message to get an int and 5 bytes WOULD make sense. Using strtok() and a string would make sense. Pissing away resources using indexOf() and substring() on a String does not.

Yea, I will always know the date, I'll be setting it manually... how would I call the function and ignore the starting "set" and reading the numbers? could you tell me more about this strtok?

could you tell me more about this strtok?

Mr. Google has way more patience for this type of question than I do.

Could anyone else help me? I've got some hours into it and couldn't do it yet with strtok it's easy to divide the string but still not able to do it

You've tried something with strtok(), which is the right thing to do.

Not posting code, and not explaining what the code actually does, or how that differs from what you want is NOT the right thing(s) to do.

I actually didn’t make much since I couldn’t figure out much about strtok and how to use later the “tokens”
I also tried string.remove, and converting to INT but immediately stopped since I realized I’d need much more chars than Int can handle…

#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeLib.h>

RTC_DS1307 rtc;

#define _sda A4 //rtc
#define _scl A5 //rtc

String msgoriginal = "";
String msgcode = "";
int msgdate = "";

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

//set 20190206165000

void loop()
{
  msgoriginal = ReadMSG();
  msgcode = CutDate(msgoriginal);

  if (msgcode == "set")
  {
    Serial.println("now inside the IF ");
    
      String varToCopy = msgoriginal;
      // varToCopy.remove(0, 4);
      msgdate = varToCopy.toInt();
      
      Serial.println(msgdate);
      /*
      rtc.adjust(DateTime(msgdate));
      Serial.println("fixed!");
    */
  }
}


String CutDate(String msg)
{
  msg.remove(3);
  return msg;
}

/*
  DateTime CutCode(String cod)
  {
  cod.remove(0, 4);
  return cod;
  }

*/

String ReadMSG()
{
  while (Serial.available() > 0)
  {
    return Serial.readString();
  }
}

so nothing much changed, back at zero

You can't use strtok() on a String instance. You've been advised to stop using Strings, but you persist.

I will NOT write code for you to parse the String that you have. You will need to learn about indexOf() and substring() and toInt() is you insist on pissing away memory using Strings.

You STILL have a stupid while loop that can iterate a maximum of once, and, if it doesn't FAILS to put anything on the stack where a String is supposed to be.

Speed = readString.substring(0, 3);

char Magic[6]; //magic needed to convert string to a number
Speed.toCharArray(Magic, sizeof(Magic));
Sp = atoi(Magic);
Serial.println(Sp);

This is what I use. It works for converting serial speed input to an usable variable.
Don't ask me how it works :wink:
Found it on a topic once, a while back.

I stopped using Strings and we decided here to uses bytes and HEX… we even managed to do it, kinda poorly, but it’s working!
Still using the while thing since we don’t know what else to use, but oh well…

Here it is if anyone’s interested:
Manually setting up RTC clock with HEX via Serial with a starting “code”

ps: don’t forget to add +40 to it’s values so it can later return to the original number so it can skip the non-number ones(like NULL, ESC etc), check http://www.asciitable.com/ for more

#include <Time.h>
#include <TimeLib.h>
#include <Wire.h>
#include <RTClib.h>

RTC_DS1307 rtc;

// PINS.
#define _sda A4 //rtc
#define _scl A5 //rtc

int i;
byte data[16] ;
int dta[7];

void setup()
{
  Serial.begin(9600);
  rtc.begin();
}

// ADD +40 TO DATE VALUE TO SKIP INVALID HEXES

//S;*07FK = 08/02/2019 15:30
//S CODE, ; YEAR, * MONTH, 0 DAY, 7 HOUR, F MIN, K SECONDS

void loop()
{
  //  Serial.println(1);
  while (!Serial.available())
  {
    if (Serial.available() > 0)
    {
      Serial.readBytes (data, 7);
      //      Serial.println(2);
      break;
    }
  }

  //Serial.println(3);
  //Serial.println(data[0]);

  if (data[0] == 0x53) {
    // Serial.println("4  inside if CODE");
    for (int i = 0; i <= 6; i++)
    {
      dta[i] = data[i] - 40; // skip invalid hexes
      // Serial.println(5);
    }
    // Serial.println(6);
    rtc.adjust(DateTime(2000 + dta[1], dta[2], dta[3] , dta[4] , dta[5] , dta[6]));
    Serial.println("7  Date fixed!");
  }
  // Serial.println(8);
}
      dta[i] = data[i] - 40; // skip invalid hexes

That is NOT what that code is doing.

What is sending data to the Arduino?

PaulS:

      dta[i] = data[i] - 40; // skip invalid hexes

That is NOT what that code is doing.

What is sending data to the Arduino?

I'm sending it myself via the serial and adding +40 to the values, and with that, I'm removing it to return to the "original" value

I'm sending it myself via the serial

You have the serial cable plugged where?

on my PC

sxarthur:
on my PC

So, you are using some app that you have not identified, to send the serial data. You are not "sending it yourself".

When you can answer the questions asked, with relevant answers, I'll become interested in your posts again. Right now, you are just wasting my time.