Bonjour, alors voici le code. C’est la version complète avec buzzer. J’ai essayé de détailler au maximum les étapes. L’idée est que le LIDAR est soit actif, soit en veille, soit en reset forcé, selon la valeur de la variable jeedom « statut » créée et utilisée dans un vitruel :

Désolé pour l’aspect vrac, c’est le résultat de fusion de plusieurs sketches et tests :
/*
* HTTP communication between ESP8266 and Jeedom Smart Home Server
* Communication HTTP (TCP/IP) entre ESP8266 et le serveur domotique Jeedom
* Copyright (C) 2017 https://www.projetsdiy.fr - http://www.diyprojects.io
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Code adapté pour TFmini
* Fonctions ajoutées à sketch de base :
* retour URL
* blink
* requestfromurl (pour lecture variable esp_statut (on/off/reset)
*
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h> //ne semble pas être utile pour sendtojeedom. Besoin pou réponse URL (192.168.86.19/test --> je suis là)
//#include <WiFiClientSecure.h> //ne semble pas être utile (origine sketch sendtojeedom)
#include <ESP8266mDNS.h> //fonctionne sans cette librairie (origine sketch test retour URL)
//---incorpo code BasicReading (Test TFmini)---
#include <Arduino.h>
#include <SoftwareSerial.h>
#include "TFMini.h"
//\\
const char* ssid = "##########";
const char* password = "#############";
const char* host = "192.168.86.236";
const int port = 80;
const char* apiKey = "##############";
const char* apiKeyJeedom = "############";
const char* idDist = "9272"; //numéro ID de l'info virtuelle
const char* idStren = "9273"; //numéro ID de l'info virtuelle
const char* idNbReset = "9279"; //numéro ID de l'info virtuelle
const long interval = 10; // interval at which to measure (milliseconds) (au delà, une latence est observée entre mesure et affichage serial.print)
const int watchdog = 10000; // Fréquence d'envoi des données à Jeedom
const long interval_check_variable = 300000; //fréquence de lecture de la variable esp_statut
unsigned long previousMillisLIDAR = millis(); // d'origine : millis()
unsigned long previousMillisSendJEEDOM = millis(); //envoi des valeurs au virtuel
unsigned long previousMillisReadJEEDOM = millis(); //lecture de la variable esp_statut (on/off/reset)
String statut = ""; //variable globale
String urlread; //variable globale
String nbReset; //attention, si déclarée comme int, posera pb compatibilité avec fonction http.getString
int nbResetInt; //variable globale
int buzzerPin=14; //Buzzer control port, default D5=GPIO14
String baseurl; //en global car aussi utilisée en fonction (en + du loop)
String url; //en global car aussi utilisée en fonction (en + du loop)
ESP8266WebServer server ( 80 ); //ne semble pas être utile pour sendtojeedom. Semble nécessaire pour retour URL
HTTPClient http;
//---incorpo code BasicReading (Test TFmini)---
// Setup software serial port
SoftwareSerial mySerial(5, 4); // connection to TFMini (=D1, D2)
TFMini tfmini; // tfmini access
//\\
void setup() {
Serial.begin(115200);
delay(10);
Serial.setDebugOutput(true);
Serial.println("Connecting Wifi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// on definit ce qui doit etre fait lorsque la route /test est appelee
server.on("/test", [](){
server.send(200, "text/plain", "tout va bien!");
});
// on definit ce qui doit etre fait lorsque la route /buzzer est appelee
server.on("/buzzer", [](){
for(int i=0; i<8; i++){
Serial.println("loop buzzer");
analogWrite(buzzerPin, 512);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
analogWrite(buzzerPin, 0);
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
});
// on commence a ecouter les requetes venant de l'exterieur
server.begin(); //ne semble pas être utile pour sendtojeedom. Besoin pour réponse URL
//---incorpo code BasicReading (Test TFmini)---
// Step 1: Initialize hardware serial port (serial debug port)
// wait for serial port to connect. Needed for native USB port only
while (!Serial);
// Step 2: Initialize the data rate for the SoftwareSerial port
mySerial.begin(TFMINI_BAUDRATE); // default boudrate is 115200
delay(10);
// Step 3: Initialize the TF Mini sensor
tfmini.begin(&mySerial);
Serial.println ("Initializing Serial and mySerial finished");
//\\
//lecture de la valeur de la variable esp_nb_reset :
String baseurlread = "/core/api/jeeApi.php?apikey=";
baseurlread += apiKeyJeedom;
baseurlread += "&type=variable&name=";
urlread = baseurlread + "esp_nb_reset";
//Serial.print("URL nb reset : ");
//Serial.println(urlread);
requestFromJeedom(urlread);
Serial.print("nb reset setup : ");
Serial.println(nbReset);
//delay(500);
//lecture de la valeur de la variable esp_statut :
urlread = baseurlread + "esp_statut";
//Serial.print("URL statut : ");
//Serial.println(urlread);
requestFromJeedom(urlread);
Serial.print("statut setup : ");
Serial.println(statut);
//delay(500);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
server.handleClient(); //ne semble pas être utile pour sendtojeedom. Besoin pour retour URL :
//a chaque iteration, on appelle handleClient pour que les requetes soient traitees
if ( statut == "reset") {
Serial.println("Resetting ESP");
nbResetInt++;
baseurl = "/core/api/jeeApi.php?apikey="; //obligé de refaire la variable même si déclarée en racine loop
baseurl += apiKeyJeedom;
baseurl += "&type=variable&name=";
url = baseurl + "esp_nb_reset";
url += url + "&value="; url += nbResetInt;
sendToJeedom(url);
Serial.print("reset nb : "); Serial.println(nbResetInt);
delay(120000); //120000 //PM variable passe de reset à on au bout de 5mn dans jeedom
ESP.restart(); //ESP.reset();
} else {
if ( statut == "off") {
Serial.println("ESP désactivé, contrôle d'activation dans 2mn");
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); //d'origine : 1000
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(120000); //300000 = 5 minutes
requestFromJeedom(urlread);
} else {
if ( currentMillis - previousMillisLIDAR >= interval ) { //remplacement > par >=
previousMillisLIDAR = currentMillis;
//---incorpo code BasicReading (Test TFmini)---
// Take one TF Mini distance measurement
uint16_t dist = tfmini.getDistance();
uint16_t strength = tfmini.getRecentSignalStrength();
// Display the measurement
Serial.print(dist);
Serial.print(" cm Signal: ");
Serial.println(strength);
//\\
} else {
if ( currentMillis - previousMillisSendJEEDOM >= watchdog ) { //remplacement > par >=
previousMillisSendJEEDOM = currentMillis;
if(WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected !");
} else {
Serial.print("Distance envoyée à Jeedom : ");
Serial.println(dist);
baseurl = "/core/api/jeeApi.php?apikey=";
baseurl += apiKey;
baseurl += "&type=virtual&id=";
url = baseurl + idDist;
url += url + "&value="; url += dist; //String(t);
sendToJeedom(url);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); //d'origine : 1000
url = baseurl + idStren;
url += url + "&value="; url += strength; //String(h);
sendToJeedom(url);
Serial.println("données envoyées");
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); //d'origine : 1000
if ( currentMillis - previousMillisReadJEEDOM >= interval_check_variable ) { //rafraîchit statut tous les interval check
previousMillisReadJEEDOM = currentMillis;
Serial.println("rafraîchissement de la valeur de la variable");
delay(500);
requestFromJeedom(urlread);
}
}
}
}
}
}
}
boolean sendToJeedom(String url) {
//Serial.print("connecting to ");
//Serial.println(host);
//Serial.print("Requesting URL: ");
//Serial.println(url);
http.begin(host,port,url);
int httpCode = http.GET();
//Serial.println("données envoyées");
http.end();
}
String requestFromJeedom(String urlread) {
http.addHeader("Content-Type", "text/html"); // sans cette ligne, httpcode = 302 au bout de qqes requetes
int httpCode = http.GET(); //ligne OBLIGATOIRE!!!
Serial.print("httpcode : ");
Serial.println(httpCode);
if ( httpCode == 302 ) { //|| httpCode ==200 ) {
Serial.println("httpcode = 302, ESP redémarre");
delay(1000); //1000
nbResetInt++;
baseurl = "/core/api/jeeApi.php?apikey="; //obligé de refaire la variable même si déclarée en racine loop
baseurl += apiKeyJeedom;
baseurl += "&type=variable&name=";
url = baseurl + "esp_nb_reset";
url += url + "&value="; url += nbResetInt;
http.end();
sendToJeedom(url);
delay(100);
ESP.restart();
}
http.begin(host,port,urlread);
//int httpCode = http.GET();
//Serial.println("données envoyées");
if ( urlread.indexOf("reset") > 0 ) {
nbReset = http.getString();
Serial.print("URL nb reset : ");
Serial.println(urlread);
Serial.print("nb reset : ");
Serial.println(nbReset);
delay(100);
nbResetInt = nbReset.toInt();
if ( nbReset == "" ) { //couvre aussi la cas où httpCode = -1
Serial.println("variable non lue, requête relancée dans 2s");
delay(1000);
requestFromJeedom(urlread);
}
}
if ( urlread.indexOf("statut") > 0 ) {
http.addHeader("Content-Type", "text/html"); // sans cette ligne, httpcode = 302 au bout de qqes requetes
int httpCode = http.GET();
statut = http.getString();
Serial.print("URL statut : ");
Serial.println(urlread);
Serial.print("statut : ");
Serial.println(statut);
delay(100);
if ( statut == "" ) { //couvre aussi la cas où httpCode = -1
Serial.println("variable non lue, requête relancée dans 2s");
delay(1000);
requestFromJeedom(urlread);
}
}
//Serial.print("statut request : ");
//delay(500);
//Serial.println(statut);
http.end();
}
A votre dispo pour tout précision.