Untitled

 avatar
unknown
plain_text
2 years ago
7.5 kB
8
Indexable
#if defined(ESP32)
#include <WiFi.h>
#include <FirebaseESP32.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#endif
#include <HX711_ADC.h>

// Provide the RTDB payload printing info and other helper functions.
#include <addons/RTDBHelper.h>

//Providing firebase credentials (connects node mcu with firebase)
#define DATABASE_URL  "weighing-scale-fcc38.firebaseio.com"
#define FIREBASE_AUTH "E06w5PKTXgZ96DdMYUffNiLn026DyJE14qPTmhpM"
//6596.25
//Providing wifi name and password
#define WIFI_SSID "GP24-Net-Hotspot"
#define WIFI_PASSWORD "2444666668888888"

/* 3. Define the Firebase Data object */
FirebaseData fbdo;

/* 4, Define the FirebaseAuth data for authentication data */
FirebaseAuth auth;

/* Define the FirebaseConfig data for config data */
FirebaseConfig config;

//pins:
const int HX711_dout = D2; //mcu > HX711 dout pin
const int HX711_sck = D3; //mcu > HX711 sck pin

//initializing variables
int calibrate_button4,mass_placed4, reset_button4,terminate;
long t;

//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);

void setup() {

  Serial.begin(115200); delay(10);

   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    Serial.print("Connecting to Wi-Fi");
    while (WiFi.status() != WL_CONNECTED)
    {
        Serial.print(".");
        delay(300);
    }
    Serial.println();
    Serial.print("Connected with IP: ");
    Serial.println(WiFi.localIP());
    Serial.println();

    Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);


    /* Assign the certificate file (optional) */
    // config.cert.file = "/cert.cer";
    // config.cert.file_storage = StorageType::FLASH;

    /* Assign the database URL(required) */
    config.database_url = DATABASE_URL;

    config.signer.test_mode = true;
  
    Firebase.reconnectWiFi(true);

    /* Initialize the library with the Firebase authen and config */
    Firebase.begin(&config, &auth);


//Initializing some variables in firebase database to zero
Firebase.setInt(fbdo,"calibrate_button4",0);
Firebase.setInt(fbdo,"reset_complete4",0);
Firebase.setInt(fbdo,"calibration_end4",0);
Firebase.setInt(fbdo,"mass_placed4",0);
Firebase.setInt(fbdo,"reset_button4",0);
Firebase.setInt(fbdo,"terminate4",0);
Firebase.setFloat(fbdo,"rear_right_weight",0);

Serial.println();
Serial.println("Starting...");

LoadCell.begin();
long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
boolean _tare = false; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag() || LoadCell.getSignalTimeoutFlag()) {
    Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
    while (1);
  }
else {
    LoadCell.setCalFactor(1.0); // user set calibration value (float), initial value 1.0 may be used for this sketch
    Serial.println("Startup is complete");
  }
while (!LoadCell.update());
Serial.println("waiting for calibrate button to press");

calibrate_button4 = Firebase.getInt(fbdo,"calibrate_button4");
while(!calibrate_button4)
  {
    calibrate_button4 = Firebase.getInt(fbdo,"calibrate_button4");
    }
    Serial.println("Calibrate Button is read as 1");
calibrate(); //start calibration procedure
Firebase.setInt(fbdo,"calibrate_button4",0);
Firebase.setInt(fbdo,"calibration_end4",0);
Serial.println("Calibration done");


  
}

