Étape 3: le code
Il suffit de télécharger ce code à l’arduino :/ * IPhone contrôlée LED
Tour à tour sous et hors tension d’une LED connectée à une broche numérique, via un ordinateur ou
IPhone (ou même un appareil android) sans utiliser internet.
exemples de combinés :
Exemples > Ethernet > Webserver et exemples > numérique > BlinkWithoutDelay
Le circuit :
* LED joint de la broche 9 à terre.
* EthernetShield à l’aide de goupilles 10,11,12,13.
créé mars 2013
par Floris
Floris (at) deboktor.nl
Cet exemple de code est dans le domaine public.
*/
#include < SPI.h >
#include < Ethernet.h >
la mac de Byte [] = {}
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED} ;
IPAddress ip(192,168,30,178) ; choisir une IPadress de sorte que vous pouvez facilement le trouver sur votre ordinateur
EthernetServer server(80) ;
String currentLine = "" ;
char de commandement [100] ;
page int = 2 ;
int prevpage = page ;
long previousMillis = 0 ;
intervalle long = 500 ;
int ledState = faible ;
PageUpdated booléen = false ;
int LedPin = 9 ;
void setup() {}
pinMode (LedPin, sortie) ;
currentLine.reserve(256) ;
Ethernet.Begin (mac, ip) ;
Server.Begin() ;
digitalWrite(LedPin,LOW) ;
}
void loop() {}
EthernetClient client = server.available() ;
Si {(client)
Boolean currentLineIsBlank = true ;
ValidCommand booléen = false ;
int i = 0 ;
tandis que (client.connected()) {}
Si (client.available()) {}
char c = client.read() ;
currentLine += c ;
if (!. ValidCommand) {}
Commande [i] = c ;
i ++ ;
}
Si (c == « \n ») {}
currentLine = "" ;
}
Si (currentLine.endsWith("HTTP/1.1")) {format //line est comme : GET /? s = 1 HTTP/1.1 ou GET /favicon.ico HTTP/1.1
ValidCommand = true ;
Si (commande [5]-'? ' == 0) {//if est le 5e karakter ?
Si (Command[6]-'s'==0) {//then s
PrevPage = page ; pour éviter ce qui donne une étrange commande
page = commande [8]-« 0 »; //after le? = est la commande réelle
Si (page! = 1 & & page! = 2 & & page! = 3) {//1 = activé, 2 = de, 3 = clignotant
page = prevpage ;
}
PageUpdated = false ;
}
}
}
Si (c == « \n » & & currentLineIsBlank) {}
Delay(10) ;
client.Print ("< ! DOCTYPE HTML > \n ") ;
client.Print ("< HTML > < HEAD > de \n \n") ;
client.Print ("< titre > conduit-online < /title > \n") ;
client.Print ("< nom meta = « fenêtre » content =' utilisateur évolutive = yes, largeur = dispositif-largeur" > ") ;
client.Print ("< style type =" text/css' > \n ") ;
client.Print ("body {\n background-color : white ; \n couleur: #222 ; \n font-family : Helvetica ; \n marge: 0 ; \n padding: 0} \n ") ;
client.Print ("div #button_On {\n background-color : blue ; \n border-bottom : 1px solid #666 ; \n couleur: #222 ; \n display : block ; \n-couleur : noir ; \n font-size : 14px ; \n font-weight : bold ; \n padding : 10px 0 ; \n text-align : center ; \n text-decoration : none\n} \n\n ") ;
client.Print ("div #button_Off {\n background-color : #ccc ; \n border-bottom : 1px solid #666 ; \n couleur: #222 ; \n display : block ; \n-couleur : noir ; \n font-size : 14px ; \n font-weight : bold ; \n padding : 10px 0 ; \n text-align : center ; \n text-decoration : none\n} \n\n ") ;
client.Print ("un {\n text-decoration : none ; \n}") ;
client.Print ("\n </style > \n") ;
Si ((page! = 0) & & (! PageUpdated)) {//1 sec après un rafraîchissement de commande la page
client.Print ("< META HTTP-EQUIV = « refresh » CONTENT = 1 ; URL = /'> \n ") ;
PageUpdated = true ;
}
else {//normally une fois everyy 10sec une page mise à jour
client.Print ("< META HTTP-EQUIV = « refresh » CONTENT = 10 ; URL = /'> \n ") ;
}
client.Print ("</HEAD > \n") ;
client.Print ("< BODY > \n\n") ;
client.Print ("< a href ='/? s = 1' > \n") ;
Si (page == 1) {}
client.Print ("< div id = « button_On » >") ;
}
else {}
client.Print ("< div id = « button_Off » >") ;
}
client.Print ("LED ON") ;
client.Print ("</div > \n") ;
client.Print ("< /a > \n") ;
client.Print ("< a href ='/? s = 2' > \n") ;
Si (page == 2) {}
client.Print ("< div id = « button_On » >") ;
}
else {}
client.Print ("< div id = « button_Off » >") ;
}
client.Print ("LED OFF") ;
client.Print ("</div > \n") ;
client.Print ("< /a > \n") ;
client.Print ("< a href ='/? s = 3' > \n") ;
Si (page == 3) {}
client.Print ("< div id = « button_On » >") ;
}
else {}
client.Print ("< div id = « button_Off » >") ;
}
client.Print ("LED clignotant") ;
client.Print ("</div > \n") ;
client.Print ("< /a > \n") ;
rupture ;
}
Si (c == « \n ») {}
currentLineIsBlank = true ;
}
ElseIf (c! = « \r ») {}
currentLineIsBlank = false ;
}
}
}
Delay(1) ;
client.Stop() ;
}
commutateur (page) {}
cas 1 :
digitalWrite(LedPin,HIGH) ;
rupture ;
cas 2 :
digitalWrite(LedPin,LOW) ;
rupture ;
cas 3 :
digitalWrite (LedPin, ledState) ;
if(Millis() - previousMillis > intervalle) {}
previousMillis = millis() ;
Si (ledState == faible)
ledState = élevé ;
d’autre
ledState = bas ;
}
rupture ;
}
}