DEV Community

CHICAGO COMERCIAL FENCING
CHICAGO COMERCIAL FENCING

Posted on

How to Build an SMS Alert System for Open Fences Using IoT and Microcontrollers

Modern fencing solutions are rapidly evolving. From smart gates to automated access systems, today's fences do more than just outline your property—they're becoming key elements of home automation and security. One growing concern is ensuring that fence gates are properly closed to prevent intrusions or accidental openings. In this tutorial, we’ll show you how to create a smart SMS alert system that notifies you via text message whenever your fence is left open.

This project uses a magnetic sensor, NodeMCU ESP8266, and an SMS API service like Twilio to send alerts. It's ideal for residential homes, small farms, or businesses using smart fencing.

📦 Materials Needed

  • NodeMCU ESP8266 (or any Wi-Fi-enabled microcontroller)
  • Magnetic door sensor
  • Jumper wires
  • Breadboard
  • Twilio account (or another SMS gateway)
  • Arduino IDE
  • Internet connection

To ensure compatibility with modern fencing systems, consider a robust setup like Aluminum fence installation in chicago, which provides the durability and structural integrity needed to support sensors and smart components.

🛠️ Step 1: Wiring the Magnetic Sensor

We use a magnetic reed switch to detect when the gate is open. Here's a basic schematic:

Magnetic Switch:
- One wire to D1 (GPIO5)
- Other wire to GND

Pull-up resistor optional if using input_pullup mode
Enter fullscreen mode Exit fullscreen mode

🧠 Step 2: NodeMCU Code (Arduino IDE)

Here's the basic code to check gate status and send a message using a webhook:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const int gatePin = D1;
bool gateOpenLastState = false;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  pinMode(gatePin, INPUT_PULLUP);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  bool gateOpen = digitalRead(gatePin) == HIGH;
  if (gateOpen && !gateOpenLastState) {
    sendSMSAlert();
  }
  gateOpenLastState = gateOpen;
  delay(5000);
}

void sendSMSAlert() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin("https://your-sms-webhook.com/send");
    http.addHeader("Content-Type", "application/json");
    int httpResponseCode = http.POST("{\"message\":\"Fence is open!\"}");
    Serial.println(httpResponseCode);
    http.end();
  }
}
Enter fullscreen mode Exit fullscreen mode

🔐 Tip: Use a secure webhook URL with authentication if possible.

📱 Step 3: Set Up SMS API (Twilio Example)

  1. Sign up at Twilio
  2. Get your Account SID, Auth Token, and phone numbers
  3. Set up a webhook or use Twilio Functions to receive HTTP requests

Example Twilio Function:

exports.handler = function(context, event, callback) {
  const client = context.getTwilioClient();
  client.messages.create({
    body: 'Alert! Your fence is open.',
    from: context.TWILIO_PHONE_NUMBER,
    to: context.ALERT_PHONE_NUMBER
  }).then(message => callback(null, message.sid));
};
Enter fullscreen mode Exit fullscreen mode

To make this system more robust, working with a trusted provider like a Chicago fence company ensures that your hardware and wiring are secure, weatherproof, and properly integrated into your fence structure.

🔒 Real-World Use Case

Imagine a home or facility with children or pets. If a fence gate is left open by accident, the system will immediately send an SMS alert to the owner. You can also integrate with smart home assistants like Alexa or Google Home via IFTTT for voice-enabled control.

🌐 Hosting the Service

You can use services like Heroku, Vercel, or even a Raspberry Pi as a local webhook server. Be sure to use SSL certificates for security.

Example for HTTPS Webhook Server Using Flask (Python)

from flask import Flask, request
import requests

app = Flask(__name__)

@app.route('/send', methods=['POST'])
def send_sms():
    data = request.get_json()
    message = data.get('message')
    twilio_url = 'https://api.twilio.com/2010-04-01/Accounts/YOUR_SID/Messages.json'
    auth = ('YOUR_SID', 'YOUR_AUTH_TOKEN')
    payload = {
        'To': 'YOUR_PHONE_NUMBER',
        'From': 'YOUR_TWILIO_NUMBER',
        'Body': message
    }
    r = requests.post(twilio_url, data=payload, auth=auth)
    return str(r.status_code)

if __name__ == '__main__':
    app.run(port=5000, ssl_context='adhoc')
Enter fullscreen mode Exit fullscreen mode

📍 Expandability

Want to go further? Here are ideas:

  • Add a camera that sends snapshots when the gate opens
  • Log all gate activity to Google Sheets via IFTTT
  • Send alerts via email, WhatsApp, or Telegram
  • Integrate solar power for off-grid installations
  • Combine with NFC or keypad-based entry

A growing number of fencing setups are including smart integrations. Solutions like chain link fence chicago provide seamless compatibility with automation tools, giving you full control and monitoring of your property’s perimeter.

📝 Final Thoughts

Creating a smart SMS alert system for your fence is an affordable and effective way to enhance property security. With a simple setup using NodeMCU and a magnetic sensor, you can receive real-time updates about your gate's status. This solution is perfect for homeowners, small business owners, and anyone interested in smart fencing technology.

Let us know in the comments if you'd like to see a version with voice alerts, solar power, or integration with home assistants!

Top comments (0)