View difference between Paste ID: SyqW5mqy and r01ignpd
SHOW: | | - or go back to the newest paste.
1
2
#include <SPI.h>
3
#include <Ethernet.h>
4-
int led1 = 2;          //relay luce 1
4+
5-
int led2 = 3;          //relay luce 2
5+
6-
int led3 = 4;          //relay luce bancone
6+
7
  "<HTML><HEAD>"  \
8-
int ritorno1 = 5;
8+
9-
int ritorno2 = 6;
9+
10-
int ritorno3 = 7;
10+
11
12
  "<TITLE>Controllo Dispositivi da Remoto</TITLE>"\
13
  "</HEAD><BODY>" \
14
  "<H1>Controllo Dispositivi da Remoto</H1><br>"  \
15-
  "<HTML><HEAD>"      \
15+
16
17
  "<button id='button1' onClick='sendRequest(this.id);'>Accendi/Spegni LED 1</button><br>"    \
18
  "<div id='outputLed1'> Spento </div><br><br>"    \
19
20-
  "<TITLE>Controllo Dispositivi da Remoto</TITLE>"    \
20+
21
  "<div id='outputLed2'> Spento </div><br><br>"    \
22
23
  "<button id='button3' onClick='sendRequest(this.id);'>Accendi/Spegni LED 3</button><br>"    \
24
  "<div id='outputLed3'> Spento </div><br><br>"    \
25
26-
  "<div id='button1on'> Acceso </div><br><br>"    \
26+
  "<button id='button4' onClick='sendRequest(this.id);'>Accendi/Spegni LED 4</button><br>"    \
27
  "<div id='outputLed4'> Spento </div><br><br>"    \
28
29-
  "<div id='button2on'> Acceso </div><br><br>"    \
29+
  "<button id='button5' onClick='sendRequest(this.id);'>Accendi/Spegni LED 5</button><br>"    \
30
  "<div id='outputLed5'> Spento </div><br><br>"    \
31
32-
  "<div id='button3on'> Acceso </div><br><br>"    \
32+
33
  
34-
  "<button id='button13' onClick='sendRequest(this.id);'>Accendi/Spegni LED 13</button><br>"    \
34+
  "<script>"                                                   \
35-
  "<div id='button13on'> Acceso </div><br><br>"    \
35+
    "function updateOutEl(obj) {"                              \  
36
    "   var outs = obj.split(';');"                            \  
37-
  "<button id='button14' onClick='sendRequest(this.id);'>Accendi/Spegni LED 14</button><br>"    \
37+
    "   outs.forEach( function(item, index) {"                 \  
38-
  "<div id='button14on'> Acceso </div><br><br>"    \
38+
    "       var el = 'outputLed' + (index+1).toString() ;"     \  
39
    "       document.getElementById( el).innerHTML = item;"    \  
40
    "   });"                                                   \  
41-
  "<script>"  \
41+
    "}"                                                        \  
42-
  "    function sendRequest(id) {"    \
42+
    
43-
  "      var elId = id + 'on';"   \
43+
    "function sendRequest(id) {"                               \  
44-
  "      document.getElementById(elId).innerHTML = '';"   \
44+
    "   var xhttp = new XMLHttpRequest();"                     \  
45-
  "      var xhttp = new XMLHttpRequest();"   \
45+
    "   xhttp.onreadystatechange = function() {"               \  
46-
  "      xhttp.onreadystatechange = function() {" \
46+
    "       if (this.readyState == 4 && this.status == 200)"   \  
47-
  "        if (this.readyState == 4 && this.status == 200) {         "    \
47+
    "           updateOutEl(this.responseText);"               \  
48-
  "          document.getElementById(elId).innerHTML = this.responseText;"    \
48+
    "   };"                                                    \  
49-
  "        }" \
49+
    
50-
  "      };"  \
50+
    "   var request = '/?' + id + 'on';"                       \  
51-
  "      var request = '/?' + id + 'on';"   \
51+
    "   console.log(request);"                                 \  
52-
  "      console.log(request);           "   \
52+
    "   xhttp.open('GET', request, true);"                     \  
53-
  "      xhttp.open('GET', request, true);"  \
53+
    "   xhttp.send();"                                         \  
54-
  "      xhttp.send();"   \
54+
    "}"                                                        \  
55-
  "    }" \
55+
    
56-
  "</script>" \
56+
    "var myVar = setInterval("                                 \  
57
    "   function(){"                                           \  
58
    "       var xhttp = new XMLHttpRequest();"                 \  
59
    "       xhttp.onreadystatechange = function() {"           \  
60
    "           if(this.readyState == 4 && this.status == 200)"\  
61
    "               updateOutEl(this.responseText);"           \  
62
    "       };"                                                \  
63
64
    "       xhttp.open('GET', '/updateOuts', true);"           \  
65
    "       xhttp.send();"                                     \  
66
    "   },"                                                    \  
67
    "1000); "                                                  \
68
  "</script>"                                                  \  
69
  "</BODY></HTML>" ;
70
71
72
const byte outputLed[] = {13, 14, 2, 3, 5};       // I pin su cui sono collegate le uscite
73
const byte inputBack[] = {13, 14, 2, 3, 5};       // I pin di ingresso per il feedback on/off (uso gli stessi per semplicità)
74
uint32_t   onTime[]    = {0, 0, 0, 0, 0};       // Il tempo in cui vengono attivate le uscite (cosi evitiamo l'uso del delay(5000))
75-
  pinMode(led1, OUTPUT);
