Problems with ESP32

I'm currently programming a game with a Pulsesensor input with my ESP32. I booted the code onto the microcontroller, yet I can't see anything on the serial plotter, placing my finger on the sensor doesn't show a pulse. This is my code:

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "xxx";  
const char* password = "xxx";  

const char* mqtt_server = "xxx";
const int mqtt_port = xxx;  
const char* mqtt_topic = "xxx"; 

WiFiClient espClient;
PubSubClient client(espClient);

const int pulse_pin = 36; 
const int led_pin = 13;  

void setup() {
  Serial.begin(115200);
  pinMode(led_pin, OUTPUT);
  
  connect_wifi();
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(mqtt_callback);
  connect_mqtt();
}

void loop() {
  if (!client.connected()) {
    connect_mqtt();
  }
  client.loop();

  int bpm = get_beats_per_minute();
  Serial.print("Schlag gefunden! BPM: ");
  Serial.println(bpm);

  String bpm_str = String(bpm);
  client.publish(mqtt_topic, bpm_str.c_str());
  
  delay(1000); 
}

void connect_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Verbinde mit ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("WLAN verbunden, IP-Adresse: ");
  Serial.println(WiFi.localIP());
}

void mqtt_callback(char* topic, byte* message, unsigned int length) {
  Serial.print("Nachricht angekommen [");
  Serial.print(topic);
  Serial.print("]: ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
  }
  Serial.println();
}

void connect_mqtt() {
  while (!client.connected()) {
    Serial.print("Verbindungsversuch zu MQTT-Broker...");
    String clientId = "ESP32Client-";
    clientId += String(random(0xffff), HEX);

    if (client.connect(clientId.c_str())) {
      Serial.println("verbunden");
      client.subscribe(mqtt_topic);
    } else {
      Serial.print("fehlgeschlagen, rc=");
      Serial.print(client.state());
      Serial.println(" erneut in 5 Sekunden...");
      delay(5000);
    }
  }
}

int get_beats_per_minute() {
  return random(60, 100);
}


This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.