Skip to content

Commit

Permalink
(#1) Work for arduino sensor, load sensor, ultra sonic sensor servo f…
Browse files Browse the repository at this point in the history
…unctions done
  • Loading branch information
ayush7aryal authored Aug 6, 2022
1 parent cbe11f5 commit deececd
Showing 1 changed file with 96 additions and 16 deletions.
112 changes: 96 additions & 16 deletions arduino/mainCode/mainCode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#include <Arduino.h>
#include "HX711.h"

//for get and post request
#include <ESP8266HTTPClient.h>
Expand All @@ -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
Expand Down Expand Up @@ -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");
Expand All @@ -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) {
Expand All @@ -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");
Expand All @@ -154,11 +232,13 @@ void loop() {
}
Serial.println(postResponseCode);
Serial.println("it means open");
closeDustbin();
}

}else{
Serial.println(httpResponseCode);
}
}
http.end();

}else{
Expand Down

3 comments on commit deececd

@vercel
Copy link

@vercel vercel bot commented on deececd Aug 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kuhackfest-solbin-marketplace – ./blockchain/app

kuhackfest-solbin-marketplace-voidash.vercel.app
kuhackfest-solbin-marketplace-git-main-voidash.vercel.app
kuhackfest-solbin.vercel.app

@vercel
Copy link

@vercel vercel bot commented on deececd Aug 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

solbin – ./mobileFrontend/hackfestwebsite

solbin-voidash.vercel.app
solbin.vercel.app
solbin-git-main-voidash.vercel.app

@vercel
Copy link

@vercel vercel bot commented on deececd Aug 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kuhackfest-solbin-map – ./maps

kuhackfest-solbin-map-voidash.vercel.app
kuhackfest-solbin-map-git-main-voidash.vercel.app
kuhackfest-map.vercel.app

Please sign in to comment.