DEV Community

Emily Johnson
Emily Johnson

Posted on

Building a Smart Fence with Raspberry Pi and Node.js: Automating Wooden and Iron Gates

In the modern era, automating fences and gates is no longer just a luxury—it's a necessity for enhancing security, convenience, and remote access control. Adding IoT capabilities can prevent unauthorized access, monitor entry points, and provide automated control using **Raspberry Pi and Node.js.

Whether you're upgrading a wood fence chicago, or securing an iron fence in the city of chicago, integrating smart technology ensures better security and accessibility.


1. Setting Up Raspberry Pi and Installing Dependencies

To get started, you need a Raspberry Pi (Model 3 or later) and some additional components:

  • Servo Motor or Relay Module (to control the gate)
  • Ultrasonic Sensor or Motion Sensor (to detect movement)
  • WiFi Module (if using a Raspberry Pi without built-in WiFi)
  • Power Supply

First, install Node.js on your Raspberry Pi:

sudo apt update
sudo apt upgrade
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
Enter fullscreen mode Exit fullscreen mode

Next, create a project directory and initialize it:

mkdir smart-fence
cd smart-fence
npm init -y
Enter fullscreen mode Exit fullscreen mode

Now, install the required Node.js dependencies:

npm install express onoff dotenv
Enter fullscreen mode Exit fullscreen mode

2. Wiring and Controlling the Fence Mechanism

We will connect the servo motor or relay module to the GPIO pins of the Raspberry Pi. The wiring for a servo motor looks like this:

  • Red wire → 5V (Power)
  • Black wire → GND (Ground)
  • Yellow wire → GPIO 18 (PWM Control)

If using a relay module (for controlling larger motors in wood fence chicago), connect:

  • VCC → 5V
  • GND → Ground
  • IN1 → GPIO 23 (Control Signal)

For an iron fence in the city of chicago, a stronger relay or motorized mechanism might be required.


3. Writing the Node.js Code for Fence Automation

Let's create a simple Express server to control the fence remotely:

const express = require("express");
const Gpio = require("onoff").Gpio;
const dotenv = require("dotenv");

dotenv.config();
const app = express();

const fenceRelay = new Gpio(23, "out"); // Control relay (Gate Open/Close)
const motionSensor = new Gpio(17, "in", "both"); // Detects motion

// API Endpoint to Open the Fence
app.get("/open", (req, res) => {
  fenceRelay.writeSync(1);
  res.json({ status: "Fence Opened" });
});

// API Endpoint to Close the Fence
app.get("/close", (req, res) => {
  fenceRelay.writeSync(0);
  res.json({ status: "Fence Closed" });
});

// Motion Sensor Event
motionSensor.watch((err, value) => {
  if (value === 1) {
    console.log("Motion Detected Near the Fence!");
  }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Smart Fence API running on port ${PORT}`);
});
Enter fullscreen mode Exit fullscreen mode

4. Deploying the Code and Running the Server

Once the code is in place, deploy it to your Raspberry Pi by running:

node index.js
Enter fullscreen mode Exit fullscreen mode

To make your Smart Fence API accessible from outside your local network, use ngrok or set up port forwarding in your router:

npm install -g ngrok
ngrok http 3000
Enter fullscreen mode Exit fullscreen mode

This will provide a public URL that allows you to control the fence from anywhere!


5. Storing and Managing the Code in a Repository

It's always a good practice to store your project in a Git repository. Use GitHub or GitLab to manage your code and collaborate with others:

git init
git add .
git commit -m "Initial smart fence project"
git remote add origin https://github.com/yourusername/smart-fence.git
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

6. Expanding the Smart Fence with More Features

To enhance security and automation, consider adding:

  • RFID or NFC Access Control for authorized entry
  • Face Recognition using a camera module and AI
  • Mobile App Integration to open/close the fence from your phone
  • SMS/Email Alerts for unauthorized access attempts

Final Thoughts

By integrating IoT with a Raspberry Pi and Node.js, we have built a smart fence system that works with a wood fence chicago while ensuring security automation.

For those with an iron fence in the city of chicago, this system can be scaled with stronger motors and access control mechanisms.

Top comments (0)