"RX" reading from a router!

I connect My router with DD-WRT to my Arduino
everything work great I can read everything over Serial Monitor without any weird charterers.
Router Arduino
TX RX
RX TX
GND GND

What I want is to send SSH command from my router to my Arduino to control PIN13
From what I know TX-RX it's a serial port like USB!

So I made simple code:

void setup () {
  pinMode(13,OUTPUT);
  Serial.begin(115200);
}
void loop () {
  int val;
  if (Serial.available ()) {
    delay (100);
    while (Serial.available() >0) {
      val=Serial.read();
      
      if(val=='1') {digitalWrite(13,HIGH); }
      else if (val=='0') digitalWrite(13,LOW);
    }
  }
}

When I send commands from Serial Monitor it's work LED off,on

But when I tried that from my Router by using this simple command it dose not work at all

echo "1" > /dev/tts/0 115200

Can someone help me in this please!

There are so many things that could be wrong on the router configuration. You should find a way to test it separately. One way is to hold the Arduino reset line low. Then you should be able to monitor the serial data from the router, using the Arduino USB serial connection. But TX and RX lines will be reversed. Perhaps they already are...

If Serial Monitor can talk directly to the Router via RX/TX then the Arduino can't talk to the Router, only to Serial Monitor. The Router and Arduino are both using TX to send to Serial Monitor and RX to receive from Serial Monitor. That means the Arduino TX and Router TX are connected together. To have the Arduino talk to the Router instead of Serial Monitor, reverse the RX and TX connections on the Router.

In order for the Arduino to use both the Router and Serial Monitor you need a second serial port. Use Software Serial, an Arduino Leonardo (Serial to Serial Monitor and Serial1 to Router), or an Arduino Mega (Serial to Serial Monitor and Serial1 , Serial2, or Serial3 to Router).

echo "1" > /dev/tts/0 115200

I have seen several Threads that had a problem with this sort of thing (mostly in the Interfacing section)

I suspect part of the problem (or most of it) has to do with the Arduino resetting when the Serial Port is opened.

Have you the option of writing a short Python program to send the data so that you can call the Python program from the command line ?

This Python-Arduino demo may give you ideas.

...R

I flip my wires it doesn't work too!, I believe the problem from my Arduino code,

sending a command from my router only work if I upload empty code to my Arduino, only inside serial Monitor
Any other suggestions!? :roll_eyes:
I only want to send commands from my router to Arduino.

narzan:
sending a command from my router only work if I upload empty code to my Arduino, only inside serial Monitor
Any other suggestions!? :roll_eyes:

Yes, re-read Robin2's post.

It is very likely your "problem" is related to the auto-reset of the Arduino board, which occurs each time the serial port is re-opened with your echo command.

  if (Serial.available ()) {
    delay (100);
    while (Serial.available() >0) {

This makes absolutely no sense. There is no reason to check if the serial buffer has at least 1 character, delay, and then check if it still has 1 character.

Connect RX to TX on your router, make sure you have local loopback.

UPDATE
It's work now if I send 1 it's ON
But if i send 0 it doesn't work :roll_eyes:
I worked befure with USB serial port + VB.net
but always worked with values at this moment I really not know what I am doing and why when I removed that it's work

 if (Serial.available ()) {
    delay (100);
    while (Serial.available() >0) {

That's my final code

void setup () {
  pinMode(13,OUTPUT);
  Serial.begin(115200);
}
void loop () {
  int val;
 
      val=Serial.read();
      if(val=='1') {digitalWrite(13,HIGH); }
      else if (val=='0') {digitalWrite(13,LOW);}}

You can't do a valid serial read without checking to see if a character is available.

Can you explain that more please more hints..
I used the same code that useauly use with VB.net over serial port, it's weird That douse it work,

narzan:
Can you explain that more please more hints..
I used the same code that useauly use with VB.net over serial port, it's weird That douse it work,

It's bad form to not at least check if there is something in the serial buffer. You didn't quite understand my point earlier.

void setup () {
  pinMode(13, OUTPUT);
  Serial.begin(115200);
}
void loop () {
  int val;
  if (Serial.available() > 0) {
    val = Serial.read();
    if (val == '1') {
      digitalWrite(13, HIGH);
    }
    if (val == '0') {
      digitalWrite(13, LOW);
    }
  }
}

The reason your code works without the check is that Serial.read() returns -1 when there are no characters in the buffer. So in theory, your code is fine. In practice, it is bad form.

Edit: Fixed typo in code

It's work on VB.net without any problem ?!
I saw some articles to be sure, I didn't see anything different.
Just see This article
you can wright the code line I will not eat you :stuck_out_tongue:

if (Serial.availaable() > 0) {
                 ^

Wrong.