📜 ⬆️ ⬇️

Just about “smart home”: how to make “smart” lighting

Coming home, it was always uncomfortable: you open the door, and then it's dark, then there was an idea to make the backlight in the hallway, but not easy, and what could be turned on from the phone before coming home. Well said - it means you have to start.
Some thoughts from the author before reading this article.
(Before further reading the article, obviously, I should include a comment here, that first of all this text should be considered as a little tutorial on working with gpio using java, shown in this example, and not an instruction that convinces every user , the need to go this way)


As further extensions of this system are planned (or not), it was decided to take a single-board computer as a basis. With the flick of a mouse on Ali, the cheapest <

orange pi zero 256mb .


')

Connection


Then the mechanical part, there is nothing complicated. In the same way as for the Arduino board, only according to the outputs on the orange pi.



We take the usual relay for "homemade". On the one hand we connect the power and control pin, on the other hand we cut the power wire of our lamp and insert it into the relay (in my case it was a diode tape).



Scheme:



That's all, you are a genius.

Some code


With the technical part on this finished. Go to the software, here, too, everything is simple:

  1. We install armbian or any other Linux on the sd card, but I chose this one. I recommend to write an image using etcher.
  2. Next, via apt-get, install java into 3 commands:

    #sudo add-apt-repository ppa:webupd8team/java #sudo apt-get update #sudo apt-get install oracle-java8-installer 
  3. With the help of the guitar, we will add a set of wiringPi for our board (it contains various libraries for working with gpio and all sorts of tools for the same)
    Also not difficult:

     #git clone https://github.com/vladikoms/WiringOP-Zero.git #cd WiringOP-Zero #chmod +x ./build #sudo ./build 

    Well, check:

     #gpio -v #gpio readall 

On this part with the board finished, go to the computer and your favorite ide.

We will deploy the server in spring, so go to their website or in ide we create a spring project with hail.



To work with gpio, you need to install the pi4j library, for this you need to add a repository and dependency.

And then the final script should look something like this:

build.gradle
 buildscript { ext { springBootVersion = '2.1.0.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.lary.sh' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 jar{ baseName = 'SH' version = '0.0.1-SNAPSHOT' } repositories { mavenCentral() maven{ url "https://oss.sonatype.org/content/groups/public" //  } } dependencies { implementation('org.springframework.boot:spring-boot-starter-data-rest') implementation('org.springframework.boot:spring-boot-starter-web') runtimeOnly('org.springframework.boot:spring-boot-devtools') testImplementation('org.springframework.boot:spring-boot-starter-test') compile 'com.pi4j:pi4j-core:1.2-SNAPSHOT' //   } 


Excellent go to the class managing gpio:

Gpio.java
 public class MyGpio{ private GpioController gpio ; private GpioPinDigitalOutput myLed; public Gpio() { try { //          ( ) PlatformManager.setPlatform(Platform.ORANGEPI);//  ,    OrangePI } catch (PlatformAlreadyAssignedException e) { e.printStackTrace(); } gpio= GpioFactory.getInstance(); myLed = gpio.provisionDigitalOutputPin(OrangePiPin.GPIO_08); //  8    //       } public void light(){ //     if(myLed.isHigh()) myLed.low(); //             else myLed.high();//        (8) } public void blink(){ //       for (int i =0;i<10;i++) { try { light(); Thread.sleep(500); } catch (Exception e){ e.printStackTrace(); } } } } 


The presented code is quite simple and does not imply good management of the board pins, but opens the door to this.

Well, the final stage of the creation of the controller in the spring

GpioController.java
 @Controller public class GpioController { @RequestMapping("/blink") @ResponseBody public String blink(HttpServletResponse response, HttpServletRequest request ) { Gpio gpio =new Gpio(); gpio.blink(); return "blink"; } @RequestMapping("/light") @ResponseBody public void light(){ Gpio gpio =new Gpio(); gpio.light(); } } 


Now a couple of recent gestures in the form of creating a jar file using gradle. After that, transferring via sftp to the board (in many ideas, you can configure the deployment directly) and run the jar on the device. Next, go to the browser on the cherished 192.168.0. ***: 8080 / light
and enjoy, in passing, hoping that it will help keep our fingers at night or turn on the light, before coming home.

In the next part (if ever I get to it) I will definitely show different ways of interacting with our “smart” home.

here is the source
and then work with a comb (gpio)

Source: https://habr.com/ru/post/430928/


All Articles