Go Down

Topic: Not receiving bluetooth data in time (MIT Appinventor) (Read 540 times) previous topic - next topic

Deranda

Hello,

I'm working on an automated indoor garden which can be controlloed through a mobilephone app via bluetooth. Therfore I use an arduino pro micro with the HC-05 Bluetooth module and the MIT Appinventor.

I start the app, connect to the arduino and everything seems to be fine. But wenn I want to check the temperature inscide the greenhouse, by clicking my Sync Button, the App does not display the current temperature. Instead it displays the temperature from the last synchronization.

So the arduino works fine I think. -> It sends the data, if the incoming command provides it.

The blocks from the Appinventor are pictured below.

Would be great if someone can help me with that.

best regards!

Robin2

Image from Original Post so we don't have to download it. See this  Simple Image Guide



...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

This is not the place to get help with AppInventor.

If it would help for someone to review your Arduino code then post your program.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Deranda

Here is the source code. There is some other code in the setup but it does not affect the serial communication.

Code: [Select]

void setup(){
  Serial.begin(9600);                               // Baudrate of the Bluetoothmodule
 
void loop() {
  if(Serial.available() > 0){                      // wait until there is data to receive
    recieveByte = Serial.read();                // Einlesen der Daten
 }
  if(receiveByte == 108) {                      // received data: l (ASCII: 108)
    lux = lichtSensor.GetLightIntensity();  // measure the brightness
    Serial.print(lux);                               // send back data
    lux = 0;
    recieveByte = 0;
  }
}

Robin2

Here is the source code.
You need to post the complete program. If the problem was where you think it is you would have found it already.

If you use SoftwareSerial to create a separate serial port for your Bluetooth device you can use Serial for debugging messages.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Deranda

Full code

Code: [Select]
#include <SPI.h>
#include <Wire.h>
#include <BMP180.h>
#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <BH1750FVI.h>

#define dht_bodenPin 4
#define dht_luftPin 5
#define relais1 6
#define relais2 7
#define relais3 8
#define relais4 9
#define mosfet_fan 11;
#define mosfet_led 12;

BMP180 barometer;                           //Barometer BMP180
Adafruit_SSD1306 display(4);                //Display
DHT dht_Luft(DHT22, dht_luftPin);           //DHT22 Luftsensor an Pin D5
DHT dht_Boden(DHT22, dht_bodenPin);         //DHT22 Bodensensor an Pin D4
BH1750FVI lichtSensor;                      //Lichtesensorobjekt

unsigned long prevMillis;
unsigned long curMillis;
unsigned long delayMillis;

long pressure;            // Druckmesswert
long lux;               // Helligkeitsmesswert
float temperature;        // Temperaturmesswert
float humidity;           // Luftfeuchtemesswert
int recieveByte;          // Empfangsdaten der seriellen Schnittstelle

void setup() {
  /*
   * -----------------------------------------
   * ***Serielle Schnittstellen und I2C Bus***
   * -----------------------------------------
   */
  Wire.begin(0x77);                           //Starte I²C des Barometers (0x77)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  //Starte I²C des Displays (0x3C)
  dht_Luft.begin();                           //Starte Luftsensor
  dht_Boden.begin();                          //Starte Bodensensor
  lichtSensor.begin();                        //Starte Lichtsensor
  lichtSensor.SetAddress(0x23);               //I²C Adresse des Sensors
  Serial.begin(9600);

  /*
   * --------------------
   * ***Sensoren Setup***
   * --------------------
   */
  //Barometer Setup()
  barometer = BMP180();
  if(barometer.EnsureConnected()){
    barometer.SoftReset();
    barometer.Initialize();
  }
  //Display Setup()
  display.setTextColor(WHITE);
  display.clearDisplay();
  //Lichtsensor Setup();
  lichtSensor.SetMode(Continuous_H_resolution_Mode);

  /*
   * -------------------
   * ***Pinmode Setup***
   * -------------------
   */
  pinMode(relais1, OUTPUT);
  pinMode(relais2, OUTPUT);
  pinMode(relais3, OUTPUT);
  pinMode(relais4, OUTPUT);

  /*
   * ---------------------
   * ***Variablen Setup***
   * ---------------------
   */
  recieveByte = 0;
  lux         = 0;
  pressure    = 0;
  temperature = 0;
  humidity    = 0;
}

void loop() {
  if(Serial.available() > 0){               // Überprüfen ob Puffer gefüllt ist
    recieveByte = Serial.read();            // Einlesen der Daten
 }
  if(recieveByte == 104) {                  // Emfpangsdaten: h (ASCII: 104)
    lux = lichtSensor.GetLightIntensity();  // Helligkeit messen
    Serial.println(lux);                    // Daten zurück senden
    lux = 0;
    recieveByte = 0;
  }
}


/*
long getPressure(){
  pressure = 0;
  if(barometer.IsConnected){
    barometer.SoftReset();
    barometer.Initialize();
    for(int i = 0; i < 10; i++){
      pressure += barometer.GetPressure();
    }
    pressure /= 10;
  }
  else{
    pressure = 0; 
  }
  return pressure;
}

float getTemperature(){
  return dht_Luft.readTemperature();
}

float getHumidity(){
  return dht_Luft.readHumidity();
}

uint16_t getLightIntensity(){
  return lichtSensor.GetLightIntensity();
}

void displayText(){
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(1,0);
  display.println("1st Row");
  display.print("2nd Row");
}

*/

Robin2

Thank you. However I am still confused.

In your Original Post you say "the App does not display the current temperature. Instead it displays the temperature from the last synchronization."

But your code only seems to be sending a light intensity value.


As I mentioned in Reply #4, change your program around so that that the HC05 is connected to another pair of pins and uses SoftwareSerial so you can use Serial for debug messages to the Arduino Serial Monitor. At the moment you have no means to know what the Arduino is actually doing.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up