Show DHT11 data on app with blouetooth

hi everyone... i'm new at app inventor, but i was able to do an app that can on-off what i want, but what i want to do now it's an app that show me the data from a DHT11 . .. this is what i do, but it shows me nothing on lines "temperatura...., humedad.... & Nivel agua"

ARDUINO CODE:

#include <Wire.h>    // incluye libreria para interfaz I2C
#include <DHT.h>    // importa la Librerias DHT
#include <DHT_U.h>
#include <SoftwareSerial.h>  // libreria que permite establecer pines digitales

// Definimos el pin digital donde se conecta el sensor DHT TEMPERATURA HUMEDAD
#define DHTPIN 2
// Dependiendo del tipo de sensor
#define DHTTYPE DHT11

SoftwareSerial miBT(11, 12);   // pin 11 como RX, pin 12 como TX


int TRIG = 10; //DISTANCIA ULTRASONIDO
int ECHO = 9; 

int DURACION;
int DISTANCIA;

/// BLUETOOTH
char DATO = 0;      // variable para almacenar caracter recibido


///DHT11

  int TEMPERATURA;
  int HUMEDAD;
  DHT dht(DHTPIN, DHTTYPE);   // creacion del objeto, cambiar segundo parametro        // por DHT11 si se utiliza en lugar del DHT22

///fin DHT22

void setup(){
  
//bluetooth
  miBT.begin(38400);    // comunicacion serie entre Arduino y el modulo a 38400 bps

// INICIAR DHT
      dht.begin();      // inicializacion de sensor

//DISTANCIA ULTRASONIDO
      pinMode (TRIG, OUTPUT);
      pinMode (ECHO, INPUT);
      Serial.begin(9600);
}
void loop(){


//DISTANCIA ULTRASONIDO
  digitalWrite(TRIG, HIGH);
  delay(1);
  digitalWrite(TRIG, LOW);
  DURACION = pulseIn(ECHO, HIGH);
  DISTANCIA = DURACION / 5.82;

//BLUETOOTH
if (miBT.available()){      // si hay informacion disponible desde modulo
  DATO = miBT.read();   // almacena en DATO el caracter recibido desde modulo

  TEMPERATURA = dht.readTemperature();  // obtencion de valor de temperatura
  HUMEDAD = dht.readHumidity();   // obtencion de valor de humedad
   
  Serial.println(TEMPERATURA);
  Serial.println("|");
  Serial.println(HUMEDAD);
  Serial.println("|");
  Serial.println(DISTANCIA);
  Serial.println("|");
  
}

delay(1000);

}

APP INVENTOR SCREENS

image

image

Here is the standard advice for multiple values on a line in BlueTooth text ...

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.

In your case,

  • use print() everywhere except at the last value per line, where you need println() to end the line
  • 38400 might be too fast. Start at 9600.
1 Like

thanks, but NOPE... i do eveything u say but it's the same.. don't send any data
NOW, when i open the app and select the bluetooth, it shows:

"----Select list item: List index too large
Select list item: Attempt to get item number 2 of a list of length 1: [""] -----"

ARDUINO CODE:

#include <Wire.h>    // incluye libreria para interfaz I2C
#include <DHT.h>    // importa la Librerias DHT
#include <DHT_U.h>
#include <SoftwareSerial.h>  // libreria que permite establecer pines digitales

// Definimos el pin digital donde se conecta el sensor DHT TEMPERATURA HUMEDAD
#define DHTPIN 2
// Dependiendo del tipo de sensor
#define DHTTYPE DHT11

SoftwareSerial miBT(11, 12);   // pin 11 como RX, pin 12 como TX


///DHT11

  int SENSOR = 2;     // pin DATA de DHT22 a pin digital 2
  int TEMPERATURA;
  int HUMEDAD;
  DHT dht(DHTPIN, DHTTYPE);   // creacion del objeto, cambiar segundo parametro        // por DHT11 si se utiliza en lugar del DHT22

///fin DHT22

void setup(){
  
//bluetooth
  miBT.begin(9600);    // comunicacion serie entre Arduino y el modulo a 38400 bps


// INICIAR DHT
   dht.begin();      // inicializacion de sensor

Serial.begin(9600);

}

void loop(){

//BLUETOOTH
if (miBT.available()){      // si hay informacion disponible desde modulo

}

//loop DHT 
  TEMPERATURA = dht.readTemperature();  // obtencion de valor de temperatura
  HUMEDAD = dht.readHumidity();   // obtencion de valor de humedad
   
  Serial.print(TEMPERATURA);
  Serial.print("|");
  Serial.print(HUMEDAD);
  Serial.println("|");

delay(1000);

}

APP INVENTOR SCREENS

image
image


image

image

No, you didn't.

1 Like

first of all, thanky U for your time and patience, but it's my first time building an app for arduino, and working with blocks codes and app inventor.... this is what i change.. and everythings work.. except the variables

