Serial comunication with Arduino Mega - Serial monitor problem

Hi,

I have problem with receiving serial data on serial monitor in Mit App application.

I need to receive different Serial1.print() data from Arduino thro Bluetooth, like different commands and sensor data: Left, Right, Distance from GPS point, Heading of compass,....... and so on.

Beside this all data that are coming thro serial monitor, I need to receive specific data of GPS Latitude and Longitude that I need to store in TinyDB so I can send it back to Arduino in case Arduino freeze or accidentally disconnect from power.

I use # at beginning of string so that App recognizes that it needs to catch that particular data and split it (split Latitude and Longitude) and store it in TinyDB.

The string looks like this: #,45.123456,22.123456

The problem is next, I can’t make the App working with both data streams Serial monitor (for all data coming) data and GPS coordinate data that I trigger by sending Bluetooth command to Arduino.

I can get one or the other to work.

Or I get GPS data, but all other data are not coming to serial monitor, or I have data on serial monitor but not receiving GPS data.

Any idea or advice how to solve this problem?

This is the wersion when only Serial Monitor works but not GPS data.

This is the wersion where GPS data works, but serial monitor not.

You are violating some standard rules for receiving text in BlueTooth:

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

1 Like

Hi ABG,

Delimiter-byte is set to 10, and I use serial.println(); at the end. The clock timer interval is set to 100.

I receive the data coming to my serial monitor.
The problem is that I can make only program work in one way:

  1. getting serial data constantly from Arduino on serial monitor (data from loops, or single action data), I get GPS string (requested on demand once) on serial monitor but I dont get split values that should be sent to text box and store in TinyDB.
    Or
  2. I get GPS data, it splits, apears in text box, and get stored to TinyDB, but than I dont get other serial data on serial monitor

So basically program works for individual serial data, but not when I combine them.

I am splitting data for GPS in global variable and checking them, see photo, if is that what you ask.

1 Like

Ok,

i manage to make it work :slight_smile:
I made another global data named gpsdata and coppy global data to it, then split it and works.

Watch out for lost readings, if you are grabbing incoming data without regard for end of message delimiters (numberofBytes = -1).

Your Serial Monitor is the Arduino Serial Monitor on your PC, or a different Monitor on your Android Device?

If you want the App to receive all the data via Bluetooth, it must be sent via Software Serial. You cannot send different data types in two streams to the App, that is totally impractical - send everything in one stream. Your App can 'know' which value is which by the order they are in the stream.

100ms is a very short time interval. It is always best to use the longest interval acceptable to the task. Note that the App receive time interval should be set 20% shorter than the Arduino send time interval.

There are errors in the Clock Timer Block that you originally uploaded. When you check for length of list, it must equal the expected length. Too long or too short, reject.

The formatting of floats should be done in the Sketch e.g.
Serial.print(Lat,6); // Set float value to 6 decimal places. (which is OTT)

Hi,

when I change to BT.ReceiveText(-1) than I dont get any data.

It serial2 from Mega on, Bluetooth is on it!
I call the incoming screen (text box) in my App serial monitor :slight_smile:
Some of data coming from Mega is just informative data like, you pressed Left turn, Right turn, Stop, distance from home,..... depending in what loop or action is Arduino program in.
But one particular data (GPS location) that I request by sending the request from App to mega i want to store to Tiny DB as backup. Its send only once when I ask for it, so no other data is coming to App beside Serial2.print > #,45.123456,13.123456 and on the end i put serial2.println().
How this time interval function, In Arduino I dont have send time interval since is one time event (when I send request code to Arduino)?
It would make sense if the data are coming from Loop or While part of the code that is regulated with some sort of time interval like delay or millis.
But in this case is direct.
What settings for clock you recommend in that case.
Other data beside GPS location are coming from Loop or while, in those cases i put 50-100 mS pause so I can also see data on my PC serial monitor.

In Arduino code i set GPS Latitude and Longitude as Double and I serial2print(latitude, 6) so I send 6 decimal place number to App.

Length of the list in my case should be 3 i presume.
#,45.123456,13.12345 so this is splited at "," so I get list with:

45.123456
13.123456
So basically i will have length = 3 of as you suggest >2
Is this correct what I think?

...then you are the only person in the world that doesn't. Math Block -1 means "collect all available data". The 'Bytes Available To Receive' Block is like a flag, it is indicating availability.

Ah yes, that is good.

I see where you are at - if your App is only ever getting data on request, that will work. If however some of the data is constantly sent, you will need to interrupt it, otherwise you will have the situation that data is sent and received at the same time as you only have one channel. This suggests you will run into that issue:

Now, a human might do that, but a microcontroller does not have to. What I said before about sending all of the data together still stands. When you want to save the GPS data, a button press and the latest GPS values sent can be saved - you do not have to complicate the stream by having some data sent continuously and some on demand.

I will try it again with new version App.
Yesterday on previous version i dint got nothing, maybe I did something wrong I dont know.
Im not so good in all this programming, but I see, compare and combine, sometimes I understand what im doing and some times its result of trials and errors, but at the end I come where I want :slight_smile:
I call my Coding "Frankenstein-Coding" :slight_smile:
I

That's a good thing, it means you are thinking about it.

Write a list of all those required values and upload your Arduino Sketch here (change extension from .ino to .txt) and I can make an example for you.

Hi Chris,

I tried again with (-1) but definitely is not working, or it works until I request GPS coordinates and then everything stops functioning and app crash.

With old version everithing works

Hi Chris,
this is the part of BT procedure of my arduino

Bluetooth_procedure.txt (10.6 KB)

That crash is what I talked about before - two streams but only one channel. Requesting all of the data available with -1 is not the cause of the crash.

If this is true:
Serial1 - for bluetooth commands being sent from the Android device

then this is wrong:
Serial1.print("#"); //Begining of line for app // send serial string to arduino "#,45.123456,13.123456 "
Serial1.print(","); //separator for app
Serial1.print(Home_LAT[0],6); //Latitude for app
Serial1.print(","); //separator for app
Serial1.print(Home_LON[0],6); //Longitude for app
Serial1.println(); // end of line for app

What I see:
Values/Info shared between App and Arduino:
Latitude
Longitude
Forward
Reverse
Left
Right
Stop
Turn Around
Compass Forward
Compass Turn Right
Compass Turn Left
Calibrate Compass
Long Left
Long Right
Speed

Edit: Is the App Controlling the boat, or is the boat telling the App what is happening?