Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Ethernet.h>
- const char web_page[] PROGMEM =
- "<HTML><HEAD>" \
- "<meta name='apple-mobile-web-app-capable' content='yes'>" \
- "<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent'>" \
- "<link rel='stylesheet' type='text/css' href='http://www.progettiarduino.com/uploads/8/1/0/8/81088074/style3.css'>" \
- "<TITLE>Controllo Dispositivi da Remoto</TITLE>"\
- "</HEAD><BODY>" \
- "<H1>Controllo Dispositivi da Remoto</H1><br>" \
- "<H2>Arduino UNO con Ethernet Shield</H2><br>" \
- "<button id='button1' onClick='sendRequest(this.id);'>Accendi/Spegni LED 1</button><br>" \
- "<div id='outputLed1'> Spento </div><br><br>" \
- "<button id='button2' onClick='sendRequest(this.id);'>Accendi/Spegni LED 2</button><br>" \
- "<div id='outputLed2'> Spento </div><br><br>" \
- "<button id='button3' onClick='sendRequest(this.id);'>Accendi/Spegni LED 3</button><br>" \
- "<div id='outputLed3'> Spento </div><br><br>" \
- "<button id='button4' onClick='sendRequest(this.id);'>Accendi/Spegni LED 4</button><br>" \
- "<div id='outputLed4'> Spento </div><br><br>" \
- "<button id='button5' onClick='sendRequest(this.id);'>Accendi/Spegni LED 5</button><br>" \
- "<div id='outputLed5'> Spento </div><br><br>" \
- "<br><br><p>Creato da Michele Zandoná </p>" \
- "<script>" \
- "function updateOutEl(obj) {" \
- " var outs = obj.split(';');" \
- " outs.forEach( function(item, index) {" \
- " var el = 'outputLed' + (index+1).toString() ;" \
- " document.getElementById( el).innerHTML = item;" \
- " });" \
- "}" \
- "function sendRequest(id) {" \
- " var xhttp = new XMLHttpRequest();" \
- " xhttp.onreadystatechange = function() {" \
- " if (this.readyState == 4 && this.status == 200)" \
- " updateOutEl(this.responseText);" \
- " };" \
- " var request = '/?' + id + 'on';" \
- " console.log(request);" \
- " xhttp.open('GET', request, true);" \
- " xhttp.send();" \
- "}" \
- "var myVar = setInterval(" \
- " function(){" \
- " var xhttp = new XMLHttpRequest();" \
- " xhttp.onreadystatechange = function() {" \
- " if(this.readyState == 4 && this.status == 200)"\
- " updateOutEl(this.responseText);" \
- " };" \
- " xhttp.open('GET', '/updateOuts', true);" \
- " xhttp.send();" \
- " }," \
- "1000); " \
- "</script>" \
- "</BODY></HTML>" ;
- const byte outputLed[] = {13, 14, 2, 3, 5}; // I pin su cui sono collegate le uscite
- const byte inputBack[] = {13, 14, 2, 3, 5}; // I pin di ingresso per il feedback on/off (uso gli stessi per semplicità)
- uint32_t onTime[] = {0, 0, 0, 0, 0}; // Il tempo in cui vengono attivate le uscite (cosi evitiamo l'uso del delay(5000))
- int val = 0;
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // mac address
- byte ip[] = { 192, 168, 2, 177 }; // configurate il vostro IP di arduino a vostra scelta("192.168.1.31")
- EthernetServer server(80); //server port
- int refreshPage = 5;
- String readString;
- String updateOutState(){
- String outState;
- for(byte i=0; i<sizeof(outputLed); i++){
- if(digitalRead(inputBack[i]))
- outState += "Acceso";
- else
- outState += "Spento";
- if(i != sizeof(outputLed)-1)
- outState += "; ";
- }
- Serial.println(outState);
- return outState;
- }
- void setup() {
- // Apro la comunicazione seriale:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- //Imposto i pin come OUTPUT
- for(byte i=0; i<sizeof(outputLed); i++){
- pinMode(outputLed[i], OUTPUT);
- }
- //Imposto i pin come INGRESSO
- for(byte i=0; i<sizeof(inputBack); i++){
- pinMode(inputBack[i], INPUT);
- }
- // Inizio la comunicazione Ethernet con il server:
- Ethernet.begin(mac, ip);
- server.begin();
- Serial.print("server is at ");
- Serial.println(Ethernet.localIP());
- }
- void loop() {
- // Spengo il led dopo 5 secondi senza delay
- for(byte i=0; i<sizeof(outputLed); i++){
- if(millis() - onTime[i] > 5000){
- digitalWrite(outputLed[i], LOW);
- }
- }
- EthernetClient client = server.available(); // Creo una connessione al client
- if (client) {
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- //Leggo i caratteri da HTTP
- if (readString.length() < 100) {
- //Inserisco i caratteri nella stringa
- readString += c;
- //Serial.print(c);
- }
- //Se la richiesta HTTP è andata a buon fine
- if (c == '\n') {
- Serial.println(readString); //scrivi sul monitor seriale per debugging
- //Controlli su Arduino:
- // Se è una richiesta di aggiornamento stato delle uscite
- if(readString.indexOf("/updateOuts") > -1){
- client.print(updateOutState());
- }
- // Se è stato premuto un pulsante qualsiasi
- else if (readString.indexOf("/?button") > -1) {
- //Ricaviamo dinamicamente le informazioni per capire quale pulsante è stato premuto
- int start = readString.indexOf("/?button") + sizeof("/?button") -1;
- int stop = readString.indexOf("on", start) ;
- int btn = readString.substring(start, stop).toInt() - 1; // Il primo elemento dell'array ha indice 0
- Serial.print("Pulsante: "); Serial.print(btn);
- Serial.print("\t Uscita: "); Serial.print(outputLed[btn]);
- digitalWrite(outputLed[btn], HIGH);
- onTime[btn] = millis();
- client.print(updateOutState());
- }
- // Nessuna delle precedenti -> invio l'homepage
- else if (readString.indexOf("/ HTTP/1.1") > -1 ){
- Serial.println("Invio dell'homepage");
- // Invio della pagina web
- // Siccome usiamo PROGMEM per non sprecare RAM, è necessario recuperare i dati dalla flash con queste istruzioni
- for (int k = 0; k < strlen_P(web_page); k++) {
- char myChar = pgm_read_byte_near(web_page + k);
- client.print(myChar);
- }
- }
- readString = "";
- client.stop();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement