Terminal window

I use the terminal to trigger script and change values on an Arduino Mega but it is very unstable.

For test I just echo back the message I sent, Here are some of the errors:

test
you wrote: tes4
test
you wrote: 4est

or

pay100
gives me on the serial window of the Arduino the following errors and will cause the Blynk connection to reset:
[153185] invalid HW cmd : 6w
[2678427] invalid HW cmd : 6w

These errors occur nearly 50% of the time!

sketch?

//Blynk//////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V0)
{
Command=param.asStr();
if (Command.startsWith(“message”) ){
terminal.println(Command.substring(7)) ;

}
else if (Command.startsWith(“credit”) ){
terminal.print("Credit = ") ; terminal.println(Credit,DEC) ;

} else if (Command.startsWith(“pay”) ){
Command=(Command.substring(3));
Credit = Command.toInt();
terminal.print("Credit = ") ; terminal.println(Credit,DEC) ;
}
else if (Command.startsWith(“door on”) ){
//digitalWrite(x, LOW);
terminal.println("Door open ") ;
}
else if (Command.startsWith(“door off”) ){
//digitalWrite(x, HIGH);
terminal.println("Door closed ") ;
}
else if (Command.startsWith(“light on”) ){
//digitalWrite(x, LOW);
terminal.println(“Light on”) ;
}
else if (Command.startsWith(“light off”) ){
//digitalWrite(x, HIGH);
terminal.println(“Light off”) ;
}
else if (Command.startsWith(“alarm”) ){
//digitalWrite(x, HIGH);
terminal.println(“Alarm on”) ;
}
else {

// Send it back
terminal.print("You wrote:");delay(5);
terminal.write(param.getBuffer(), param.getLength());
terminal.println();

}

// Ensure everything is sent
terminal.flush();
Screen();
}

BLYNK_WRITE(V1){Mach[2].State = param.asInt();}

90% of the time bad results are down to a bad sketch. Suggest you run basic terminal sketch for a few hours. If it works fine it confirms you have a bad sketch.

What is the meaning of these errors?
[153185] invalid HW cmd : 6w
[2678427] invalid HW cmd : 6w

@Dmitriy can probably give a better answer but my guess is it translates to “bad sketch”.

Check my code to have an idea @michel.richir.

WidgetTerminal terminal(V20); // in global

BLYNK_WRITE(V20)
{
  if (String("Ssid") == param.asStr()) {
    terminal.println(String(ssid)) ;
    terminal.flush();
  }
  else if (String("Pass") == param.asStr()) {
    terminal.println(String(password)) ;
    terminal.flush();
  }
  else if (String("Auth") == param.asStr()) {
    terminal.println(String(auth)) ;
    terminal.flush();
  }
  else if (String("Volt") == param.asStr()) {
    terminal.println(String(voltage)) ;
    terminal.flush();
 } else {
    terminal.print("unknown command:");
    terminal.write(param.getBuffer(), param.getLength());
    terminal.println();
    terminal.flush();
 }
    terminal.flush();
}

Thanks for your help but not very helpful :wink:

All other functions from the loop have been commented out to have the sketch nearly limited to Blynk functions…

But I will do as you suggest

Hard to say. Many possible reasons. Please try with very simple sketch. Does it work as expected? Also please use formatting for code in future. So we could read it.

Thanks elanozturk

@elanozturk a single terminal.flush() at the end of the function should be enough as it will always get to the end of the function when it has passed through the if and else if statements.

1 Like

If my system was working badly I would be sending all the terminal.print() commands to Serial Monitor.

But remember Serial Monitor has many more features than the Terminal widget so it’s not a guarantee, more of a guide.

Thank you @Costas for the info,i just wanted to guarantee :slight_smile:

Sorry for the code. Better formatting?

BLYNK_WRITE(V0)
{
  Command=param.asStr();
  if (Command.startsWith("message") ){
    terminal.println(Command.substring(7)) ;

  }
   else if (Command.startsWith("credit") ){
    terminal.print("Credit = ") ; terminal.println(Credit,DEC) ;

  }    else if (Command.startsWith("pay") ){
    Command=(Command.substring(3));
    Credit = Command.toInt();
    terminal.print("Credit = ") ; terminal.println(Credit,DEC) ;
  } 
  else if (Command.startsWith("door on") ){
     //digitalWrite(x, LOW);
    terminal.println("Door open ") ;
    }  
    else if (Command.startsWith("door off") ){
     //digitalWrite(x, HIGH);
    terminal.println("Door closed ") ;
  }
    else if (Command.startsWith("light on") ){
     //digitalWrite(x, LOW);
    terminal.println("Light on") ;
  }  
     else if (Command.startsWith("light off") ){
     //digitalWrite(x, HIGH);
    terminal.println("Light off") ;
  }  
  else if (Command.startsWith("alarm") ){
     //digitalWrite(x, HIGH);
    terminal.println("Alarm on") ;
  }
  else {

// Send it back
terminal.print("You wrote:");delay(5);
terminal.write(param.getBuffer(), param.getLength());
terminal.println();

  }

  // Ensure everything is sent
  terminal.flush();
  Screen();
}
1 Like

There are some online code beautifier sites like http://codebeautify.org/c-formatter-beautifier :slight_smile:

Thanks!

@michel.richir, i wonder if you are able to test the same with UNO/NANO etc. I’m asking because your problem is very similar to mine: strings, or more precisely char arrays were getting corrupted but ONLY when using MEGA board. That was weird…

@michel.richir and @marvin7 the extract provided by Michel, and with the unknown function of Screen() commented out, works 100% of the time on my Mega (Ethernet) and my WeMos (WiFi).

@Costas: The reason doesn’t have to be the same, but I had weird problems with cut/distorted strings (chars) just with “blank” blynk examples on Mega board. And the same works as expected on Atmega328. Heh, that is something I have never met before! @Costas: what version of board definitions you are using? Recently I upgraded Arduino IDE along with board definitions.

Board definition 1.6.14 in IDE version 1.6.12.