Dave Wilson
Published © MIT

Harry's Horn

An empty Harry's razor blade box is the perfect project enclosure for a small panic alarm which can be activated by an Alexa Echo or Dot.

IntermediateFull instructions provided2 hours1,760
Harry's Horn

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
N-channel Power MOSFET
×1
Tiny Breadboard
×1
Industrial Electronic Alarm, 12 Volt DC, 2 wire, 85db
×1
Terminal Block 2-pin 5mm Pitch
×1
Solid Core Wire 22AWG
×3
A23 Battery
×1
A23 Battery Clip
×1
Harry's Shave Plan Plastic Box
×1
Adafruit 5V 2.4A Switching Power Supply with 20AWG MicroUSB Cable
×1
Plastic screw and nut
×1

Hand tools and fabrication machines

Razor Blade Knife
Needle Nose Pliers
Small Hand Held Saw
Wire Strippers

Story

Read more

Custom parts and enclosures

Harry's Blade Box

Empty Harry's blade boxes are great enclosures for small projects. The can also be used together.

Schematics

Simple Breadboard Construction

No soldering is required. All parts can be added to a tiny breadboard. Three solid core 22 AWG wires are need and two pins from the ESP8266.

Code

ESP8266 Alarm

C/C++
This is a pretty simple hack to the MQTT example code for the AsyncMqttClient. I do a couple of things to integrate a device into my home automation system. I use a topic based on the MAC address to assign a device to a location. I've also moved passwords and the like to include files.
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <AsyncMqttClient.h>

#include "networkConfig.h"
#include "mqttConfig.h"
#include "arduinoConfig.h"

///////
// pin assignments

#define PIEZO_PIN       D1_PIN

AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;

WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
Ticker wifiReconnectTimer;

// WIFI and MQTT stuff
char    macAddressTopic[64];  // used to associate MAC address with function
char    locationTopic[64];    // used to associate function with house location
char    clientId[64];         // MQTT client id
uint8_t MAC_array[6];
char    MAC_char[18];

/////////////////////////////////////////////////////////////////////////////
// WIFI STUFF
///////

void connectToWifi() {
  Serial.println("Connecting to Wi-Fi...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void onWifiConnect(const WiFiEventStationModeGotIP& event) {
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  WiFi.macAddress(MAC_array);
  for (int i = 0; i < sizeof(MAC_array); ++i) {
    sprintf(MAC_char, "%s%02x:", MAC_char, MAC_array[i]);
  }
  sprintf(macAddressTopic, "diyhas/esp_%02x%02x%02x", MAC_array[3], MAC_array[4], MAC_array[5]);
  sprintf(clientId, "ESP_%02X%02X%02X", MAC_array[3], MAC_array[4], MAC_array[5]);
  Serial.print("topic="); Serial.println(macAddressTopic);
  Serial.print("clientId="); Serial.println(clientId);

  connectToMqtt();
}

void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
  Serial.println("Disconnected from Wi-Fi.");
  mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
  wifiReconnectTimer.once(2, connectToWifi);
}

/////////////////////////////////////////////////////////////////////////////
// MQTT STUFF
///////

void connectToMqtt() {
  Serial.println("Connecting to MQTT...");
  mqttClient.connect();
}

void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);
  uint16_t packetIdSub = mqttClient.subscribe(macAddressTopic, 2);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  Serial.println("Disconnected from MQTT.");

  if (WiFi.isConnected()) {
    mqttReconnectTimer.once(2, connectToMqtt);
  }
}

void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
}

void onMqttUnsubscribe(uint16_t packetId) {
}

void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  Serial.println("Publish received.");
  if (strcmp((char *)topic, macAddressTopic) == 0) {
    Serial.print("topic="); Serial.print(topic);
    Serial.print("\tpayload="); Serial.println(payload);
    Serial.print("\tlen="); Serial.println(len);
    payload[len] = 0;
    setupTopics(payload);
  } else if (strcmp((char *)topic, "diyhas/system/panic") == 0) {
    if (strncmp((char *)payload, "ON", 2) == 0) {
      Serial.println("panic ON!");
      digitalWrite(PIEZO_PIN, HIGH);
    } else {
      Serial.println("panic OFF!");
      digitalWrite(PIEZO_PIN, LOW);
    }
  } else if (strcmp((char *)topic, "diyhas/system/fire") == 0) {
    if (strncmp((char *)payload, "ON", 2) == 0) {
      Serial.println("fire ON!");
      digitalWrite(PIEZO_PIN, HIGH);
    } else {
      Serial.println("fire OFF!");
      digitalWrite(PIEZO_PIN, LOW);
    }
  }
}