75+
76-
  pinMode(led2, OUTPUT);
76+
77-
  pinMode(led3, OUTPUT);
77+
78
byte ip[] = { 192, 168, 2, 177 };                      // configurate il vostro IP di arduino a vostra scelta("192.168.1.31")
79-
  pinMode(ritorno1, INPUT);  //Il pin del pulsante è un'entrata
79+
80-
  pinMode(ritorno2, INPUT);
80+
81-
  pinMode(ritorno3, INPUT);
81+
82
String readString;
83
84
85
String updateOutState(){
86
    String outState;
87
    for(byte i=0; i<sizeof(outputLed); i++){
88
      if(digitalRead(inputBack[i]))
89
        outState += "Acceso";
90
      else
91
        outState += "Spento";
92
      if(i != sizeof(outputLed)-1)
93
        outState += "; ";
94
    }
95
    Serial.println(outState);
96
    return outState;
97
}
98
99
void setup() {
100
  // Apro la comunicazione seriale:
101
  Serial.begin(9600);
102
  while (!Serial) {
103
    ; // wait for serial port to connect. Needed for Leonardo only
104
  }
105
  //Imposto i pin come OUTPUT
106
  for(byte i=0; i<sizeof(outputLed); i++){
107
      pinMode(outputLed[i], OUTPUT);    
108
  }
109
110
  //Imposto i pin come INGRESSO
111
  for(byte i=0; i<sizeof(inputBack); i++){
112-
          // Se è stato premuto il pulsante 1 sul webserver
112+
      pinMode(inputBack[i], INPUT);    
113-
          if (readString.indexOf("/?button1on") > -1) {
113+
114-
            digitalWrite(led1, HIGH);
114+
115
116-
            if (digitalRead (ritorno1)) {
116+
117-
              client.println("Acceso");
117+
118-
            }
118+
119-
            else {
119+
120-
              client.println("Spento");
120+
121-
            }
121+
122
123-
            delay (5000);
123+
124-
            digitalWrite(led1, LOW);
124+
125
126
  // Spengo il led dopo 5 secondi senza delay
127-
          // Se è stato premuto il pulsante 2 sul webserver
127+
  for(byte i=0; i<sizeof(outputLed); i++){
128-
          else if (readString.indexOf("/?button2on") > -1) {
128+
    if(millis() - onTime[i] > 5000){
129-
            digitalWrite(led2, HIGH);
129+
      digitalWrite(outputLed[i], LOW);   
130
    }
131-
            if (digitalRead (ritorno2)) {
131+
132-
              client.println("Acceso");
132+
    
133-
            }
133+
134-
            else {
134+
135-
              client.println("Spento");
135+
136-
            }
136+
137-
            delay (5000);
137+
138-
            digitalWrite(led2, LOW);
138+
139
140
        //Leggo i caratteri da HTTP
141-
          //Se è stato premuto il pulsante 3 sul webserver
141+
142-
          else if (readString.indexOf("/?button3on") > -1) {
142+
143-
            digitalWrite(led3, HIGH);
143+
144-
            if (digitalRead (ritorno3)) {
144+
145-
              client.println("Acceso");
145+
146-
            }
146+
147-
            else {
147+
148-
              client.println("Spento");
148+
149-
            }
149+
150-
            delay (5000);
150+
151-
            digitalWrite(led3, LOW);
151+
152
153
          // Se è una richiesta di aggiornamento stato delle uscite
154
          if(readString.indexOf("/updateOuts") > -1){
155-
          // Estrazione del pin dal comando ricevuto
155+
            client.print(updateOutState());
156
          }
157
          
158
          // Se è stato premuto un pulsante qualsiasi
159
          else if (readString.indexOf("/?button") > -1) {
160-
            int pin = readString.substring(start, stop).toInt();
160+
            //Ricaviamo dinamicamente le informazioni per capire quale pulsante è stato premuto
161
            int start = readString.indexOf("/?button") + sizeof("/?button") -1;
162-
            Serial.print("Pin: "); Serial.println(pin);
162+
163
            int btn = readString.substring(start, stop).toInt() - 1;  // Il primo elemento dell'array ha indice 0
164-
            digitalWrite(pin, HIGH);
164+
165-
             if (digitalRead(pin)) 
165+
            Serial.print("Pulsante: "); Serial.print(btn);
166-
              client.println("Acceso");            
166+
            Serial.print("\t Uscita: "); Serial.print(outputLed[btn]);
167-
            else 
167+
168-
              client.println("Spento");
168+
            digitalWrite(outputLed[btn], HIGH);
169-
           
169+
            onTime[btn] = millis();
170-
            delay (5000);
170+
            client.print(updateOutState());
171-
            digitalWrite(pin, LOW);
171+
172
          }
173
174
          // Nessuna delle precedenti -> invio l'homepage
175
          else if (readString.indexOf("/ HTTP/1.1") > -1 ){
176
            Serial.println("Invio dell'homepage");
177
            
178
            // Invio della pagina web
179
            // Siccome usiamo PROGMEM per non sprecare RAM, è necessario recuperare i dati dalla flash con queste istruzioni
180
            for (int k = 0; k < strlen_P(web_page); k++) {
181
              char myChar = pgm_read_byte_near(web_page + k);
182
              client.print(myChar);
183
            }    
184
                  
185
          }
186
187
          readString = "";  
188
          client.stop();
189
        }        
190
      }      
191
    }
192
  }
193
}