126 lines
2.8 KiB
C++
Executable File
126 lines
2.8 KiB
C++
Executable File
#include <ESP8266WiFi.h>
|
|
|
|
const char* ssid = "Livebox-37cc";
|
|
const char* password = "8A6060920A8A86896F770F2C47";
|
|
|
|
const char* host = "votre_serveur";
|
|
const char* apikey = "1234567890azertyuiop";
|
|
const char* idSonde = "1234";
|
|
|
|
|
|
// définition des broches utilisées
|
|
int trig = 4;
|
|
int echo = 5;
|
|
int alim = 12;
|
|
long lecture_echo;
|
|
long cm;
|
|
|
|
void setup()
|
|
{
|
|
//Serial.begin(115200);
|
|
|
|
//Serial.println();
|
|
//Serial.println();
|
|
//Serial.print("Connecting to ");
|
|
//Serial.println(ssid);
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
IPAddress ip (192,168,0,31);
|
|
IPAddress gateway (192,168,0,1);
|
|
IPAddress subnet (255,255,255,0);
|
|
|
|
WiFi.config(ip, gateway, subnet);
|
|
delay(100);
|
|
uint8_t i = 0;
|
|
while (WiFi.status() != WL_CONNECTED){// && i++ < 10) {
|
|
delay(500);
|
|
//Serial.print(".");
|
|
}
|
|
/*
|
|
if(i>9){
|
|
//Serial.println("Connexion echouée-> deepsleep 5s");
|
|
ESP.deepSleep(1000000*5, WAKE_NO_RFCAL);
|
|
delay(500); //attente activation du deep sleep
|
|
}*/
|
|
|
|
//Serial.println("");
|
|
//Serial.println("WiFi connected");
|
|
//Serial.println("IP address: ");
|
|
//Serial.println(WiFi.localIP());
|
|
|
|
pinMode(trig, OUTPUT);
|
|
pinMode(alim, OUTPUT);
|
|
digitalWrite(trig, LOW);
|
|
pinMode(echo, INPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
|
|
//Serial.println("Alimentation");
|
|
digitalWrite(alim, HIGH);
|
|
delay(1000);
|
|
//Serial.println("Alimentation up for 1s");
|
|
|
|
digitalWrite(trig, LOW);
|
|
delayMicroseconds(2);
|
|
digitalWrite(trig, HIGH);
|
|
delayMicroseconds(10);
|
|
digitalWrite(trig, LOW);
|
|
lecture_echo = pulseIn(echo, HIGH);
|
|
cm = lecture_echo / 58;
|
|
//Serial.print(" - Distancem : ");
|
|
//Serial.println(cm);
|
|
|
|
digitalWrite(alim, LOW);
|
|
delay(100);
|
|
|
|
//Serial.print("connecting to ");
|
|
//Serial.println(host);
|
|
|
|
// Use WiFiClient class to create TCP connections
|
|
WiFiClient client;
|
|
const int httpPort = 80;
|
|
if (!client.connect(host, httpPort)) {
|
|
//Serial.println("connection failed");
|
|
return;
|
|
}
|
|
|
|
// We now create a URI for the request
|
|
String url = "/core/api/jeeApi.php?apikey=";
|
|
url += apikey;
|
|
url += "&type=cmd&id=";
|
|
url += idSonde;
|
|
url += "&slider=";
|
|
url += cm;
|
|
|
|
//Serial.print("Requesting URL: ");
|
|
//Serial.println(url);
|
|
|
|
// This will send the request to the server
|
|
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
|
|
"Host: " + host + "\r\n" +
|
|
"Connection: close\r\n\r\n");
|
|
delay(10);
|
|
|
|
// Read all the lines of the reply from server and print them to Serial
|
|
while(client.available()){
|
|
String line = client.readStringUntil('\r');
|
|
//Serial.print(line);
|
|
}
|
|
|
|
//Serial.println();
|
|
|
|
//Serial.println("closing connection");
|
|
WiFi.disconnect();
|
|
|
|
|
|
//Serial.println("Deep Sleep 1h+");
|
|
ESP.deepSleep(4200000000, WAKE_NO_RFCAL); /// WAKE_RF_DISABLED
|
|
delay(500); //attente activation du deep sleep
|
|
}
|