#include #include "HX711.h" #include #ifndef LED_BUILTIN #define LED_BUILTIN 2 // pin number is specific to your esp32 board #endif #define DefaultSSID "yourSSID" #define DefaultPassword "YourPassword" //see attached instructions to obtain these #define FIREBASE_HOST "yourhost" #define FIREBASE_AUTH "yourAUTH" //Firebase FirebaseData fbdo; byte macAddr[6]; String macString; //Beam Sensor #define beamEmitterPin 19 #define beamReceiverPin 18 #define beamReceiverPowerPin 21 #define beamSensorPresent false //Ultrasonic HC-SR04P sensor #define usPresent true #define usPowerPin 21 #define usEchoPin 18 #define usTriggerPin 5 #define distanceThresholdUpperLimit 100 //in cm #define distanceThresholdBottomLimit 2 //HX711 Pins const int LOADCELL_DOUT_PIN = 33; const int LOADCELL_SCK_PIN = 32; #define weightThreshold 7000 //in grams. If measured weight exceeds this value the bin is considered full HX711 scale; float loadCellCalibration=21.4; //Upload HX711calibration.ino to find loadCellCalibration and zeroFactor int zeroFactor=660228; //removes the need to tare String ssid=DefaultSSID; String password=DefaultPassword; const int SerialBaud=115200; const bool debug=true; void setup() { /********************ESP initialize********************/ delay(1000); //wait for everything to settle Serial.begin(SerialBaud); connectWiFi(false); if(debug) { Serial.print("MAC:"); Serial.println(macString); } /********************Scale initialize*******************/ //bscale.begin(LOADCELL_DOUT_PIN,LOADCELL_SCK_PIN); calibrateScale(LOADCELL_DOUT_PIN,LOADCELL_SCK_PIN,loadCellCalibration,zeroFactor); /********************BeamSensor initialize**************/ if(beamSensorPresent==true){ //if beamSensor exists pinMode(beamEmitterPin,OUTPUT); pinMode(beamReceiverPowerPin,OUTPUT); pinMode(beamReceiverPin,INPUT); digitalWrite(beamReceiverPowerPin,LOW); //turn the receiver off digitalWrite(beamEmitterPin,LOW); //turn the emmiter off } /********************HC-SR04P initialize**************/ else if(usPresent==true){ pinMode(usEchoPin,INPUT); pinMode(usTriggerPin,OUTPUT); pinMode(usPowerPin,OUTPUT); } /********************Firebase initialize****************/ Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); //Enable automatic recconections Firebase.reconnectWiFi(true); } void loop(){ float weight=readScaleData2(true); if(weight>=weightThreshold) binIsFull(true); if(beamSensorPresent){ if(!checkBeam(beamEmitterPin,beamReceiverPin,beamReceiverPowerPin)) binIsFull(true); } if(usPresent){ //if(getDistance(usPowerPin,usTriggerPin,usEchoPin,100)distanceThresholdUpperLimit) binIsFull(true); //insert timer here to run in 60sec and 120sec to check if it is still occupied } delay(500); } bool setFirebaseValue(String path, String value){ if(Firebase.setString(fbdo, path, value)) { //Success return true; }else{ //Failed?, get the error reason from fbdo return fbdo.errorReason(); } } String getFirebaseValue(String path){ if(Firebase.getString(fbdo, path)) { //Success Serial.print("Get string data success, string = "); Serial.println(fbdo.stringData()); }else{ //Failed?, get the error reason from fbdo Serial.print("Error in getString, "); Serial.println(fbdo.errorReason()); } } void binIsFull(bool debug){ //send command to server if(debug)Serial.println("Sending Value to Firebase"); setFirebaseValue("/"+macString+"/isFull/","true"); pinMode(LED_BUILTIN,OUTPUT); digitalWrite(LED_BUILTIN,HIGH); delay(1000); digitalWrite(LED_BUILTIN,LOW); return; } void calibrateScale(int DOUT, int SCK, float loadCellCalibration, int zeroFactor){ scale.begin(DOUT, SCK); scale.set_scale(loadCellCalibration); // this value is obtained by calibrating the scale with known weights; see the README for details //scale.tare(); // reset the scale to 0 scale.set_offset(zeroFactor); } bool checkBeam(int emitterPin,int receiverPin,int receiverPowerPin){ int states=0; digitalWrite(receiverPowerPin,HIGH); //turn the receiver on delay(100); for(int i=0;i<10;++i){ digitalWrite(emitterPin,HIGH); //digitalWrite(emmitterPowerPin,HIGH); //digitalWrite(receiverPowerPin,HIGH); delay(10); //wait for beam to settle states+=digitalRead(receiverPin); digitalWrite(emitterPin,LOW); delay(100); //wait 1 sec to see if beam is still cut } digitalWrite(receiverPowerPin,LOW); //turn the receiver off to reduce power consumption if(states==0) return 0;//all beams are broken else return 1; } float readScaleData(bool debug){ scale.set_scale(loadCellCalibration); float measurment=scale.get_units(); if(debug) { Serial.print("Weight:"); Serial.println(measurment); } return measurment; } float readScaleData2(bool debug){ //scale.set_scale(loadCellCalibration); scale.power_up(); float measurment=scale.get_units(10); //average 10 readings if(debug) { Serial.print("Weight:"); Serial.println(measurment); } scale.power_down(); return measurment; } int connectWiFi(bool debug){ if(WiFi.status() == WL_CONNECTED) { if(debug) Serial.print("ESP32: Connected\n"); return 1; } int timecounter=0; WiFi.begin(DefaultSSID,DefaultPassword); if (debug) Serial.print("ESP32: Connecting"); while (WiFi.status() != WL_CONNECTED) { //Check for the connection delay(2000); timecounter++; if (debug) Serial.print("."); if(timecounter>10){ timecounter=0; if (debug) Serial.print("ESP32: Could not connect to network. Please check your credentials\n"); return 0; } } //Serial.println(); if(debug) Serial.print("Connected\n"); //WiFi.macAddress(macAddr); //macString=(char*)macAddr; macString=WiFi.macAddress(); return 1; } float getDistance(int PowerPin, int TriggerPin,int EchoPin, unsigned long timeout){ // Hold the trigger pin high for at least 10 us digitalWrite(PowerPin,HIGH); //enable power delay(100); //wait for power on unsigned long t1 = micros(),t2; digitalWrite(TriggerPin, HIGH); delayMicroseconds(10); digitalWrite(TriggerPin, LOW); // Wait for pulse on echo pin while (digitalRead(EchoPin) == 0 && (micros()-t1)