Randall Chavez
Published © GPL3+

Cohart 12 Silent alarm

In a world where anything can take place, Now you can safeguard any part of your home and be notified by text or email.

BeginnerShowcase (no instructions)24 hours63
Cohart 12 Silent alarm

Things used in this project

Hardware components

Photon
Particle Photon
This is the main component of the project. It holds the codes that make it do what it was designed to do.
×1
VBBMicro 'Arduino UNO Avatar'
Virtual Breadboard VBBMicro 'Arduino UNO Avatar'
This component secures all items to insure the program is working by providing current to each item to make the operation preform as designed.
×1
NeoPixel Ring: WS2812 5050 RGB LED
Adafruit NeoPixel Ring: WS2812 5050 RGB LED
The NeoPixel ring indicates the status of the Cohart 12 by turning green for secure and red for unsecure status.
×1
OLED Expansion
Onion Corporation OLED Expansion
The Oled is a vital part of the Cohort 12 by displaying the status of the system.
×1
Hall Effect Sensor
Hall Effect Sensor
This is the heart of the system. This part is connected to the base on the door that is to remain secured. If there is a breech and it loses contact to the sensor that is mounted on any surface, it will help send a signal about the breech in security.
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
This is what makes the system work. It is connected to the Photon 2 to provide the power to run the operation of the system.
×1
Jumper wires (generic)
Jumper wires (generic)
They are the life lines that provide the current to run all operations of the code to preform the tasks they are assigned to.
×1
Switch Replacement Magnet, Honeywell 2SS
Switch Replacement Magnet, Honeywell 2SS
This informs the sensor that the door is closed by closing the circuit.
×1

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)
The 3d printer is used to make the case to store the items to protect them from the environment.
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Used to cut and strip the wire to preform the function.
10 Pc. Jumper Wire Kit, 20 cm Long
10 Pc. Jumper Wire Kit, 20 cm Long
To help make connections on the board to complete the project.

Story

Read more

Custom parts and enclosures

Capstone Case

Magnet Holder

Schematics

Capstone breadboard

Capstone Schematic

Code

Cohort 12

C/C++
/* 
 * Project L13_07_Alarm
 * Author: Randall Chavez
 * Date: 03-26-2024
 * For comprehensive documentation and examples, please visit:
 * https://docs.particle.io/firmware/best-practices/firmware-template/
 */

// Include Particle Device OS APIs
#include "Particle.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#include <Adafruit_MQTT.h>
#include "Adafruit_MQTT/Adafruit_MQTT_SPARK.h"
#include "Adafruit_MQTT/Adafruit_MQTT.h"
#include "credentials.h"
#include "neopixel.h"
#include <colors.h>
#include "IoTTimer.h"
 bool hallState;
 int lastInputValue;
 bool neoflash;
 
 
void pixelFill(int start,int end,int color);

// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(SEMI_AUTOMATIC);

const int hallsensorpin = D3;
int inputValue;

// Run the application and system concurrently in separate threads

const int PIXELCOUNT = 12;
Adafruit_NeoPixel pixel ( PIXELCOUNT, SPI1, WS2812B); 
Adafruit_SSD1306 display(-1);

TCPClient TheClient;
Adafruit_MQTT_SPARK mqtt(&TheClient,AIO_SERVER,AIO_SERVERPORT,AIO_USERNAME,AIO_KEY); 

Adafruit_MQTT_Publish publishAlarm=Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/alarmState");

void MQTT_connect();
bool MQTT_ping();


// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'


// setup() runs once, when the device is first turned on
void setup() {

  Serial.begin (9600);
   pinMode(D3,INPUT);

   
   
   //toggle x (if x = 1 then set x = buttonState=! buttonState);
   
pixel.begin();
 pixel.setBrightness (30); 
 pixel.show();

 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextSize(1);
display.setTextColor(WHITE);
display.clearDisplay();
display.display();

 WiFi.on();
  WiFi.connect();
  while(WiFi.connecting()) {
    Serial.printf(".");
  }
  Serial.printf("\n\n");
 
  
   
  // Put initialization like pinMode and begin functions here
}


// loop() runs over and over again, as quickly as it can execute.
void loop() {
  display.setCursor(0,0); // top of loop
  MQTT_connect();
  MQTT_ping();
  

//Serial.printf("%d\n",inputValue);
 
 
    inputValue = digitalRead(D3);
    if(!inputValue){
      pixelFill(0,12,green);
      display.printf("Alarm Secured");
      display.display();
      display.clearDisplay();
      Serial.printf("green\n");
     
      
      
  }
  else{
    
    
        
     pixelFill(0,12,red);
     display.printf("Alarm Unsecured");
     display.display();
     display.clearDisplay();
      }
     
     if (inputValue != lastInputValue){ 
        publishAlarm.publish(inputValue);
        lastInputValue = inputValue;
     }
  
  }
  
  void MQTT_connect() {
  int8_t ret;
 
  // Return if already connected.
  if (mqtt.connected()) {
    return;
  }
 
  Serial.print("Connecting to MQTT... ");
 
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.printf("Error Code %s\n",mqtt.connectErrorString(ret));
       Serial.printf("Retrying MQTT connection in 5 seconds...\n");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds and try again
  }
  Serial.printf("MQTT Connected!\n");
  }

  // Example: Publish event to cloud every 10 seconds. Uncomment the next 3 lines to try it!
  // Log.info("Sending Hello World to the cloud!");
  // Particle.publish("Hello world!");
  // delay( 10 * 1000 ); // milliseconds and blocking - see docs for more info!

bool MQTT_ping() {
  static unsigned int last;
  bool pingStatus;

  if ((millis()-last)>120000) {
      Serial.printf("Pinging MQTT \n");
      pingStatus = mqtt.ping();
      if(!pingStatus) {
        Serial.printf("Disconnecting \n");
        mqtt.disconnect();
      }
      last = millis();
  }
  return pingStatus;
}


void pixelFill(int start, int end, int pixColor){ 
  int i;
   for(i=start; i<end; i ++){
    pixel.setPixelColor (i, pixColor);
    
   }
    pixel.show();
    }

Credits

Randall Chavez

Randall Chavez

3 projects • 3 followers
Thanks to Ethan Rashap.

Comments