gallery/lic control logo

HVAC & Control Engineering

ARDUINO - CONTROLLER OF LIGHTING SYSTEMS
WITH WEB-SERVER ON ORANGE PI ZERO

I offer a description of a simple and inexpensive device for controlling a lighting system for 6 groups of light through bell (spring-loaded) switches with the ability to control from a computer and / or smartphone through a local computer network without external connection to the Internet. The role of a local WEB server that provides WEB - interface will play Linux microcomputer, for example Orange Pi Zero, NanoPi, Raspberry Pi or any other similar.

In such a scheme, each group of light can be controlled from any number switches, which are connected in parallel, what solves the problem of pass-through switches and provides an opportunity remote control via the network from a smartphone or computer.

This device can be assembled by yourself following this instruction. It is assumed that you have the skills to work with a voltage of 220V, prototyping and programming of microcontrollers Arduino.

All the components necessary for the assembly of the device can be purchased at the online store.

We will need:

gallery/arduino_nano

Microcontroller ARDUINO NANO
or any other similar microcontroller,
for example ARDUINO UNO, MICRO, MINI

gallery/orange_pi_zero

Microcompute Orange Pi Zero
or any other similar, for example Raspberry Pi.
It will act as a local WEB server and is required for
displaying the WEB-page of lighting control.

gallery/enc28j60

Ethernet module ENC28J60

gallery/tsp_05_220b

Power Supply Module

TSP-05 220В - 5В 3Вт
or any other 5V/300mA power

supply for Arduino

gallery/rele_module

Three 2-channel relay

modules 5V 10A with opto-isolated. You can use other, for example 8-channel relay modules 5V 10A with opto-isolation or 1-channel relay modules 5V 10A with opto-isolation

gallery/switch

Bell Switch (Push switch):
A push button is a momentary or non-latching switch which causes a temporary change in the state of an electrical circuit only while the temporary change in the state.

An spring returns the switch to its default position immediately afterwards, restoring the initial circuit condition. To control a group of light from several points, the switches can be connected in parallel.

 

Schematic diagram of the device:

The ARDUINO NANO microcontroller controls power relays that switch voltage 220V for six independent lighting groups in which can be used lamps of any type. The maximum switched power is limited by the power of the applied relays. The relay blocks offered in the description are designed for current up to 10A when using incandescent lamps, this is about 2 kW per lighting group. If you want to ensure the reliable and durable operation of these relays, Do not exceed the power of lamps with incandescent lamps greater than 1 kW per group.

Inputs A0 to A5 are used to control the wall switches. The control mode is trigger. Each subsequent pressing of the switch key changes the status of the output relay to the opposite: ON <-> OFF. Thanks to this approach, you can control one group of light from any number of points. To do this, enough connect the required number of switches in parallel. For connection of switches it is not necessary to use power cable or cable, you can use any signal wire, for example FTP.

To control power relays, the ARDUINO NANO controller uses the digital outputs D4, D5, D5, D7, D8, D9, which are connected to the corresponding inputs on the relay modules IN1, IN2.

To supply the circuit, use a + 5V source that will provide a current of at least 250-300 mA. If the power supply capacity is insufficient, the network module ENC28J60 may not work stably.

gallery/arduino_light_control

The algorithm of the program.

The program to be loaded into the ARDUINO NANO controller contains three basic logical blocks, which are executed cyclically in an infinite loop loop ().

The first block of the program ReadInput () is responsible for periodic interrogation of the state of the inputs for detecting the moment of pressing on the button of the switch. At the moment the key is pressed, the corresponding input of the controller is supplied with a voltage of + 5V which is interpreted by the controller as a logical unit. The absence of input voltage means a logical zero.

The second block of the program EthernetUpdate () processes GET requests from WEB-pages to include and switching off groups of light and also transmits the state (ON / OFF) of groups of light for display on WEB-page in a smartphone or computer.

