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!
Sanjay Rajjan
Published

Smart Waste Management System

A smart waste management system for cities that can monitor and manage waste levels in real-time using the AVR-IoT C

BeginnerFull instructions provided160
Smart Waste Management System

Things used in this project

Hardware components

AVR-IoT WG Development Board
Microchip AVR-IoT WG Development Board
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Smart Trash Schematic

Code

TrashBin Alert

Arduino
/**
 * This code is for smart waste management system that alerts cities to manage trash cans in the city
 * 
 */

#include <Arduino.h>
#include <http_client.h>
#include <led_ctrl.h>
#include <log.h>
#include <lte.h>
#include <sequans_controller.h>



#define NUMBER "6260"

char response[128] = "";


void setup() {

    LedCtrl.begin();
    LedCtrl.startupCycle();

    Log.begin(115200);
    Log.info(F("Starting\r\n"));

  setupSMS();
}

void loop() {
  
const int trigPin = PIN_PF3; // Trigger Pin of Ultrasonic Sensor
const int echoPin = PIN_PF2; // Echo Pin of Ultrasonic Sensor
   long duration, inches, cm;
   pinMode(trigPin, OUTPUT);
   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   
   if (inches < 2 ) {
    String message = "---Trash is Full at Trash Bin Id: 100---";
    Log.info(message);
    sendSMS(message);
   }
  // Log.infof(F("Distance %d in %d cm\r\n"), inches, cm);
   delay(100);
  }

long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}


void setupSMS() {
   
    Log.info(F("Starting SMS setup"));

    if (!SequansController.begin()) {
        Log.error(F("Failed to start the modem"));
        return;
    }

    // Configure UE for EPS combined attach
    ResponseResult response_result = SequansController.writeCommand(
        F("AT+CEMODE=2"),
        response,
        sizeof(response));

    if (response_result != ResponseResult::OK) {
        Log.errorf(F("Error writing combined attach command, the response was: "
                     "%s\r\n"),
                   response);
        return;
    }

    // --- Start LTE modem and connect to the operator ---
    if (!Lte.begin()) {
        Log.error(F("Failed to connect to the operator"));
        return;
    }

    Log.infof(F("Connected to operator: %s\r\n"), Lte.getOperator().c_str());
    
    // --- Configure the UE to work under SMS text mode ---
    response_result = SequansController.writeCommand(F("AT+CMGF=1"),
                                                     response,
                                                     sizeof(response));

    if (response_result != ResponseResult::OK) {
        Log.errorf(F("Failed to configure the UE to work under SMS text mode, "
                     "the response was: %s\r\n"),
                   response);
        return;
    }

    // --- Query the SMS service centre address to configure it is already
    // correctly configured ---
    response_result = SequansController.writeCommand(F("AT+CSCA?"),
                                                     response,
                                                     sizeof(response));

    if (response_result != ResponseResult::OK) {
        Log.errorf(F("Failed to query the SMS service centre address, the "
                     "response was: %s\r\n"),
                   response);
        return;
    } 
    SequansController.clearReceiveBuffer();


}

void sendSMS (String message) {
  
    Log.infof(F("Sending SMS to %s...\r\n"), NUMBER);
    ResponseResult response_result = SequansController.writeCommand(
        F("AT+SQNSMSSEND=\"%s\",\"--- Trash is Full ---\""),
        response,
        sizeof(response),
        NUMBER);

    if (response_result != ResponseResult::OK) {
        Log.errorf(F("Failed to send SMS, the response was: %s\r\n"), response);
        return;
    }

    SequansController.clearReceiveBuffer();
    
    Log.info(F("Waiting for send confirmation..."));
    if (!SequansController.waitForURC(F("SQNSMSSEND"),
                                      response,
                                      sizeof(response),
                                      40'000)) {
        Log.errorf(F("Timed out waiting for SMS confirmation\r\n"));
        return;
    }

    Log.infof(F("Got the following response: %s"), response);

}

Credits

Sanjay Rajjan

Sanjay Rajjan

1 project • 1 follower

Comments