DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • IoT Needs To Get Serious About Security
  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC
  • Connection Pooling With BoneCP, DBCP, and C3PO [Video Tutorials]
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java

Trending

  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • Part 4: Multi-Agent Orchestration — Pairing Goose With Quarkus Flow State Machines
  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy
  • Spring AI on Local LLMs

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Make Your Own Light Switch With Java and a Raspberry Pi

Make Your Own Light Switch With Java and a Raspberry Pi

Looking for an IoT project? Look no further than developing your own switch with the helping hand of a library, some Java, and a Raspberry Pi.

By 
Hemambara Vamsi Kotari
·
Nov. 23, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
19.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In this article, you'll learn the simple steps to control a light using a Raspberry Pi and Java.

Hardware (Total Cost: $43):

  • Raspberry Pi (I have chosen the Raspberry Pi 3 model B, which has a built-in Wi-Fi dongle, ethernet cable support, 4 USB ports, and 2 GB of RAM) — $35 on Amazon.

  • Arduino relay module (1 pin) — less than $5 on eBay.

  • Female-to-female connector wires — $1 on eBay.

  • Light bulb and a conducting wire — $2 on eBay.

Setup

  • Install Apache Tomcat on your Raspberry Pi to enable connectivity between the Pi and your other devices. I know it might be heavy to run a container on small scale devices. We can also achieve our goals by using a card reader or voice recognizer, but I chose this easy way to establish the connection.

  • Include the pi4j library to send signals to the Raspberry Pi GPIO to control the power.

        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-core</artifactId>
            <version>1.1</version>
        </dependency>


  • Connect the GPIO pins and Arduino module using the female-female connector wires. GPIO pin numbers and Raspberry Pi pin numbers are different, so look at the diagram below for clear info. Also take a look at the video at the bottom of this article. Connect your Raspberry Pi:

    • Ground pin (6) to Arduino GRND.

    • VDC power pin (2) to Arduino VCC.

    • Any GPIO pin (22) to Arduino IN1.Connecting GPIO pins to Arduino moduleImage title

  • Use the below code snippet to switch on/off (or) control the power using the pi4j lib. More examples of pi4j can be found here.

// create gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        // lookup the pin by address
        Pin pin = CommandArgumentParser.getPin(
                RaspiPin.class,    // pin provider class to obtain pin instance from
                RaspiPin.GPIO_06);             // argument array to search in
// We are using PIN 06 as per the attached diagram

        GpioPinDigitalOutput output = gpio.provisionDigitalOutputPin(pin, "My Output", PinState.HIGH);

// switch ON
        output.high();
      // switch OFF
        output.low();

I have designed a web-app with JSP and a controller to send commands from the broswer. Based on the request parameter, we can send signals to GPIO pins. The complete web-app code is available here. Download and building it will generate smart-home-ui.war.

if("on".equalsIgnoreCase(request.getParameter("control"))) {
            output.high();
        } else {
            output.low();
        }

After following the above steps, copy  smart-home-ui.war to the Tomcat/webapps directory and start Tomcat. Enter the URL (https://<raspberry pi hostname (or) ip address>:8080/smarthome-ui/index) and click the Switch On/Off buttons and control the light in your home. Here is the sample UI.

Image title

Video


I hope you have enjoyed it. Any constructive comments/suggestions are than more welcome.

raspberry pi Light (web browser) Java (programming language) Apache Tomcat

Opinions expressed by DZone contributors are their own.

Related

  • IoT Needs To Get Serious About Security
  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC
  • Connection Pooling With BoneCP, DBCP, and C3PO [Video Tutorials]
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook