How to divide location sensor speed with data received from arduino

155 views
Skip to first unread message

A P

unread,
Aug 14, 2018, 3:11:57 PM8/14/18
to MIT App Inventor Forum
Hi,
im developing boat computer based on Arduino uno and hall sensor fuel flow sensor.
Sensor is picking pulse when fuel flow. Arduino send data with bluetooth serial comunication to Android smart phone.
On smartphone I dewelop app with MIT APP Inventor that receive next data string: PulseCount(total pulses since arduino is on),count(liters per hour),FlowPuls(pulses per second colected in 5 second interval).
I also add location sensor to get GPS speed in miles per hour MPH.
Wath I would like to do is to get specific consuption in Liters/mile by dividing Liters/hour with miles/hour.
I dont know how to do that, can somebody help me.
Attached you will find aia file of my project and also arduino code.

Thanks
OUTBOARD_MONITOR_130818_LITARA_NA_MILJU_UKUPNO_LITARA.aia
float_5sek_200718_mj_protoka.ino

Abraham Getzler

unread,
Aug 14, 2018, 4:30:09 PM8/14/18
to MIT App Inventor Forum
I added Delimiter handling to your project, see attached ...

Use println() to separate messages when you transmit.
Set Delimiter = 10 in BlueTooth Designer.
Check for Bytes Available > 0 in blocks,
but then request -1 bytes to get only full  messages.

I removed the disruptive pop up messages,
and sped up the Clock to 100ms.

I also tightened up the list length checks to insure you
don't fall off the end of your list.

That might not be fast enough to catch all your transmissions,
so I recommend adding a BytesAvailable Label to show the backlog.

ABG
blocks.png
BlueToothClient.PNG
OUTBOARD_MONITOR_130818_LITARA_NA_MILJU_UKUPNO_LITARA_ABG.aia

A P

unread,
Aug 15, 2018, 3:31:23 AM8/15/18
to mitappinv...@googlegroups.com
Hi Abraham,

thanx for helping. 

Im compleatly new at programing and I can only copy paste solutions find on the net and modify it to what I need.

The data received from arduino are sent (serialprint) every 5 seconds (i get better average readings of flow sensor), so transmition of data should not be too fast (at least i think???).
The anoing popup note I used to keep screen alive all the time when aplication is on.

Is it possible to divide value from text box 2 (liters/hour , received from arduino) with walue from text box 4 (nautical miles/hour or MPH from location sensor) to get liters per nautic miles? Check the blocks attached at the end. Im not sure will this work. 
blocks.png

SteveJG

unread,
Aug 15, 2018, 8:52:36 AM8/15/18
to MIT App Inventor Forum
The LocationSensor speed units is meters/second.   Unfortunately MIT has not documented that anywhere

To display km/h  you need some math blocks to convert   m/sec .    1 m / sec = 3.6 km/hr .
To display mph you need some math blocks to convert  m/sec.    1 m/sec = 2.23695 mph

