pleasedontcode

BLE Telemetry rev_01

Sep 21st, 2025
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: BLE Telemetry
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-09-21 08:59:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* We want the sensors to show their output in the */
  21.     /* app serial bluetooth */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <MPU6050.h>              // https://github.com/electroniccats/mpu6050
  30. #include <Adafruit_BME280.h>
  31. #include <Adafruit_Sensor.h>
  32.  
  33. #include <BLEDevice.h>
  34. #include <BLEServer.h>
  35. #include <BLEUtils.h>
  36. #include <BLE2902.h>
  37.  
  38. /****** FUNCTION PROTOTYPES *****/
  39. void setup(void);
  40. void loop(void);
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t M1_MPU6050_Interrupt_PIN_D4       = 4;
  44.  
  45. /***** DEFINITION OF I2C PINS *****/
  46. const uint8_t M1_MPU6050_I2C_PIN_SDA_D21        = 21;
  47. const uint8_t M1_MPU6050_I2C_PIN_SCL_D22        = 22;
  48. const uint8_t M1_MPU6050_I2C_SLAVE_ADDRESS      = 104; // 0x68
  49.  
  50. /****** DEFINITION OF BUFFERS/DEVICES *****/
  51. // BME280 I2C address (commonly 0x76 or 0x77)
  52. #define BME280_I2C_ADDRESS 0x76
  53.  
  54. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  55. MPU6050 mpu(M1_MPU6050_I2C_SLAVE_ADDRESS);
  56. Adafruit_BME280 bme; // I2C
  57.  
  58. // BLE (Nordic UART Service) definitions
  59. static boolean bleInitialized = false;
  60. static boolean deviceConnected = false;
  61.  
  62. #define BLE_SERVICE_UUID        "6e400001-b5a3-f393-e0a9-e50e24dcca9e" // NUS service
  63. #define BLE_NUS_TX_CHAR_UUID    "6e400003-b5a3-f393-e0a9-e50e24dcca9e" // TX from device (notify)
  64. #define BLE_NUS_RX_CHAR_UUID    "6e400002-b5a3-f393-e0a9-e50e24dcca9e" // RX to device (write)
  65.  
  66. BLECharacteristic *pTxCharacteristic = nullptr;
  67. BLECharacteristic *pRxCharacteristic = nullptr;
  68.  
  69. // Server callbacks to track connection state
  70. class MyServerCallbacks: public BLEServerCallbacks {
  71.   void onConnect(BLEServer* pServer) {
  72.     deviceConnected = true;
  73.   }
  74.  
  75.   void onDisconnect(BLEServer* pServer) {
  76.     deviceConnected = false;
  77.   }
  78. };
  79.  
  80. // Optional: RX callback (not strictly needed unless you want to process data from central)
  81. class MyRxCallbacks: public BLECharacteristicCallbacks {
  82.   void onWrite(BLECharacteristic *pCharacteristic) {
  83.     // Data received from central (not used in this example)
  84.   }
  85. };
  86.  
  87. void setup(void)
  88. {
  89.     // put your setup code here, to run once:
  90.     Serial.begin(115200);
  91.     while (!Serial) { delay(10); } // wait for Serial
  92.  
  93.     // Initialize I2C
  94.     Wire.begin(M1_MPU6050_I2C_PIN_SDA_D21, M1_MPU6050_I2C_PIN_SCL_D22, 100000);
  95.  
  96.     // Mount/interfacing sensors
  97.     // BME280 setup
  98.     if (!bme.begin(BME280_I2C_ADDRESS)) {
  99.         Serial.println("Could not find BME280 sensor. Check wiring!");
  100.         while (1) { delay(1000); }
  101.     }
  102.     Serial.println("BME280 initialized");
  103.  
  104.     // MPU6050 setup
  105.     mpu.initialize();
  106.     // Basic check (some libraries provide testConnection; adjust if needed)
  107.     // if (!mpu.testConnection()) Serial.println("MPU6050 connection failed!");
  108.     Serial.println("MPU6050 initialized");
  109.  
  110.     // Optional: configure interrupt pin if used
  111.     pinMode(M1_MPU6050_Interrupt_PIN_D4, INPUT);
  112.  
  113.     // Initialize BLE (NUS)
  114.     Serial.println("Initializing BLE...");
  115.     BLEDevice::init("MissilePod");
  116.     BLEServer *pServer = BLEDevice::createServer();
  117.     pServer->setCallbacks(new MyServerCallbacks());
  118.  
  119.     BLEService *pService = pServer->createService(BLE_SERVICE_UUID);
  120.  
  121.     pTxCharacteristic = pService->createCharacteristic(BLE_NUS_TX_CHAR_UUID, BLECharacteristic::PROPERTY_NOTIFY);
  122.     pRxCharacteristic = pService->createCharacteristic(BLE_NUS_RX_CHAR_UUID, BLECharacteristic::PROPERTY_WRITE);
  123.  
  124.     pRxCharacteristic->setCallbacks(new MyRxCallbacks());
  125.  
  126.     // Start the service
  127.     pService->start();
  128.  
  129.     // Start advertising
  130.     BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  131.     pAdvertising->addServiceUUID(BLE_SERVICE_UUID);
  132.     pAdvertising->setScanResponse(true);
  133.     pAdvertising->start();
  134.  
  135.     bleInitialized = true;
  136.  
  137.     Serial.println("BLE initialized. Waiting for connections...");
  138. }
  139.  
  140. void loop(void)
  141. {
  142.     // put your main code here, to run repeatedly:
  143.  
  144.     // --- Get BME280 data ---
  145.     float temp = bme.readTemperature();
  146.     float pressure = bme.readPressure() / 100.0f; // Pa to hPa
  147.     float humidity = bme.readHumidity();
  148.  
  149.     // --- Get MPU6050 data ---
  150.     int16_t ax, ay, az, gx, gy, gz;
  151.     // The function name depends on the library version. Commonly:
  152.     mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  153.  
  154.     // Prepare data string (Arduino String for easy BLE/PIN string handling)
  155.     String data = "Temp: " + String(temp, 2) +
  156.                   ", Pressure: " + String(pressure, 2) +
  157.                   ", Humidity: " + String(humidity, 2) +
  158.                   ", Accel: X=" + String(ax) + " Y=" + String(ay) + " Z=" + String(az) +
  159.                   ", Gyro: X=" + String(gx) + " Y=" + String(gy) + " Z=" + String(gz);
  160.  
  161.     Serial.println(data);  // Debug on serial monitor
  162.  
  163.     // Send over Bluetooth if connected (NUS TX characteristic)
  164.     if (bleInitialized && deviceConnected && pTxCharacteristic != nullptr) {
  165.         pTxCharacteristic->setValue((uint8_t*)data.c_str(), data.length());
  166.         pTxCharacteristic->notify();
  167.     }
  168.  
  169.     // Match the Python loop delay
  170.     delay(1000);
  171. }
  172.  
  173. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment