Arduino Bluetooth App

Hi! I'm trying to make a bluetooth app for my arduino project. I'm trying to display the data from 2 DHT22 sensors and one MQ-2 in the app for now.
I'm designing the App in MIT App Inventor and when I tried running it, it doesn't show any value. This is my code and my app (temporarily because I have to develop the second screen and the LED functionality isn't added yet).

#include "DHT.h" // libraria pentru senzorii de temperatura
#include <MQ2.h> //libraria pentru senzorul de gaz

int gaz = A0;
int lpg, co, smoke;

MQ2 mq2(gaz); //s-a creat obiectul corespunaztor senzorului de gaz

DHT dht22in(8, DHT22); //s-au creat obiectele pentru senzori si li s-au atribuit pinii corespunzatori
DHT dht22out(9, DHT22);

int cm = 0;
String temp;
String hum;
String gaze;
char caracter;
/*long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT);  // Clear the trigger
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echoPin, HIGH);
}
*/

void setup(void) {
  Serial.begin(9600);                          
  dht22in.begin();
  dht22out.begin();
  mq2.begin();
  //Serial.println("Sensors are warming up!");
  delay(2000);
}

void loop(void) {

  int temp1 = dht22in.readTemperature();
  int temp2 = dht22out.readTemperature();
  int hum1 = dht22in.readHumidity();
  int hum2 = dht22out.readHumidity();
  
  float* values= mq2.read(false); //set it false if you don't want to print the values in the Serial
  
  lpg = mq2.readLPG();
  co = mq2.readCO();
  smoke = mq2.readSmoke();

  
/*  cm = 0.01723 * readUltrasonicDistance(7, 6);
  if (cm<50) {
  Serial.print(cm);
  Serial.print(" cm");
  delay(1000); // Wait for 100 millisecond(s)
  Serial.println(" ");
  } else Serial.println(" ");
  delay(1000);
  */
  delay(1000);  
   if(Serial.available()) {
  caracter = Serial.read();
  
    if(caracter == 'P'){temp = (String) temp1 + ","+ (String) temp2;
    Serial.println(temp);};
    if(caracter == 'U'){hum = (String) hum1 + ","+ (String) hum2;
    Serial.println(hum);};
    if(caracter == 'G'){gaze = (String) lpg + "," + (String) co+ "," + (String) smoke);
    Serial.println(gaze);
    }
  } 
}

Cabina_de_vopsit.zip (2.9 MB)

I appreciate any suggestion!

What Arduino board are you using?

I suggest that, for development at least, you use a different serial port for the Bluetooth module. Save the main hardware serial port (USB) for program upload, program output and debug prints.

I'm using an Arduino Mega 2560 and how do I use a different port for the Bluetooth module? (I'm a begginer, sorry for asking stupid questions :sweat_smile:)

Then use on of the extra serial ports (Serial1, Serial2 or Serial3) for the Bluetooth module.

Your code modified to use Serial1 for Bluetooth module. Mega pin 18 (TX1) to BT RX (through voltage dibvider), Mega RX1 to BT TX. The incoming from the Bluetooth module is then printed to serial monitor. Untested.

#include "DHT.h" // libraria pentru senzorii de temperatura
#include <MQ2.h> //libraria pentru senzorul de gaz

int gaz = A0;
int lpg, co, smoke;

MQ2 mq2(gaz); //s-a creat obiectul corespunaztor senzorului de gaz

DHT dht22in(8, DHT22); //s-au creat obiectele pentru senzori si li s-au atribuit pinii corespunzatori
DHT dht22out(9, DHT22);

int cm = 0;
String temp;
String hum;
String gaze;
char caracter;
/* long readUltrasonicDistance(int triggerPin, int echoPin)
   {
   pinMode(triggerPin, OUTPUT);  // Clear the trigger
   digitalWrite(triggerPin, LOW);
   delayMicroseconds(2);
   // Sets the trigger pin to HIGH state for 10 microseconds
   digitalWrite(triggerPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(triggerPin, LOW);
   pinMode(echoPin, INPUT);
   // Reads the echo pin, and returns the sound wave travel time in microseconds
   return pulseIn(echoPin, HIGH);
   }
*/

void setup(void)
{
   Serial.begin(9600);
   Serial1.begin(9600); // ********* open serial1 for Bluetooth module
   dht22in.begin();
   dht22out.begin();
   mq2.begin();
   //Serial.println("Sensors are warming up!");
   delay(2000);
}

void loop(void)
{

   int temp1 = dht22in.readTemperature();
   int temp2 = dht22out.readTemperature();
   int hum1 = dht22in.readHumidity();
   int hum2 = dht22out.readHumidity();

   float* values = mq2.read(false); //set it false if you don't want to print the values in the Serial

   lpg = mq2.readLPG();
   co = mq2.readCO();
   smoke = mq2.readSmoke();


   /*  cm = 0.01723 * readUltrasonicDistance(7, 6);
      if (cm<50) {
      Serial.print(cm);
      Serial.print(" cm");
      delay(1000); // Wait for 100 millisecond(s)
      Serial.println(" ");
      } else Serial.println(" ");
      delay(1000);
   */
   delay(1000);
   if (Serial1.available())  // ******** change to Serial1
   {
      caracter = Serial1.read(); // ******** change to Serial1
      // print incoming character to serial monitor.  The < and > will show
      // if there is an invisible character (\r and/or \n) sent.
      Serial.print("incoming character <");
      Serial.print(caracter);
      Serial.println(">");

      if (caracter == 'P')
      {
         temp = (String) temp1 + "," + (String) temp2;
         Serial.println(temp);
      };
      if (caracter == 'U')
      {
         hum = (String) hum1 + "," + (String) hum2;
         Serial.println(hum);
      };
      if (caracter == 'G')
      {
         gaze = (String) lpg + "," + (String) co + "," + (String) smoke);
         Serial.println(gaze);
      }
   }
}

Post the output from serial monitor (incoming character).

You may want to reconsider using the String class. There is potential for memory corruption using Strings. Use c_strings instead.

I tweaked it a bit, but it worked eventually. Thank you! :blush:

1 Like

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