/* Test Esp32 BlueTooth Connection with Mit App inventor Android App HX711 library for Arduino - example file https://github.com/bogde/HX711 */ #include #include "HX711.h" #include "soc/rtc.h" #include "BluetoothSerial.h" #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it #endif BluetoothSerial SerialBT; // HX711 circuit wiring const int LOADCELL_DOUT_PIN_01 = 16; const int LOADCELL_SCK_PIN_01 = 4; HX711 scale; bool firstTimeRun_Cell01 = false ; // Check if it is first time that the program runs in order to run some staff for loadcell calibration unsigned long cell01 = 0; int Sellection = 0; // incoming from the android app how many sensors will run String message = ""; char incomingChar; const int analogInPin_01 = 25; // Analog input pin that the potentiometer is attached to const int analogInPin_02 = 26; // Analog input pin that the potentiometer is attached to const int analogInPin_03 = 27; // Analog input pin that the potentiometer is attached to int sensorValue_01 = 0; // value read from the pot int sensorValue_02 = 0; // value read from the pot int sensorValue_03 = 0; // value read from the pot void setup() { Serial.begin(115200); rtc_clk_cpu_freq_set(RTC_CPU_FREQ_80M); // They saying that we must run Esp32 to lower speed in order to work with HX711 sensor Serial.println("HX711 and other sensors"); SerialBT.begin("Esp32_Sensors"); //Bluetooth device name Serial.println("The device started, now you can pair it with bluetooth!"); } void loop() { while (Sellection == 0) { Serial.println("waiting for Sensor numbers"); if (SerialBT.available()) { char incomingChar = SerialBT.read(); if (incomingChar != '\n') { message += String(incomingChar); } else { message = ""; } Serial.write(incomingChar); } // Check received message and control output accordingly //////////////////////////////////////////////////////////////////// if (message == "1" ) { //Voice Command to ON Relay 01 Sellection = 1; } if (message == "2" ) { //Voice Command to ON Relay 01 Sellection = 2; } if (message == "3" ) { //Voice Command to ON Relay 01 Sellection = 3; } if (message == "4" ) { //Voice Command to ON Relay 01 Sellection = 4; } } if (Sellection == 1) { Loadcell(); Bluetooth_send(); } if (Sellection == 2) { Loadcell(); Potentiometer_01; Bluetooth_send(); } if (Sellection == 3) { Loadcell(); Potentiometer_01; Potentiometer_02(); Bluetooth_send(); } if (Sellection == 4) { Loadcell(); Potentiometer_01; Potentiometer_02(); Potentiometer_03(); Bluetooth_send(); } } void Loadcell() { scale.begin(LOADCELL_DOUT_PIN_01, LOADCELL_SCK_PIN_01); if (firstTimeRun_Cell01 == false) { Serial.println("Initializing the scale"); Serial.println("Before setting up the scale:"); Serial.print("read: \t\t"); Serial.println(scale.read()); // print a raw reading from the ADC Serial.print("read average: \t\t"); Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC Serial.print("get value: \t\t"); Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet) Serial.print("get units: \t\t"); Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided by the SCALE parameter (not set yet) scale.set_scale(-471.497); // this value is obtained by calibrating the scale with known weights; see the README for details scale.tare(); // reset the scale to 0 Serial.println("After setting up the scale:"); Serial.print("read: \t\t"); Serial.println(scale.read()); // print a raw reading from the ADC Serial.print("read average: \t\t"); Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC Serial.print("get value: \t\t"); Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare() Serial.print("get units: \t\t"); Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided // by the SCALE parameter set with set_scale firstTimeRun_Cell01 = true; } //Serial.print("one reading:\t"); //Serial.print(scale.get_units(), 1); //Serial.print("\t| average:\t"); //Serial.println(scale.get_units(10), 5); cell01 = (scale.get_units(10)); cell01 = random(10, 20); } void Potentiometer_01() { sensorValue_01 = analogRead(analogInPin_01); sensorValue_01 = random(190, 200); } void Potentiometer_02() { sensorValue_02 = analogRead(analogInPin_02); } void Potentiometer_03() { sensorValue_03 = analogRead(analogInPin_03); } void Bluetooth_send () { if (SerialBT.available()) { if (Sellection == 1) { SerialBT.print(cell01); SerialBT.println(); delay(50); } if (Sellection == 2) { SerialBT.print(cell01); SerialBT.print("|"); SerialBT.print(sensorValue_01); SerialBT.println(); delay(50); } if (Sellection == 3) { SerialBT.print(cell01); SerialBT.print("|"); SerialBT.print(sensorValue_01); SerialBT.print("|"); SerialBT.print(sensorValue_02); SerialBT.println(); delay(50); } if (Sellection == 4) { SerialBT.print(cell01); SerialBT.print("|"); SerialBT.print(sensorValue_01); SerialBT.print("|"); SerialBT.print(sensorValue_02); SerialBT.print("|"); SerialBT.println(sensorValue_03); SerialBT.println(); delay(50); } } Serial.print("Load Cell:\t"); Serial.print(cell01); Serial.print("\t| Pot 01:\t"); Serial.print(sensorValue_01); Serial.print("\t| Pot 02:\t"); Serial.print(sensorValue_02); Serial.print("\t| Pot 03:\t"); Serial.println(sensorValue_03); }