Étape 3: Code, il
Voici le code pour le déplacement de servo basé sur ce qui est écrit à l’adresse ip.
#include
#include #include #include #include
#define WIFI_AP « Votre réseau WiFi »
#define WIFI_PASSWORD « Mot de passe »
#define WIFI_AUTH LWIFI_WPA / / LWIFI_OPEN, LWIFI_WPA ou LWIFI_WEP, choisissez selon votre configuration WiFi AP / / si cela ne fonctionne pas, essayez LWIFI_WEP. Si votre réseau ne possède pas un mot de passe, utilisez LWIFI_OPEN.
serverPort int = 80 ;
LWiFiServer server(serverPort) ;
int LED = 13 ;
Servo camservo ;
void setup()
{
camservo.Attach(3) ;
pinMode (LED, sortie) ;
LWiFi.begin() ;
Serial.Begin(115200) ; garder une nouvelle tentative jusqu'à ce que connecté à AP
Serial.println (« Connecting to AP ») ;
tandis que (0 == LWiFi.connect (WIFI_AP, LWiFiLoginInfo (WIFI_AUTH, WIFI_PASSWORD))) {}
digitalWrite (LED, haute) ;
Delay(300) ;
digitalWrite (LED, faible) ;
}
Server.Begin() ;
}
loopCount int = 0 ;
void loop()
{
Dim str As String = "" ;
String url = "" ;
int i ;
Delay(250) ;
loopCount ++ ;
LWiFiClient client = server.available() ;
Si (client)
{
Boolean currentLineIsBlank = true ;
tandis que (client.connected())
{
Si (client.available())
{
char c = client.read() ;
Serial.Print(c) ;
Si (c! = « \n »)
Str += c ;
Si (c == « \n »)
{
Serial.println(STR) ;
Si (str.startsWith ("GET"))
{
URL = str.substring (4, str.lastIndexOf("")) ;
Serial.Print("URL:") ;
Serial.Print(URL) ;
Serial.println(":") ;
}
Str = "" ;
}
Si (c == « \n » & & currentLineIsBlank)
{
Serial.println (la « réponse envoyer") ;
client.println ("HTTP/1.1 200 OK") ;
client.println ("Content-Type : text/html") ;
client.println ("Connection : close") ;
client.println() ;
Si (url! = String("favicon.ico"))
{
client.println("") ;
J’ai = digitalRead(LED) ;
url.toLowerCase() ;
if(URL == String("/lookleft"))
{
pour (pos = 90; pos < = 180; pos += 1)
{
camservo.Write(POS) ;
Delay(10) ;
}
digitalWrite (LED, haute) ;
}
d’autre if(url == String("/lookright"))
{
digitalWrite (LED, faible) ;
pour (pos = 90; pos > = 00; pos-= 1)
{
camservo.Write(POS) ;
Delay(10) ;
}
}
client.println() ;
rupture ;
}
}
Si (c == « \n »)
{
currentLineIsBlank = true ;
}
ElseIf (c! = « \r »)
{
currentLineIsBlank = false ;
}
}
}
Delay(30) ;
client.Stop() ; } }