Sensor reading, bluetooth data reading

Nothing happened... no errors, no crashes. Just not showing the data.
I didn't hide labels and I kept one clock because I don't have connection problems.
I've noticed that I need a "list" variable and a "input" variable, without one of them the code doesn't work at all. The new code is right below.




blocks3
blocks4

One more thing. "Serial.connected()" is not a thing in Arduino. I used my old sketch just to test the app. What should I used instead of "Serial.connected()" ?

Thanks for your patience by the way :+1:t2: :+1:t2: :+1:t2:

There is a bit missing from your Blocks, and this is wrong, similar to the way it was wrong before:

Let's dissect the error so you understand:

  1. The Block 'set ListPicker Selection'. With this Block, the developer can programatically select an item in the List - it is then moved to become the first item in the List. This is intended for use with static lists, for example a list of different types of fruit or different types of cars. It's simply not appropriate for your App because until available devices have been scanned, you do not know what is in the list or where any particular device may be in the list.

  2. On top of that, your Blocks attempt to change the selection with the value output by 'call Bluetooth Client Connect' - which is meaningless from the perspective of the list, as it is a 1 or 0 (True or False boolean).

  3. Immediately after attempting to connect, the code will start Clock1 if the Bluetooth Client is connected - that leaves your connection down to luck alone because the test is too soon and if the test finds that the Bluetooth Client is not connected, the program stops working right there! So do you see:

You do have connection problems.

Unfortunately you have repeated the same mistake as before, when you could copy my code to get it right. In my code there is a note "Allow some time to Connect" and the Blocks within a Clock Timer make five connection attempts - if the connection was not achieved, the User is Notified.

Note, if you want the ListPicker Button to confirm to the User what has been picked:

ListPickerButton

The code could not work without both of them.

  1. The 'input' variable is used to store the data (a string of text) that arrives, via Bluetooth, from the Arduino.
  2. The 'list' variable is used to store the converted data, which initially looks like this, a single continuous string (of text):

value1|value2|value3

The string is converted (split-up) into a List:
value1
value2
value3

.... discarding the value delimiters ("|") and making it easy to test that all of the expected data, consisting of three values, has been received. If the List failed the test, perhaps one or all of the values had failed to arrive, no harm is done - the App continues and processes the next data packet on arrival.

Another thing you have missed in my example, in addition to reminding the User that the Arduino must be switched on, the phone's Bluetooth must be switched on and late versions of Android may require Location switched on, is this:

ScreenError

If an error occurs (perhaps silently or not noticed), knowing what error and the source is very valuable.

