Home Automation - NEED HELP!!!

Hello everyone,

I need help with the code of my home automation. I’m using an Arduino Mega 2560 and one Ethernet shield w5100.
I have a problem because I need activate 16 relays from the Internet, but with my code I am only able to activate 8 relays. Can anyone help me with my code?

My code is this:

#include <SPI.h>
#include <Ethernet.h>

byte mac = {
0x90, 0xA2, 0xDA, 0x00, 0x9B, 0x36 }; //physical mac address
byte ip = { 192,168,1,106 }; // ip in lan
byte gateway = { 192,168,1,254 }; // internet access via router
byte subnet = { 255,255,255,0 }; //subnet mask
EthernetServer server(80); //server port

String readString;

int pin[16];
char* nome_pin[16];
int estado_pin[16];
int tipo_pin[16];

//////////////////////

void setup(){

//CONFIGURACAO DAS PORTAS EM QUE OS RELES ESTAO CONECTADOS:

pin[0] = 26;
pin[1] = 27;
pin[2] = 28;
pin[3] = 29;
pin[4] = 30;
pin[5] = 31;
pin[6] = 32;
pin[7] = 33;
pin[8] = 34;
pin[9] = 35;
pin[10] = 36;
pin[11] = 37;
pin[12] = 38;
pin[13] = 39;
pin[14] = 40;
pin[15] = 41;

//NOME DOS BOTOES
nome_pin[0] = “rele 1”;
nome_pin[1] = “rele 2”;
nome_pin[2] = “rele 3”;
nome_pin[3] = “rele 4”;
nome_pin[4] = “rele 5”;
nome_pin[5] = “rele 6”;
nome_pin[6] = “rele 7”;
nome_pin[7] = “rele 8”;
nome_pin[8] = “rele 9”;
nome_pin[9] = “rele 10”;
nome_pin[10] = “rele 11”;
nome_pin[11] = “rele 12”;
nome_pin[12] = “rele 13”;
nome_pin[13] = “rele 14”;
nome_pin[14] = “rele 15”;
nome_pin[15] = “rele 16”;

//TIPO DOS BOTOES 0-> toggle, 1-> pulse
tipo_pin[0] = 0;
tipo_pin[1] = 0;
tipo_pin[2] = 0;
tipo_pin[3] = 0;
tipo_pin[4] = 0;
tipo_pin[5] = 0;
tipo_pin[6] = 0;
tipo_pin[7] = 0;
tipo_pin[8] = 0;
tipo_pin[9] = 0;
tipo_pin[10] = 0;
tipo_pin[11] = 0;
tipo_pin[12] = 0;
tipo_pin[13] = 0;
tipo_pin[14] = 0;
tipo_pin[15] = 0;

//ESTADO INICIAL DOS BOTOES 0 → desligado, 1 → ligado:
estado_pin[0] = 0;
estado_pin[1] = 0;
estado_pin[2] = 0;
estado_pin[3] = 0;
estado_pin[4] = 0;
estado_pin[5] = 0;
estado_pin[6] = 0;
estado_pin[7] = 0;
estado_pin[8] = 0;
estado_pin[9] = 0;
estado_pin[10] = 0;
estado_pin[11] = 0;
estado_pin[12] = 0;
estado_pin[13] = 0;
estado_pin[14] = 0;
estado_pin[15] = 0;

pinMode(pin[0], OUTPUT);
pinMode(pin[1], OUTPUT);
pinMode(pin[2], OUTPUT);
pinMode(pin[3], OUTPUT);
pinMode(pin[4], OUTPUT);
pinMode(pin[5], OUTPUT);
pinMode(pin[6], OUTPUT);
pinMode(pin[7], OUTPUT);
pinMode(pin[8], OUTPUT);
pinMode(pin[9], OUTPUT);
pinMode(pin[10], OUTPUT);
pinMode(pin[11], OUTPUT);
pinMode(pin[12], OUTPUT);
pinMode(pin[13], OUTPUT);
pinMode(pin[14], OUTPUT);
pinMode(pin[15], OUTPUT);

//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//the pin for the servo co
//enable serial data print
Serial.begin(9600);
Serial.println(“Automacao Residencial”); // so I can keep track of what is loaded

}