The third block of the program SetOutput () controls the outputs of the controller. In this block, the appearance of a logical the unit at the controller input switches the state of the corresponding output to the reverse state. Ie, if the output was OFF, it will go ON and vice versa.

To program the ARDUINO UNO you need the UIPEthernet library. In Arduino IDE 1.8.4 it can be installed via the menu "Sketch" - "Connect the library" - "Manage libraries" - "Library Manager" - UIPEthernet.
You can also download the library from GitHub at https://github.com/UIPEthernet/UIPEthernet.

Below is a listing of the program for the controller ARDUINO NANO.

/*
LIGHT CONTROLLER WITH WEB-INTERFACE ON ARDUINO UNO + ENC28J60
(C)2020 Design by LIC CONTROL  http://en.lic.com.ua
Schema: http://liccontrol.com/article19.htm
Connecting Arduino Nano <-> ENC28J60
    D2  <-> INT
    D3  <-> RESET
    D10 <-> CS
    D11 <-> SI (ST)
    D12 <-> SO
    D13 <-> SCK
Inputs for bell switch: A0,A1,A2,A3,A4,A5
Outputs for relay connecting: D4,D5,D6,D7,D8,D9        
*/

#include <avr/wdt.h>    //Comment out this line if the controller firmware does not support Watchdog
#include <UIPEthernet.h>

const int LedPin = 13;
const int pins_in_count=6;           //Number of inputs for connecting switches
int pins_in[pins_in_count]  = {A0,A1,A2,A3,A4,A5};

const int pins_out_count=6;          //Number of outputs for connecting relays controlling light groups
int pins_out[pins_out_count] = {4,5,6,7,8,9};

int pins_state[pins_in_count];       //Array for storing the current state of the light group
int pins_old_state[pins_in_count];   //Array for storing the previous state of the light group

boolean myDebug = true;              //Set to false to disable debugging output
static uint8_t mac[6] = {0xBE, 0x9F, 0x18, 0x00, 0x00, 0x00};  //MAC-address
static uint16_t port = 80;
String result="";

IPAddress ip(192, 168, 100, 80);     //Enter the appropriate address for the controller on your network
EthernetServer server(80);
const char server_ip[] PROGMEM = "http://192.168.100.125"; //Enter the IP address of the WEB server (OrangePi, RaspberryPi)
const char page1[] PROGMEM =
"<!DOCTYPE html>\n<html dir='ltr' lang='en'>\n<head lang='en'>\n<meta charset='UTF-8'>\n"
"<title>LIGHTING CONTROL</title>\n"
"</head><body>\n"
"<div id='heder'></div>\n<div id='blank'></div>\n"
"<div id='main'><h3>No connection to the WEB server at: @!</div>\n"
"<link rel='stylesheet' href='@/static/css/style.css' type='text/css'>\n"   
"<script src='@/static/js/jquery-3.1.1.min.js'></script>\n"
"<script type='text/javascript' src='@/static/js/index.js'> </script>\n"
"</body></html>\n";

const char page2[] PROGMEM =
"HTTP/1.1 200 OK\n"
"Content-Type: text/html\n"
"Pragma: no-cache\n"
"Connection: close\r\n\r\n";

//Sending data stored in PROGMEM to the network
void printProgStr(EthernetClient client, const char * str, const char * str1) {
char c,c1;
  if (!str) return;
  while ((c = pgm_read_byte(str++))) {
    if (c=='@') {     
      for (int k = 0; k < strlen_P(server_ip); k++)  {
          c =  pgm_read_byte_near(server_ip + k);
          client.print(c);
      }   
    } else
    client.print(c);
  }
}

 

//Restarting the Ethernet module ENC28J60
void resetENC28J60() {
  digitalWrite(3, LOW);   
  delay(200);           
  digitalWrite(3, HIGH);
  delay(50);
}

 