It is a thing, but only when Software Serial is being used (instead of or in addition to Arduino's built-in Serial Comms). So you can remove the the Serial Connected() test. Sorry, that's my bad.

I haven't used the 'SimpleDHT' library, which appears to be automatically delivering the values as integers (bytes). The standard DHT.h library delivers the values as floats. So if, after correcting your Blocks and testing again, you do not receive values in the App, do not perform a value type conversion in Serial on the DHT output:

Serial.print(temperature);
Serial.print("|");
Serial.print(humidity);
Serial.print("|");
Serial.print(lvl, 4); //4 decimal places
Serial.println();

Make sure the Text Colour is not the same as the Back Colour in the Value Labels!

1 Like

Dear @Noobie ,
By looking to your Arduino code, a question arise to me: what kind of Arduino board and BT shield are you using ? And to which pins of the Arduino board are you connecting the shield ?
Please be aware that the serial.write() instruction drives the hardware serial line of the Arduino, and is used to communicate toward the PC monitor.
In other words if you haven't connected the BT shield to pins 0,1 of the Arduino board, but to other pins, you shall use the Softwareserial library instead. And connect the BT shield to pins 10,11 (typically). Please take a sight to my answer to another very similar topic:

Unless that you are really sure that the Arduino board is granted to send the data, you can break your head against the wall before having the AI2 code working... :flushed:
Hoping it helps.
ciao, Ugo.

2 Likes

I wrote the code like below. Is the miBT thing right like that? I kept the Serial.print just because I can see if it is working and what I'm doing

#include <SimpleDHT.h>
#include <SoftwareSerial.h>
int pinDHT11 = 2;
SimpleDHT11 dht11;
#define light (A1)
int lvl;
SoftwareSerial miBT(0, 1);
byte temperature;
byte humidity;

void setup() {
Serial.begin(9600);
}

void loop(){
if(dht11.read(pinDHT11, &temperature, &humidity, NULL)){
return;
}
lvl=analogRead(light);
Serial.print(temperature, DEC);
Serial.print("|");
Serial.print(humidity, DEC);
Serial.print("|");
Serial.print(lvl);
Serial.println();
miBT.print(temperature);
miBT.print("|");
miBT.print(humidity);
miBT.print("|");
miBT.print(lvl);
miBT.println("|");
delay(6000);
}

Hello @Noobie,
if you use the SoftwareSerial library you can leave the pins 0,1 to the hardware serial to communicate toward the PC, and you shall re-allocate the miBT line on pins 10,11 (Rx, Tx), like this:
SoftwareSerial miBT(10, 11);
Obviously you have then to connect the BT shield to those pins (be sure to reverse the Arduino Tx to the HC05 Rx and visa versa).
In your setup() you shall then initialize the communication toward the shield, by coding: miBT.begin(38400);
This is typically the baudrate of the BT shield. Also in this case, please be aware that the SoftwareSerial library does not work properly with higher baudrates, so 38400 is the maximum speed that you can use satisfactorily (higher speeds can incur in framing errors).
The loop() seems correct.
Looking for the next step.
Ciao.

PS remember that the HC05 (or HC06) works preferably with a 3.3 V on its pins, so the Tx from Arduino toward the Rx of the shield, would preferably be reduced with a resistor partition, like the following :

image

3 Likes

The app still doesn't work at all. But the code works fine, no errors, no crashes...
What would happen if the Rx and Tx are reversed instead of being in the correct way?
And what can I do about the app in AI2?
By the way, I'm using a logic converter to convert the 5V to 3.3V. I wrote 3 before, but I intended 3.3V. I read that is preferable to use a logic converter instead of using resistors, is it right?

Dear @Noobie,
there is no need to use a voltage converter, a partition is more than enough. For sure if you dont cross the lines (Tx vs Rx) it won't work. But don't forget to separate the miBT from the hardware serial pins 0,1.
And initialize correctly the BT line in terms of pins and baudrate.
Cheers

Hi Noobie

If you are using Software Serial in addition to the board's, you need to identify the digital pins used and apply a name. You have used the name miBT which is fine, but if using an Arduino UNO for example, the Software Serial cannot use pins 0,1 - connect your Bluetooth Module to other pins and declare those in the Sketch -This is dependent on both the Arduino you have and the exact Software Serial Library, of which there are a few.

You still have not told us exactly what Arduino (and what chip, what board revision), what Bluetooth module and make/model of your Android Device you are using.

1 Like

Hi ChrisWard. I'm using pin 10 and pin 11 (to be Rx and Tx) instead of 0 and 1.
I'm using a copy of Arduino UNO Rev3 (the actual name is Elegoo UNO R3) using a ATmega328P, a HC-05 and a Samsung Galaxy A52.
Sorry, I didn't read the request previously

Hi @Noobie,
the HW seems OK.
I would suggest also to remove the voltage adapter and to put two simple resistors (I don't know if the voltage converter has the ground properly connected or if it smoothes the edges of the serial line).
So please follow my hints. Be sure the pins are crossed (take a sight to the drawing) and initialize the library.

Before the setup instantiate the pins assignment
SoftwareSerial miBT(10, 11);

And in the setup the baudrate
void setup()
{
...
miBT.begin(38400);
...
...

}

1 Like

...Just noticed you are using a Delay() in the Sketch Loop instead of elapsed time. :scream:

I'll let you know what will happen following your hints, thank you

Yeah, the code i wrote here is just a fast way to see if I'm connecting everything in right way, hardware side. I'll use the elapsed time method like you suggested before :grin:
Thank you so much to keep trying to resolve this topic. I'll let you know where this is going on
:wink: :wink:

The app still doesn't work. Im getting enough about this AI2.

Firs: the AI2 code. The location can't be enabled in settings, so I just get an error using the "when Screen1.ErrorOccurred" block all the way down.


Before picking anything from the list of devices found I let the Clock_TimerConnect to run to actually find all devices. Is it right? I don't know because I don't understand anything about AI2.

if the bluetooth is connected the clock to get the data will run. How do I set the clock to run after picking the device I want? If I am right this clock will run regardless if im connected to the hc05 or not. But I dont know how do i change it.
What do I need to change here? The Connect address part?

blocks3
blocks4
when the clock for data run, if the bluetooth is connected i check if there are bytes >0 to receive. Here is the problem: Using a "else" statement I know that there is an error in the If block for bytes>0 because I get the error "Error in call BT>0".

Second: The arduino code. I cant get rid of this error (probably because im dumb)
Note that the serial.print is used just see if the sensors are working

This code is working:
ard

If you know the exact way please write it down. I'm scarily running low on time, if I cant get it right I'll probably ditch this project.
Thanks for all the replies.

You are using "If Serial Connected" before the Serial functions - it should only be used for the miBT functions.

Can we assume you have the Bluetooth Module wired correctly?
Does Serial Print show the Sensors are working?

Do not convert the values to decimals for temperature and humidity, see my post #22

When you say 'this code is working" -is it? No issues?

I can feel your stress Noobie - but you bring it on to yourself! You don't need to know much about programming but you do need to think with a clear head. Look at the name of that timer: Clock_Timer_Connect. It's for the Bluetooth Connection.

Now think - when are you going to be able to make a connection - before you have picked a device to connect or after you have picked it?

Hi Noobie

Let's try this (do not modify the content of either file):
BT_Receive_3_vals.aia (7.8 KB)
Read_DHT11.txt (1.7 KB) (Rename this ext from .txt to .ino)

  1. Compile the attached Sketch, upload to Arduino UNO
  2. Build the attached Project as an APK and install on your Phone
  3. Check that:
    a) Arduino is ON, HC-05 is on pins 10, 11; DHT11 is on pin 2;
    b) Phone Bluetooth is on, Phone Location is on.
  4. Run the App - what happens? If an error is reported, screenshot it and upload here.
2 Likes

Hi Chris, that is a magnificent app, I couldn't do it to be honest. So...

Data not showing up. That's it. I turned on Location and the Bluetooth before everything else. I built the project, uploaded it, it scans, it connects, it doesn't show data. The serial monitor on my pc works extra fine.
In these days I asked some friends who work with apps, but they didn't give any answer.
I'm wondering if it is something hardware side and not software side :confused: What should I do? I really don't know what is the actual problem.
EDIT: This is what I see:

PS: I'll study this app so well done I'll burn my eyes.

...maybe that's a bit extreme :grin:

So:

  1. The expected values do show-up on the Serial Monitor.
  2. The App Scans, lists your Arduino and connects, but data never arrives.