Send SerialData from keyboard to Arduino connected on Android Device

Okay, the title might be a brain freezer, but... I have an arduino Nano/Uno/ProMicro attached to an Android device, which runs an custom app that shows some info based on serial data received from the arduino (which is connected through an OTG cable). What I want to to, is to send manual data from the arduino, from the SerialMonitor, for debbuging the Android app. Now, I could write a code, to send regular messages from arduino, but it looses the debuging point.
Is there a simple way to achieve this idea?

Could I connect two arduinos and echo the message received?

You could connect two arduinos together. The second one connects to the PC and uses the Serial Monitor. It receives a message and then sends the message to the other Arduino. You would need to use the SoftwareSerial library since the Nano/Uno only has one hardware serial port. You can also read the Serial Input Basics - updated tutorial.

I think what are wanting is to send data to the NANO/UNO on a second Serial (software serial) via a USB/ttl cable to the UNO pins send/reciveve to the Serial (USB) connection

see AltSoftSerial Library, for an extra serial port

#include <AltSoftSerial.h>

AltSoftSerial altSerial;

void setup() {
  Serial.begin(9600);
  Serial.println("AltSoftSerial Test Begin");
  altSerial.begin(9600);
  altSerial.println("Hello World");
}

void loop() {
  char c;

  if (Serial.available()) {
    c = Serial.read();
    altSerial.print(c);
  }
  if (altSerial.available()) {
    c = altSerial.read();
    Serial.print(c);
  }
}

Using a Mega2560 would be better as it has multiple hardware Serial ports

So I’ve tried sending data from an arduino pro micro using pins 7 (RX) and 8 (TX) with this sketch:

#include <SoftwareSerial.h>
SoftwareSerial SerialSend(7, 8); //RX, TX
String readString;

void setup() {
  Serial.begin(9600);
  SerialSend.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {
 
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString);
      SerialSend.print("h"); //prints string to serial port out
      readString=""; //clears variable for new input     
     } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

and used an UNO to receive the data using pins 10 and 11 with this sketch:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //RX, TX
String readString;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  mySerial.listen();
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (mySerial.available())  {
    char c = mySerial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString
        readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

I am connecting the RX to TX and viceversa, and ground to ground. on the serial monitor for the pro micro (sending), I am getting the feedback from what i am typing with “,” at the end. But no luck receiving anything on the other device. the UNO is not a chinese knock of.

Try something simpler. Just print a fixed message from the pro micro to software serial and try and read it character by character on the nano and echo it to serial.

Is the pro micro the 5V (sparkfun) version?
I think your send code is missing a SerialSend

#include <SoftwareSerial.h>
SoftwareSerial SerialSend(7, 8); //RX, TX
String readString;

void setup() {
  Serial.begin(9600);
  SerialSend.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {
 
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString);
      SerialSend.print("h"); //prints string to serial port out
      readString=""; //clears variable for new input     
     }
    else {     
      readString += c; //makes the string readString
      Serial.print(c); // echo
      SerialSend.print(c);
    }
  }
}

Thank you! the missing SerialSend was the issue. The working codes are now: Sender

#include <SoftwareSerial.h>
SoftwareSerial SerialSend(7, 8); //RX, TX
String readString;

void setup() {
  Serial.begin(9600);
  SerialSend.begin(9600);
  delay(1000);
  Serial.println("Please enter text ending with character *"); // so I can keep track of what is loaded
}

void loop() {
 
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == '*') {
      Serial.println(readString); //prints string to serial monitor for debugging
      SerialSend.print(readString); //prints string to serial port out
      SerialSend.print("*");
      readString=""; //clears variable for new input     
     } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

and Receiver

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //RX, TX
String readString;
int long byteTime = 0;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  mySerial.listen();
  Serial.println("begin listening"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string ending with character "$" like  --  Hello world!$  --

  if (mySerial.available())  {
    byteTime = millis();
    char c = mySerial.read();  //gets one byte from serial buffer
    if (c == '*') {
      if (readString.length() > 0) { // check to see if string has any character in it
        Serial.println(readString); //prints string to serial port out
        Serial.println(millis() - byteTime);
        readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString, aka adds one byte at a time to the string
      //Serial.print(c);
    }
  }
  if (millis() - byteTime > 100){
    //Serial.println(byteTime);
    readString="";  
  }
}

But my problem is now that I can’t delete a string that was sent by accident without the delimiter " * ". I’ve tryed to use a time constant of 100 microseconds to check if anything was received in that interval, and to erase the string if not, but with no luck. Any better aproach?

This works for me (uno → mega)

// MEGA
#include <SoftwareSerial.h>
#include <millisDelay.h>
// install SafeString library from Arduino manager for millisDelay
// see this tutorial https://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html

SoftwareSerial mySerial(10, 11); //RX, TX
String readString;

millisDelay readTimeout;
unsigned long READ_TIMEOUT_MS = 100;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(i); Serial.print(' ');
    delay(500);
  }
  Serial.println();
  readString.reserve(100); // assign some initial space
  delay(1000);
  Serial.println("Receiver read"); // so I can keep track of what is loaded
}

void loop() {

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    mySerial.print(c);
  }
  if (mySerial.available())  {
    char c = mySerial.read();  //gets one byte from serial buffer
    if (c == '*') {
      readTimeout.stop();
      Serial.println(readString); //prints string to serial port out
      readString = ""; //clears variable for new input
    }
    else {
      readString += c; //makes the string readString, aka adds one byte at a time to the string
      readTimeout.start(READ_TIMEOUT_MS);
     // Serial.print(c);
    }
  }

  if (readTimeout.justFinished()) {
    Serial.println(F("read timeout no * clear readString - "));
    Serial.println(readString); //prints string to serial port out
    readString = "";
  }
}

Here is the UNO test code

// UNO
#include <SoftwareSerial.h>
SoftwareSerial SerialSend(7, 8); //RX, TX
String readString;

void setup() {
  Serial.begin(9600);
  SerialSend.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(i); Serial.print(' ');
    delay(500);
  }
  Serial.println();
  delay(1000);
  readString.reserve(100); // allow some space
  Serial.println("Please enter text ending with character *"); // so I can keep track of what is loaded
}

void loop() {
  if (SerialSend.available()) {
    char c = SerialSend.read();  //gets one byte from serial buffer
    Serial.print(c);
  }
  // for testing missing *
  if (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    SerialSend.print(c);
  }

//  if (Serial.available())  {
//    char c = Serial.read();  //gets one byte from serial buffer
//    if (c == '*') {
//      Serial.println(readString); //prints string to serial monitor for debugging
//      SerialSend.print(readString); //prints string to serial port out
//      SerialSend.print("*");
//      readString = ""; //clears variable for new input
//    }
//    else {
//      readString += c; //makes the string readString
//    }
//  }
}

Took a while to debug until I realized the send code would not allow sends until a * terminated the data

Actually your problem might be in the Sender.
If the sender does not get a * it just keeps the data so far and waits for a *
So add the timeout to the sender as well, to dump lines that don't end in a *

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.