BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Need help converting ESP8266 code to ESP32

Hello everyone. I am running several sensors and the attached code has worked great, with minor edits, for ESP8266 using wifi. I want to have this run on Bluetooth and I’m afraid it is not working. I understand ESP32 may need different coding than ESP8266. I would be grateful for help with this. Thank you for reviewing my code. Please disregard the “buzzers.” I will not be using them. I removed the auth code for this posting.



#include <BlynkSimpleEsp32_BLE.h>

#include <DHT.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>


char auth[] ="";
/************************* Pin Definition *********************************/


#define Relay1 D6
#define Relay2 D2
#define Relay3 D1
#define Relay4 D5


#define DHTPIN 23


#define buzzer 02


#define Multiplexer_Pin1 03
#define Multiplexer_Pin2 01
.
#define Multiplexer_DataPin 34

#define Multiplexer_Gas1 LOW
#define Multiplexer_Gas2 LOW
#define Multiplexer_Motion1 HIGH
#define Multiplexer_Motion2 LOW

int proximity;
int light;
int gas;
int motion;
int Readmotion;

#define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor() {
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V10, t);
  Blynk.virtualWrite(V11, h);
  Blynk.virtualWrite(V2, proximity);
  Blynk.virtualWrite(V12, gas);
  Blynk.virtualWrite(V14, motion);
  Blynk.virtualWrite(V5, light);
}

void setup() {

  Serial.begin(9600);
  
  Blynk.setDeviceName("Blynk");
Blynk.begin(auth);

  dht.begin();
  pinMode(Multiplexer_Pin1, OUTPUT);
  pinMode(Multiplexer_Pin2, OUTPUT);
  pinMode(Multiplexer_DataPin, INPUT);
  pinMode(buzzer, OUTPUT);


  timer.setInterval(1000L, sendSensor);
}

void loop() {

  digitalWrite(Multiplexer_Pin1, Multiplexer_Gas1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Gas2);
  gas = analogRead(Multiplexer_DataPin);
  Serial.print("Gas - ");
  Serial.println(gas);

  // Address 10
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion2);
  motion = analogRead(Multiplexer_DataPin);
  if (motion > 512) {
    motion = 1;

  } else {
    motion = 0;

  }
  // Address 01
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion2);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion1);
  proximity = analogRead(Multiplexer_DataPin);
  Serial.print("Proximity - ");
  Serial.println(proximity);
  if (proximity < 512) {
    proximity = 1;
    digitalWrite(buzzer, Multiplexer_Motion1);
  } else {
    proximity = 0;
    digitalWrite(buzzer, LOW);
  }

  // Address 11
  digitalWrite(Multiplexer_Pin1, HIGH);
  digitalWrite(Multiplexer_Pin2, HIGH);
  int raw_light = analogRead(Multiplexer_DataPin);
  light = map(raw_light, 1024, 0, 0, 100);
  Serial.print("Light - ");
  Serial.println(light);

  Blynk.run();
  timer.run();
}

What exactly does that mean?

Does the code compile?
Do you see the device in the BLE widget in the app?
What do you see in the serial monitor?
What Blynk library version are you using?
What App OS and version are you using?
Have you tried testing using the ESP32 BLE example sketch?

Pete.

My apologies.
It does compile.
It shows up in the tablet’s bluetooth settings and says connected.
Serial monitor shows sensors working.
Blynk 2.27.28
No.

That’s not what I asked. What do you see in the BLE widget?

Pete.

Widget is white. Notification says “Wasn’t online yet”

So you can’t see the device listed in the BLE widget?

You may need the Direct Connect line in your code, but I’d try the example code first.

Pete.

Example code uploaded. Says “Can’t connect”
auth code is correct.
tried both BLUETOOTH and BLE setting in Blynk. Serial monitor shows "Waiting for connections…
[11] "
Tablet settings shows “paired”
Board selected is “NODEMCU-32S” per manufacturer
Soha

In your app, what is the device type and connection method?

Pete.

ESP32 Dev Board
Bluetooth (I tried BLE also)
Soha

The example I’m using is the Blynk “ESP32 BLE”
Soha

Any other suggestions?
Thank you.
Soha

No, I don’t use Bluetooth with Blynk, so I’ve reached the limit of my knowledge on the subject.

Pete.