You are using
Serial.println() to send the message. That function will add the combination
\r\n as line ending to your message, which are the ASCII codes for return carriage and newline.
In your receiving code, you read until
\n, so your message now reads
"0\r", which is of course not equal to
"0". Thus the if condition will never trigger.
msg == '0' you are taking the first character out of the string and compare that to the character
'0' (especially note the difference between
"0", which is a string, and
'0', which is a single character). Since you are only using the first character of the string, the
\r is out of the picture; you aren't using it.
What to do now?
You could send your message only with the
\n character as line ending. For this only use
Serial.print() instead of
Serial.println() and add the line
Serial.print("\n"); directly after you send your message.
You could write your own read-until code and just ditch every occurrence of the
You can use
msg.trim() to remove any white space characters at the start or end of your string, including the
\r at the end. Then you can use the code, that you already wrote.
Note, that using
Strings on an Arduino is mostly considered evil. Especially if you want to use the code for a longer period or you are using
String very often. The memory for a
String is allocated dynamically, which can easily lead to heap fragmentation. For a better and longer explanation, look at The Evils of Arduino Strings, a blog entry from Majenko, who is one of our biggest contributors here.