Notification problem

How to make my MQ2 gas sensor at arduino send data to bluetooth and send notification and this how my works so far.I do not know where the problem in blocks


Show us your Arduino code too.

Probable issue is your use of THREE If statements. Perhaps combine them using a single If elseif elseif statement.
elseif
and you may get the result you expect (assuming there are no issues with your Bluetooth import of the arduino values).

You asked for exactly 10 bytes of Bluetooth text.

In life, what you expect is rarely what you get.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>


LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial BTSerial(10, 11); // RX, TX

#define MQ2pin (0)
int buzzer = 13;
int greenLEDPin = 7;
int yellowLEDPin = 6;
int redLEDPin = 12;
int sensorValue;  // variable to store sensor value

void setup()
{
  pinMode(buzzer, OUTPUT);
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0, 0);
  Serial.begin(9600);
  BTSerial.begin(9600);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
  

  lcd.print("GAS DETECTOR");
  delay(1000);
  lcd.setCursor(0, 1);
  lcd.print("  WARMING UP ! ");
  delay(20000); // allow the MQ-6 to warm up
}

void loop()
{
  lcd.clear();
  sensorValue = analogRead(MQ2pin); // read analog input pin 0
  lcd.setCursor(0, 0);
  lcd.print("GAS VALUE: ");
  lcd.print(sensorValue);
  lcd.print("ppm");

  if (sensorValue > 250)
  {
    lcd.setCursor(0, 1);
    lcd.print("DANGEROUS GAS LEAK");
    digitalWrite(greenLEDPin, LOW);
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(redLEDPin, HIGH);
    tone(buzzer, 400, 500);
    Serial.print("DANGER");
    delay(1000);
    noTone(buzzer);
    
  }
  else if (sensorValue >= 231 && sensorValue <= 250)
  {
    lcd.setCursor(0, 1);
    lcd.print("CAUTIOUS GAS LEAK");
    digitalWrite(greenLEDPin, LOW);
    digitalWrite(yellowLEDPin, HIGH);
    digitalWrite(redLEDPin, LOW);
    tone(buzzer, 100, 500);
    Serial.print("WARNING");   
    delay(1000);
    noTone(buzzer);  
  
    
  }
  else if (sensorValue < 230)
  {
    lcd.setCursor(0, 1);
    lcd.print("NO GAS LEAK");
    digitalWrite(greenLEDPin, HIGH);
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(redLEDPin, LOW);
    Serial.print("SAFE");
    noTone(buzzer);
  }
  
  noTone(buzzer);
  delay(1000); // wait 1s for next reading
}

This is my code

1 Like

I think the problem is in the application, but it does not show notifications because the application is open or you do not have the code to run the application in the background.

1 Like

Does your device have a console attached, for the Serial data stream, in addition to the Bluetooth serial stream BTSerial?

I see you writing to Serial, but never to BTSerial.

I also notice the sketch duplicates the same limit checks that you coded into the app.

The app expects numbers, but the most you send is those three text messages.

I guessed where you got that 10 byte request length. Maybe you heard about the BlueTooth message Delimiter of newLine, which is decimal 10, and did not know where to put it?

This project looks like it was done by two people who did not read each others' code.

Here is the stock advice for message Delimiters:
Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

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.

Also, be sure you actually have Bluetooth data arriving, by testing with a Bluetooth terminal app from the Play Store.

1 Like