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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

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

  • Deploy Serverless Lambdas Confidently Using Canary
  • Top NoSQL Databases and Use Cases
  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Are Traditional Data Warehouses Being Devoured by Agentic AI?
  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 user avatar
Hemambara Vamsi Kotari
·
Nov. 23, 16 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
18.9K Views

Join the DZone community and get the full member experience.

Join For Free

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

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: