Saturday, August 5, 2017

TCP/IP Remote Controller for Light Switch and Battery powered by Solar Panels

TCP/IP Remote Controller for Light Switch and Battery powered by Solar Panels


12V Battery, Board (including raspberry pi, relay module and converter), Solar charge controller (from right to left)

Intro:


Last year, we bought a bunch of solar cells for me and my younger brother, Jeffery, to install into our backyard. We then got a bunch of LED lights to help light up our work area as it wasn’t properly lit, which was powered by a 12V battery that was in turn charged by the installed solar panel. However, whenever the light was needed, I had to physically unplug the wires connecting the solar panel to the transformer and attach the light’s wires with that of the battery, every single time.

After awhile, it got too tiring, switching it every time we needed the light. And so, since I had an extra Raspberry Pi 2 (a popular single-board computer) lying around, I decided to make a program so that I could control the light switch and the charging of the battery through the solar cells all at the same time. If accomplished, then anyone with the program/app would be able to control the lights and the charging of the battery without the need to laboriously detach and attach the cables all the time. And so, my new project was born!

Materials:
  • Raspberry Pi (with Wifi)
  • Installed Solar Panels
  • Solar Charge Controller 
  • 12V Battery
  • LED Lights
  • Many Wires
  • Relay Module
  • Solder (iron, material, etc..)
  • Solderable Breadboard
  • Solderable Signal Converter 

(Keep in mind, you can customize/change/swap out these items for your own projects)
**This guide assumes that a functioning solar panel has already been installed*
I will link our solar panel that we installed in our backyard about a year ago: https://solarpanelsang.blogspot.ca/


Installing the Board

Technically, you don’t even need a circuit board, but it just makes life a lot easier as the raspberry pi, the relay module, and the converter are all connected together on a single board.


Once everything is attached firmly onto the board, the next step is to connect all of the GPIO pins on the raspberry pi to the converter, as well as to the relay module. I needed the GPIO pinout of the raspberry pi (or find your specific GPIO pinout).
header_pinout.jpg



Basically, I connected the pi to the signal converter, through the DC 3.3V and 5V, as well as the ground and some GPIO pins from the pi to the relay module as well. This is essentially how the pi communicates with the switch (ex the GPIO outputs either HIGH voltage or LOW voltage, and this power gets sent over, for my case GPIO 17 on the pi to the switch. The converter is simply to convert this signal to the module. Then, depending on the received voltage, the relay module would either switch on of off (to the Normally Open [NO] pin or the Normally Closed [NC] pin).


Programming

The magic that will allow the remotely-controlled switch to work is the client-server tcp/ip communication protocol. But in order to do this, we need a platform to build our app on: Android Studio (https://developer.android.com/studio/index.html). For my project, I used Java (on Android) as well as C to control the Raspberry Pi’s GPIO pins, which in turn either turns the switch on or off. 



The Client:

Here is the source code for the client-side of the tcp/ip communication through wifi, (written in Java 1.8 and Android Studio version 2.3.3) found on my Github:

https://github.com/dave2000sang/remote_switch_solar_panel/blob/3091131fa40eac9628c33a6d1f26d411dc89d07b/server

Using Java, I created the Client-side TCP/IP on Android Studio. In essence, the program was quite simple, and only took a few hours to understand.

First, I created a ToggleButton with ID toggleBtn1, which is basically a push button on the app, which creates a boolean isChecked upon being called by the abstract class CompoundButton’s setOnCheckedChangeListener method. 

The actual client requires a Socket to communicate with the server, which is mSocket in my case. To use the Client as output whenever the button is clicked, which is based on the boolean value of isChecked, I used a PrintWriter class, which prints out characters into the output stream using getOutputStream() to the socket  mSocket. Also, I optionally added two TextView text outputs just to print the current status of the light and the battery. 

Whenever isChecked carries a true boolean value (button clicked) then the Client program will output a string into the Socket connecting the server to the client. And on the server side, it will check if this string equals to “LIGHTON” or "LIGHTOFF", in which case the server will know that the button was clicked; thus, allowing it to proceed to turn the switch on or off.



1 comment:

  1. It was really insightful.
    Thanks for the info.
    Wanna have more contents from you.
    Cheers
    BTW if anyone interested more have a look Click Here For Visit My Site thanks

    ReplyDelete