Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Sashrika Das
Published © GPL3+

Smart Trash Buddy

Community trash pickups can be optimized better using Smart Trash Buddy

IntermediateFull instructions providedOver 1 day797

Things used in this project

Hardware components

AVR-IoT WG Development Board
Microchip AVR-IoT WG Development Board
×1
SparkFun Distance Sensor Breakout - 4 Meter, VL53L1X
×1
SparkFun Triple Axis Digital Accelerometer Breakout - ADXL313 (Qwiic)
×1
Battery, 3.7 V
Battery, 3.7 V
×1

Software apps and online services

Arduino IDE
Arduino IDE
AWS IoT
Amazon Web Services AWS IoT

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Base

Body

Code

TrashBuddyV1

C/C++
#include <Wire.h>
#include "SparkFun_VL53L1X.h"
#include <SparkFunADXL313.h>
#include <log.h>
#include <ecc608.h>
#include <led_ctrl.h>
#include <lte.h>
#include <mqtt_client.h>
#include <low_power.h>

#define SHUTDOWN_PIN 2
#define INTERRUPT_PIN 3

SFEVL53L1X distanceSensor;
ADXL313 myAdxl;

const char MQTT_SUB_TOPIC_FMT[] PROGMEM = "smarttrash/%s/sensors";
const char MQTT_PUB_TOPIC_FMT[] PROGMEM = "smarttrash/%s/sensors";
const char AWS_PAYLOAD_FMT [] PROGMEM = "{\"d\": %f, \"acc\": %f, \"bat\":%f}";

char mqtt_sub_topic[128];
char mqtt_pub_topic[128];
char aws_payload_data[128];

bool initTopics() {
    ATCA_STATUS status = ECC608.begin();

  if (status != ATCA_SUCCESS) {
    Log.errorf(F("Failed to initialize ECC, error code: %X\r\n"), status);
    return false;
  }

  // Find the thing ID and set the publish and subscription topics
  uint8_t thing_name[128];
  size_t thing_name_length = sizeof(thing_name);

  status = ECC608.readProvisionItem(AWS_THINGNAME, thing_name, &thing_name_length);

  if (status != ATCA_SUCCESS) {
    Log.errorf(
      F("Could not retrieve thingname from the ECC, error code: %X\r\n"),
      status);
    return false;
  }
  Log.infof(F("Thing name %s\r\n"), thing_name);

  snprintf_P(mqtt_sub_topic,
             sizeof(mqtt_sub_topic),
             MQTT_SUB_TOPIC_FMT,
             thing_name);
  snprintf_P(mqtt_pub_topic,sizeof(mqtt_pub_topic),MQTT_PUB_TOPIC_FMT,thing_name);

  return true;
}

void setup() {
  
  LedCtrl.begin();
  LedCtrl.startupCycle();

  Log.begin(115200);
  Log.info(F("Starting HTTP Get Time Example\r\n"));

  Wire1.begin();
  
  if (distanceSensor.begin(Wire1) != 0) //Begin returns 0 on a good init
  {
    while (1)
      ;
  }
  
  if (myAdxl.begin(0x1D , Wire1) == false) //Begin communication over I2C
  {

    while (1); //Freeze
  }
  
  Log.info("Sensor initialized");
  myAdxl.measureModeOn();
  
  LowPower.configurePowerDown();

  LedCtrl.on(Led::USER);
  Lte.begin();
  Log.infof("Connected to operator: %s\r\n", Lte.getOperator().c_str());

}

void loop() {
  LedCtrl.toggle(Led::DATA);
  distanceSensor.startRanging();
  while (!distanceSensor.checkForDataReady())
  {
    delay(1);
  }
  
  int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
  distanceSensor.clearInterrupt();
  distanceSensor.stopRanging();
  float distanceInches = distance * 0.0393701;
  double totalAcc = 0;
  float voltage = LowPower.getSupplyVoltage();

  Log.infof(F("Distance(inch): %f \r\n"), distanceInches);
 // Log.infof(F("%f \r\n"), distanceInches);
  
  
  if(myAdxl.dataReady()){
    myAdxl.readAccel(); 
      int x = myAdxl.x ; int y= myAdxl.y ; int z= myAdxl.z;
      Log.infof(F("X: %d Y: %d Z: %d \r\n"), x,y,z);
      

     totalAcc = sqrt(pow(x,2) + pow(y,2)+ pow(z,2));
     Log.infof(F("Movement: %f \r\n"), totalAcc);
  }
  


if (initTopics()) {
    delay(500);
    Log.infof(F("Initialization of AWS was successful"));
    
    if (MqttClient.beginAWS()) {
      Log.infof(F("Connected to AWS"));
      delay(500);
      snprintf_P(aws_payload_data,
                 sizeof(aws_payload_data),
                 AWS_PAYLOAD_FMT,
                 distanceInches, totalAcc, voltage);
      const bool published_successfully =
      MqttClient.publish(mqtt_pub_topic, aws_payload_data);

      if (published_successfully) {
        Log.infof(F("Published successfully"));
      }

    } else {
      Log.infof(F("MQTT connect failed"));
      LedCtrl.on(Led::ERROR);
      delay(5000);

    }
  } else {
    Log.infof(F("Initialization of AWS  failed"));
  }

  MqttClient.end();
  LowPower.powerDown(5*60);

  
}

Github Repo

Credits

Sashrika Das

Sashrika Das

4 projects • 32 followers
I am a 9th grade student, passionate about technology.
Thanks to Mithun Das.

Comments