152 lines
3.0 KiB
C++
Executable File
152 lines
3.0 KiB
C++
Executable File
#include <SoftwareSerial.h>
|
|
#include "DHT.h"
|
|
|
|
/** DHT **/
|
|
#define DHTPIN 5
|
|
#define DHTTYPE DHT11
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
|
|
/** ESP8266 **/
|
|
String ssid = "Livebox-37cc";
|
|
String key = "8A6060920A8A86896F770F2C47";
|
|
String serverHost = "192.168.0.10";
|
|
String serverPort = "8080";
|
|
|
|
SoftwareSerial esp8266(10, 11); // RX, TX
|
|
bool done = false;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
|
|
/** DHT **/
|
|
dht.begin();
|
|
Serial.println("1");
|
|
/** ESP8266 **/
|
|
esp8266.begin(9600);
|
|
delay(500);
|
|
esp8266.println("AT+RST");
|
|
Serial.println("2");
|
|
/**
|
|
* Initialisation
|
|
*/
|
|
delay(1000);
|
|
esp8266.println("AT");
|
|
done = esp8266.find("OK");
|
|
if(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
}
|
|
Serial.println(done);
|
|
/**
|
|
* Se mettreen mode CLIENT
|
|
*/
|
|
esp8266.println("AT+CWMODE=1");
|
|
done = esp8266.find("OK");
|
|
if(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
}
|
|
Serial.println(done);
|
|
/**
|
|
* Connexion auWifi
|
|
*/
|
|
esp8266.println("AT+CWJAP=\""+ssid+"\",\""+key+"\"");
|
|
done = esp8266.find("OK");
|
|
while(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
}
|
|
Serial.println(done);
|
|
/**
|
|
* Se mettre en mode multiple connexions
|
|
*/
|
|
esp8266.println("AT+CIPMUX=1");
|
|
done = esp8266.find("OK");
|
|
if(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
}
|
|
Serial.println(done);
|
|
/**
|
|
* Récupération de l'adresse IP
|
|
*/
|
|
esp8266.println("AT+CIFSR");
|
|
done = esp8266.find("STAIP");
|
|
if(!done){
|
|
delay(1000);
|
|
done = esp8266.find("OK");
|
|
}
|
|
Serial.println(done);
|
|
}
|
|
|
|
void loop() {
|
|
int maxLoops = 5;
|
|
|
|
/**
|
|
* DHT11 : Temperature
|
|
*/
|
|
|
|
float temperature = dht.readTemperature();
|
|
if( isnan(temperature) ){
|
|
// return;
|
|
temperature = 19;
|
|
}
|
|
// convert float --> String
|
|
String temperatureStr = "";
|
|
char temperatureChar[15];
|
|
dtostrf(temperature,5,2,temperatureChar);
|
|
temperatureStr = temperatureChar;
|
|
|
|
/**
|
|
* HTTP GET
|
|
*/
|
|
String cmd = "AT+CIPSTART=4,\"TCP\",\""+serverHost+"\","+serverPort;
|
|
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 = "/lda/index.php/data/createdatajson/append?datflval="+
|
|
temperatureStr +
|
|
"&thing=FIRST";
|
|
String cmdGET = "GET " + url + " HTTP/1.1\r\n"+
|
|
"Host: "+serverHost+"\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);
|
|
|
|
// Fermeture de toutes les connexions
|
|
esp8266.println("AT+CIPCLOSE=5");
|
|
delay(1000);
|
|
|
|
// repart à zero
|
|
esp8266.println("AT");
|
|
|
|
// 4 secondes sont déjà passées
|
|
delay(16000);
|
|
|
|
}
|