Arduino Mega+HC-06+LED+DHT11

Hello,

I’m new here, I need some help.

I made a program to read temperature with the DHT11 and to light a led with a push button and I used a HC-06 Bluetooth to make the connection with the Blynk.

Testing the software by removing Blynk.begin. The arduino works correctly, but when I enable Blynk.begin does not do anything else and only via blynk turns on and turns off my led.

Why does it stop running the rest of the program?

Thank you

We don’t know… you haven’t shown us any program to look at :wink:

Please format any posted code, as required for clear viewing, thank you.


#include "defines.h"

//DHT11
#include "DHT.h"
DHT dht(DHTPIN, DHTTYPE);


//Blynk Bluetooth
#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial
#include <BlynkSimpleSerialBLE.h>
char auth[] = "7f........";
WidgetLED LEDs(V0); // Echo signal to Sensors Tab at Blynk App
WidgetLED LEDa(V1); // Echo signal to Actuators Tab at Blynk App

#include <SoftwareSerial.h>
SoftwareSerial SerialBLE(10, 11); // RX, TX

BlynkTimer timer; //Insert a Timer

void setup()
{
  pinMode(LAMP_PIN,OUTPUT);
  pinMode(LAMP_ON_BUTTON,INPUT_PULLUP);
  startTimers();
  Serial.begin(9600);
  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE, auth);
  dht.begin();
  LEDs.off();
  LEDa.off();
  
}
void loop()
{
  timer.run(); // Initiates SimpleTimer
  Blynk.run();
}

/****************************************************************
* Read local commands (Pump and Lamp buttons are normally "HIGH"):
****************************************************************/
void readLocalCmd() 
{  
  boolean digiValue = debounce(LAMP_ON_BUTTON);
  if (!digiValue) 
  {
    lampStatus = !lampStatus;
    aplyCmd();
  }
}

/***************************************************
* Receive Commands and act on actuators
****************************************************/
void aplyCmd()
{
  if (lampStatus == 1) 
  {
    //Blynk.notify("HAS: Warning ==>> LAMP ON"); // Envia Notificacao para celular
    digitalWrite(LAMP_PIN, HIGH);
    LEDs.on();
    LEDa.on();
  }
  else
  {
    digitalWrite(LAMP_PIN, LOW);
    LEDs.off();
    LEDa.off();
  }
  //Serial.print("Lamp Status ");
  //Serial.println(lampStatus);
}

/****************************************************************
* Read remote commands 
****************************************************************/
BLYNK_WRITE(V4) // Lamp remote control
{
  int i = param.asInt();
  if (i == 1) 
  {
    lampStatus = !lampStatus;
    aplyCmd();
  }
}

/***************************************************
 * Send data to Blynk
 **************************************************/
void sendUptime()
{
  Blynk.virtualWrite(V10, airTemp); //virtual pin V10
  Blynk.virtualWrite(V11, airHum); // virtual pin V11
}
void startTimers(void)
{
  timer.setInterval(READ_BUTTONS_TM*1000, readLocalCmd); // Read buttons at every x seconds
  timer.setInterval(READ_AIR_DATA_TM*1000, getDhtData); // Read DHT Sensor at every x seconds
  timer.setInterval(SEND_DATA_BLYNK_TM*1000, sendUptime); // Send Data to blynk at every x seconds
}

/***************************************************
* Debouncing a key
****************************************************/
boolean debounce(int pin)
{
  boolean state;
  boolean previousState;
  const int debounceDelay = 30;
  
  previousState = digitalRead(pin);
  for(int counter=0; counter< debounceDelay; counter++)
  {
    delay(1);
    state = digitalRead(pin);
    if(state != previousState)
    {
      counter = 0;
      previousState = state;
    } 
  }
  return state;
}

Thanks for your help

Please read what I said about about how to insert code for proper viewing… I had to edit your post to fix the formatting.

Sorry my friend.

Thanks for that.

