Skip to content

Commit deececd

Browse files
authored
(#1) Work for arduino sensor, load sensor, ultra sonic sensor servo functions done
1 parent cbe11f5 commit deececd

File tree

1 file changed

+96
-16
lines changed

1 file changed

+96
-16
lines changed

arduino/mainCode/mainCode.ino

+96-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#include <Servo.h>
88
#include <LiquidCrystal_I2C.h>
99
#include <Wire.h>
10-
10+
#include <Arduino.h>
11+
#include "HX711.h"
1112

1213
//for get and post request
1314
#include <ESP8266HTTPClient.h>
@@ -17,6 +18,14 @@
1718
const char* ssid = "TP-Link_6DE6";
1819
const char* password = "AAAAPH69";
1920

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;
2029
String serverName = "http://solanabin.pythonanywhere.com";
2130
String uid = "1a77d81f-4ed4-45f0-bdf6-c8980cbfac1e";
2231
// for nodemcu pins
@@ -53,18 +62,42 @@ String uid = "1a77d81f-4ed4-45f0-bdf6-c8980cbfac1e";
5362

5463
Servo servo1;
5564
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;
5668
int angle;
5769
LiquidCrystal_I2C lcd(0x3F, 16, 2);
5870

5971
unsigned long lastTime = 0;
6072
// for nodemcu
6173

74+
void ultrasonicSetup() {
75+
pinMode(trigP, OUTPUT);
76+
pinMode(echoP, INPUT);
77+
}
78+
6279
void lcdSetup() {
6380

6481
lcd.begin();
6582
lcd.home();
6683
lcd.print("Hello, NodeMCU");
6784
}
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+
68101
void setupWifi() {
69102
WiFi.begin(ssid,password);
70103
Serial.println("Connecting");
@@ -85,32 +118,61 @@ void attachServo() {
85118
}
86119

87120
void openDustbin() {
121+
Serial.println("opening Dustbin");
88122
int posn = 180;
89123
for (posn = 180; posn >=90; posn -= 1) // goes from 0 degrees to 180 degrees
90124
{ // in steps of 1 degree
91125
servo1.write (posn);
92126
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
94128
}
95129
}
96130

97131
void closeDustbin(){
132+
Serial.println("closing dustbin");
98133
int posn = 90;
99134
for (posn = 90; posn <=180; posn += 1) // goes from 0 degrees to 180 degrees
100135
{ // in steps of 1 degree
101136
servo1.write (posn);
102137
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
104139
}
105140
}
106-
141+
142+
107143
void setup() {
108-
Serial.begin(115200);
144+
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
145+
ultrasonicSetup();
109146
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
111155

112156
}
113157

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+
114176
// the loop function runs over and over again forever
115177
void loop() {
116178
if((millis() - lastTime) > 5000) {
@@ -130,18 +192,34 @@ void loop() {
130192

131193
// if we want to open the dustbin
132194
// 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
134207

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) {
143221
http.end();
144-
222+
145223
String postServerPath = serverName + "/unit_value_history/" + uid + "/";
146224
http.begin(client,postServerPath);
147225
http.addHeader("Content-Type","application-json");
@@ -154,11 +232,13 @@ void loop() {
154232
}
155233
Serial.println(postResponseCode);
156234
Serial.println("it means open");
235+
closeDustbin();
157236
}
158237

159238
}else{
160239
Serial.println(httpResponseCode);
161240
}
241+
}
162242
http.end();
163243

164244
}else{

0 commit comments

Comments
 (0)