16x relay control - Multiuser web app + Arduino + Ethernet
The article is a tutorial on how to control 16 relay through Arduino microcontroller with Ethernet shield W5100.
Hello, I'm going to share again with another project that helps many of you to solve the issue of how to switch the relay remotely.
I have done a bit of a specific project for 2 chats that are remotely controlled when each user has their own site where they manage their outputs. Arduino Due and the Ethernet shield W5100 have the microcontroller, which has enough GPIO pins for the 16 channel relay , or 2x 8-channel relay. Since Due does not have everyone, the prepared source codes for this project are customized for 2 Arduino Uno boards with W5100 Ethernet shields and you will be able to test them yourself.
The web part is written in PHP procedural style. There are 2 user accounts where you can control 8 outputs for each user. One user does not see the other outputs and can not change them. At the same time, both users can simultaneously access and manage their outputs in the web application. The microcontroller is connected to the Internet in one of the chalets. Web interface: https://arduino.php5.sk/rele/
Login data to web application
1st user
The pages consist of a login page and a functional login page. If the user is not logged in, he / she will not be able to get to the demo site, it will automatically be redirected to the login page.
Functional pages have the role of:
I have done a bit of a specific project for 2 chats that are remotely controlled when each user has their own site where they manage their outputs. Arduino Due and the Ethernet shield W5100 have the microcontroller, which has enough GPIO pins for the 16 channel relay , or 2x 8-channel relay. Since Due does not have everyone, the prepared source codes for this project are customized for 2 Arduino Uno boards with W5100 Ethernet shields and you will be able to test them yourself.
The web part is written in PHP procedural style. There are 2 user accounts where you can control 8 outputs for each user. One user does not see the other outputs and can not change them. At the same time, both users can simultaneously access and manage their outputs in the web application. The microcontroller is connected to the Internet in one of the chalets. Web interface: https://arduino.php5.sk/rele/
Login data to web application
1st user
- Name: user1
- Password: user1
- Name: admin
- Password: admin
The pages consist of a login page and a functional login page. If the user is not logged in, he / she will not be able to get to the demo site, it will automatically be redirected to the login page.
Functional pages have the role of:
- Switching the relay
- Changing relay names
- Change your login information
- Save Arduino code
- Safe sign out of the app
#include
#include //Zmenit pre Ethernet2.h pre W5500
const int vystup1 = 0;
const int vystup2 = 1;
const int vystup3 = 2;
const int vystup4 = 3;
//D4 vynechany - pre SD kartu
const int vystup5 = 5;
const int vystup6 = 6;
const int vystup7 = 7;
const int vystup8 = 8;
byte mac[] = { 0xAA, 0xBB, 0xCC, 0x81, 0x7B, 0x4A }; //fyzicka adresa MAC
char serverName[] = "www.arduino.php5.sk"; // webserver
IPAddress ip(192, 168, 1, 254);
EthernetClient client;
String readString;
int x=0; //pocitadlo riadkov
char lf=10; //line feed character
void setup(){
pinMode(vystup1, OUTPUT);
pinMode(vystup2, OUTPUT);
pinMode(vystup3, OUTPUT);
pinMode(vystup4, OUTPUT);
pinMode(vystup5, OUTPUT);
pinMode(vystup6, OUTPUT);
pinMode(vystup7, OUTPUT);
pinMode(vystup8, OUTPUT);
if (Ethernet.begin(mac) == 0) {
Serial.println("DHCP nepridelilo adresu, skusam so statickou...");
Ethernet.begin(mac, ip); //nastav k mac adrese i IP adresu staticku
}
Serial.begin(9600);
}
void stav_vystup1(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele1.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup1, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup1, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup2(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele2.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup2, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup2, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup3(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele3.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup3, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup3, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup4(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele4.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup4, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup4, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup5(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele5.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup5, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup5, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup6(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele6.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup6, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup6, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup7(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele7.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup7, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup7, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup8(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele8.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup8, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup8, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void loop(){
stav_vystup1();
stav_vystup2();
stav_vystup3();
stav_vystup4();
stav_vystup5();
stav_vystup6();
stav_vystup7();
stav_vystup8();
delay(5000); //opakuj slucku loop o 5 sekund
}
Sketch for Arduino 2 (login admin admin admin):
#include
#include
const int vystup1 = 0;
const int vystup2 = 1;
const int vystup3 = 2;
const int vystup4 = 3;
//D4 vynechany - pre SD kartu
const int vystup5 = 5;
const int vystup6 = 6;
const int vystup7 = 7;
const int vystup8 = 8;
byte mac[] = { 0xAA, 0xBB, 0xCC, 0x81, 0x7B, 0x4C }; //fyzicka adresa MAC
char serverName[] = "www.arduino.php5.sk"; // webserver
IPAddress ip(192, 168, 1, 253);
EthernetClient client;
String readString;
int x=0; //pocitadlo riadkov
char lf=10; //line feed character
void setup(){
pinMode(vystup1, OUTPUT);
pinMode(vystup2, OUTPUT);
pinMode(vystup3, OUTPUT);
pinMode(vystup4, OUTPUT);
pinMode(vystup5, OUTPUT);
pinMode(vystup6, OUTPUT);
pinMode(vystup7, OUTPUT);
pinMode(vystup8, OUTPUT);
if (Ethernet.begin(mac) == 0) {
Serial.println("DHCP nepridelilo adresu, skusam so statickou...");
Ethernet.begin(mac, ip); //nastav k mac adrese i IP adresu staticku
}
Serial.begin(9600);
}
void stav_vystup1(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele10.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup1, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup1, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup2(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele11.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup2, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup2, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup3(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele12.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup3, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup3, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup4(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele13.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup4, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup4, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup5(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele14.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup5, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup5, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup6(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele15.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup6, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup6, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup7(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele16.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup7, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup7, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void stav_vystup8(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele17.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(vystup8, HIGH);
}else if(readString=="VYP"){
digitalWrite(vystup8, LOW);
}
else{
Serial.println("Nepodporovany stav! - chyba");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
}
void loop(){
stav_vystup1();
stav_vystup2();
stav_vystup3();
stav_vystup4();
stav_vystup5();
stav_vystup6();
stav_vystup7();
stav_vystup8();
delay(5000); //opakuj slucku loop o 5 sekund
}
Both these sketches for Arduin are linked to https://arduino.php5.sk/rele/, where you can also sketch the sketches directly using the web interface on this page. Thanks to Bootstrap, the relay in a column can also be controlled via a smartphone or other device, and it can be customized. Source codes for Arduino can also be used for an Ethernet shield / W5500 that has fully compatible commands with the Ethernet2.h library, which you replace with Ethernet.h.
Discussie (0 opmerking(en))