ESP32 BLE is not responding when "WIFI router is down but connected to ESP32" but it works fine otherwise

Aaditya Dhanraj
Posts: 1
Joined: Wed Nov 25, 2020 11:03 am

ESP32 BLE is not responding when "WIFI router is down but connected to ESP32" but it works fine otherwise

Postby Aaditya Dhanraj » Wed Nov 25, 2020 11:21 am

I even tried to run wifi and Bluetooth on different cores of ESP32 but I failed. I want to use BLE and WIFI together which runs for now when WIFI router is Online and connected to ESP32 or WIFI router is not connected to electricity and not connected to ESP32, but doesn't work when WIFI is down but ESP32 is connected to router. Please review my code because I don't see any solution around it. Thank you.
  1. TaskHandle_t Task0;
  2. TaskHandle_t Task1;
  3. SemaphoreHandle_t Semaphore;
  4.  
  5. #define BLYNK_PRINT Serial
  6.  
  7. #include <WiFi.h>
  8. #include <WiFiClient.h>
  9. #include <BlynkSimpleEsp32.h> // https://github.com/blynkkk/blynk-library
  10. #include "BluetoothSerial.h" // https://github.com/espressif/arduino-esp32/tree/master/libraries/BluetoothSerial
  11. BlynkTimer timer;
  12. #define DEBUG_SW 1
  13.  
  14.  
  15. #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
  16. #error Bluetooth is not enabled! Please run
  17. `make menuconfig` to and enable it
  18. #endif
  19.  
  20.  
  21. BluetoothSerial SerialBT;
  22.  
  23. int bluedata; // variable for storing bluetooth data
  24.  
  25. // Pins of Switches
  26. #define S5 32
  27. #define S6 35
  28. #define S7 34
  29. #define S8 39
  30.  
  31. // Pins of Relay (Appliances Control)
  32. #define R5 15
  33. #define R6 2
  34. #define R7 4
  35. #define R8 22
  36.  
  37. #define LED1 25
  38.  
  39. // Global variables, available to all
  40. static volatile unsigned long count0 = 0;
  41.  
  42. // By default the mode is with_internet
  43. int MODE = 0;
  44.  
  45. char auth[] = "BLYNK_AUTH_TOKEN";
  46. char ssid[] = "WIFI_SSID";
  47. char pass[] = "WIFI_PASSWORD";
  48.  
  49.  
  50. int switch_ON_Flag1_previous_I = 0;
  51. int switch_ON_Flag2_previous_I = 0;
  52. int switch_ON_Flag3_previous_I = 0;
  53. int switch_ON_Flag4_previous_I = 0;
  54.  
  55. BLYNK_WRITE(V1)
  56. {
  57.   int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  58.   digitalWrite(R5, pinValue);
  59.   // process received value
  60. }
  61.  
  62. BLYNK_WRITE(V2)
  63. {
  64.   int pinValue = param.asInt(); // assigning incoming value from pin V2 to a variable
  65.   digitalWrite(R6, pinValue);
  66.   // process received value
  67. }
  68.  
  69. BLYNK_WRITE(V3)
  70. {
  71.   int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
  72.   digitalWrite(R7, pinValue);
  73.   // process received value
  74. }
  75.  
  76. BLYNK_WRITE(V4)
  77. {
  78.   int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable
  79.   digitalWrite(R8, pinValue);
  80.   // process received value
  81. }
  82.  
  83. void loop0(void * parameter) {
  84.   for (;;) {
  85.  
  86.    //  Blynk.run();
  87.     timer.run(); // Initiates SimpleTimer
  88.     if(MODE == 0){
  89.       count0 = 0;
  90.       }
  91.       else{
  92.         count0 = 1;
  93.         }
  94.     // Add to the queue - wait forever until space is available
  95.     xSemaphoreTake(Semaphore, portMAX_DELAY);
  96.    
  97.     Serial.println("THIS MODE IS : " + String(MODE));
  98.     Serial.println("THIS IS COUNT : " + String(count0));
  99.   }
  100. }
  101.  
  102. void loop1(void * parameter) {
  103.   for (;;) {
  104.     // Get the number of flashes required
  105.     xSemaphoreGive(Semaphore);
  106.     if(count0 == 1){
  107.       without_internet();
  108.       }
  109.      if(count0 == 0){
  110.       Blynk.run();
  111.       with_internet();
  112.       }
  113.       if (SerialBT.available())
  114.       {
  115.         Bluetooth_handle();
  116.       }
  117.   }
  118. }
  119.  
  120. void setup()
  121. {
  122.   Serial.begin(115200);
  123.   Serial.println("Setup started.");
  124.  
  125.   btStart();  Serial.println("Bluetooth On");
  126.  
  127.   SerialBT.begin("AadityaBt-ESP"); //Bluetooth device name
  128.   Serial.println("The device started, now you can pair it with bluetooth!");
  129.   delay(1000);
  130.  
  131.   pinMode(S5, INPUT);
  132.   pinMode(S6, INPUT);
  133.   pinMode(S7, INPUT);
  134.   pinMode(S8, INPUT);
  135.  
  136.   pinMode(R5, OUTPUT);
  137.   pinMode(R6, OUTPUT);
  138.   pinMode(R7, OUTPUT);
  139.   pinMode(R8, OUTPUT);
  140.  
  141.   pinMode(LED1, OUTPUT);
  142.  
  143. Serial.println("Connecting to Internet");
  144.   WiFi.begin(ssid, pass); Serial.println("WiFi On");
  145.   timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
  146.   Blynk.config(auth);
  147.   delay(2000);
  148.  
  149. Semaphore = xSemaphoreCreateMutex();
  150.  
  151.   xTaskCreatePinnedToCore(
  152.       loop0, /* Function to implement the task */
  153.       "Task0", /* Name of the task */
  154.       2048, /* Stack size in words */
  155.       NULL, /* Task input parameter */
  156.       0, /* Priority of the task */
  157.       &Task0, /* Task handle. */
  158.       0); /* Core where the task should run */
  159.  
  160.   xTaskCreatePinnedToCore(
  161.       loop1, /* Function to implement the task */
  162.       "Task1", /* Name of the task */
  163.       4096, /* Stack size in words */
  164.       NULL, /* Task input parameter */
  165.       0, /* Priority of the task */
  166.       &Task1, /* Task handle. */
  167.       1); /* Core where the task should run */
  168.  
  169.   Serial.println("Setup completed.");
  170. }
  171.  
  172. void loop()
  173. {
  174.   vTaskDelete (NULL);
  175. }
  176.  
  177.  
  178.  
  179. void with_internet()
  180. {
  181. // FOR SWITCH
  182.   if (digitalRead(S5) == LOW)
  183.   {
  184.     if (switch_ON_Flag1_previous_I == 0 )
  185.     {
  186.       digitalWrite(R5, HIGH);
  187.       if (DEBUG_SW) Serial.println("Relay1- ON");
  188.       Blynk.virtualWrite(V1, 1);
  189.       switch_ON_Flag1_previous_I = 1;
  190.     }
  191.     if (DEBUG_SW) Serial.println("Switch1 -ON");
  192.  
  193.   }
  194.   if (digitalRead(S5) == HIGH )
  195.   {
  196.     if (switch_ON_Flag1_previous_I == 1)
  197.     {
  198.       digitalWrite(R5, LOW);
  199.       if (DEBUG_SW) Serial.println("Relay1 OFF");
  200.       Blynk.virtualWrite(V1, 0);
  201.       switch_ON_Flag1_previous_I = 0;
  202.     }
  203.     if (DEBUG_SW)Serial.println("Switch1 OFF");
  204.   }
  205.  
  206.  
  207.   if (digitalRead(S6) == LOW)
  208.   {
  209.     if (switch_ON_Flag2_previous_I == 0 )
  210.     {
  211.       digitalWrite(R6, HIGH);
  212.       if (DEBUG_SW)  Serial.println("Relay2- ON");
  213.       Blynk.virtualWrite(V2, 1);
  214.       switch_ON_Flag2_previous_I = 1;
  215.     }
  216.     if (DEBUG_SW) Serial.println("Switch2 -ON");
  217.  
  218.   }
  219.   if (digitalRead(S6) == HIGH )
  220.   {
  221.     if (switch_ON_Flag2_previous_I == 1)
  222.     {
  223.       digitalWrite(R6, LOW);
  224.       if (DEBUG_SW) Serial.println("Relay2 OFF");
  225.       Blynk.virtualWrite(V2, 0);
  226.       switch_ON_Flag2_previous_I = 0;
  227.     }
  228.     if (DEBUG_SW)Serial.println("Switch2 OFF");
  229.     //delay(200);
  230.   }
  231.  
  232.   if (digitalRead(S7) == LOW)
  233.   {
  234.     if (switch_ON_Flag3_previous_I == 0 )
  235.     {
  236.       digitalWrite(R7, HIGH);
  237.       if (DEBUG_SW) Serial.println("Relay3- ON");
  238.       Blynk.virtualWrite(V3, 1);
  239.       switch_ON_Flag3_previous_I = 1;
  240.     }
  241.     if (DEBUG_SW) Serial.println("Switch3 -ON");
  242.  
  243.   }
  244.   if (digitalRead(S7) == HIGH )
  245.   {
  246.     if (switch_ON_Flag3_previous_I == 1)
  247.     {
  248.       digitalWrite(R7, LOW);
  249.       if (DEBUG_SW) Serial.println("Relay3 OFF");
  250.       Blynk.virtualWrite(V3, 0);
  251.       switch_ON_Flag3_previous_I = 0;
  252.     }
  253.     if (DEBUG_SW)Serial.println("Switch3 OFF");
  254.     //delay(200);
  255.   }
  256.  
  257.   if (digitalRead(S8) == LOW)
  258.   {
  259.     if (switch_ON_Flag4_previous_I == 0 )
  260.     {
  261.       digitalWrite(R8, HIGH);
  262.       if (DEBUG_SW) Serial.println("Relay4- ON");
  263.       Blynk.virtualWrite(V4, 1);
  264.       switch_ON_Flag4_previous_I = 1;
  265.     }
  266.     if (DEBUG_SW) Serial.println("Switch4 -ON");
  267.  
  268.   }
  269.   if (digitalRead(S8) == HIGH )
  270.   {
  271.     if (switch_ON_Flag4_previous_I == 1)
  272.     {
  273.       digitalWrite(R8, LOW);
  274.       if (DEBUG_SW) Serial.println("Relay4 OFF");
  275.       Blynk.virtualWrite(V4, 0);
  276.       switch_ON_Flag4_previous_I = 0;
  277.     }
  278.     if (DEBUG_SW) Serial.println("Switch4 OFF");
  279.     //delay(200);
  280.   }
  281.  
  282.  
  283. }
  284.  
  285.  
  286. void without_internet()
  287. {
  288.  // FOR SWITCH
  289.   digitalWrite(R5, !digitalRead(S5));
  290.   digitalWrite(R6, !digitalRead(S6));
  291.   digitalWrite(R7, !digitalRead(S7));
  292.   digitalWrite(R8, !digitalRead(S8));
  293. }
  294.  
  295.  
  296.  
  297.  
  298. void Bluetooth_handle()
  299. {
  300.   bluedata = SerialBT.parseInt();
  301.   Serial.println("bluedata is : " + String(bluedata));
  302. //  delay(20);
  303. if (1 == bluedata) {
  304.     digitalWrite(R5, HIGH);
  305.     Blynk.virtualWrite(V1, 1);
  306.     SerialBT.println("relay1 on");
  307.     Serial.print("relay1 on\n");
  308.   }
  309.   else if (2 == bluedata) {
  310.     digitalWrite(R5, LOW);
  311.     Blynk.virtualWrite(V1, 0);
  312.     SerialBT.println("relay1 off");
  313.     Serial.print("relay1 off\n");
  314.   }
  315.   else if (3 == bluedata) {
  316.     digitalWrite(R6, HIGH);
  317.     Blynk.virtualWrite(V2, 1);
  318.     SerialBT.println("relay2 on");
  319.     Serial.print("relay2 on\n");
  320.   }
  321.   else if (4 == bluedata) {
  322.     digitalWrite(R6, LOW);
  323.     Blynk.virtualWrite(V2, 0);
  324.     SerialBT.println("relay2 off");
  325.     Serial.print("relay2 off\n");
  326.   }
  327.   else if (5 == bluedata) {
  328.     digitalWrite(R7, HIGH);
  329.     Blynk.virtualWrite(V3, 1);
  330.     SerialBT.println("relay3 on");
  331.     Serial.print("relay3 on\n");
  332.   }
  333.   else if (6 == bluedata) {
  334.     digitalWrite(R7, LOW);
  335.     Blynk.virtualWrite(V3, 0);
  336.     SerialBT.println("relay3 off\n");
  337.     Serial.print("relay3 off\n");
  338.   }
  339.   else if (7 == bluedata) {
  340.     digitalWrite(R8, HIGH);
  341.     Blynk.virtualWrite(V4, 1);
  342.     SerialBT.println("relay4 on\n");
  343.     Serial.print("relay4 on\n");
  344.   }
  345.   else if (bluedata == 8) {
  346.     digitalWrite(R8, LOW);
  347.     Blynk.virtualWrite(V4, 0);
  348.     SerialBT.println("relay4 off");
  349.     Serial.print("relay4 off\n");
  350.   }
  351.   else
  352.   {
  353.     SerialBT.println("SOMETHING IS WRONG");
  354.   }
  355. }
  356.  
  357.  
  358. void checkBlynk()
  359. {
  360.   bool isconnected = Blynk.connected();
  361.   if (isconnected == false)
  362.   {
  363.     MODE = 1;
  364.     WiFi.begin(ssid, pass);
  365.     delay(1000);
  366.   }
  367.   if (isconnected == true)
  368.   {
  369.     MODE = 0;
  370.   }
  371. }

Who is online

Users browsing this forum: No registered users and 124 guests