HELP! Problem receiving numbers from a sensor via bluetooth

4,784 views
Skip to first unread message

Carlos Vendrell

unread,
Mar 15, 2015, 1:34:12 PM3/15/15
to mitappinv...@googlegroups.com
Hey! I wonder if anyone can help me with my problem. I am trying to finish this project as my thesis but after a month trying different ways to solve it, I really don't know what else to do now.

I am building my app with App inventor 2 and with my arduino. I have a gas sensor which via bluetooth sends the numbers to the app and whenever the value of the gas increases the phone has to give a notification and to speak some text. 

My problem is that I am trying different ways of receiving the value and I can not find the correct way to receive the numbers and then with a math block is needed to compare if that value is higher or lower than another value.

So I will explain what I have and let's see if it helps you to understand what I am doing wrong:
Inside the timer it checks first if the bluetooth is connected, if so, set variable Sensor to Call Bluetooth BytesAvailableToReceive. And if button On is not enabled, set value text to get global Sensor.
And right after that it comes the comparison, if get global Sensor is higher or equal than 700 then set Color Alarm red, call notification and call VoiceAlert.

And here I just receive a number 1 on my label value. I have also tried different ways of receiving the value but none of them has worked yet, so if you could please help me to solve this I would really appreciate it.

There is attached a screenshot of this part of app inventor!

Thank you in advance for your help.

Carlos.
Appinventor_screenshot.png

Taifun

unread,
Mar 15, 2015, 2:08:02 PM3/15/15
to mitappinv...@googlegroups.com
see the documentation http://ai2.appinventor.mit.edu/reference/components/connectivity.html#BluetoothClient

number BytesAvailableToReceive()
Returns an estimate of the number of bytes that can be received without blocking

which means, I would try to use one of the following blocks to actually receive the value...
Taifun

Trying to push the limits of App Inventor! Snippets and Tutorials from Pura Vida Apps by Taifun.         


