From deececd3c02aab63d1cb1a60965e260437de2f2c Mon Sep 17 00:00:00 2001 From: Ayush Aryal <58439228+ayush7aryal@users.noreply.github.com> Date: Sat, 6 Aug 2022 21:43:49 +0545 Subject: [PATCH] (#1) Work for arduino sensor, load sensor, ultra sonic sensor servo functions done --- arduino/mainCode/mainCode.ino | 112 +++++++++++++++++++++++++++++----- 1 file changed, 96 insertions(+), 16 deletions(-) diff --git a/arduino/mainCode/mainCode.ino b/arduino/mainCode/mainCode.ino index dd62270d..f4d3a92f 100644 --- a/arduino/mainCode/mainCode.ino +++ b/arduino/mainCode/mainCode.ino @@ -7,7 +7,8 @@ #include #include #include - +#include +#include "HX711.h" //for get and post request #include @@ -17,6 +18,14 @@ const char* ssid = "TP-Link_6DE6"; const char* password = "AAAAPH69"; +const int LOADCELL_DOUT_PIN = 12; +const int LOADCELL_SCK_PIN = 13; + +HX711 scale; + + +int duration; +int distance; String serverName = "http://solanabin.pythonanywhere.com"; String uid = "1a77d81f-4ed4-45f0-bdf6-c8980cbfac1e"; // for nodemcu pins @@ -53,18 +62,42 @@ String uid = "1a77d81f-4ed4-45f0-bdf6-c8980cbfac1e"; Servo servo1; Servo servo2; + const int trigP = 0; //D3 Or GPIO-2 of nodemcu + const int echoP = 2; //D4 Or GPIO-0 of nodemcu + int posn=0; int angle; LiquidCrystal_I2C lcd(0x3F, 16, 2); unsigned long lastTime = 0; // for nodemcu +void ultrasonicSetup() { + pinMode(trigP, OUTPUT); + pinMode(echoP, INPUT); +} + void lcdSetup() { lcd.begin(); lcd.home(); lcd.print("Hello, NodeMCU"); } + +int getDistanceFromUltrasonic() { + +digitalWrite(trigP, LOW); // Makes trigPin low +delayMicroseconds(2); // 2 micro second delay + +digitalWrite(trigP, HIGH); // tigPin high +delayMicroseconds(10); // trigPin high for 10 micro seconds +digitalWrite(trigP, LOW); // trigPin low + +duration = pulseIn(echoP, HIGH); //Read echo pin, time in microseconds +distance= duration*0.034/2; //Calculating actual/real distance +return distance; +} + + void setupWifi() { WiFi.begin(ssid,password); Serial.println("Connecting"); @@ -85,32 +118,61 @@ void attachServo() { } void openDustbin() { + Serial.println("opening Dustbin"); int posn = 180; for (posn = 180; posn >=90; posn -= 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree servo1.write (posn); servo2.write(180-posn);// tell servo to go to position in variable 'pos' - delay (10); // waits 10ms for the servo to reach the position + delay (5); // waits 10ms for the servo to reach the position } } void closeDustbin(){ + Serial.println("closing dustbin"); int posn = 90; for (posn = 90; posn <=180; posn += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree servo1.write (posn); servo2.write(180-posn);// tell servo to go to position in variable 'pos' - delay (10); // waits 10ms for the servo to reach the position + delay (5); // waits 10ms for the servo to reach the position } } - + + void setup() { - Serial.begin(115200); + scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); + ultrasonicSetup(); setupWifi(); - //attachServo(); + attachServo(); + Serial.begin(115200); + + + + scale.set_scale(2.0052); + + // this value is obtained by calibrating the scale with known weights; see the README for details } +//void loop() { +// delay(5000); +// openDustbin(); +// delay(5000); +// closeDustbin(); +// delay(1000); +// int i = getDistanceFromUltrasonic(); +// delay(1000); +// Serial.println(i); +// Serial.println("data from scale"); +// +//} + + + + + + // the loop function runs over and over again forever void loop() { if((millis() - lastTime) > 5000) { @@ -130,18 +192,34 @@ void loop() { // if we want to open the dustbin // openDustbin(); - + bool weightAdded = false; + float m = 0; + int b_height = 0; + for(int i =0; i <= 12; i++) { + float k = scale.read_average(20); //get load sensor value + if(abs(k-m) > 300) { + // weight was added + weightAdded = true; + break; + } + delay(1000); + int height = getDistanceFromUltrasonic(); // get the height using ultrasonic sensor -// for(int i =0; i <= 12; i++) { -// -// // get the load sensor value -// // get the height using ultrasonic sensor -// // show it on our LCD -// delay(5000); -// -// } + if(abs(height-b_height) > 3) { + delay(100); + height = getDistanceFromUltrasonic(); + if(abs(height-b_height) > 3) { + weightAdded = true; + break; + } + } + m = k; + b_height = height; + delay(5000); + } + if(weightAdded) { http.end(); - + String postServerPath = serverName + "/unit_value_history/" + uid + "/"; http.begin(client,postServerPath); http.addHeader("Content-Type","application-json"); @@ -154,11 +232,13 @@ void loop() { } Serial.println(postResponseCode); Serial.println("it means open"); + closeDustbin(); } }else{ Serial.println(httpResponseCode); } + } http.end(); }else{