loadcell/firmware/firmware.ino

125 lines
3.3 KiB
Arduino
Raw Permalink Normal View History

2024-05-08 20:31:38 +00:00
#include <bluefruit.h>
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
#include "HX711.h"
2024-05-10 00:27:32 +00:00
#define calibration_factor -6980.0 // black wired load cell
2024-05-08 20:31:38 +00:00
#define DOUT 30
#define CLK 27
HX711 scale;
// BLE Service
BLEDfu bledfu; // OTA DFU service
BLEDis bledis; // device information
BLEUart bleuart; // uart over ble
BLEBas blebas; // battery
void setup()
{
Serial.begin(115200);
scale.begin(DOUT, CLK);
digitalWrite(CLK, LOW); // hangs without this
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
Serial.println("");
Serial.println("");
Serial.println("Load Cell Demo");
Serial.println("--------------\n");
Bluefruit.autoConnLed(true);
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.begin();
Bluefruit.setTxPower(4);
Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
bledfu.begin();
bledis.setManufacturer("Tanner");
bledis.setModel("Load Cell");
bledis.begin();
bleuart.begin();
blebas.begin();
blebas.write(100);
startAdv();
Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode");
}
void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
// Include bleuart 128-bit uuid
Bluefruit.Advertising.addService(bleuart);
// Secondary Scan Response packet (optional)
// Since there is no room for 'Name' in Advertising packet
Bluefruit.ScanResponse.addName();
/* Start Advertising
* - Enable auto advertising if disconnected
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
* - Timeout for fast mode is 30 seconds
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
*
* For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
void loop()
{
char buf[100] = "";
int reading = (int) scale.get_units();
String message = String(reading) + "\n";
message.toCharArray(buf, message.length()+1);
2024-05-10 00:27:32 +00:00
bleuart.write(buf, message.length());
2024-05-08 20:31:38 +00:00
delay(50);
}
// callback invoked when central connects
void connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);
char central_name[32] = { 0 };
connection->getPeerName(central_name, sizeof(central_name));
Serial.print("Connected to ");
Serial.println(central_name);
}
/**
* Callback invoked when a connection is dropped
* @param conn_handle connection where this event happens
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;
Serial.println();
Serial.print("Disconnected, reason = 0x");
Serial.println(reason, HEX);
}