I am not realy interested in Apps, its just a small Park for a project, so learning Android Studio is not an Option, which lead me to NoCode Apps, but i can't get it done here either. I only found one tutorial on youtube, which lead me so far. (see below) I get everything connected and it looks fine, but how do i get my bytes into a float or what ever, to display it, as what it is: a Weight (Kg in my case).
Any help or direction? All tutorials i find seem to be outdated?
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <BLE2901.h>
#include "HX711.h"
// Display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define calibration_factor 21910.0
// HX711 Pins
#define LOADCELL_DOUT_PIN 3
#define LOADCELL_SCK_PIN 4
// BLE
BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
// UUIDs
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
// Display object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Weight-related variables
float highestValue = 0.0;
HX711 scale;
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer *pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
// Initialize HX711
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor);
scale.tare();
// Initialize BLE
BLEDevice::init("ESP32");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->addDescriptor(new BLE2902());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
BLEDevice::startAdvertising();
Serial.println("Waiting for a client to connect...");
// Initialize Display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 not found!"));
for (;;);
}
display.clearDisplay();
display.setTextSize(2.5);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
// Reading from the scale
float currentValue = scale.get_units(1);
if (currentValue > highestValue) {
highestValue = currentValue;
}
// Display values
display.clearDisplay();
display.setCursor(70, 10); // Textposition (x, y)
display.print("Max:");
// Aktuellen Wert anzeigen
display.setCursor(70, 30);
display.print(highestValue, 1); // Zwei Nachkommastellen
display.setCursor(0, 10); // Textposition (x, y)
display.print("Now:");
// Aktuellen Wert anzeigen
display.setCursor(0, 30);
display.print(currentValue, 1); // Zwei Nachkommastellen
// Inhalt auf dem Display aktualisieren
display.display();
// Send the highest value via BLE
if (deviceConnected) {
pCharacteristic->setValue(highestValue);
pCharacteristic->notify();
}
// Handle reconnection
if (!deviceConnected && oldDeviceConnected) {
delay(500);
pServer->startAdvertising();
oldDeviceConnected = deviceConnected;
}
// Handle new connection
if (deviceConnected && !oldDeviceConnected) {
oldDeviceConnected = deviceConnected;
}
delay(50);
}