Is this the complete code? It seems to be missing stuff. For example:

 timer.setInterval(READ_BUTTONS_TM*1000, readLocalCmd); // Read buttons at every x seconds

Where is READ_BUTTONS_TM defined?

O create another file with The definitions

Gotcha.

I was about to comment on that :wink: I haven’t seen timers setup this way before… will that work?

void setup()
{
// other stuff
  startTimers();
// other stuff
}

void startTimers(void)
{
  timer.setInterval(READ_BUTTONS_TM*1000, readLocalCmd); // Read buttons at every x seconds
  timer.setInterval(READ_AIR_DATA_TM*1000, getDhtData); // Read DHT Sensor at every x seconds
  timer.setInterval(SEND_DATA_BLYNK_TM*1000, sendUptime); // Send Data to blynk at every x seconds
}

And possibly unrelated to your connection issue, but DHT11 is a notoriously slow sensor… i would give it between 2-5 seconds for read cycles.

/* DHT11*/
#define DHTPIN 6  
#define DHTTYPE DHT11 
float airHum = 0;
float airTemp = 0;

/* TIMER */
#define READ_BUTTONS_TM 0.2L  // definitions in seconds
#define READ_AIR_DATA_TM 2L
#define SEND_DATA_BLYNK_TM 5L

/* LAMPS */
#define LAMP_PIN 4              
boolean lampStatus = 0;

/* Buttons */
#define LAMP_ON_BUTTON 5

/***************************************************
 * Get DHT data
 **************************************************/
void getDhtData(void)
{
  float tempIni = airTemp;
  float humIni = airHum;
  airTemp = dht.readTemperature();
  airHum = dht.readHumidity();
  if (isnan(airHum) || isnan(airTemp))   // Check if any reads failed and exit early (to try again).
  {
    //Serial.println("Failed to read from DHT sensor!");
    airTemp = tempIni;
    airHum = humIni;
    return;
  }
  /*Serial.print("Temperatura: ");
  Serial.println(airTemp);
  Serial.print("Humidade: ");
  Serial.println(airHum);*/
}

I’ve never done (or seen) it that way either. Seems like a little more work than just putting them in the setup()

void setup()
{
  pinMode(LAMP_PIN,OUTPUT);
  pinMode(LAMP_ON_BUTTON,INPUT_PULLUP);
  timer.setInterval(READ_BUTTONS_TM*1000, readLocalCmd); // Read buttons at every x seconds
  timer.setInterval(READ_AIR_DATA_TM*1000, getDhtData); // Read DHT Sensor at every x seconds
  timer.setInterval(SEND_DATA_BLYNK_TM*1000, sendUptime); // Send Data to blynk at every x 
  Serial.begin(9600);
  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE, auth);
  dht.begin();
  LEDs.off();
  LEDa.off();  
}

Also, it is good form to put a “L” on the time for the timers

Yes, I used 2 seconds.

When I comment the Blynk.begin the program works in the arduino.

OK, well BLE/BT is in beta still, and I personally have noticed that it tends to be a bit unstable with lots of things happening… perhaps your “timing” might be putting it off??

Have you setup the BLE widget properly in the app?

In the app i can connect the hc-06 normaly.

Also, i can press a button and off or on the led, But just this. More nothing.

Ah, now i see, you are multiplying the define by 1000… ok. But as @Toro_Blanco mentioned Timer uses LONG integer, so it should have the L in there… but how that affects the rest of the math… iduno :wink:

I will try to make this changes now.

Got ya… you did mention that, my bad.

OK, sounds like a timing issue… i had similar reactions trying to run servos using the BLE/BT connections, it seemed to lock up the connection for some of the processes.

All I can recommend is scale a test project down to one thing at a time and slowly add to it and see how it reacts.

Thanks. I will try.

I Think that the problem is the ARDUINO or the Bluetooth.

I change for a ESP8266Nodemcu e works ok. I dont need to change nothing in my program.

I think that is memory problem.

Thanks for all