7
7
#include < Servo.h>
8
8
#include < LiquidCrystal_I2C.h>
9
9
#include < Wire.h>
10
-
10
+ #include < Arduino.h>
11
+ #include " HX711.h"
11
12
12
13
// for get and post request
13
14
#include < ESP8266HTTPClient.h>
17
18
const char * ssid = " TP-Link_6DE6" ;
18
19
const char * password = " AAAAPH69" ;
19
20
21
+ const int LOADCELL_DOUT_PIN = 12 ;
22
+ const int LOADCELL_SCK_PIN = 13 ;
23
+
24
+ HX711 scale;
25
+
26
+
27
+ int duration;
28
+ int distance;
20
29
String serverName = " http://solanabin.pythonanywhere.com" ;
21
30
String uid = " 1a77d81f-4ed4-45f0-bdf6-c8980cbfac1e" ;
22
31
// for nodemcu pins
@@ -53,18 +62,42 @@ String uid = "1a77d81f-4ed4-45f0-bdf6-c8980cbfac1e";
53
62
54
63
Servo servo1;
55
64
Servo servo2;
65
+ const int trigP = 0 ; // D3 Or GPIO-2 of nodemcu
66
+ const int echoP = 2 ; // D4 Or GPIO-0 of nodemcu
67
+ int posn=0 ;
56
68
int angle;
57
69
LiquidCrystal_I2C lcd (0x3F , 16 , 2 );
58
70
59
71
unsigned long lastTime = 0 ;
60
72
// for nodemcu
61
73
74
+ void ultrasonicSetup () {
75
+ pinMode (trigP, OUTPUT);
76
+ pinMode (echoP, INPUT);
77
+ }
78
+
62
79
void lcdSetup () {
63
80
64
81
lcd.begin ();
65
82
lcd.home ();
66
83
lcd.print (" Hello, NodeMCU" );
67
84
}
85
+
86
+ int getDistanceFromUltrasonic () {
87
+
88
+ digitalWrite (trigP, LOW); // Makes trigPin low
89
+ delayMicroseconds (2 ); // 2 micro second delay
90
+
91
+ digitalWrite (trigP, HIGH); // tigPin high
92
+ delayMicroseconds (10 ); // trigPin high for 10 micro seconds
93
+ digitalWrite (trigP, LOW); // trigPin low
94
+
95
+ duration = pulseIn (echoP, HIGH); // Read echo pin, time in microseconds
96
+ distance= duration*0.034 /2 ; // Calculating actual/real distance
97
+ return distance;
98
+ }
99
+
100
+
68
101
void setupWifi () {
69
102
WiFi.begin (ssid,password);
70
103
Serial.println (" Connecting" );
@@ -85,32 +118,61 @@ void attachServo() {
85
118
}
86
119
87
120
void openDustbin () {
121
+ Serial.println (" opening Dustbin" );
88
122
int posn = 180 ;
89
123
for (posn = 180 ; posn >=90 ; posn -= 1 ) // goes from 0 degrees to 180 degrees
90
124
{ // in steps of 1 degree
91
125
servo1.write (posn);
92
126
servo2.write (180 -posn);// tell servo to go to position in variable 'pos'
93
- delay (10 ); // waits 10ms for the servo to reach the position
127
+ delay (5 ); // waits 10ms for the servo to reach the position
94
128
}
95
129
}
96
130
97
131
void closeDustbin (){
132
+ Serial.println (" closing dustbin" );
98
133
int posn = 90 ;
99
134
for (posn = 90 ; posn <=180 ; posn += 1 ) // goes from 0 degrees to 180 degrees
100
135
{ // in steps of 1 degree
101
136
servo1.write (posn);
102
137
servo2.write (180 -posn);// tell servo to go to position in variable 'pos'
103
- delay (10 ); // waits 10ms for the servo to reach the position
138
+ delay (5 ); // waits 10ms for the servo to reach the position
104
139
}
105
140
}
106
-
141
+
142
+
107
143
void setup () {
108
- Serial.begin (115200 );
144
+ scale.begin (LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
145
+ ultrasonicSetup ();
109
146
setupWifi ();
110
- // attachServo();
147
+ attachServo ();
148
+ Serial.begin (115200 );
149
+
150
+
151
+
152
+ scale.set_scale (2.0052 );
153
+
154
+ // this value is obtained by calibrating the scale with known weights; see the README for details
111
155
112
156
}
113
157
158
+ // void loop() {
159
+ // delay(5000);
160
+ // openDustbin();
161
+ // delay(5000);
162
+ // closeDustbin();
163
+ // delay(1000);
164
+ // int i = getDistanceFromUltrasonic();
165
+ // delay(1000);
166
+ // Serial.println(i);
167
+ // Serial.println("data from scale");
168
+ //
169
+ // }
170
+
171
+
172
+
173
+
174
+
175
+
114
176
// the loop function runs over and over again forever
115
177
void loop () {
116
178
if ((millis () - lastTime) > 5000 ) {
@@ -130,18 +192,34 @@ void loop() {
130
192
131
193
// if we want to open the dustbin
132
194
// openDustbin();
133
-
195
+ bool weightAdded = false ;
196
+ float m = 0 ;
197
+ int b_height = 0 ;
198
+ for (int i =0 ; i <= 12 ; i++) {
199
+ float k = scale.read_average (20 ); // get load sensor value
200
+ if (abs (k-m) > 300 ) {
201
+ // weight was added
202
+ weightAdded = true ;
203
+ break ;
204
+ }
205
+ delay (1000 );
206
+ int height = getDistanceFromUltrasonic (); // get the height using ultrasonic sensor
134
207
135
- // for(int i =0; i <= 12; i++) {
136
- //
137
- // // get the load sensor value
138
- // // get the height using ultrasonic sensor
139
- // // show it on our LCD
140
- // delay(5000);
141
- //
142
- // }
208
+ if (abs (height-b_height) > 3 ) {
209
+ delay (100 );
210
+ height = getDistanceFromUltrasonic ();
211
+ if (abs (height-b_height) > 3 ) {
212
+ weightAdded = true ;
213
+ break ;
214
+ }
215
+ }
216
+ m = k;
217
+ b_height = height;
218
+ delay (5000 );
219
+ }
220
+ if (weightAdded) {
143
221
http.end ();
144
-
222
+
145
223
String postServerPath = serverName + " /unit_value_history/" + uid + " /" ;
146
224
http.begin (client,postServerPath);
147
225
http.addHeader (" Content-Type" ," application-json" );
@@ -154,11 +232,13 @@ void loop() {
154
232
}
155
233
Serial.println (postResponseCode);
156
234
Serial.println (" it means open" );
235
+ closeDustbin ();
157
236
}
158
237
159
238
}else {
160
239
Serial.println (httpResponseCode);
161
240
}
241
+ }
162
242
http.end ();
163
243
164
244
}else {
0 commit comments