Code doesn't work without port monitor

Hello everyone, There was a problem with the Esp32-c3 board. I made a code using AI (since I am weak in programming) which should send data from the Bmp280 module, namely temperature, pressure, and altitude. The problem is that when loading the sketch, the code works correctly and the data is sent. but when the power is reconnected, only the value “nan” comes in. For the code to work again, I need to open the port monitor, but I won’t be able to do this, since I want to solder the battery to this circuit and I won’t be able to open the port monitor. I will be grateful for any help!!! Thank's

#include <Wire.h>
#include <Adafruit_BMP280.h>
#include <ArduinoBLE.h>
 
#define BMP_SDA   (4)
#define BMP_SCL   (5)
 
Adafruit_BMP280 bmp;
BLEService weatherService("19b10000-e8f2-537e-4f6c-d104768a1214"); // Custom BLE Service UUID
BLEStringCharacteristic temperatureCharacteristic("19b10001-e8f2-537e-4f6c-d104768a1214", BLERead | BLENotify, 20); // Characteristic for temperature
BLEStringCharacteristic pressureCharacteristic("19b10002-e8f2-537e-4f6c-d104768a1214", BLERead | BLENotify, 20); // Characteristic for pressure
BLEStringCharacteristic altitudeCharacteristic("19b10003-e8f2-537e-4f6c-d104768a1214", BLERead | BLENotify, 20); // Characteristic for altitude
 
void setup() {
  Wire.begin(BMP_SDA, BMP_SCL);
  bmp.begin(0x77);
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
                  Adafruit_BMP280::SAMPLING_X2,
                  Adafruit_BMP280::SAMPLING_X16,
                  Adafruit_BMP280::FILTER_X16,
                  Adafruit_BMP280::STANDBY_MS_500);
 
  BLE.begin();
  BLE.setLocalName("WeatherSensor");
  BLE.setAdvertisedService(weatherService);
  weatherService.addCharacteristic(temperatureCharacteristic);
  weatherService.addCharacteristic(pressureCharacteristic);
  weatherService.addCharacteristic(altitudeCharacteristic);
  BLE.addService(weatherService);
  BLE.advertise();
}
 
void loop() {
  BLEDevice central = BLE.central();
  if (central && central.connected()) {
    float temperature = bmp.readTemperature();
    float pressure = bmp.readPressure() / 100.0; // Convert pressure to hPa
    float altitude = bmp.readAltitude(1013.25);
 
    String temperatureString = String(temperature, 2);
    String pressureString = String(pressure, 2);
    String altitudeString = String(altitude, 2);
 
    temperatureCharacteristic.writeValue(temperatureString);
    pressureCharacteristic.writeValue(pressureString);
    altitudeCharacteristic.writeValue(altitudeString);
 
    delay(2000);
  }
}

What does AI say to the story You told here?

All the advice he gave me on this matter does not work. He tells me to check the bluetooth connection, add a delay, etc.

What changes or improvements did you make to the "AI" code?

I didn't make any changes

Circuit diagram thanks ...( not fritzing)
Include whatever you are using for power supply.

Sorry, I'm using a translator, and I don't really understand what you mean :confused:

English forum, therefore understanding basic English required.

@emilkaqq ,
AI generated code posted here for help generally doesn't get a good reception because the people here mostly want to help you learn to code, they are not so interested in helping fix AI generated code if doing so means you don't learn how to code yourself. If you are interested there is a long running debate about this here:

It might help if you read the forum guide:

Of particular relevance is the need to post a schematic (circuit diagram), and the preference for a hand drawn diagram, which can be photographed and the photograph uploaded. Fritzing diagrams are better then nothing but miss important information that can easilly included in a hand drawn diagram.

There are forum categories for other languages, perhaps one of them would be better for you. If you do want to start again in a different language please mention this here so people know.

I don’t think there is an error in the code, I think the problem is in the board itself and loading the sketch into it. I didn’t find any information on the Internet about this, so I’m here if there is a person who I faced the same problem, I think it will help me

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