You have liters/hour; you can calculate miles/hour,  you want liters/miles    so since both measurements are in hours, divide liters by the miles (speed x 2.2395    hint:  liters/hour : miles/hour

How did you determine that you need to multiply   speed by 1.944 as a conversion factor?

A note about using speed;  the LocationSensor will not provide an accurate value immediately.  The sensor has to register several satellite fixes so it can determine  change in distance/time reported as meter/sec.


Regards,
Steve

A P

unread,
Aug 15, 2018, 3:19:36 PM8/15/18
to MIT App Inventor Forum
3,6 / 1,852 = 1,9438
I calculate nautical miles.
Message has been deleted

A P

unread,
Aug 15, 2018, 4:22:10 PM8/15/18
to MIT App Inventor Forum
My question is how to devide two diferent source of data, one received from arduino and other from android.
One data is in clock block and other is in location sensor block. How to combine those two values.

Abraham Getzler

unread,
Aug 15, 2018, 4:42:27 PM8/15/18
to MIT App Inventor Forum
Dividing two number when you might not yet have values for both of them requires taking precautions
to avoid dividing by zero, or presenting bad results.

By now, you should have created an output Label in the Designer, whose .Text value is the
eventual destination of your division calculation.

Your numerator and denominator are either in separate Label.Text values or maybe in global variables,
awaiting the moment when our app decides it's okay to do the division and display the result.

You have two possible triggers for the division calculation, either the Location Sensor or the Clock Timer,
depending on which one last received data.  To avoid duplicating the same code, then, I recommend
creating a common procedure (call it Calculate_And_Display_LPM, for Liters_per_mile) to hold the dirty work,
and call that procedure from both events.

To signal incomplete data, I recommend you decide on an unlikely value to keep in your numerator and denominator
.Text fields or global variables, say '?'.

The procedure would go like this ...

procedure Calculate_And_Display_LPM
   if and(numerator not equal to '?' , denominator not equal to '?') then
      if denominator not equal to 0 then
         set output-Label.Text to numerator / denominator
      else
         set output_Label.Text to '?'
  else
         set output_Label.Text to '?'
end procedure

(I have no experience with the Location Sensor, but I see at http://code.appinventor.mit.edu/reference/components/sensors.html#LocationSensor
Location information might not be immediately available when an app starts. You'll have to wait a short time for a location provider to be found and used, or wait for the OnLocationChanged event
so that's where you would put the other procedure call.)

ABG


SteveJG

unread,
Aug 15, 2018, 4:48:27 PM8/15/18
to mitappinv...@googlegroups.com
This might be what you want to do:

create a global variable    nauticalMPH      .
In the LocationSensor.LocationChanged block, do your speed to nautical miles per hour  set NauticalMPH to this value.
In your Clock block, use nauticalMPH as needed.

Be aware, the GPS hardware in most Android devices will not update more frequently than every 20 to 30 seconds, the LocationSensor1.TimeInterval should be set to something like 30000 ms.


A P

unread,
Aug 15, 2018, 6:06:59 PM8/15/18
to MIT App Inventor Forum
Hi Steve,
I already did that and that part works fine, no problem with GPS speed (my location sensor on my android phone is always ON), as soon I start to mowe GPS speed is displayed and its corect, I compare it with other aplication that measure speed in nautical miles.

Can somebody check my latest version of my app?
Does the last part in LocationSensor block where I divide liter/hour with GPS MPH make sense?
OUTBOARD_MONITOR_150818_LITER_PER_HOUR.aia
blocks.png

SteveJG

unread,
Aug 15, 2018, 7:06:11 PM8/15/18
to MIT App Inventor Forum
if you divide liters/hour   by knots/hour     the result  is liters / knot       is that what you want?


liters/hour      x    1 hour
________           ____
knots/hour           1 hour               (1 hour/ 1 hour is 1 )      results in liters/knots

A P

unread,
Aug 16, 2018, 4:36:23 AM8/16/18
to MIT App Inventor Forum
Hi Steve,

i know the math :)
The part that I dont know is how make it work in blocks.
This morning I tried last version (attached in my previous post) riding my Zodica RIB boat over the sea, it look that everything works OK beside "liters per mile".
Its working in strange way, its like it works for few seconds than it stall, and keep showing last value.
Its probably like Abraham explained, APP is collecting two values and wait right moment to do the calculation.
Will see, trials and errors. 
It doesnt cost nothing, only free time and few gallons of petrol :)

By the way just to translate Croatian words in screenshots:

TRENUTNA POTROŠNJA (l/h) = current consumption in liters per hour
POTROŠNJA (L) = total consumption since Arduino is on
LITARA NA MILJU = liters per mile current 
PULS/SEK Hz = it is just the frequency of signal coming from flowmeter 
GPS SPEED MPH = is what it say :)

 

Screenshot_20180816-080416.png
Screenshot_20180816-080424.png

SteveJG

unread,
Aug 16, 2018, 7:51:19 AM8/16/18
to mitappinv...@googlegroups.com
A significant part of your problem is here

LStoofast.PNG



You are updating your GPS every one second (1000 ms).  You are giving your device a 'heart attack'.  I said it earlier, it takes the LocationSensor about 20 to 30 seconds to acquire a satellite fix required to update location and speed.  You are requesting a new update before your device hardware can provide the initial update (satellite fix).  Set the  LocationSensor.TimeInterval to 30000 ms  and try again.  Disappointing since you want rapid, constant updates; that is not possible with most cell phone hardware. 
 TimeInterval
Determines the minimum time interval, in milliseconds, that the sensor will try to use for sending out location updates. However, location updates will only be received when the location of the phone actually changes, and use of the specified time interval is not guaranteed. For example, if 1000 is used as the time interval, location updates will never be fired sooner than 1000ms, but they may be fired anytime after.

Did you read and understand how the LocationSensor works?  
  • Using the Location Sensor   explains why 1000 ms is not a practical sampling value.  I wish MIT would eliminate that option on the Designer.
Confirm this behavior.  Build a small app to display the speed (convert the meters/sec to any units you want); use a ListPicker to change the TimeInterval;   drive on a highway at various speeds and let your partner monitor the vehicle's speed from the speedometer and your device's test app simultaneously at various TimeIntervals.  Don't do this alone.  The speed you monitor will be 'jerky' compared to what your partner monitor's on the Android.  The LocationSensor does NOT provide instantaneous speed.

Experiment, you might be able to use a value smaller than 30000 ms with your hardware, or you might not.

You update your Clock every 100 ms.  I think that might be too fast but don't know how often you must sample your pulses.  To prevent both your Clock and LocationSensor 'firing' simultaneously, you might set one of them to an odd value...perhaps 30001 instead of 30000 ms for the LS.


Regards,
Steve
Reply all
Reply to author
Forward
0 new messages