void onMqttPublish(uint16_t packetId) {
}

void setupMqtt() {
  mqttClient.onConnect(onMqttConnect);
  mqttClient.onDisconnect(onMqttDisconnect);
  mqttClient.onSubscribe(onMqttSubscribe);
  mqttClient.onUnsubscribe(onMqttUnsubscribe);
  mqttClient.onMessage(onMqttMessage);
  mqttClient.onPublish(onMqttPublish);
  mqttClient.setServer(MQTT_HOST, MQTT_PORT);
}

void setupTopics(char *payload) {
  sprintf(locationTopic, "%s/alarm", payload);
  Serial.print("setup topics: location=> "); Serial.println(locationTopic);
  for (int i = 0; i < MQTT_TOPICS; i++) {
    mqttClient.subscribe(mqttTopics[i], 2);
  }
}

/////////////////////////////////////////////////////////////////////////////
// SETUP INITIALIZATION
///////

void setup() {
  Serial.begin(115200);
  Serial.println();Serial.println();
  Serial.println("Source:   EspAlarmV2");
  Serial.println("Platform: ESP8266 WEMOS D1 Pro Mini");

  pinMode(PIEZO_PIN, OUTPUT);
  digitalWrite(PIEZO_PIN, LOW);

  // start tasks to handle acync messaging

  wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
  wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);

  // prepare MQTT services and event handlers

  setupMqtt();
  Serial.println("MQTT setup");

  // start WiFi

  connectToWifi();

  // wait for connection and initalization messages from the MQTT Broker

  while (locationTopic[0] == 0) {
    delay(100);
  }
}

/////////////////////////////////////////////////////////////////////////////
// MAIN PROCESSING
///////

void loop() {
}

Arduino Configuration Definitions

C Header File
I've separated pin definitions to an include file for each device type.
/*
   Arduino D1 Pro Mini
*/

#ifndef Arduino_Config_h
#define Arduino_Config_h

#define D0_PIN			  16 	//	GPIO16
#define D1_PIN			  5  	//	GPIO05	SCL
#define D2_PIN			  4  	//	GPIO04	SDA
#define D3_PIN			  0  	//	GPIO00	Flash or Run
#define D4_PIN			  2   //	GPIO02	LED	
#define D5_PIN			  14	//	GPIO14	SCK
#define D6_PIN			  12	//	GPIO12	MISO
#define D7_PIN			  13	//	GPIO13	MOSI
#define D8_PIN        15	//	GPIO15	SS

#define A0_PIN        17	//	GPIO17

#define SPI_SS_PIN		15	//	D8_PIN
#define SPI_MOSI_PIN 	13	//	D7_PIN
#define SPI_MISO_PIN 	12	//	D6_PIN
#define SPI_SCK_PIN  	14	//	D5_PIN

#define I2C_SDA_PIN   4	  //	D2_PIN
#define I2C_SCL_PIN   5	  //	D1_PIN

#define RX_PIN			  3	  //	Orange wire
#define TX_PIN			  1	  //	Green wire


#endif /* Arduino_Config_h */

Network Configuration

C Header File
Created a separate include file for network configuration settings.
#ifndef Network_Config_h
#define Network_Config_h

#define WIFI_SSID 		"SSID"
#define WIFI_PASSWORD	"PASSWORD"

#endif /* Network_Config_h */

MQTT Configuration Settings

C Header File
Separate MQTT settings into an include file.
#ifndef MQTT_Config_h
#define MQTT_Config_h

#define MQTT_HOST 		IPAddress(x, x, x, x)
#define MQTT_PORT 		1883

#define MQTT_TOPICS 5
char mqttTopics[MQTT_TOPICS][64] = {
  "x/system/who",
  "x/system/calibrate",
  "x/system/intruder",
  "x/system/panic",
  "x/system/fire"
};

#endif /* MQTT_Config_h */

Credits

Dave Wilson

Dave Wilson

3 projects • 9 followers
Software guy that is learning to design digital home automation and wearable products using open source hardware and software.

Comments