As you can see the inputted string is printed as a string whereas the scripted string is formated according to the "\n"s in the string. How can I do this formatting with the inputted string?
Thanks for the reply. How do you mean to add a second print line?
Why will the program run out of memory, I only want to send some new code now and again? That's what we hobbyists do daily with our Arduinos, isn't it?
All depends on the number of times your program creates or modifies a "String". Strings are never changed in place they can ONLY be created new and that uses some memory. The used memory is never reclaimed until you execute the program the next time or your program ends.
is something that you can enter via the REPL, you should know about the REPL if you have not come across the REPL yet do a search to get familiar.
It is possible to create a desktop app with your own REPL or there is a debugging tool called aiorepl which allows you to modify and query a running application.
So the answer to your original question is yes you can but it would help to know what you want to achieve and if you have a particular reason why you would want to do that.
But as you can see from the OP, the serial monitor does format the code if the string is included (as a string) in the sketch. It is only the string that is inputted that isn't formatted. What I want to achieve is to send MicroPython code to a device running MicroPython. I can do this, for instance, with the response from the OpenAI API or by including the string in the sketch, so why can't I do it with a string inputted via the monitor?
You can input code via the monitor but not the Arduino IDE serial monitor, the Arduino IDE is used for c\c++ .
Also you need a monitor/terminal that recognizes and responds to Ctrl A CtrlB CtrlC CtrlD , this keyboard input does things like break enter Raw REPL , exit Raw Repl reboot etc.
Give a simple example of what you would like to see so we can see if it is possible.
I use the Arduino Lab for MicroPython to program my MicroPython devices and do not have a problem in this area.
I'm afraid you seem to have misunderstood my issue, probably my fault for mentioning MicroPython, but my problem is getting the Arduino IDE 2.3.2 serial monitor to recognize the \n in the string and put the following input on a new line. Just like the serial monitor does with a string in a sketch. I can't really explain it better than I did in the OP if you ignore the references to MicroPython, so I have edited the OP to remove the MP references.
The Serial monitor has nothing to do with it. Your sketch needs to recognise the \n in the String and print a Newline at that point
Read each character in the String and print it until you get a backslash. Do not print it. If the next character is a "n" then do not print it, print a Newline instead
Which is what I want from the input string "import machine\nimport utime\n\ngpio0 = machine.Pin(0, machine.Pin.OUT)\ngpio45 = machine.Pin(45, machine.Pin.OUT)\nutime.sleep(1)\ngpio45.value(0)\ngpio0.value(1)"
Yes, it works with the string shown above perfectly! I can also do it wth the response from OpenAI API. I just can't figure out how to format the input from the serial monitor.
String input = "";
void setup()
{
Serial.begin(115200);
}
void loop()
{
if (Serial.available())
{
input = Serial.readStringUntil('\n');
formatInput();
}
}
void formatInput()
{
int index = 0;
while (index < input.length())
{
if (input.charAt(index) != '\\') //note use of escape character
{
Serial.print(input.charAt(index));
index++;
}
else
{
Serial.println();
index += 2; //skip past next character whatever it is !
}
}
}
Thank you, I think you are on the right track, but actually the string that gets printed DOES have the \n characters as can be seen from the monitor output in the OP. The \n that is thrown away is right at the end, the "\n"s within the "..." are retained but ignored, it seems.
That seems to work! Thank you very much. I need to have a good look at it tomorrow as it is past midnight here and I need to go to bed. I will mark it as the solution tomorrow.