Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
fedeele
Published © GPL3+

SmarTrash #ArduinoCloudGames

A smart IoT solution for trash monitoring and management in cities and homes. #ArduinoCloudGames

IntermediateFull instructions provided2,258
SmarTrash #ArduinoCloudGames

Things used in this project

Hardware components

Arduino Oplà IoT Kit
Arduino Oplà IoT Kit
×1
PIR Sensor, 7 m
PIR Sensor, 7 m
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino Web Editor
Arduino Web Editor
Arduino IoT Cloud
Arduino IoT Cloud

Hand tools and fabrication machines

Bin/Trash can

Story

Read more

Code

smarTrash_code

Arduino
This is the fully commented code for smarTrash.
** If you are going to implement this very code with the dashboarding proposed remember to change variable names accordingly :)
// -------------- smarTrash ---------------------

#include "thingProperties.h"
#include <Arduino_MKRIoTCarrier.h>
MKRIoTCarrier carrier;
#define TRIG_PIN 10 // trigger pin for Ultrasonic sensor
#define ECHO_PIN 4  // Echo pin for ultrasonic sensor


void setup(){
  float temperatura; // definition of temperature variable
  float soglia;      // definition of threshold variable
  float distanza;    // definition of ultrasonic measurement variable
  float umidita;     // definition of humidity variable 
  
  
  // Connectiion to Wi-Fi network 
  Serial.begin(9600);
  while (!Serial);
  initProperties();
// Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  //Get Cloud Info/errors , 0 (only errors) up to 4
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
 
  //Wait to get cloud connection to init the carrier
  while (ArduinoCloud.connected() != 1) {
    ArduinoCloud.update();
    delay(500);
  }
  
  //Carrier Initialization 
  carrier.begin();
  carrier.display.setRotation(0);
  delay(1500);
  
  
  // PinMode definition
  pinMode(TRIG_PIN, OUTPUT); //trigger pin in output
  pinMode(ECHO_PIN, INPUT); // echo pin in output
  digitalWrite(TRIG_PIN, LOW); // initialization of trigger pin to low 
  pinMode(2,INPUT); // pinMode def. for PIR motion sensor

}
void loop() {
  ArduinoCloud.update();
  // whenever the motion sensor detects a movement the measurement 
  // is paused, in order to avoid incorrect trash level measurements.
  // The serial port also prints that bin is open.
  
  if(digitalRead(2)==HIGH){
   Serial.print("The bin is open");
   Serial.print('\n');
  }
  
  // When the sensor does not detect any movement for a while (the "while"
  // is set via the manual regulation of one screw on the motion sensor), the 
  // measurement restarts. The measurements consists of a trash level,
  // temperature and humidity
  else{
    // Serial print on the state of the bin
    Serial.print("The bin is closed ");
    Serial.print('\n');
    // Distance measured by the ultrasonic sensor
    digitalWrite(TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG_PIN, LOW);
    unsigned long tempo = pulseIn(ECHO_PIN, HIGH);
    // Trash level (distanza) is measured in centimetres as the height of the bin (26.5cm)
    // - the distance measured between the ultrasonic sensor and the bin.
    distanza = (-(0.03438 * tempo / 2)+26.5);// 
    delay(1000);
    // Serial print for the trash level
    Serial.println("Trash Level: " + String(distanza) + "cm");
    Serial.print('\n');
    // Carrier measurement of temperature and serial print
    temperatura = carrier.Env.readTemperature(CELSIUS);
    Serial.print("Temperature: " +String(temperatura)+ "C"); 
    Serial.print('\n');
    // Carrier measurement of humidity and serial print
    umidita = carrier.Env.readHumidity();
    Serial.print("Humidity: " +String(umidita) +"%"); 
    Serial.print('\n');
    // A threshold adjustment is introduced for temperature variations, 
    // in case the temperature is too high the threshold is reduced to 15 cm.
    // Otherwise it is set to 20 cm
    // The goal of this is to reduce the chance of rotting in food waste,
    // it can be disactivated in case the bin is used for plastic.  
    if (temperatura>20){
      soglia=15;
    }
    else {
      soglia=20;
    }
    // In case the distance is above the defined threshold then the
    // carrier display turns red and a message is sent via the cloud 
    // that notifies that the bin is full.
    if(distanza>soglia){
      carrier.display.fillScreen(ST77XX_RED);
      notifica = "The bin is full! Please take out the trash";  
    }
    // In case the threshold is below the defined threshold then the
    // carrier display turns red and a message is sent via the cloud 
    // that notifies that the bin is no longer full.
    else{
      carrier.display.fillScreen(ST77XX_GREEN);
      notifica = "The bin is no longer full!"; 
    }
  }
  delay(2000);
  
  // All credits to DevotiLabs
  // feel free to use our code and project, and don't hesitate to tell
  // us what you end up doing with it ! 
  
  // Cheers,
  // Eleonora, Federico, Claudio 
  
  }

Credits

fedeele
0 projects • 0 followers

Comments