Bluetooth client does not disconnect when bluetooth off

Hi,
I have a problem my did not sense when bluetooth device is dissconnected or turned off it keep display connect until i send a command from app like turn on device @ this time it sense device is dissconnected when i try to send random text for example (query ) inside clock block the app can sense when bluetooth device disconnected or turned off but app stop receiving updated data from arduino so it seems that check command added stop receiving from bluetooth device so what i can do to solve this issue

Project_final.aia (580.2 KB)
project.txt (3.7 KB)

generally see here concerning the IsConnected property

this thread might help

Taifun

hi,
when i add check command inside the clock block to send random text to arduino to test keep alive and raise error in case of faliure of transmission the program on arduino stop work and no updated data comes to arduino through bluetooth it seems a stuck occured

Sorry, I did not check your aia file .

If you are asking for help, I recommend you to make it as easy for others to be able to help you ...
You probably will get more feedback then...

which means in your case post a screenshot of your relevant blocks...

To download the aia file, upload it to App Inventor, open it, do some bug hunting for you, etc... this takes time, and most people will not do that...
Thank you.

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

1 Like

Dear @ahmed_salah,
the "alive" is not sent from the APP to the Arduino board, but visa-versa.
The Arduino board sends every (i.e) 2 seconds an "alive"character to the APP. If the app does not receive the "alive" character within this period, it can decide that the BT line went down on the Arduino side, therefore it can try to reset the Arduino BT communication.or, at least, it can raise a pop-up, warning the user that the BT line went down.
Hoping it can help.

Hi,
blocks image uploaded , arduino code also

arduino code

#include <LiquidCrystal.h>
#include <dht.h> 
dht DHT;

String text;
String value;
int var;
int i;

// rs,e,d4,d5,d6,d7
//LiquidCrystal lcd(22, 23, 24, 25, 26, 27); // Assigning arduino pins to LCD display module
LiquidCrystal lcd(10, 9, 7, 6, 5, 4);
int greenLED = 11; //2   
int redLED = 12;   //3       
int gasPin1 = A0;
int pirPin1 = A1;  //36    
int tempPin1 = A2; //A8
int ldrPin1 = A3;
int speakerPin = 13; //10
int relayPin = A5;
int testPin = 8;
int zone = 0;
int alarmActive = 0;  

void setup(){
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.clear();
      
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(testPin, OUTPUT);
  
  pinMode(pirPin1, INPUT);     //Motion
  pinMode(gasPin1, INPUT);     //Gas
  pinMode(ldrPin1, INPUT);     //LDR
  
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, HIGH);
  digitalWrite(speakerPin, LOW);
  lcd.print("   Warrming up   ");
  //delay(20000);
  delay(100);
  lcd.clear();
  lcd.print("  Welcome AAST   ");
  delay(100);
  
 
  
}

void loop(){
  alarmActive = 1;
  readstatus();
  Serial.flush();
  checksensor();
  readtemp();
  //sendstatus();
  }
/////////////////////////  Functions  /////////////////////////////////

void readtemp()    // Read Tempreature
{
  lcd.clear();
  int chk = DHT.read11(tempPin1);
  value = (String) DHT.temperature + "C" + "," + (String) DHT.humidity + "%" + "," + (String) digitalRead(relayPin);
  Serial.println(value);
  lcd.setCursor(0,0);
  lcd.print("Temp : ");
  lcd.setCursor(10,0);
  lcd.print(DHT.temperature);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Humidity: ");
  lcd.print(DHT.humidity);
  lcd.print("%");
  delay(1500);
    
}

void alarmTriggered(){
  
  //alarmActive = 1;
  digitalWrite(speakerPin, HIGH);
  digitalWrite(greenLED, LOW);
  digitalWrite(redLED, HIGH);
  lcd.clear(); //asm
  lcd.print("SYSTEM TRIGGERED");
  //lcd.setCursor(0,3);
  //lcd.print("                    ");
   
   if (zone == 0) { 
    lcd.setCursor(0,1);
    lcd.print("Motion Detected");
    delay(500);
    lcd.clear();
    delay(500);
  }

  if (zone == 5) { 
    lcd.setCursor(0,1);
    lcd.print("Light Detected");
    delay(500);
    lcd.clear();
    delay(500);
  }
  
   else if (zone == 4){
   lcd.setCursor(0,1);
   lcd.print("Gas Detected");
   delay(500);
   }
   digitalWrite(speakerPin, LOW);
   digitalWrite(greenLED, HIGH);
   digitalWrite(redLED, LOW);
}

