#include #include #include #include #include #include //Pin for HX711: const int LOADCELL_DOUT_PIN = 18; const int LOADCELL_SCK_PIN = 17; //HX711 constructor: HX711_ADC LoadCell(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); //DS18B20 Pin: const int SensorDataPin = 5; // ESP32 pin GIOP21 connected to DS18B20 sensor's DQ pin //DS18B20 constructor: OneWire oneWire(SensorDataPin); DallasTemperature sensors(&oneWire); // UUIDs for scale service, and mass, buttons characteristics BLEService scaleService("1eb9bb89-186e-4d2a-a204-346da73c061c"); BLEFloatCharacteristic massChar("5977b71a-a58c-40d2-85a4-34071043d9ca", BLERead | BLENotify); // this reads to the main display in app BLEIntCharacteristic buttonChar("a8f2d9f3-c93a-4479-8208-7287262eacf6", BLERead | BLEWrite | BLENotify); // this captures buttons pressed on the app BLEFloatCharacteristic tempChar("e3223119-9445-4e96-a4a1-85358c4046a2", BLERead | BLENotify); // UUIDs for temp service and degree characteristics //BLEService tempService("beb5483e-36e1-4688-b7f5-ea07361b26a8"); // Variables for calibration and t for timer const int calVal_eepromAdress = 0; unsigned long t = 0; // temp A_A unsigned long previousMillis = 0; const long interval = 5000; //Create variable to send to app for mass float bleMass = 0.00; // starting BLE mass value //Create variable to send to app for temp float temp = 0.0; // temperature in Celsius void setup() { // put your setup code here, to run once: Serial.begin(115200); LoadCell.begin(); sensors.begin(); // initialize the DS18B20 sensor Serial.println(); Serial.println("starting..."); float calibrationValue; calibrationValue = -460.46; #if defined(ESP8266) || defined(ESP32) //EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom #endif //EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom unsigned long stabilizingtime = 2000; boolean _tare = true; LoadCell.start(stabilizingtime, _tare); if (LoadCell.getTareTimeoutFlag()) { Serial.println("Timeout, check MCU>HX711 wiring and pin designations"); while (1) //creates an endless loop ; } else { LoadCell.setCalFactor(calibrationValue); Serial.println("Startup is complete"); } // begin BLE initialization if (!BLE.begin()) { Serial.println("BLE failed to start!"); while (1) ; } // Set BLE device name (loadcell) BLE.setLocalName("Frensh_Press_coffee"); BLE.setAdvertisedService(scaleService); // add the service UUID scaleService.addCharacteristic(massChar); // add the mass characteristic scaleService.addCharacteristic(buttonChar); // add the buttons characteristic scaleService.addCharacteristic(tempChar); BLE.addService(scaleService); // add the service massChar.writeValue(bleMass); // set initial value for mass tempChar.writeValue(0.0); // start adverising BLE.advertise(); Serial.println("Bluetooth® device active, waiting for connections..."); } void loop() { // put your main code here, to run repeatedly: // wait for bluetooth low energy central BLEDevice central = BLE.central(); static boolean newDataReady = 0; const int serialPrintInterval = 15; // increase value to show down serial print/BLE activitiy // check for new data/ start next conversion if (LoadCell.update()) newDataReady = true; // Scale Read float newMass = bleMass; if (newDataReady) { if (millis() > t + serialPrintInterval) { float i = LoadCell.getData(); newMass = i; //Serial.print("load_cell output val: "); // Serial.print(i); newDataReady = 0; t = millis(); if (!central.connected()) { Serial.println("No Connection."); } if (central.connected()) { Serial.println("BLE Connected."); if (newMass != bleMass) { bleMass = newMass; massChar.writeValue(bleMass); } } } } if (central.connected()) { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; sensors.requestTemperatures(); temp = sensors.getTempCByIndex(0); tempChar.writeValue(temp); Serial.println(temp); } } // receive command from BT app, send '2' to initiate tare operation. if (buttonChar.written()) { int32_t k = 0; buttonChar.readValue(k); Serial.print("Received Button val: "); Serial.println(k); if (k == 2) { LoadCell.tareNoDelay(); if (LoadCell.getTareStatus() == true) { Serial.println("Tare complete"); } } } }