number ReceiveSigned1ByteNumber()
Receive a signed 1-byte number from the connected Bluetooth device.
number ReceiveSigned2ByteNumber()
Receive a signed 2-byte number from the connected Bluetooth device.
number ReceiveSigned4ByteNumber()
Receive a signed 4-byte number from the connected Bluetooth device.
list ReceiveSignedBytes(number numberOfBytes)
Receive multiple signed byte values from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.
text ReceiveText(number numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.
number ReceiveUnsigned1ByteNumber()
Receive an unsigned 1-byte number from the connected Bluetooth device.
number ReceiveUnsigned2ByteNumber()
Receive a unsigned 2-byte number from the connected Bluetooth device.
number ReceiveUnsigned4ByteNumber()
Receive a unsigned 4-byte number from the connected Bluetooth device.
list ReceiveUnsignedBytes(number numberOfBytes)
Receive multiple unsigned byte values from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

Carlos Vendrell

unread,
Mar 15, 2015, 6:30:46 PM3/15/15
to mitappinv...@googlegroups.com
Hey! Thank you for answering! Okey I have already tried those before but what it does is that it gives me random numbers and it doesn't show me the value of the sensor. If I use ReceiveSigned1ByteNumber() it gives me random numbers of 2 digits, with ReceiveSigned2ByteNumber random numbers of 4 digits and so on and I can't understand why and I really don't know what to do. 

I don't know if you know something else that I could do to make it work! Or maybe I am not using correctly those blocks.

Thanks!

MartynC

unread,
Mar 16, 2015, 3:41:17 AM3/16/15
to mitappinv...@googlegroups.com
You are assigning the number of bytes available to Global Sensor not the actual data.
Once you have the number bytes available you need to read them.

This is how I do something similar. The data I am receiving is ascii text rather than number values.
If your data is short then you don't need to keep adding it to the received data variable like I do  (ie. receivedDataFromBT = receivedDataFromBT + Bluetooth.RecieveText).  You can simply load what ever is available and (fairly) safely assume you have it all.


AI2_BT.jpg

Abraham Getzler

unread,
Mar 16, 2015, 11:49:23 AM3/16/15
to mitappinv...@googlegroups.com
Do you have access to the Arduino code that runs the sensor
and transmits its output to Bluetooth?

That might give us insight into the data format.

ABG

Carlos Vendrell

unread,
Mar 16, 2015, 1:54:47 PM3/16/15
to mitappinv...@googlegroups.com
Yes I have written this code which receive the text on or off to turn on and off a led.
So here is the code:


/* Smartphone Alarm via Bluetooth with Arduino */


int AnalogInput = 5;    // select the analog input pin 
int SensorValue = 0;    // variable to store the value coming from the sensor
int buzzer=8;           // Output pin for the buzzer
int ledPin = 9;// define pin 13 of the arduino
int Alarma = 900;
String readString;



void setup() {
  Serial.begin(9600);         // intialize communication at 9600 bps
  digitalWrite(buzzer,LOW);   // define buzzer as a digital output
  pinMode(ledPin,OUTPUT);
  
}

void loop() {
  
  // NOW WE CHECK IF OUR BLUETOOTH MODULE IS ON OR OFF
  while(Serial.available()){
    delay(3);
    char text = Serial.read();
    readString += text;
  }
  if(readString.length() > 0){
    Serial.println(readString);
    if(readString == "on")
    {
      digitalWrite(ledPin, HIGH);
    }
    if(readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }
    readString = "";
  }
  
  SensorValue = analogRead(AnalogInput);    // read the value from the sensor
  analogWrite(AnalogInput,SensorValue);
  Serial.print(" ");
  Serial.println(SensorValue);  // show the value in Monitor Serial and Smartphone
  
  delay(200);
     
  
  if(SensorValue >= Alarma)     // if the SensorValue is higher than ALARMA
  tone(buzzer,600,3000);
  Alarma = SensorValue + 20;  
}



Thank you for your time, I really need help here and I appreciate it!


Carlos

Carlos Vendrell

unread,
Mar 16, 2015, 1:57:53 PM3/16/15
to mitappinv...@googlegroups.com
Yes I am using Gas Sensor MQ-5, Arduino uno and a bluetooth hc-06.

Any more information?

Carlos Vendrell

unread,
Mar 16, 2015, 2:19:18 PM3/16/15
to mitappinv...@googlegroups.com
Hello! This is the error that occurs when I assign my data as a receiveTextNumberofBytes. Just to let you understand more, what I receive from the sensor is usually a number between 500 and 1000. So if I receive that as a text then I can not compare it with another number or i can not make a sum with that number.

Here I attach the errors.


Thank you for your time!!


Carlos
error_screenshot.png
error2_screenshot.png

Taifun

unread,
Mar 16, 2015, 3:47:31 PM3/16/15
to mitappinv...@googlegroups.com
so where in your blocks are you using the >= block?



unfortunately the large error message is hiding most of your blocks...
Taifun

Abraham Getzler

unread,
Mar 16, 2015, 4:58:45 PM3/16/15
to mitappinv...@googlegroups.com
I notice in your first screen shot that whenever a new reading comes in,
you JOIN it to your previous reading in the global value Sensor.

You should be over writing the previous value, not JOINing it.

If you want a log of sensor values, do the JOIN to a separate 
global variable , call it Log, or to a label.Text value.

No wonder your error message complains about a bunch of separate numbers
being presented to the math comparison block.  

Sensor should only hold the latest reading.

ABG

Abraham Getzler

unread,
Mar 16, 2015, 5:09:22 PM3/16/15
to mitappinv...@googlegroups.com
I also notice in your blocks that after every reading, you set your alarm level to 10 more than the latest reading.
That would trigger the alarm only if there would be a large instantaneous jump in readings, and would
ignore a long term slow increase in gas levels.

I wouldn't want to be watched over by such a sensor!

I suggest using a slider for your alarm level , in the 750 to 850 range,
judging by your readings in the error message.

ABG

Carlos Vendrell

unread,
Mar 17, 2015, 3:08:21 PM3/17/15
to mitappinv...@googlegroups.com
On the third if.


Appinventor_screenshot.png

Abraham Getzler

unread,
Mar 17, 2015, 3:21:12 PM3/17/15
to mitappinv...@googlegroups.com
This reminds me of the story of Tantalus, who was cursed by the Greek Gods 
to never be able to reach something (grapes?) forever out of his reach.

Your poor suffering sensor will see its target (global alarm) reset 10 units too high after every clock tick, because
that third if statement is inside the clock timer block.

Look back at that error message you got earlier, with blank-delimited sensor values inside the error message.  They were values very close together in the high 700's.  But they never jumped high in a single clock tick.

Maybe you have a way of emitting a blast of gas at your sensor that will make the reading shoot up very quickly.
(insert a gas joke here.)

ABG


Carlos Vendrell

unread,
Mar 17, 2015, 5:08:31 PM3/17/15
to mitappinv...@googlegroups.com
Okey, it is true that I will have to check that and it will be needed to change the global Alarm, but the first and more important problem still is that I don't find the correct way of receiving the numbers from the sensor. So I need to find this out and then go and fix the issue with the alarm. But after trying many different ways, I don't know which is the correct way to do that.

Carlos Vendrell

unread,
Mar 17, 2015, 5:12:20 PM3/17/15
to mitappinv...@googlegroups.com
I don't know exactly how a slider works or how I could use it on my app. Could you please explain me more or less how it will help in my app and how should I add it?

Thank you so much for your time!


Carlos.

Abraham Getzler

unread,
Mar 17, 2015, 5:21:54 PM3/17/15
to mitappinv...@googlegroups.com
There are many fine short videos on sliders on YouTube.

Professor David Wolber's are short and clear.

ABG

Abraham Getzler

unread,
Mar 17, 2015, 5:28:31 PM3/17/15
to mitappinv...@googlegroups.com
You are very close to success on your app.
You had reasonable readings coming in before, just you were accumulating them when you shouldn't have.

Once you have your global alarm set to the slider thumb when the thumb changes,
you will be able to set your sensitivity using the slider during live tests.

In Prof. Wolber's example, he skips a step and uses the thumb value directly to set the dot radius in his HelloPurr example.

You can do likewise in your alarm vs sensor test, comparing sensor vs thumb value.

And stop teasing the sensor !

ABG



Abraham Getzler

unread,
Mar 17, 2015, 5:32:38 PM3/17/15
to mitappinv...@googlegroups.com
but the first and more important problem still is that I don't find the correct way of receiving the numbers from the sensor. 

What makes you think you aren't receiving the numbers from the sensor?

 You should be assigning the sensor value into a Label.text field, so you can see the numbers change.

Are you doing this?

ABG

Carlos Vendrell

unread,
Mar 23, 2015, 7:08:08 AM3/23/15
to mitappinv...@googlegroups.com
Hey guys! Thank you so much for your help! I finally got it to work!
Here I leave the link to youtube where I uploaded a video of the app:



Thanks again!


Carlos

Abraham Getzler

unread,
Mar 23, 2015, 10:32:49 AM3/23/15
to mitappinv...@googlegroups.com
Glad you persisted.

Your gas emitter looks much easier than my method!
(cough)

ABG

Christopher Renz Gaviola

unread,
Mar 30, 2015, 4:08:20 AM3/30/15
to mitappinv...@googlegroups.com
hi! could you help me with this one: whenever the gas sensor(mq7) reaches high(ex 500) the app inventor shows a negative or a different value. do you know the reason why??  I would really appreciate your help! thank you!

Abraham Getzler

unread,
Mar 30, 2015, 12:08:11 PM3/30/15
to mitappinv...@googlegroups.com
Please start a new thread for your new problem.
Could you also include a link to the mq7 API
and well trimmed screen shots of your relevant blocks?
ABG

Abraham Getzler

unread,
Mar 30, 2015, 3:14:55 PM3/30/15
to mitappinv...@googlegroups.com
Oh, I forgot - also include your Arduino code in your new post.
ABG

Abraham Getzler

unread,
Mar 15, 2015, 11:21:21 PM3/15/15
to mitappinv...@googlegroups.com
Do you have a model number for the sensor, so we can search the web for its API?
ABG

Reply all
Reply to author
Forward
0 new messages