void checksensor(){
if (alarmActive == 1)
    {
    if (digitalRead(pirPin1) == HIGH)
    {
      zone = 0;
      alarmTriggered();
    }
    if (digitalRead(gasPin1) == HIGH)
     {
      zone = 4;
      alarmTriggered();
     }
     if (digitalRead(ldrPin1) == LOW)
     {
      zone = 5;
      alarmTriggered();
     }
   }
   else
   
   //Serial.println("readtemp"); 
   readtemp();
}
void readstatus()
{while (Serial.available())
  { delay(100);
    char c = Serial.read();
    text += c;
  }
  if (text.length() > 0)
  {
    //Serial.print(text);
    if (text == "1")
    {
      digitalWrite(relayPin, HIGH);
    }
    if (text == "0")
    {
      digitalWrite(relayPin, LOW);
    }
    if (text == "query")
    {
      digitalWrite(testPin, LOW);
    }
    text = "";
  }
}

void sendstatus(){
    //var = 0;
    //while ( var < 200)
    while(Serial.available())
  { value = (String) DHT.temperature + "C" + "," + (String) DHT.humidity + "%" + "," + (String) digitalRead(relayPin);
    Serial.println(value);
    delay(1500);
    //var++;
  }
}

void sendstatus1(){
  for (int i=0; i<10; i++)
  Serial.println(value);
}

Hi,
can u support me how to do that based on my posted code and blocks

Hi @ahmed_salah, it's a bit difficult to try to "inject" both Arduino and AI2 code into yours.
So I've written two chunks of code from scratch.
One is the AI2 and the other is the Arduino.
The pdf contains instructions.
Salah.aia (1.7 MB)
Salah.txt (2.1 KB)
Salah.pdf (110.1 KB)

Hoping it helps.

Hi,
i do not know what i say sooooooooooo thx for your effort and support but i got lost on your code i am not so proffisional :))) so is there some thing more simple i can inject in my code :frowning:

Dear @ahmed_salah,
as I told you, it's not easy to insert pieces of code into another one already written by a different mind.
To this purpose I've written new ones so you can use the idea behind them and adapt to your code.
Moreover I haven't your hardware, so I cannot test in any way if it's working. It can become a nghtmare for both (me and you). As you can see I can dedicate a few minutes at the evening to AI2 since I'm travelling and working often abroad. Yesterday it was Sunday so I had stolen just one hour to my family to write down what I posted, but during the week it's very hard.
My hint is to try my code as it is, by flashing the Arduino on your board (supposing it's a UNO board), and to load the AI2 code on your phone (be sure to write the correct BT address in the AI2 code, by replacing mine).
I thought that the pdf was enough explanatory :disappointed:sorry if it's not.
Hoping you can fix it.
Cheers.

Hello,
sure i will test it and will try to inject it into my code soooooooooooooooooooooooo thx for your help :slight_smile:

Hello @ahmed_salah ,
another hint is: try to put in communication your Arduino board with a simple transmission of a single character every two seconds to your AI2 simplest code; i.e. try to receive this character and display it on a label.
Until you cannot reach this result, don't try to do anything else more complex otherwise you will not be never capable to find where and when the code fails.
Once you are done with this, the job is almost done. the kernel of the watchdog system is operating. You can then add a clock (for example 4 seconds) on the AI2 code so it writes on a dedicated label if it fires, and restart it any time the character is received from the Arduino board. If the Arduino is sending each two seconds the alive character, then the clock on AI2 will never fire and the relevant label will never be written. Then, switch off the Arduino: at that point you should see that the AI2 clock will fire and will write on its label. If it works, your job is done 100%. You have implemented a watchdog.
Sorry, please do not consider this post as a lesson, but just a hint :slight_smile:
Good luck !

Hi,
i update code based on your idea but instead of sending special charachters from arduino every 2 seconds already arduino send 8 values for readings every 1 second so inside clock1 timer which check for recived data i add else for if command


which enable clock4 if bytes available to receive is 0
and inside clock 4 i make countdown for 10 then disconnect is that right since it does not work
but same idea ???? :frowning:
i attach code after modification with your idea and marked your idea
note that code already working fine but issue when i switch off arduino and bluetooth the app can not sense that

Dear @ahmed_salah,
as far as I can see, in your AI2 code it would be better if you reinitialize countdown to 5 (or 10 ?) and also to stop clock4, every time that the BT receives something. It will not be the solution, but for sure is better.
What is the time period of clock1 ?
Anyway, before getting crazy with multiple clocks, and multiple Tx and Rx messages, I would rather start by testing the simplest code as possible : just timely sending only one character from Arduino to AI2, and set a clock in AI2 so to check if the BT has received something (the sent character). Then if no characters are received within the timeout, the AI2 should raise a warning popup.
Try once more :slight_smile:

hi
clock1 time interval 5 ( used to check data stream from arduino )
clock 4 time interval 1000