void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {

//store characters to string
readString += c;
//Serial.print(c);
}

//if HTTP request has ended
if (c == ‘\n’) {

///////////////////// control arduino pin
Serial.println(readString); //print to serial monitor for debuging

char pesquisa = “?xx”;
for(int i=2 ; i <= 17 ; i++){
pesquisa[2] = i + 48;

pesquisa[1] = ‘l’;
if(readString.indexOf(pesquisa) > 0){
if(tipo_pin[i-2] == 1){
digitalWrite(pin[i-2], HIGH);
Serial.print(“Rele”);
Serial.print(i);
Serial.println(“On”);
delay(200);
digitalWrite(pin[i-2], LOW);
Serial.print(“Rele”);
Serial.print(i);
Serial.println(“Off”);
} else {
digitalWrite(pin[i-2], HIGH);
Serial.print(“Rele”);
Serial.print(i);
Serial.println(“On”);
estado_pin[i-2] = 1;
}
}

pesquisa[1] = ‘d’;
if(readString.indexOf(pesquisa) > 0){
digitalWrite(pin[i-2], LOW);
Serial.print(“Rele”);
Serial.print(i);
Serial.println(“Off”);
estado_pin[i-2] = 0;
}

}

//clearing string for next read
readString="";

///////////////

client.println(“HTTP/1.1 200 OK”); //envia uma nova página
client.println(“Content-Type: text/html”); // identifica a linguagem que a pagina vai utilizar
client.println();

client.println(""); //inicializa o html na página
client.println(""); //cria o cabeçalho da página
client.println(“Automacão Residencial”); //atribui ao cabeçalho o titulo de automacão residencial
client.println(""); //identifica quais o caracteres que vamos utilizar na nossa página
client.println(""); //vai buscar a outra pagina o css
client.println(""); //vai buscar a outra pagina o javascript

client.println(""); //fecha o cabeçalho
client.println("");//cria o corpo da página
client.println("

Automacão Residencial
");

client.println("

");

for(int i=0;i<=15;i++){
client.print("

");
client.print(estado_pin*);*

  • client.println("
");*
  • client.print("<div id='porta");*
  • client.print(i+2);*
  • client.print("titulo’>");*
    client.print(nome_pin*);
    _
    client.println("
  • ");*
    * client.print("
    ");
    _
    if(i==3){*
    * client.println("
    ");*
    * }*
    * }*

    * client.println("

    ");*
    * client.println("");*
    * client.println("");//fecha o corpo*
    * client.println(“”);*
    * client.println("");*
    * delay(1);*
    * //stopping client*
    * client.stop();*
    * }*
    * }*
    * }*
    * }*
    }
    [/td]
    [/tr]
    [/table]
    Thanks in advance

    Hello Fr0zEnG0d! I’ve noticed that you are a native Portuguese speaker, if you desire just send me a personal message and we could talk in Portuguese! :slight_smile:

    If I understood your code right, you want to check if the readString contains the “l2” to “l18” characters, as you have 16 relays to activate. So, in pesquisa[2] you try to use the ASCII code for numbers 2 to 18.

    Buuuut it just work for 2<i<9 as “i + 48” generates the codes 50 to 59 (numbers 2 to 9). After this, the code start to generate random punctuation characters. That’s why just 8 relays are activated.

    for(int i=2 ; i <= 17 ; i++){
                pesquisa[2] = i + 48;
    
                pesquisa[1] = 'l';
                if(readString.indexOf(pesquisa) > 0){
    ...
    

    My bet is try to concatenate the strings to get a better result, like in this code:

    String char1, char2;
    for(int i=2 ; i <= 17 ; i++){
        char1 = 'l';
        char2 = String(i)
        pesquisa = strcat(char1,char2);
        if(readString.indexOf(pesquisa) > 0){