0

I'm trying to execute programs on my arduino based on values received via bluetooth. Although I've experimented a lot, I'm unable to get the code to work.

I have build a simple android app with three buttons that sends either value "11", "21" or "31" to the arduino over bluetooth, which should trigger the code to operate a DIY wake-up-light (ESP32 + NeoPixels). Can anyone tell me what I'm doing wrong? Thanks in advance!

#include "BluetoothSerial.h" 
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>

// init Class:
BluetoothSerial ESP_BT; 

#define LED_PIN    5
#define LED_COUNT 7

Adafruit_NeoPixel matrix = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  matrix.begin();
  matrix.show(); // Initialize all pixels to 'off'

  Serial.begin(19200);
  ESP_BT.begin("Wake_Up_Light"); //Name of Bluetooth interface
}

int led_pin_1 = 5;
int incoming;


void loop() {

// ------------ call wakeup program or gotosleep program ----------------
  if (ESP_BT.available()) 
  {
    incoming = ESP_BT.read(); //Read what we receive 


    int rnumber = incoming;
    
    switch (rnumber) {
      case 1:  
        Serial.print("11"); //----- Activate wake-up light after 6 hours -----
          6hours();
          wakeup();
          ledsoff();
        break;
      
      case 2:  
        Serial.print("21"); //----- Activate go-to-sleep mode -----
          gotosleep();
        break;
      
      case 3:  
        Serial.print("31"); //----- Activate powernap mode -----
          gotosleep();
          wakeupdelay();
          wakeup(); 
          ledsoff();
        break;
      }
  }
}

void wakeup() {

    uint16_t i, j;

  for (j = 1; j < 10; j++) {
    for (i = 0; i <LED_COUNT; i++) {
      matrix.setBrightness(255);
      matrix.setPixelColor(i, 3, j, j);
    }
    matrix.show();
    delay(18000); //-------- 180.000ms (3 minutes) / 10 steps = 18.000 ---------
  }
}

void gotosleep() {

    uint16_t i, j;
  for (j = 10; j > 0; j--) {
    for (i = 0; i <LED_COUNT; i++) {
      matrix.setBrightness(255);
      matrix.setPixelColor(i, 3, j, j);
    }
    matrix.show();
    delay(16363); // ---- 180000ms (3 minutes) / 11 steps = 16363 --------
  }
  matrix.setBrightness(0);
  delay(40000); // ---- 40 seconds delay
  }

void wakeupdelay() {
  delay(1200000);
}

void ledsoff() {
    matrix.setBrightness(0);
}

void 6hours() {
  delay(21600000)
}
4
  • 1
    It is not clear where you are sending what. In your text you state to send "11", etc from the android app to the ESP. But your code reacts to binary data with values of 1 to 3 from bluetooth and will itself send "11" etc. Please clarify
    – chrisl
    Commented Jan 11, 2022 at 17:19
  • why are you not printing the received value? ... that would be the first thing to do when debugging ... also, why are you not saying what the problem is?
    – jsotola
    Commented Jan 12, 2022 at 4:30
  • The entire code is loaded on the ESP32. I connect my phone to the ESP32 via bluetooth and send the number 11, 21 or 31 to the ESP32 (Send1ByteNumber method). The ESP32 should be listening to anything received over bluetooth and for example, when receiving value 11, it should run 6hours(); wakeup(); and ledsoff(); after each other and then wait for further instructions (bluetooth input). Nothing needs to be send back to the phone/app.
    – Raptor452
    Commented Jan 12, 2022 at 7:18
  • The "case" values inside your "switch" statement currently compare the received value to 1, 2 and 3, not to 11, 21 and 31 like your intention seems to be.
    – StarCat
    Commented Jan 12, 2022 at 9:37

1 Answer 1

1

Thanks for the replies. I've taken another aproach and was able to get the code to work. I'll post it here. Not yet 100% finished, but mostly doing what I wanted.

#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "BluetoothSerial.h"
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>

#define LED_PIN    5
#define LED_COUNT 7

Adafruit_NeoPixel matrix = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

String  Recvd;
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial ESP_BT;
void printDeviceAddress() {

const uint8_t* point = esp_bt_dev_get_address();

for (int i = 0; i < 6; i++) {
   char str[3];
   sprintf(str, "%02X", (int)point[i]);
   Serial.print(str);
   if (i < 5){
   Serial.print(":");}
}
Serial.println();
}

void setup()
{
Recvd = "";

Serial.begin(19200);

  ESP_BT.begin("Wake_Up_Light");
  Serial.println("Address:");
  printDeviceAddress();

}

int led_pin_1 = 5;
int incoming;

void loop()
{
matrix.setBrightness(0);
matrix.show();
    if (ESP_BT.available()) {
      Recvd = (ESP_BT.read());
      Serial.println("Received value:");
      Serial.println(Recvd);
      if (Recvd == "11") {
        Serial.println("Running wake-up>6hr mode");
        sixhours();
        wakeup();
        ledsoff();

      }
      if (Recvd == "21") {
        Serial.println("Running go-to-sleep mode");
        gotosleep();

      }
      if (Recvd == "31") {
        Serial.println("Running powernap mode");
        gotosleep();
        wakeupdelay();
        wakeup(); 
        ledsoff();

      }

    }

}

void wakeup() {

    uint16_t i, j;

  for (j = 1; j < 10; j++) {
    for (i = 0; i <LED_COUNT; i++) {
      matrix.setBrightness(255);
      matrix.setPixelColor(i, 3, j, j);
    }
    matrix.show();
    delay(18000); //-------- 180.000ms (3 minutes) / 10 steps = 18.000 ---------
  }
}

void gotosleep() {

    uint16_t i, j;
  for (j = 10; j > 0; j--) {
    for (i = 0; i <LED_COUNT; i++) {
      matrix.setBrightness(255);
      matrix.setPixelColor(i, 3, j, j);
    }
    matrix.show();
    delay(16363); // ---- 180000ms (3 minutes) / 11 steps = 16363 --------
  }
  matrix.setBrightness(0);
  delay(40000); // ---- 40 seconds delay
  }

void wakeupdelay() {
  delay(1200000); // (20 minutes)
}

void ledsoff() {
    matrix.setBrightness(0);
}

void sixhours() {
  delay(21600000); // (6 hours)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.