- TaskHandle_t Task0;
- TaskHandle_t Task1;
- SemaphoreHandle_t Semaphore;
- #define BLYNK_PRINT Serial
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <BlynkSimpleEsp32.h> // https://github.com/blynkkk/blynk-library
- #include "BluetoothSerial.h" // https://github.com/espressif/arduino-esp32/tree/master/libraries/BluetoothSerial
- BlynkTimer timer;
- #define DEBUG_SW 1
- #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
- #error Bluetooth is not enabled! Please run
- `make menuconfig` to and enable it
- #endif
- BluetoothSerial SerialBT;
- int bluedata; // variable for storing bluetooth data
- // Pins of Switches
- #define S5 32
- #define S6 35
- #define S7 34
- #define S8 39
- // Pins of Relay (Appliances Control)
- #define R5 15
- #define R6 2
- #define R7 4
- #define R8 22
- #define LED1 25
- // Global variables, available to all
- static volatile unsigned long count0 = 0;
- // By default the mode is with_internet
- int MODE = 0;
- char auth[] = "BLYNK_AUTH_TOKEN";
- char ssid[] = "WIFI_SSID";
- char pass[] = "WIFI_PASSWORD";
- int switch_ON_Flag1_previous_I = 0;
- int switch_ON_Flag2_previous_I = 0;
- int switch_ON_Flag3_previous_I = 0;
- int switch_ON_Flag4_previous_I = 0;
- BLYNK_WRITE(V1)
- {
- int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
- digitalWrite(R5, pinValue);
- // process received value
- }
- BLYNK_WRITE(V2)
- {
- int pinValue = param.asInt(); // assigning incoming value from pin V2 to a variable
- digitalWrite(R6, pinValue);
- // process received value
- }
- BLYNK_WRITE(V3)
- {
- int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
- digitalWrite(R7, pinValue);
- // process received value
- }
- BLYNK_WRITE(V4)
- {
- int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable
- digitalWrite(R8, pinValue);
- // process received value
- }
- void loop0(void * parameter) {
- for (;;) {
- // Blynk.run();
- timer.run(); // Initiates SimpleTimer
- if(MODE == 0){
- count0 = 0;
- }
- else{
- count0 = 1;
- }
- // Add to the queue - wait forever until space is available
- xSemaphoreTake(Semaphore, portMAX_DELAY);
- Serial.println("THIS MODE IS : " + String(MODE));
- Serial.println("THIS IS COUNT : " + String(count0));
- }
- }
- void loop1(void * parameter) {
- for (;;) {
- // Get the number of flashes required
- xSemaphoreGive(Semaphore);
- if(count0 == 1){
- without_internet();
- }
- if(count0 == 0){
- Blynk.run();
- with_internet();
- }
- if (SerialBT.available())
- {
- Bluetooth_handle();
- }
- }
- }
- void setup()
- {
- Serial.begin(115200);
- Serial.println("Setup started.");
- btStart(); Serial.println("Bluetooth On");
- SerialBT.begin("AadityaBt-ESP"); //Bluetooth device name
- Serial.println("The device started, now you can pair it with bluetooth!");
- delay(1000);
- pinMode(S5, INPUT);
- pinMode(S6, INPUT);
- pinMode(S7, INPUT);
- pinMode(S8, INPUT);
- pinMode(R5, OUTPUT);
- pinMode(R6, OUTPUT);
- pinMode(R7, OUTPUT);
- pinMode(R8, OUTPUT);
- pinMode(LED1, OUTPUT);
- Serial.println("Connecting to Internet");
- WiFi.begin(ssid, pass); Serial.println("WiFi On");
- timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
- Blynk.config(auth);
- delay(2000);
- Semaphore = xSemaphoreCreateMutex();
- xTaskCreatePinnedToCore(
- loop0, /* Function to implement the task */
- "Task0", /* Name of the task */
- 2048, /* Stack size in words */
- NULL, /* Task input parameter */
- 0, /* Priority of the task */
- &Task0, /* Task handle. */
- 0); /* Core where the task should run */
- xTaskCreatePinnedToCore(
- loop1, /* Function to implement the task */
- "Task1", /* Name of the task */
- 4096, /* Stack size in words */
- NULL, /* Task input parameter */
- 0, /* Priority of the task */
- &Task1, /* Task handle. */
- 1); /* Core where the task should run */
- Serial.println("Setup completed.");
- }
- void loop()
- {
- vTaskDelete (NULL);
- }
- void with_internet()
- {
- // FOR SWITCH
- if (digitalRead(S5) == LOW)
- {
- if (switch_ON_Flag1_previous_I == 0 )
- {
- digitalWrite(R5, HIGH);
- if (DEBUG_SW) Serial.println("Relay1- ON");
- Blynk.virtualWrite(V1, 1);
- switch_ON_Flag1_previous_I = 1;
- }
- if (DEBUG_SW) Serial.println("Switch1 -ON");
- }
- if (digitalRead(S5) == HIGH )
- {
- if (switch_ON_Flag1_previous_I == 1)
- {
- digitalWrite(R5, LOW);
- if (DEBUG_SW) Serial.println("Relay1 OFF");
- Blynk.virtualWrite(V1, 0);
- switch_ON_Flag1_previous_I = 0;
- }
- if (DEBUG_SW)Serial.println("Switch1 OFF");
- }
- if (digitalRead(S6) == LOW)
- {
- if (switch_ON_Flag2_previous_I == 0 )
- {
- digitalWrite(R6, HIGH);
- if (DEBUG_SW) Serial.println("Relay2- ON");
- Blynk.virtualWrite(V2, 1);
- switch_ON_Flag2_previous_I = 1;
- }
- if (DEBUG_SW) Serial.println("Switch2 -ON");
- }
- if (digitalRead(S6) == HIGH )
- {
- if (switch_ON_Flag2_previous_I == 1)
- {
- digitalWrite(R6, LOW);
- if (DEBUG_SW) Serial.println("Relay2 OFF");
- Blynk.virtualWrite(V2, 0);
- switch_ON_Flag2_previous_I = 0;
- }
- if (DEBUG_SW)Serial.println("Switch2 OFF");
- //delay(200);
- }
- if (digitalRead(S7) == LOW)
- {
- if (switch_ON_Flag3_previous_I == 0 )
- {
- digitalWrite(R7, HIGH);
- if (DEBUG_SW) Serial.println("Relay3- ON");
- Blynk.virtualWrite(V3, 1);
- switch_ON_Flag3_previous_I = 1;
- }
- if (DEBUG_SW) Serial.println("Switch3 -ON");
- }
- if (digitalRead(S7) == HIGH )
- {
- if (switch_ON_Flag3_previous_I == 1)
- {
- digitalWrite(R7, LOW);
- if (DEBUG_SW) Serial.println("Relay3 OFF");
- Blynk.virtualWrite(V3, 0);
- switch_ON_Flag3_previous_I = 0;
- }
- if (DEBUG_SW)Serial.println("Switch3 OFF");
- //delay(200);
- }
- if (digitalRead(S8) == LOW)
- {
- if (switch_ON_Flag4_previous_I == 0 )
- {
- digitalWrite(R8, HIGH);
- if (DEBUG_SW) Serial.println("Relay4- ON");
- Blynk.virtualWrite(V4, 1);
- switch_ON_Flag4_previous_I = 1;
- }
- if (DEBUG_SW) Serial.println("Switch4 -ON");
- }
- if (digitalRead(S8) == HIGH )
- {
- if (switch_ON_Flag4_previous_I == 1)
- {
- digitalWrite(R8, LOW);
- if (DEBUG_SW) Serial.println("Relay4 OFF");
- Blynk.virtualWrite(V4, 0);
- switch_ON_Flag4_previous_I = 0;
- }
- if (DEBUG_SW) Serial.println("Switch4 OFF");
- //delay(200);
- }
- }
- void without_internet()
- {
- // FOR SWITCH
- digitalWrite(R5, !digitalRead(S5));
- digitalWrite(R6, !digitalRead(S6));
- digitalWrite(R7, !digitalRead(S7));
- digitalWrite(R8, !digitalRead(S8));
- }
- void Bluetooth_handle()
- {
- bluedata = SerialBT.parseInt();
- Serial.println("bluedata is : " + String(bluedata));
- // delay(20);
- if (1 == bluedata) {
- digitalWrite(R5, HIGH);
- Blynk.virtualWrite(V1, 1);
- SerialBT.println("relay1 on");
- Serial.print("relay1 on\n");
- }
- else if (2 == bluedata) {
- digitalWrite(R5, LOW);
- Blynk.virtualWrite(V1, 0);
- SerialBT.println("relay1 off");
- Serial.print("relay1 off\n");
- }
- else if (3 == bluedata) {
- digitalWrite(R6, HIGH);
- Blynk.virtualWrite(V2, 1);
- SerialBT.println("relay2 on");
- Serial.print("relay2 on\n");
- }
- else if (4 == bluedata) {
- digitalWrite(R6, LOW);
- Blynk.virtualWrite(V2, 0);
- SerialBT.println("relay2 off");
- Serial.print("relay2 off\n");
- }
- else if (5 == bluedata) {
- digitalWrite(R7, HIGH);
- Blynk.virtualWrite(V3, 1);
- SerialBT.println("relay3 on");
- Serial.print("relay3 on\n");
- }
- else if (6 == bluedata) {
- digitalWrite(R7, LOW);
- Blynk.virtualWrite(V3, 0);
- SerialBT.println("relay3 off\n");
- Serial.print("relay3 off\n");
- }
- else if (7 == bluedata) {
- digitalWrite(R8, HIGH);
- Blynk.virtualWrite(V4, 1);
- SerialBT.println("relay4 on\n");
- Serial.print("relay4 on\n");
- }
- else if (bluedata == 8) {
- digitalWrite(R8, LOW);
- Blynk.virtualWrite(V4, 0);
- SerialBT.println("relay4 off");
- Serial.print("relay4 off\n");
- }
- else
- {
- SerialBT.println("SOMETHING IS WRONG");
- }
- }
- void checkBlynk()
- {
- bool isconnected = Blynk.connected();
- if (isconnected == false)
- {
- MODE = 1;
- WiFi.begin(ssid, pass);
- delay(1000);
- }
- if (isconnected == true)
- {
- MODE = 0;
- }
- }
ESP32 BLE is not responding when "WIFI router is down but connected to ESP32" but it works fine otherwise
-
- 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
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.
Who is online
Users browsing this forum: Google [Bot] and 46 guests