Code : Tout sélectionner
16:25:13.697 -> Etat du volet : Off
16:25:15.692 -> Start fonction iEnvoiDomoticz
16:25:15.692 -> Url d'appel domoticz = /json.htm?type=command¶m=switchlight&idx=1165&switchcmd=Off
16:25:16.148 -> Domoticz response error
16:25:16.148 -> <html><head><title>Unauthorized</title></head><body><h1>401 Unauthorized</h1></body></html>
16:25:16.194 -> Adresse IP ESP : 192.168.1.58
16:25:16.194 -> iHttpCode : 401
16:25:16.194 -> iEnvoiDomoticz NOK
16:25:16.194 -> On passe en mode sommeilCi-dessous le programme:
Code : Tout sélectionner
/*Position Trappe à poules :
========
Programme pour ESP01s :
- Se connecte au wifi
- Lecture de l'état de la broche définie par DT
- Envois de la valeur On ou Off au serveur domotique via l'appel d'une URL.
Notes :
- La constante LED_BUILDIN ne fonctionne pas sur ESP01s qui est associée à GPIO2
Versions :
-v3 : DT est configurée en INPUT. La résistance de PULL_UP est gérée en externe.
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
/*===================================================================
* Constantes
====================================================================*/
// Quand on compilera la version finale on désactivera le mode debug.
#define DEBUG true
#define GPIO0 0 //Doit être sur + au démarrage si sur - démarre en mode programmation
#define SGL_RST 1 // Tx ou GPIO01 doit etre connecte sur RST pour que la fonction reboot fonctionne
#define DT 3 // GPIO3 ou Rx : Pin GPIO pour savoir si microrupteur sur - ou ouvert (doit avoir une resistance de pullup).
#define LED 2 // Pin GPIO associée à la LED (c'est GPIO02 sur un esp01s)
// temps maxi d'attente pour se connecter au wifi
#define MAX_TIME_WAIT_WIFI 120
// Idx du switch domoticz a mettre à jour
#define DOMOTICZ_SWITCH 1165
/*=====================================================================
* Variables
=====================================================================*/
/* Variables pour le wifi */
IPAddress oipIpEsp(192, 168, 1, 18);
IPAddress oipGateway(192, 168, 1, 1);
IPAddress oipSubnet(255, 255, 255, 0);
IPAddress oipDns(192, 168, 1, 1);
const char* sSsid = "TP-Link_8003_EXT";
const char* sPassword = "********";
const char* sHostname ="esp01-1";
/* Variables pour le serveur domotique */
char *sDomoticzIp = "192.168.1.***";
int iDomoticzPort = 8084;
/* Variables globales */
bool bValeur = true;
/*=====================================================================
* Setup
* =====================================================================*/
void setup() {
// -----------------------------------------------------------------------------------------------------
// Variables locales
// -----------------------------------------------------------------------------------------------------
byte tbMac[ 6 ] = {0,0,0,0,0,0};
// -----------------------------------------------------------------------------------------------------
// Initialisation port série
// -----------------------------------------------------------------------------------------------------
#if DEBUG
Serial.begin(115200);
delay( 100 );
Serial.println( "\nInitialisation liaison serie : OK" );
#endif
// -----------------------------------------------------------------------------------------------------
// Initialisation Wifi
// -----------------------------------------------------------------------------------------------------
WiFi.config(oipIpEsp, oipGateway, oipSubnet, oipDns );
delay(100);
WiFi.disconnect(1); // utile en cas de connexion/deconnexions successives.
WiFi.begin( sSsid, sPassword );
delay( 1000 );
// On va attendre que la connexion s'établisse
int iCompteur = 0;
#if DEBUG
Serial.printf("Connection status: %d\n", WiFi.status());
Serial.print( "Connecting WiFi ." );
#endif
while (WiFi.status() != WL_CONNECTED ){
delay( 1000 ); //on attend une seconde
#if DEBUG
Serial.print( "." );
#endif
if ( iCompteur++ > MAX_TIME_WAIT_WIFI ) {
// On a dépassé le temps maxi autorisé pour se connecter. On reboot l'ESP.
vFonctionReboot();
// Si ces lignes sont executee c'est que le wifi ne s'est pas connecte
// ET que le reboot n'a pas fonctionné (pb de broches ?)
// On met donc l'esp en mode sommeil pour economiser la batterie
ESP.deepSleep( 0, WAKE_RF_DISABLED );
}
}
#if DEBUG
String stRes = "\nConnected to : " + String( sSsid );
stRes += "\nAdresse IP : ";
stRes += WiFi.localIP().toString() ; //Type IPAddress = tableau de 4 entiers.
WiFi.macAddress( tbMac ); //récupère l'adresse MAC
stRes += "\nAdresse MAC : ";
for (int i = 0; i<=5; i++ ) {
if ( i == 0 ) stRes += String( tbMac[ i ], HEX );
else stRes += "." + String( tbMac[ i ], HEX );
}
Serial.println( stRes );
#endif
// -----------------------------------------------------------------------------------------------------
// Initialisation des broches
// -----------------------------------------------------------------------------------------------------
// initialise la broche Detection
pinMode( DT , INPUT );
}
/*=====================================================================
* Loop
* =====================================================================*/
void loop() {
// Variable pour la valeur à envoyer au serveur domotique en fonction de l'état du volet.
String osValue = "";
#if DEBUG
if ( bValeur == true ) Serial.println( "bValeur0 == true" );
else Serial.println( "bValeur0 == false" );
#endif
// On calcul l'état du volet sur la base de la broche NC du microrupteur du haut.
// Si NC est HIGH : le volet est baissé => on renvoi Off
// Si NC est LOW : le volet est ouvert => on renvoi On.
int iDt = digitalRead( DT );
if ( iDt == HIGH ) {
bValeur = false;
osValue = "Off";
} else {
bValeur = true;
osValue = "On";
}
#if DEBUG
Serial.println( "Etat du volet : " + osValue );
delay( 2000 );
#endif
// On envoi la valeur à domoticz
int iResCallDomo = iEnvoiDomoticz( osValue );
#if DEBUG
switch ( iResCallDomo ){
case 0 :
// L'appel de la fonction c'est bien passé
Serial.println( "iEnvoiDomoticz OK" );
break;
default :
// On a un code <> 0 donc il y a une erreur.
// Les codes erreurs sont 1,2.
Serial.println( "iEnvoiDomoticz NOK" );
break;
}
#endif
#if DEBUG
Serial.println( "On passe en mode sommeil" );
#endif
ESP.deepSleep( 0, WAKE_RF_DISABLED );
}
/*=====================================================================
* Fonction lance le reboot de l'ESP en placant D0 à LOW
* La broche SGL_RST doit etre connectée sur RST pour que la fonction reboot fonctionne
* =====================================================================*/
void vFonctionReboot(){
#if DEBUG
Serial.println( "Dans vFonctionReboot" );
#endif
pinMode( SGL_RST, OUTPUT );
digitalWrite( SGL_RST, LOW ); //Broche "SGN_RST" connectée sur RST obligatoire sinon ne fonctionne pas
}
/*=====================================================================
* Fonction pour mettre a jour le capteur dans domoticz
* =====================================================================*/
int iEnvoiDomoticz( String osValue ){
String osUrl = "";
HTTPClient oDomoticzClient;
int iRes = 0;
#if DEBUG
Serial.println( "Start fonction iEnvoiDomoticz" );
#endif
// Construction de l'URL
// Cette URL sera a adapter en fonction de votre installation domotique. La mienne est valable pour domoticz.
osUrl = "/json.htm?type=command¶m=switchlight&idx=";
osUrl += String( DOMOTICZ_SWITCH );
osUrl += "&switchcmd=";
osUrl += String( osValue );
#if DEBUG
Serial.print( "Url d'appel domoticz = " );
Serial.println( osUrl );
#endif
oDomoticzClient.begin( sDomoticzIp, iDomoticzPort, osUrl );
int iHttpCode = oDomoticzClient.GET();
if ( iHttpCode ){
if ( iHttpCode == 200 ){
#if DEBUG
String osPayload = oDomoticzClient.getString();
Serial.println( "Domoticz response" );
Serial.println( osPayload );
#endif
iRes = 0;
} else {
#if DEBUG
String osPayload = oDomoticzClient.getString();
Serial.println( "Domoticz response error " );
Serial.println( osPayload );
Serial.print( "Adresse IP ESP : " );
Serial.println( WiFi.localIP().toString() );
Serial.print( "iHttpCode : " );
Serial.println( iHttpCode );
#endif
iRes = 1;
}
} else {
#if DEBUG
Serial.println( "Domoticz not response" );
#endif
iRes = 2;
}
oDomoticzClient.end();
return iRes;
}