182 lines
3.4 KiB
C++
Executable File
182 lines
3.4 KiB
C++
Executable File
#include <SoftwareSerial.h>
|
|
|
|
SoftwareSerial esp8266(10,11);
|
|
|
|
String ssid = "Livebox-37cc"; // Garder les guillements
|
|
String key = "8A6060920A8A86896F770F2C47"; // Garder les guillements
|
|
|
|
boolean done = false;
|
|
/****************************************************************/
|
|
/* INIT */
|
|
/****************************************************************/
|
|
void setup() {
|
|
// put your setup code here, to run once:
|
|
|
|
//esp8266.begin(115200);
|
|
delay(500);
|
|
esp8266.println("AT+RST");
|
|
|
|
/**
|
|
* Initialisation
|
|
*/
|
|
delay(1000);
|
|
esp8266.println("AT");
|
|
|
|
done = esp8266.find("OK");
|
|
if(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
}
|
|
|
|
|
|
// /**
|
|
// * Se mettre en mode DHCP
|
|
// */
|
|
// esp8266.println("AT+CWDHCP=1,1");
|
|
// done = esp8266.find("OK");
|
|
// if(!done){
|
|
// delay(1000);
|
|
// done = esp8266.find("OK");
|
|
// }
|
|
|
|
/**
|
|
* Se mettre en mode CLIENT
|
|
*/
|
|
esp8266.println("AT+CWMODE=1");
|
|
|
|
done = esp8266.find("OK");
|
|
if(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
}
|
|
|
|
/**
|
|
* Affecter son adresse IP manuellement
|
|
*/
|
|
|
|
// delay(1000);
|
|
// esp8266.println("AT+CIPSTA_DEF=\"192.168.0.200\",\"192.168.0.1\",\"255.255.255.0\"");
|
|
// done = esp8266.find("OK");
|
|
// while(!done){
|
|
// delay(1000);
|
|
// done = esp8266.find("OK");
|
|
// }
|
|
|
|
/**
|
|
* Rechercher les points d'accès WIFI
|
|
*/
|
|
/*
|
|
delay(1000);
|
|
esp8266.println("AT+CWLAP");
|
|
done = esp8266.find("OK");
|
|
while(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
delay(3000);
|
|
break;
|
|
}*/
|
|
|
|
/**
|
|
* Se connecter au point d'accès Wifi défini dans la variable "ssid"
|
|
*/
|
|
esp8266.println("AT+CWJAP=\""+ssid+"\",\""+key+"\"");
|
|
done = esp8266.find("OK");
|
|
while(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
}
|
|
|
|
/**
|
|
* Se mettre en mode connexions multiples
|
|
*/
|
|
esp8266.println("AT+CIPMUX=1");
|
|
done = esp8266.find("OK");
|
|
if(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
}
|
|
|
|
/**
|
|
* afficher son adresse IP
|
|
*/
|
|
esp8266.println("AT+CIFSR");
|
|
|
|
done = esp8266.find("STAIP");
|
|
if(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
}
|
|
|
|
/**
|
|
* faire un ping sur un server
|
|
*/
|
|
/*
|
|
delay(1000);
|
|
esp8266.println("AT+PING=\"192.168.1.100\"");
|
|
done = false;
|
|
if(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
}*/
|
|
|
|
}
|
|
|
|
void loop() {
|
|
int maxLoops = 5;
|
|
|
|
/**
|
|
* Faire un HTTP GET
|
|
*/
|
|
String cmd = "AT+CIPSTART=4,\"TCP\",\"192.168.1.10\",8080";
|
|
esp8266.println(cmd);
|
|
|
|
delay(500);
|
|
done = esp8266.find("OK");
|
|
int currentLoop = 0;
|
|
while(!done){
|
|
delay(500);
|
|
done = esp8266.find("OK");
|
|
if(currentLoop >= maxLoops){
|
|
break;
|
|
}
|
|
currentLoop++;
|
|
}
|
|
|
|
String url = "/json.htm?type=command¶m=switchlight&idx=99&switchcmd=On";
|
|
String cmdGET = "GET " + url + " HTTP/1.1\r\n"+
|
|
"Host: 192.168.1.10\r\nUser-Agent: ESP8266_HTTP_Client\r\nConnection: close\r\n\r\n";
|
|
esp8266.print("AT+CIPSEND=4,");
|
|
esp8266.println(cmdGET.length());
|
|
|
|
delay(1000);
|
|
done = esp8266.find(">");
|
|
currentLoop = 0;
|
|
while(!done){
|
|
delay(500);
|
|
done = esp8266.find(">");
|
|
if(currentLoop >= maxLoops){
|
|
break;
|
|
}
|
|
currentLoop++;
|
|
}
|
|
esp8266.println(cmdGET+"\r\n\r\n");
|
|
delay(1000);
|
|
|
|
|
|
esp8266.println("AT+CIPSTATUS");
|
|
delay(1000);
|
|
|
|
// Close all connections
|
|
|
|
esp8266.println("AT+CIPCLOSE=5");
|
|
delay(1000);
|
|
|
|
// restart from zero
|
|
esp8266.println("AT");
|
|
|
|
// 4 secondes déjà passées
|
|
delay(20000);
|
|
|
|
}
|
|
|