Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Yamilett Estrada-Reyes
Published

Crime Alert System

An LED alert system that lights up different colors to indicate how many assaults have occurred during that given time interval (4 hrs).

IntermediateFull instructions provided2 hours263
Crime Alert System

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
Argon
Particle Argon
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Jumper wires (generic)
Jumper wires (generic)
×5
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
Resistor 220 ohm
Resistor 220 ohm
×3

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Socrata Chicago Crime API

Story

Read more

Schematics

Wiring Diagram

See brief description under "Hardware Setup"

Code

crime.ino

C/C++
See brief description under "The Code"
#include <ArduinoJson.h>

StaticJsonDocument<1024> doc;

const int greenPin = 2;
const int yellowPin = 3;
const int redPin = 4;

int minute;

int hour;
int counter;
int lim_hour;

String date;

void getDate() {
    Time.zone(-6); // time zone offset for CST
    int day = int(Time.day() - 7); 
    int month = int(Time.month());
    if(day < 1) { // checks for negative days
        if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            day = 31 - abs(day); 
        }
        else {
            day = 30 - abs(day);
        }
    }
    String str_day = String(day);
    String str_month = String(month);
    if(str_day.length() < 2) { // checks if day is zero-padded
        str_day = "0" + str_day;
    }
    if(str_month.length() < 2) { // checks if month is zero-padded
        str_month = "0" + str_month;
    }
    date = String(Time.year()) + "-" + str_month + "-" + str_day; // YYYY-MM-DD format
}

void getHour() {
    hour = int(Time.hour());
    if((Time.hour() + 4) > 23) { //checks if hour exceeds 23 (starts at 0 index)
        lim_hour = (Time.hour() + 4) % 24;
    }
    else {
        lim_hour = Time.hour() + 4;
    }
    minute = int(Time.minute());
}

void lights() {
    if(counter == 0) {
        digitalWrite(greenPin, HIGH);
        digitalWrite(yellowPin, LOW);
        digitalWrite(redPin, LOW);
    }
    if(counter >= 1 && counter < 3) {
        digitalWrite(greenPin, LOW);
        digitalWrite(yellowPin, HIGH);
        digitalWrite(redPin, LOW);
    }
    if(counter >= 3) {
        digitalWrite(greenPin, LOW);
        digitalWrite(yellowPin, LOW);
        digitalWrite(redPin, HIGH);
    }
}

void setup() {
    getDate();
    getHour();
    pinMode(4, OUTPUT); 
    pinMode(3, OUTPUT); 
    pinMode(2, OUTPUT); 
    Serial.begin(9600);
    Particle.subscribe("hook-response/info", myHandler);
    
    //OPTIONAL
    Serial.print("Today is ");
    Serial.println(date + "T" + hour + ":" + minute);
    Serial.println();
}

void loop() {
    String data = String(10);
    counter = 0;
    Particle.publish("info", data, PRIVATE);
    delay(60000);
}

void myHandler(const char *event, const char *data) {
    const char* json = data;
    deserializeJson(doc, json);
    
    for (JsonObject elem : doc.as<JsonArray>()) {
        // turns the JsonObj into a pointer
        String dataStr = String(elem["date"].as<char*>()); 
        
        //extracts date (YYYY-MM-DD) from the entire data string
        char StrBuffer[17] = ""; // formerly 22
        dataStr.toCharArray(StrBuffer,17);
        String r_date = strtok(StrBuffer,"T");

        // if case date equals our target date (last week)
        if(r_date.equals(date)) {
            //match!
            Serial.print("Date Retrieved: ");
            Serial.println(r_date);
            
            //retrieve hour of crime
            String r_hour = strtok(NULL,":");
            int int_r_hour = atoi(r_hour);
            
            Serial.print("Hour of Reported Incident: ");
            Serial.println(int_r_hour);
            
            //retrieve minute of crime
            String r_minute = strtok(NULL,":");
            int int_r_minute = atoi(r_minute);
            
            Serial.print("Minute of Reported Incident: ");
            Serial.println(r_minute);
            
            Serial.println();
            
            //if incident occured between now and four hours from now last week
            if(int_r_hour >= hour && int_r_hour <= lim_hour) {
                counter++;
                /*check minute, if reported minute is greater than current minute, 
                it doesn't technically fall within the 4 hr bound, so subtract 1 from the added 1*/
                if(int_r_hour == lim_hour) {
                    if(int_r_minute > minute) {
                       counter--;
                   }
                }
            }
            
        }
    }
    
    //OPTIONAL
    Serial.println();
    Serial.print("Total Count: ");
    Serial.println(counter);
    
    lights();
}

Credits

Yamilett Estrada-Reyes
3 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.