//We look at the status of the inputs for the detection of key presses
void ReadInput() {
  unsigned long PressTime = 0;
  int d=0;
  for (int i=0; i<pins_in_count; i++) {
    d = digitalRead(pins_in[i]);
    if (d != pins_old_state[i]) {
      PressTime = millis();       
      while ((d != pins_old_state[i]) and ((millis()-PressTime)<50)) {
        d = digitalRead(pins_in[i]);
        delay(10);
      }
      if (not (millis()-PressTime)<50) {
        if (d<1) {
          pins_state[i] = not pins_state[i];
          if (myDebug) Serial.println("RELE("+String(i)+"), STATE="+String(pins_state[i]));    
        }
      }
    }
    pins_old_state[i] = d;
  }
}

 

//Set the outputs that control the relay
void SetOutput() {
  for (int i=0;i<pins_out_count;i++) {    
     digitalWrite(pins_out[i], pins_state[i]);
  }   
}

 

//We form the JSON line of status of the outputs (relays) for transmission to the WEB browser
String StringInput() {
  String _return = "[";
  for (int i=0; i<pins_in_count; i++)
    _return = _return + String(int(pins_state[i]>0))+",";
  _return[_return.length()-1]=']';
  return _return;
}

 

//Initial settings
void setup() {
  pinMode(LedPin, OUTPUT);  
  digitalWrite(LedPin, HIGH);
  //Initialization of inputs to which switch keys are connected
  for (int i=0;i<pins_in_count;i++) {
    pinMode(pins_in[i], INPUT_PULLUP);
    pins_state[i] = 0;
  }
  //Initializing the outputs that control the relay
  for (int i=0;i<pins_out_count;i++) {    
     pinMode(pins_out[i], OUTPUT);  
     digitalWrite(pins_out[i], LOW);
  }       

  resetENC28J60();  
  Ethernet.begin(mac, ip);
  server.begin();
 
  if (myDebug) {
    Serial.begin(9600);
    Serial.println("START PROGRAMM!");
    Serial.print("START SERVER AT:");
    Serial.println(Ethernet.localIP());
  }  
 
  delay(10);
  //Comment out this line if the controller firmware does not support Watchdog
  wdt_enable(WDTO_8S);   
}

 

//Processing GET requests from WEB-pages on / off light groups
void EthernetUpdate() {
  EthernetClient client = server.available();
  if (client) {
    result="";
    while (client.connected()) {
      if (client.available()) {        
        char c = client.read();
        result+=c;
        if (c == '\n') {
          if (myDebug) Serial.print("Ethernet: "+result);    
          if (result.indexOf("GET / HTTP/1.1")>=0) {
            printProgStr(client, page1, server_ip);
          } else
          if (result.indexOf("GET /ctrl/")>=0) {
            for (int i=0; i<pins_in_count; i++)
              if (result.indexOf("GET /ctrl/BT"+String(i))>=0)
                pins_state[i] = int(not pins_state[i]);
            printProgStr(client, page2, server_ip);      
            client.println(StringInput());                                 
          }  
          client.flush();             
          break;
        }        
      }
    }
    delay(10);
    client.stop();
  }
}

void loop() {
  ReadInput();
  EthernetUpdate();
  SetOutput();
  //Comment out this line if the controller firmware does not support Watchdog
  wdt_reset();
}

 

 

For debugging, you do not have to immediately compile the entire schema. To begin with, you can simply connect the Arduino Nano and the ENC28J60 and load the program into the Arduino Nano controller via the Arduino IDE. If you need detailed instructions how to download the program, use Google search.

After loading the program into the controller, you can test the reading of keystrokes and transmission of commands from the WEB-browser. To do this, open the port monitor in the Arduino IDE (Tools -> Serial Monitor, Tools -> Port Monitor) and apply + 5V in turns to the feet A0, A1, A2, A3, A4, A5.
In the port monitor, you should see messages, for example: RELE (0), STATE = 1.