image
image

image

image

What do you get in datos_entradaBT?

LabelX.Text = datos_entradaBT

Here an example:
https://community-appinventor-mit-edu.ezproxy.canberra.edu.au/t/bluetooth-hc-06-arduino-send-receive-send-text-file-multitouch-image/9518/6

(you can also separate values ​​by comma, instead of |)

thanks @Juan_Antonio ... i'm thinking it's the cellphone... i made EXACTLY as the sample U send me and it's ok when i select bluettoth... but when i press "Check temperature" it freezing and the cell send me a message that the app it's not responding.. and it close....

How do we know which button that is?

You never renamed your buttons in the Designer, so the blocks images don't tell us which button is which.

hi @ABG .... i made the sample that post @Juan_Antonio - 5.- App requests temperature and humidity to the Arduino. The Arduino sends values. - Bluetooth HC-06. Arduino. Send. Receive. Send text file. Multitouch. Image - #6 by Juan_Antonio

How long is the Clock1.TimerInterval?

1 Like

@Juan_Antonio on my original project??... 2000... i change it to 500 but nothing happend... and also separate values ​​by comma, instead of | ... but nothing

In example "5.- App requests temperature and humidity to the Arduino. The Arduino sends values" I didn't use Clock.

Try example.
7.- App gets the value of two potentiometers. but...

  • Do not use potentiometers.
  • This code
    int value_pot0;
    int value_pot1;
    String value;

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

void loop() {
value_pot0 = random(0,1023);
value_pot1 = random(0,1023);
value = (String) value_pot0 + "," + (String) value_pot1;
Serial.println(value);
delay(200); // It should be slower than the Clock Interval.
}

Clock. Interval = 151
DelymiterByte = 10

thanks again @Juan_Antonio and i know , in fact, i copy everything in your sample.... the code... the fisical mount of arduino ... and the app---- but it dosn't work.... thats why i'm thinking it's my smartphone...

now, i'm gona try the sample 7

Location permission allowed?

Location permission allowed?... yes...
i surrender... i made all the samples but noeone send me any data.... but everythings work.. i can turn on / off everything i want... i can read the data on the monitor and screen of arduino... THE ONLY thing i can't do.. it's recive the data on the app-----

I better hire a professional... this is beyond me
THANKS TO ALL FOR YOUR TIME AND HELP-----

Dear David (@David_Vyhmeister ),
but is your Arduino code the one you posted in your first message ?
If so, please be aware that it's not sending anything on the BT line.
In your code you use the:
Serial.println(TEMPERATURA);
Serial.println("|");
......
and so on, but the Serial.print() just sends to the PC monitor.
To send to the BT line you shall send data by using the miBT serial line instead.
In this way
miBT.print(TEMPERATURA);
miBT.print("|");
.......
'till the last sending, in which you shall use the miBT.println("|") to close the frame.
(i.e. just print for the values and the delimiters, and println for the the last one).

Also by reading your Arduino code: why are you waiting characters from the BT to allow reading the sensors? This means that your AI2 code is triggering the readings ? In my understanding the aim of your application is to send sensors' data from the Arduino board to the phone. In this case you don't have to wait nothing from the phone and just to send out every second (your delay(1000)) the acquired values. Something like:

void loop()
{
//DISTANCIA ULTRASONIDO
digitalWrite(TRIG, HIGH);
delay(1);
digitalWrite(TRIG, LOW);
DURACION = pulseIn(ECHO, HIGH);
DISTANCIA = DURACION / 5.82;
TEMPERATURA = dht.readTemperature(); // obtencion de valor de temperatura
HUMEDAD = dht.readHumidity(); // obtencion de valor de humedad

Serial.print("TEMPERATURA: "); // value to the PC monitor
Serial.println(TEMPERATURA); // each value on a new line (only on PC monitor)
miBT(TEMPERATURA); // value to the BT
miBT.print("|"); // data separator to the BT
Serial.print("HUMEDAD: "); // value to the PC monitor
Serial.println(HUMEDAD);
miBT.print(HUMEDAD); // value to the BT
miBT.print("|"); // data separator
Serial.print("DISTANCIA :"); // value to the PC monitor
Serial.println(DISTANCIA);
miBT.print(DISTANCIA); // value to the BT
miBT.println("|"); // close the BT frame
delay(1000);
} // end loop

Hoping this helps.
Cheers, Ugo.

1 Like

uskiara .... U ARE A F_____ GOD !!!!...
THAT WAS ALL THE TIME!!!
WHERE CAN I SEND U THE BOTTLE???????... THANKS.. THANKS AND THANKS!!!

Dear @David_Vyhmeister,
just the pleasure to have helped you is my bottle. :champagne: :champagne: :champagne:

Cheers, Ugo.

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