Select connection Type via Switch

WOW! That totally worked! Thanks a Lot! You made my day:)

If I leave IO14 unconnected i.e. floating, it uses WiFi and if I pull it to GND, i.e. LOW, it uses Bluetooth as it is supposed to!

Thanks for all your efforts! Appreciate it!

1 Like

Great hack @khoih Just shows what a real programmer can do to make things work :slight_smile:

@Crosswalkersam However, for both a “Within WiFi router range” and “Out on the field, but still in internet range of cell (else even Blynk BT wouldn’t work)” wouldn’t simply setting a hotspot on the Cell with same SSID (as at home) do the trick? That is how I get tablets and laptops that I normally use in home, to work without any changes when on the road.

Of course this depends on your cell supplier allowing tethering

2 Likes

Thanks for your request and original code which is now included in the new hack library.
Sometimes, unusual requests :wink: , like this, can create a real headache, but we all can learn from that by doing something different and strange (hacking), hopefully helpful for other users.

Your contribution (request / code) was used (hope you don't mind) and recognized in GitHub page.

  1. Thanks to Crosswalkersam for the original code and request to inspire the work.
2 Likes

Sadly not. When I am outside, looking for radioactive rocks, I am often in the forest or in the mountains, where I either have E or no cellphone service at all. When I have the possibility to use a hotspot, I will do that.

The only issue I found was that the device does not stream the Voltage via Bt or WiFi. The Blynk app always reads 0.0000V. Thats why I had to call

voltage = (float)analogRead(36) / 4096 * 4.2 * (3667 / 3300);

twice, once in void checkStatus and once in void sendDatatoBLClient. Now it works.

I have turned it into an Instructable! https://www.instructables.com/id/Hack-GMC-Geigercounter-With-Blynk

1 Like

So nice and pro :heart_eyes:. Looking forward to your next project and idea.
Regards,

Then Blynk’s BT will not work either… basically Blynk, as an IoT product, requires constant communication link to/from the Server (the App is primarily just the GUI, not the brains).

The BT link just used the phone to act as that Device ↔ BT ↔ WiFi/Cell ↔ SERVER ↔ WiFi/Cell ↔ App link.

So unless you are also packing a portable Local Server, as an AP for both the phone/App & devices to connect too, then you will NOT get what you want with either WiFi or BT devices when out of cell/WiFi range.

I guess @Crosswalkersam meant that the cellphone is close (to use BT) as well as the cell service must be available (to use Blynk).
I also guess the reason to use this way is as follows:

  1. The SSID and PASS is hardcoded to the WiFi at home, (unless using WiFiMulti lib or similar feature to auto connect to available WiFi)
  2. Not comfortable for every user to change Cell Hotspot SSID and PASS to the same as Home WiFi.
  3. Use BT when outside in the field is the easiest way for normal user without changing anything in the code, Blynk APP

Certainly, using cell Hotspot is the better way, if possible.

I took this literally :wink: … but alas, Blynk needs some form of server connection at ALL times.

And based on many past posts… Blynk’s Bluetooth and BLE are NOT as reliable/consistent as WiFi, nor are they as fast, as they act as an additional relay to/from the Device - App - Server - App - Device

I suspect that uninitiated users expect Blynk’s BT/BLE to act like most other such Apps… as in a simple link between device and App, with NO other considerations required… which is just NOT the case with Blynk.

I already change the library so that you can run both BT and WiFi at the same time.
Can you test the code to see if it’s running OK with both (WiFi and BT) at the same time.
Note

  1. You must have 2 different Blynk projects and tokens, 1 for WiFi, one for BT.
  2. Currently, Blynk.begin() is blocking function, so that when there is no WiFi/Blynk, the code can’t run further. If this is OK, we’ll change to use non-blocking calls Blynk.config() and Blynk.connect()

The code:

#define BLYNK_PRINT Serial

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define BLYNK_USE_BT_ONLY      false    //true

#if BLYNK_USE_BT_ONLY
#include <BlynkSimpleEsp32_BT_WF.h>
#else
#include <BlynkSimpleEsp32_BT_WF.h>
#include <BlynkSimpleEsp32_WF.h>

char ssid[] = "SSID";
char pass[] = "PASS";
// WiFi Blynk token
char WiFi_auth[]  = "WiFi-token";
#endif

// BT Blynk token, not shared between BT and WiFi
char BT_auth[]    = "BT-token";

bool USE_BT = true;

#define WIFI_BT_SELECTION_PIN     14   //Pin D14 mapped to pin GPIO14/HSPI_SCK/ADC16/TOUCH6/TMS of ESP32
#define GEIGER_INPUT_PIN          18   // Pin D18 mapped to pin GPIO18/VSPI_SCK of ESP32
#define VOLTAGER_INPUT_PIN        36   // Pin D36 mapped to pin GPIO36/ADC0/SVP of ESP32  

// OLED SSD1306 128x32
#define OLED_RESET_PIN            4   // Pin D4 mapped to pin GPIO4/ADC10/TOUCH0 of ESP32
#define SCREEN_WIDTH              128
#define SCREEN_HEIGHT             32

#define CONV_FACTOR                   0.00658

#define DEBOUNCE_TIME_MICRO_SEC       4200L
#define MEASURE_INTERVAL_MS           20000L
#define COUNT_PER_MIN_CONVERSION      (60000 / MEASURE_INTERVAL_MS)
#define VOLTAGE_FACTOR                ( ( 4.2 * (3667 / 3300) ) / 4096 )

float voltage               = 0;
long  count                 = 0;
long  countPerMinute        = 0;
long  timePrevious          = 0;
long  timePreviousMeassure  = 0;
long  _time                 = 0;
long  countPrevious         = 0;
float radiationValue        = 0.0;
float radiationDose         = 0;

void IRAM_ATTR countPulse();
volatile unsigned long last_micros;

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET_PIN);
BlynkTimer timer;

