This commit is contained in:
2025-04-13 22:55:55 +02:00
commit 9abfb936a3
16 changed files with 527 additions and 0 deletions

37
esp32_readout/display.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include "display.h"
#include <Adafruit_SharpMem.h>
Display::Display() {
this->_display = new Adafruit_SharpMem(SHARP_SCK, SHARP_MOSI, SHARP_SS, DISPLAY_HEIGHT, DISPLAY_WIDTH);
this->mid_x = DISPLAY_WIDTH / 2;
this->mid_y = DISPLAY_HEIGHT / 2;
}
void Display::setup() {
// start & clear the display
this->_display->begin();
this->_display->clearDisplay();
this->_display->setRotation(2);
this->_display->setTextColor(BLACK, WHITE);
this->_display->setTextSize(FONT_SIZE);
this->_display->setFont(FONT);
this->print("Smaage");
}
void Display::print(const char* text) {
this->_display->clearDisplay();
this->_display->setCursor(12, this->mid_x);
this->_display->println(text);
this->_display->refresh();
}
void Display::print_weight(double val) {
this->_display->clearDisplay();
this->_display->setCursor(12, this->mid_x);
this->_display->printf("%.2f g", val);
this->_display->refresh();
}

25
esp32_readout/display.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef DISPLAY_DEF
#define DISPLAY_DEF
#include <Adafruit_GFX.h>
#include <Adafruit_SharpMem.h>
#include <string>
#include "env.h"
class Display {
private:
Adafruit_SharpMem* _display;
int mid_x;
int mid_y;
public:
Display();
void setup();
void print(const char* text);
void print_weight(double weight);
};
#endif

67
esp32_readout/env.h Normal file
View File

@@ -0,0 +1,67 @@
#ifndef ENV
#define ENV
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <Adafruit_NAU7802.h>
#include <Adafruit_GFX.h>
#include <Fonts/FreeSans9pt7b.h>
/********************* NAU7802 ****************************/
#define LDO NAU7802_3V3
#define GAIN NAU7802_GAIN_128
#define SPS NAU7802_RATE_20SPS
/****************************** DISPLAY *******************/
// any pins can be used
#define SHARP_SCK 7
#define SHARP_MOSI 9
#define SHARP_SS 44
#define DISPLAY_HEIGHT 144
#define DISPLAY_WIDTH 168
#define BLACK 0
#define WHITE 1
#define FONT_SIZE 2
#define FONT &FreeSans9pt7b
/********************* BLE ********************************/
#define BLE true
#if BLE
#define SERVICE_UUID "9f0dfdb2-e978-494c-8f15-68dbe8d28672"
#define MILLIS_UUID "abb92561-a809-453c-8c7c-71d3fff5b86e"
#define WEIGHT_UUID "123e4567-e89b-12d3-a456-426614174000"
#endif
#if GAIN == NAU7802_GAIN_1
#define CALIBRATION_FACTOR 100.0 / 424.47
#elif GAIN == NAU7802_GAIN_2
#define CALIBRATION_FACTOR 100.0 / 2457.96
#elif GAIN == NAU7802_GAIN_4
#define CALIBRATION_FACTOR 100.0 / 3622.82
#elif GAIN == NAU7802_GAIN_8
#define CALIBRATION_FACTOR 100.0 / 6630.74
#elif GAIN == NAU7802_GAIN_16
#define CALIBRATION_FACTOR 100.0 / 13179.24
#elif GAIN == NAU7802_GAIN_32
#define CALIBRATION_FACTOR 100.0 / 25955.84
#elif GAIN == NAU7802_GAIN_64
#define CALIBRATION_FACTOR 100.0 / 52865.63
#elif GAIN == NAU7802_GAIN_128
#define CALIBRATION_FACTOR 100.0 / 104167.17
#endif
#endif

View File

@@ -0,0 +1,121 @@
#include "env.h"
#include "display.h"
Display display;
Adafruit_NAU7802 nau;
unsigned long _millis = 0;
int32_t val = 0;
#if BLE
BLEServer *pServer = NULL;
BLECharacteristic *millisCharacteristic = NULL;
BLECharacteristic *weightCharacteristic = NULL;
BLEAdvertising *pAdvertising = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
// Callback class to handle connection events
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
BLEDevice::startAdvertising();
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
#endif
void setup() {
Serial.begin(115200);
display.setup();
if (! nau.begin()) {
Serial.println("Failed to find NAU7802");
while (1) delay(10); // Don't proceed.
}
nau.setLDO(LDO);
nau.setGain(GAIN);
nau.setRate(SPS);
// Take 10 readings to flush out readings
for (uint8_t i=0; i<10; i++) {
while (! nau.available()) delay(1);
nau.read();
}
while (! nau.calibrate(NAU7802_CALMOD_INTERNAL)) {
Serial.println("Failed to calibrate internal offset, retrying!");
delay(1000);
}
while (! nau.calibrate(NAU7802_CALMOD_OFFSET)) {
Serial.println("Failed to calibrate system offset, retrying!");
delay(1000);
}
#if BLE
// initialize the Bluetooth® Low Energy hardware
BLEDevice::init("Smaage");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
millisCharacteristic = pService->createCharacteristic(
MILLIS_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
);
weightCharacteristic = pService->createCharacteristic(
WEIGHT_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
);
millisCharacteristic->setValue((uint8_t *)&_millis, sizeof _millis);
weightCharacteristic->setValue((uint8_t *)&val, sizeof val);
pService->start();
pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
BLEDevice::startAdvertising();
Serial.println("BLE device is now advertising...");
Serial.print("BLE Address: ");
Serial.println(BLEDevice::getAddress().toString().c_str());
#endif
display.print("Smaage is ready!");
}
void loop() {
while (!nau.available()) {
delay(1);
}
val = nau.read();
#if BLE
if (deviceConnected) {
// Send the sensor reading
_millis = millis();
millisCharacteristic->setValue((uint8_t *)&_millis, sizeof _millis);
millisCharacteristic->notify();
weightCharacteristic->setValue((uint8_t *)&val, sizeof val);
weightCharacteristic->notify();
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
#else
Serial.print(millis()); Serial.print(","); Serial.println(val);
#endif
}