From ef48107c814cdaea896dcdf6bb02d396c584329a Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 9 Apr 2022 18:40:28 -0600 Subject: [PATCH] Initial commit --- bluetooth/bluetooth.ino | 125 ++++++++++++++++++++++++++++++++++++++++ calibrate/calibrate.ino | 80 +++++++++++++++++++++++++ measure/measure.ino | 51 ++++++++++++++++ 3 files changed, 256 insertions(+) create mode 100644 bluetooth/bluetooth.ino create mode 100644 calibrate/calibrate.ino create mode 100644 measure/measure.ino diff --git a/bluetooth/bluetooth.ino b/bluetooth/bluetooth.ino new file mode 100644 index 0000000..4a7f4b9 --- /dev/null +++ b/bluetooth/bluetooth.ino @@ -0,0 +1,125 @@ +#include +#include +#include +#include "HX711.h" + +//#define calibration_factor -6980.0 // black wired load cell +#define calibration_factor -5760.0 // orange wired load cell + +#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("Odai"); + bledis.setModel("TUT 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); + bleuart.write(buf, message.length()+1); + + 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); +} diff --git a/calibrate/calibrate.ino b/calibrate/calibrate.ino new file mode 100644 index 0000000..36754d6 --- /dev/null +++ b/calibrate/calibrate.ino @@ -0,0 +1,80 @@ +/* + Example using the SparkFun HX711 breakout board with a scale + By: Nathan Seidle + SparkFun Electronics + Date: November 19th, 2014 + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). + + This is the calibration sketch. Use it to determine the calibration_factor that the main example uses. It also + outputs the zero_factor useful for projects that have a permanent mass on the scale in between power cycles. + + Setup your scale and start the sketch WITHOUT a weight on the scale + Once readings are displayed place the weight on the scale + Press +/- or a/z to adjust the calibration_factor until the output readings match the known weight + Use this calibration_factor on the example sketch + + This example assumes pounds (lbs). If you prefer kilograms, change the Serial.print(" lbs"); line to kg. The + calibration factor will be significantly different but it will be linearly related to lbs (1 lbs = 0.453592 kg). + + Your calibration factor may be very positive or very negative. It all depends on the setup of your scale system + and the direction the sensors deflect from zero state + This example code uses bogde's excellent library: https://github.com/bogde/HX711 + bogde's library is released under a GNU GENERAL PUBLIC LICENSE + Arduino pin 2 -> HX711 CLK + 3 -> DOUT + 5V -> VCC + GND -> GND + + Most any pin on the Arduino Uno will be compatible with DOUT/CLK. + + The HX711 board can be powered from 2.7V to 5V so the Arduino 5V power should be fine. + +*/ + +#include "HX711.h" + +#define DOUT 30 +#define CLK 27 + +HX711 scale; + +float calibration_factor = -7050; //-7050 worked for my 440lb max scale setup + +void setup() { + Serial.begin(115200); + Serial.println("HX711 calibration sketch"); + Serial.println("Remove all weight from scale"); + Serial.println("After readings begin, place known weight on scale"); + Serial.println("Press + or a to increase calibration factor"); + Serial.println("Press - or z to decrease calibration factor"); + + scale.begin(DOUT, CLK); + digitalWrite(CLK, LOW); // hangs without this + scale.set_scale(); + scale.tare(); //Reset the scale to 0 + + long zero_factor = scale.read_average(); //Get a baseline reading + Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects. + Serial.println(zero_factor); +} + +void loop() { + + scale.set_scale(calibration_factor); //Adjust to this calibration factor + + Serial.print("Reading: "); + Serial.print(scale.get_units(), 1); + Serial.print(" lbs"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person + Serial.print(" calibration_factor: "); + Serial.print(calibration_factor); + Serial.println(); + + if(Serial.available()) + { + char temp = Serial.read(); + if(temp == '+' || temp == 'a') + calibration_factor += 10; + else if(temp == '-' || temp == 'z') + calibration_factor -= 10; + } +} diff --git a/measure/measure.ino b/measure/measure.ino new file mode 100644 index 0000000..fab369f --- /dev/null +++ b/measure/measure.ino @@ -0,0 +1,51 @@ +/* + Example using the SparkFun HX711 breakout board with a scale + By: Nathan Seidle + SparkFun Electronics + Date: November 19th, 2014 + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). + + This example demonstrates basic scale output. See the calibration sketch to get the calibration_factor for your + specific load cell setup. + + This example code uses bogde's excellent library: https://github.com/bogde/HX711 + bogde's library is released under a GNU GENERAL PUBLIC LICENSE + + The HX711 does one thing well: read load cells. The breakout board is compatible with any wheat-stone bridge + based load cell which should allow a user to measure everything from a few grams to tens of tons. + Arduino pin 2 -> HX711 CLK + 3 -> DAT + 5V -> VCC + GND -> GND + + The HX711 board can be powered from 2.7V to 5V so the Arduino 5V power should be fine. + +*/ + +#include "HX711.h" + +#define calibration_factor -10160.0 //This value is obtained using the SparkFun_HX711_Calibration sketch + +#define DOUT 3 +#define CLK 2 + +HX711 scale; + +void setup() { + Serial.begin(9600); + Serial.println("HX711 scale demo"); + + scale.begin(DOUT, CLK); + 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("Readings:"); +} + +void loop() { + //Serial.print("Reading: "); + //Serial.print(scale.get_units(), 1); //scale.get_units() returns a float + //Serial.print(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor + //Serial.println(); + Serial.println(scale.get_units(), 1); +}