void IRAM_ATTR countPulse()
{
  if ((long)(micros() - last_micros) >= DEBOUNCE_TIME_MICRO_SEC)
  {
    Pulse();
    last_micros = micros();
  }
}

void Pulse()
{
  count++;
}

void sendDatatoBlynk()
{
  //if (USE_BT)
  {
    Blynk_BT.virtualWrite(V1, countPerMinute);
    Blynk_BT.virtualWrite(V3, radiationValue);
    Blynk_BT.virtualWrite(V5, radiationDose);
    Blynk_BT.virtualWrite(V7, voltage);
  }
#if !BLYNK_USE_BT_ONLY   
  //else
  {
    Blynk_WF.virtualWrite(V1, countPerMinute);
    Blynk_WF.virtualWrite(V3, radiationValue);
    Blynk_WF.virtualWrite(V5, radiationDose);
    Blynk_WF.virtualWrite(V7, voltage);
  }
#endif 
}

void Serial_Display()
{
  Serial.print("cpm = ");
  Serial.print(countPerMinute, DEC);
  Serial.print(" - ");
  Serial.print("RadiationValue = ");
  Serial.print(radiationValue, 2);
  Serial.print("uSv/h");
  Serial.print(" - ");
  Serial.print("Equivalent RadiationDose = ");
  Serial.print(radiationDose, 4);
  Serial.println("uSv");
}

void OLED_Display()
{
  display.setCursor(0, 0);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print(countPerMinute, DEC);
  display.setCursor(40, 0);
  display.print("CPM");
  display.setCursor(0, 10);
  display.print(radiationDose, 3);
  display.setCursor(40, 10);
  display.print("uSv");
  display.setCursor(0, 20);
  display.println(voltage, 2);
  display.setCursor(40, 20);
  display.println("V");

  if ((radiationValue, 2) < 9.99)
  {
    display.setTextSize(2);
  }
  else
  {
    display.setTextSize(1);
  }

  display.setCursor(65, 10);
  display.print(radiationValue, 2);
  display.setTextSize(1);
  display.setCursor(90, 25);
  display.print("uSv/h");
  display.display();
}

void checkStatus()
{
  static float voltage;

  if (millis() - timePreviousMeassure > MEASURE_INTERVAL_MS)
  {
    timePreviousMeassure = millis();
    countPerMinute = COUNT_PER_MIN_CONVERSION * count;
    radiationValue = countPerMinute * CONV_FACTOR;
    radiationDose = radiationDose + (radiationValue / float(240.0));

    // can optimize this calculation
    voltage = (float) analogRead(VOLTAGER_INPUT_PIN) * VOLTAGE_FACTOR;

    if (radiationDose > 99.999)
    {
      radiationDose = 0;
    }

    Serial_Display();
    OLED_Display();
    
    count = 0;
  }
}