To check the Ethernet module, enter the address of your controller in the address bar of the browser, for example as in the listing of the program http://192.168.100.80
If you have assembled the circuit correctly and the ENC28J60 module is working,
in the browser you will see the answer: There is no connection to the WEB server at: http://192.168.100.125!
At the same time, the message should appear in the port monitor GET / HTTP / 1.1
If everything works fine, you can proceed with installing the WEB server on a Linux microcomputer.

 

INSTALLING WEB SERVER ON ORANGE PI ZERO

In this example, the Orange Pi Zero microcomputer is used, as one of the most inexpensive. Other microcomputers may be used. In this case, the installation order may be different.

First you need to prepare an SD card with the image of the Linux operating system. For Orange Pi Zero it's Armbian. To do this, go to the official website of the system developer and download the latest stable version. Downloading an archive file of about 220MB in size. You need to unpack it with the archiver in any folder convenient for you. As a result, you will get a folder with files, one of which will be a file with the .img extension. This is the file with the operating system image, for example Armbian_20.02.1_Orangepizero_bionic_current_5.4.20.img. Start the Etcher program, click the Select image button and select the previously downloaded and unzipped file with the operating system image. Select a microSD card, press Flash! and wait for the end of the recording process. After the process is completed, the message will appear: Flash Complete! Safely ejected and ready for use, meaning, that the recording was successful and you can insert a microSD card into the Orange Pi Zero.
Power up the microcomputer. The first download usually takes about 3 minutes. With the help of any network analyzer, for example Angry IP Scanner for Windows, or Fing for Android, find address Orange Pi Zero in your network, for example http://192.168.100.125.

Replace in 36 line listing the program address const char server_ip [] PROGMEM = "http://192.168.100.125";
to the real address of Orange Pi Zero in your network and reload the program into the controller ARDUINO NANO.

Now you need to connect to the microcomputer.
The easiest way to do this is through SSH using the PuTTY program. Install the PuTTY program, run it, specify the address of the Orange Pi Zero microcomputer in the Host Name: 192.168.100.125 line. Port: 22 and click Open. A terminal window opens where you must enter the root user name and password 1234. Then, according to the requests, which will appear on the screen, change the password for root and add a new user, for example pi.
You can learn more about how to do this search Google.

The completion of the initial installation will be indicated by a message in the terminal about the need reboot the system. Enter the

sudo reboot

The system will ask for your new password, enter it. After rebooting, you will need to reconnect to the microcomputer via PuTTY. Now enter as user pi with your new password.

To work as a microcomputer as a WEB server, it needs to install the APACHE package, and for the possibility connection to the microSD controller over the network, you need to install the SAMBA package. You also need to write css and javascript files providing the work of the WEB server.

To facilitate the installation of these packages and to record WEB server files, use our special script, which is loaded by the command:

sudo wget http://lic.com.ua:/dat/apache.sh

Warning! This script will create a new configuration for SAMBA and APACHE. If you want to install WEB server on your microcomputer which already has SAMBA and APACHE installed and you want to save their configuration you need to manually perform only copying WEB server files from http://lic.com.ua:/dat/www to the APACHE html directory.

Make this script executable:

sudo chmod + x apache.sh

And run it for execution:

sudo ./apache.sh

During the installation, you will be prompted to allow the installation of packages, answer Y and press Enter. Also you are asked to set passwords to access network drives. Enter them at your discretion. In the future, they will be required you for network access to the microSD microcomputer Orange Pi Zero.
If everything went well then typing the address of your controller ARDUINO NANO in the address bar of the browser, for example, as in the listing of the program http://192.168.100.80, you will see the WEB page for lighting control.

Now you can complete the circuit assembly by connecting switches, relay modules and groups of light.
REMEMBER ON SAFETY RULES WHEN WORKING WITH HIGH VOLTAGE.
HIGH VOLTAGE IS DANGEROUS FOR LIFE!