void loop() {
  static boolean newDataReady = 0;
  const int serialPrintInterval = 0; //increase value to slow down serial print activity

  // check for new data/start next conversion:
  if (LoadCell.update()) newDataReady = true;

  // get smoothed value from the dataset:
  if (newDataReady) {
    if (millis() > t + serialPrintInterval) {
      float i = LoadCell.getData();
      Serial.print("Load_cell output val: ");
      Serial.println(i);
      Firebase.setFloat(fbdo,"rear_right_weight",i);
      newDataReady = 0;
      t = millis();
    }
  }

  terminate = Firebase.getInt(fbdo,"terminate4");

  reset_button4 = Firebase.getInt(fbdo,"reset_button4");
  if(reset_button4 == 1)
  {
    Firebase.setInt(fbdo,"reset_button4",0);
    LoadCell.tareNoDelay();
    
    } 
    if (LoadCell.getTareStatus() == true) {
    Serial.println("Tare complete");
  }
  


  calibrate_button4 = Firebase.getInt(fbdo,"calibrate_button4");
  if(calibrate_button4 ==1)
  {
    calibrate();
    Firebase.setInt(fbdo,"calibrate_button4",0);
    }

    if(terminate == 1)
    terminate1();
   
    
}
void calibrate() {
  reset_button4=0;
  Serial.println("***");
  Serial.println("Start calibration:");
  Serial.println("Place the load cell on a level stable surface.");
  Serial.println("Remove any load applied to the load cell and then press ok from the application.");

// Reset the value to zero

 Serial.println("Waiting for ok button to press.");
 reset_button4 = Firebase.getInt(fbdo,"reset_button4");
  while(!reset_button4)
  {
    reset_button4 = Firebase.getInt(fbdo,"reset_button4");
    }
   Serial.println("OK button is read as one");
     
    if (reset_button4 == 1) {
     LoadCell.tareNoDelay();
      }
    
    if (LoadCell.getTareStatus() == true) {
      Serial.println("Tare complete");
     
    }
    Firebase.setInt(fbdo,"reset_button4",0);
    Firebase.setInt(fbdo,"reset_complete4",1);
      
    Serial.println("Now, place your 50gm mass on the loadcell and press ok button.");

   mass_placed4 = Firebase.getInt(fbdo,"mass_placed4");
  while(!mass_placed4)
  {
    mass_placed4 = Firebase.getInt(fbdo,"mass_placed4");
    }
  
  Firebase.setInt(fbdo,"mass_placed4",0);
  
  float known_mass = 1.0;
  LoadCell.update();
  LoadCell.refreshDataSet(); //refresh the dataset to be sure that the known mass is measured correct
  float newCalibrationValue = LoadCell.getNewCalibration(known_mass); //get the new calibration value

Serial.println(newCalibrationValue);
  Serial.println("End calibration");
  Firebase.setInt(fbdo,"calibration_end4",1);
  Serial.println("***");
}
void terminate1()
{
  //Initializing some variables in firebase database to zero
Firebase.setInt(fbdo,"calibrate_button4",0);
Firebase.setInt(fbdo,"reset_complete4",0);
Firebase.setInt(fbdo,"calibration_end4",0);
Firebase.setInt(fbdo,"mass_placed4",0);
Firebase.setInt(fbdo,"reset_button4",0);
Firebase.setInt(fbdo,"terminate4",0);
Firebase.setFloat(fbdo,"rear_right_weight",0);

Serial.begin(57600); delay(10);
Serial.println();
Serial.println("Starting...");

LoadCell.begin();
long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
boolean _tare = false; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag() || LoadCell.getSignalTimeoutFlag()) {
    Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
    while (1);
  }
else {
    LoadCell.setCalFactor(1.0); // user set calibration value (float), initial value 1.0 may be used for this sketch
    Serial.println("Startup is complete");
  }
while (!LoadCell.update());
Serial.println("waiting for calibrate button to press");

calibrate_button4 = Firebase.getInt(fbdo,"calibrate_button4");
while(!calibrate_button4)
  {
    calibrate_button4 = Firebase.getInt(fbdo,"calibrate_button4");
    }
    Serial.println("Calibrate Button is read as 1");
calibrate(); //start calibration procedure
Firebase.setInt(fbdo,"calibrate_button4",0);
Firebase.setInt(fbdo,"calibration_end4",0);

 }
Editor is loading...