void setup()
{
  pinMode(GEIGER_INPUT_PIN, INPUT);

  Serial.begin(115200);
  attachInterrupt(GEIGER_INPUT_PIN, countPulse, HIGH);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  display.display();
  delay(200);
  display.clearDisplay();

#if BLYNK_USE_BT_ONLY
  Blynk_BT.setDeviceName("GeigerCounter");
  Blynk_BT.begin(BT_auth);
#else
  //if (digitalRead(WIFI_BT_SELECTION_PIN) == HIGH)
  {
    Serial.println(F("Use WiFi to connect Blynk"));
    Blynk_WF.begin(WiFi_auth, ssid, pass);
    USE_BT = false;
  }
  //else
  {
    Serial.println(F("Use BT to connect Blynk"));
    Blynk_BT.setDeviceName("GeigerCounter");
    Blynk_BT.begin(BT_auth);
  }
#endif

  timer.setInterval(5000L, sendDatatoBlynk);
}

void loop()
{
  //if (USE_BT)
    Blynk_BT.run();
#if!BLYNK_USE_BT_ONLY    
  //else
    Blynk_WF.run();
#endif

  timer.run();
  checkStatus();
}

Blynk’s Bluetooth and BLE are NOT as reliable/consistent as WiFi, nor are they as fast, as they act as an additional relay to/from the Device - App - Server - App - Device

That’s why we, you and I personally, all try to avoid using BT/BLE whenever possible.
But we can’t know what the normal users would like and are comfortable to use. :wink:

I believe @Crosswalkersam has very good reason to use this BT/WiFi way as specified in Instructables so that nothing needs to be changed in the system at all, just press a SW.

I wanted an option to use WiFi for a stationary setup at home and Bluetooth to use the device out in the field

Yep… I (once) fought the losing battle(s) to educate those fans of the old tune… “Users just wanna use BT/BLE”… but I should quit the slaughter while I still have some blood left in me :laughing:

1 Like

I’ve never felt the need to stick needles on my eyes, or use Blynk Bluetooth/BLE, but my understanding was that Bluetooth could be used in phone to server mode without any internet/GPRS connectivity.
I recall some issues about not being able to start the app (not getting past the juggling balls screen), but that was fixed by adding a facility to connect without any internet connection.

As I said, I’ve never tried this myself, so could be totally wrong.

Pete.

You don’t know what fun you are missing :crazy_face:“They’re coming to take me away ho ho hee hee ha haaa…”

Ya, I don’t recall those details… but since Account Login, Project Data and Device Authentication is supposedly stored/processed on the Server, I don’t see how that could have been overcome without total workover (which may be the case with Blynk 2.0ooomygoodnessitisneverhappening)

This was three and a half years ago, but the boss man says…

Pete.

Yep, da boss man has said a lot of things :stuck_out_tongue_winking_eye:

Honestly, I don’t care and shouldn’t have gotten involved in this topic as I never liked Blynk’s BT/BLE (it was not reliable enough for me) and I actually don’t actively use Blynk anymore either. Came for the Python interest, got stuck in the debating again… my bad :blush:

1 Like

I agree, I really hope that Bluetooth/BLE doesn’t make it into Blynk 2.0 when it eventually makes an appearance.

Pete.

Update
Just tested and verified the double-dipping WiFi and BT/BLE can work simultaneously using the above sketch / lib mod. All the previously-working widgets are still working correctly.

The terminal output

Use WiFi to connect Blynk
[251] Connecting to HueNet1
[2412] Connected to WiFi
[2412] IP: 192.168.2.109
[2412] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on ESP32

[2418] BlynkArduinoClient.connect: Connecting to acount.duckdns.org:8080
[2524] Ready (ping: 6ms).
Use BT to connect Blynk
[2594] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on ESP32

cpm = 0 - RadiationValue = 0.00uSv/h - Equivalent RadiationDose = 0.0000uSv
[25805] BT connect
[25831] Ready (ping: 25ms).
cpm = 0 - RadiationValue = 0.00uSv/h - Equivalent RadiationDose = 0.0000uSv

Mine has worked with one Project! I just selected Bluetooth as connection Type and WiFi worked well:)

1 Like