Revision de comportamiento en perdidas de conexion.
Preparacion para conexion por api configuraciones nuevasdevelop
parent
108675e4a7
commit
89869031c8
|
|
@ -0,0 +1,76 @@
|
||||||
|
#ifndef DomoEspApiConexionDef
|
||||||
|
#define DomoEspApiConexionDef 1
|
||||||
|
#include "defines.h"
|
||||||
|
#include <ESP8266HTTPClient.h>
|
||||||
|
//sensor Replica, sensor interno que se setea con get y no se publica
|
||||||
|
class DomoEspApiConexion
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DomoEspApiConexion()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool send(char *topic, char*payload)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getDataPendiente(char * idArduino, char *topic, char *payloadOut)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool sendPost(char * buff, char* srv, char* datos)
|
||||||
|
{
|
||||||
|
HTTPClient http;
|
||||||
|
WiFiClient client;
|
||||||
|
int httpCode =-1;
|
||||||
|
if(buff)
|
||||||
|
*buff=0;
|
||||||
|
if(http.begin(client, srv))
|
||||||
|
{
|
||||||
|
http.addHeader("Content-Type", "application/json"); // O el tipo de contenido adecuado
|
||||||
|
//String httpRequestData = "{\"dato1\":\"valor1\", \"dato2\":\"valor2\"}"; // Datos en formato JSON
|
||||||
|
httpCode = http.POST(datos);
|
||||||
|
|
||||||
|
if (httpCode > 0) {
|
||||||
|
String payload = http.getString();
|
||||||
|
if(buff)
|
||||||
|
strcpy( buff, payload.c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
http.end();
|
||||||
|
}
|
||||||
|
return httpCode==200;
|
||||||
|
}
|
||||||
|
bool sendGet(char * buff, char* srv, char* datos)
|
||||||
|
{
|
||||||
|
HTTPClient http;
|
||||||
|
WiFiClient client;
|
||||||
|
int httpCode =-1;
|
||||||
|
if(buff)
|
||||||
|
*buff=0;
|
||||||
|
if(http.begin(client, srv))
|
||||||
|
{
|
||||||
|
http.addHeader("Content-Type", "application/json"); // O el tipo de contenido adecuado
|
||||||
|
//String httpRequestData = "{\"dato1\":\"valor1\", \"dato2\":\"valor2\"}"; // Datos en formato JSON
|
||||||
|
if(datos)
|
||||||
|
httpCode = http.sendRequest("GET",datos);
|
||||||
|
else
|
||||||
|
httpCode = http.GET();
|
||||||
|
|
||||||
|
|
||||||
|
if (httpCode == HTTP_CODE_OK ) {
|
||||||
|
String payload = http.getString();
|
||||||
|
if(buff)
|
||||||
|
strcpy( buff, payload.c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
http.end();
|
||||||
|
}
|
||||||
|
return httpCode==200;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
@ -9,7 +9,9 @@ class DomoEspConfig
|
||||||
char ssidWifi[24];//nombre wifi al que conectar
|
char ssidWifi[24];//nombre wifi al que conectar
|
||||||
char keyWifi[32];//key wifi a conectar
|
char keyWifi[32];//key wifi a conectar
|
||||||
char ideEsp[32];//identificador unico por esp
|
char ideEsp[32];//identificador unico por esp
|
||||||
char hostMQTT[16];
|
char hostMQTT[32];//ip o url al host
|
||||||
|
char keyhost[32];//key para conectar al host
|
||||||
|
bool conexionPorApi;
|
||||||
int portMQTT;
|
int portMQTT;
|
||||||
|
|
||||||
int velocidadPortSerie;
|
int velocidadPortSerie;
|
||||||
|
|
@ -19,6 +21,8 @@ class DomoEspConfig
|
||||||
|
|
||||||
DomoEspConfig()
|
DomoEspConfig()
|
||||||
{
|
{
|
||||||
|
conexionPorApi=false;
|
||||||
|
keyhost[0]=0;
|
||||||
velocidadPortSerie=115200;
|
velocidadPortSerie=115200;
|
||||||
strcpy(ssidWifi,"Idhun");//nombre wifi
|
strcpy(ssidWifi,"Idhun");//nombre wifi
|
||||||
strcpy(keyWifi,"Ardileorca1234.");//key wifi
|
strcpy(keyWifi,"Ardileorca1234.");//key wifi
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ DomoEspManager::DomoEspManager()
|
||||||
tiempo=0;
|
tiempo=0;
|
||||||
suscrito=false;
|
suscrito=false;
|
||||||
SetTimeRefres(15);
|
SetTimeRefres(15);
|
||||||
|
tNoConexion.set(300);
|
||||||
|
tNomqtt.set(300);
|
||||||
}
|
}
|
||||||
void DomoEspManager::SetTimeRefres(int seg)
|
void DomoEspManager::SetTimeRefres(int seg)
|
||||||
{
|
{
|
||||||
|
|
@ -20,7 +22,7 @@ void DomoEspManager::inicia(DomoEspConfig* cnf)
|
||||||
conf=cnf;
|
conf=cnf;
|
||||||
#ifdef DEBUG_PS
|
#ifdef DEBUG_PS
|
||||||
Serial.begin(conf->velocidadPortSerie);
|
Serial.begin(conf->velocidadPortSerie);
|
||||||
delay(100);
|
delay(1000);
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println("Iniciando");
|
Serial.println("Iniciando");
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -31,7 +33,14 @@ void DomoEspManager::inicia(DomoEspConfig* cnf)
|
||||||
#ifdef DEBUG_PS
|
#ifdef DEBUG_PS
|
||||||
Serial.println("Inicia Mqtt");
|
Serial.println("Inicia Mqtt");
|
||||||
#endif
|
#endif
|
||||||
|
if(conf->conexionPorApi)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
mqtt.inicia(&clienteMqtt,conf->ideEsp, conf->hostMQTT, conf->portMQTT, this);
|
mqtt.inicia(&clienteMqtt,conf->ideEsp, conf->hostMQTT, conf->portMQTT, this);
|
||||||
|
//loguea------------
|
||||||
|
MqttSend("casa/log", conf->ideEsp);
|
||||||
|
|
||||||
#ifdef DEBUG_PS
|
#ifdef DEBUG_PS
|
||||||
Serial.println("Configura Sensores");
|
Serial.println("Configura Sensores");
|
||||||
|
|
@ -51,15 +60,22 @@ void DomoEspManager::loop()
|
||||||
if(!wifi.loop())
|
if(!wifi.loop())
|
||||||
{
|
{
|
||||||
suscrito=false;
|
suscrito=false;
|
||||||
|
if(!wifi.conecta())
|
||||||
|
{
|
||||||
|
if(tNoConexion.onTimer())
|
||||||
|
ESP.reset();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
tNoConexion.inicia();
|
||||||
if(!mqtt.loop())
|
if(!mqtt.loop())
|
||||||
{
|
{
|
||||||
suscrito=false;
|
suscrito=false;
|
||||||
|
if(tNomqtt.onTimer())
|
||||||
|
ESP.reset();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
tNomqtt.inicia();
|
||||||
if(!suscrito)
|
if(!suscrito)
|
||||||
{
|
{
|
||||||
for(int i=0; i<n; i++)
|
for(int i=0; i<n; i++)
|
||||||
|
|
@ -71,7 +87,16 @@ void DomoEspManager::loop()
|
||||||
int tiempoAux=0;
|
int tiempoAux=0;
|
||||||
if(timer.onTimerReset())
|
if(timer.onTimerReset())
|
||||||
{
|
{
|
||||||
|
if(conf->conexionPorApi)
|
||||||
|
{
|
||||||
|
char topic_aux[32];
|
||||||
|
char payload_aux[128];
|
||||||
|
|
||||||
|
while(api.getDataPendiente(conf->ideEsp,topic_aux, payload_aux))
|
||||||
|
{
|
||||||
|
OnMqtt(topic_aux, payload_aux);
|
||||||
|
}
|
||||||
|
}
|
||||||
tiempo=(tiempo+1)%2;
|
tiempo=(tiempo+1)%2;
|
||||||
tiempoAux=tiempo+1;
|
tiempoAux=tiempo+1;
|
||||||
#ifdef DEBUG_PS
|
#ifdef DEBUG_PS
|
||||||
|
|
@ -137,10 +162,21 @@ void DomoEspManager::SubscribeMqtt(PubSubClient *client_mqtt)
|
||||||
|
|
||||||
void DomoEspManager::MqttSend(char* topic, char* payload)
|
void DomoEspManager::MqttSend(char* topic, char* payload)
|
||||||
{
|
{
|
||||||
|
if(conf->conexionPorApi)
|
||||||
|
api.send(topic, payload);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(suscrito);
|
||||||
clienteMqtt.publish(topic, payload);
|
clienteMqtt.publish(topic, payload);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
void DomoEspManager::MqttSubs(char* topic)
|
void DomoEspManager::MqttSubs(char* topic)
|
||||||
{
|
{
|
||||||
|
if(conf->conexionPorApi)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
clienteMqtt.subscribe(topic);
|
clienteMqtt.subscribe(topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include "utiles.h"
|
#include "utiles.h"
|
||||||
|
#include "DomoEspApiConexion.h"
|
||||||
class DomoEspConfig;
|
class DomoEspConfig;
|
||||||
class IDomoEspLisener
|
class IDomoEspLisener
|
||||||
{
|
{
|
||||||
|
|
@ -21,13 +22,15 @@ class DomoEspManager: public MqttReceiver, public IMqttManager, public ISensorMa
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
PubSubClient clienteMqtt;
|
PubSubClient clienteMqtt;
|
||||||
MqttManager mqtt;
|
MqttManager mqtt;
|
||||||
|
DomoEspApiConexion api;
|
||||||
|
|
||||||
DomoEspSensorReceiver* sensores[MAXSENS];
|
DomoEspSensorReceiver* sensores[MAXSENS];
|
||||||
int n;
|
int n;
|
||||||
int tiempo;
|
int tiempo;
|
||||||
bool suscrito;
|
bool suscrito;
|
||||||
Ctimer timer;
|
Ctimer timer;
|
||||||
|
Ctimer tNoConexion;
|
||||||
|
Ctimer tNomqtt;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
DomoEspManager();
|
DomoEspManager();
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ class SensorDHT: public DomoEspSensorReceiver
|
||||||
void set(uint8_t _pin, char* topic_id)
|
void set(uint8_t _pin, char* topic_id)
|
||||||
{
|
{
|
||||||
pin=_pin;
|
pin=_pin;
|
||||||
t=h=0;
|
t=h=9.99998;
|
||||||
strcpy(topic, topic_id);
|
strcpy(topic, topic_id);
|
||||||
}
|
}
|
||||||
virtual void procesa(IMqttManager * man, int tiempo)
|
virtual void procesa(IMqttManager * man, int tiempo)
|
||||||
|
|
@ -43,8 +43,17 @@ class SensorDHT: public DomoEspSensorReceiver
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
/*if(t==h && t==9.99998)
|
||||||
|
{*/
|
||||||
t=ta;
|
t=ta;
|
||||||
h=ha;
|
h=ha;
|
||||||
|
/*}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
t=(t+ta)/2;
|
||||||
|
h=(h+ha)/2;
|
||||||
|
}*/
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -168,9 +168,9 @@ bool WifiManager::loop()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#ifdef DEBUG_PS
|
#ifdef DEBUG_PS
|
||||||
Serial.println("Fallo de conexion, se reinicia arduino");
|
Serial.println("Fallo de conexion wifi");
|
||||||
#endif
|
#endif
|
||||||
ESP.reset();
|
//ESP.reset();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//**************************************************************************************************************************************************
|
//**************************************************************************************************************************************************
|
||||||
|
|
@ -207,9 +207,9 @@ bool MqttManager::loop()
|
||||||
|
|
||||||
}
|
}
|
||||||
#ifdef DEBUG_PS
|
#ifdef DEBUG_PS
|
||||||
Serial.println("Fallo de conexion, se reinicia arduino");
|
Serial.println("Fallo de conexion a mqtt broker");
|
||||||
#endif
|
#endif
|
||||||
ESP.reset();
|
//ESP.reset();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
void MqttManager::subscribe_mqtt()
|
void MqttManager::subscribe_mqtt()
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,29 @@
|
||||||
|
|
||||||
|
|
||||||
//configuraciones-----------------------------------------------------
|
//configuraciones-----------------------------------------------------
|
||||||
|
|
||||||
|
class PruebaConexiones: public DomoEspConfig
|
||||||
|
{
|
||||||
|
SensorDHT dht;
|
||||||
|
SensorDout led;
|
||||||
|
public:
|
||||||
|
PruebaConexiones()
|
||||||
|
{
|
||||||
|
strcpy(hostMQTT,"192.168.2.115");//servidor mqttBroker
|
||||||
|
strcpy(ssidWifi,"IdhunDesa");//nombre wifi
|
||||||
|
strcpy(ideEsp,"Esp8266_PCon");//idenitificador del esp (sera único)
|
||||||
|
dht.set(D1,"casa/PCon");
|
||||||
|
led.set(D4, "casa/PCon/led",0,0);
|
||||||
|
}
|
||||||
|
virtual void inicia(ISensorManager* man)
|
||||||
|
{
|
||||||
|
man->Add(&dht);
|
||||||
|
man->Add(&led);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
class ConfAutomatismos: public DomoEspConfig
|
class ConfAutomatismos: public DomoEspConfig
|
||||||
{
|
{
|
||||||
SensorRF presencia;
|
SensorRF presencia;
|
||||||
|
|
@ -391,10 +414,10 @@ class ConfigAltavozCocina: public DomoEspConfig
|
||||||
{
|
{
|
||||||
|
|
||||||
strcpy(ssidWifi,"IdhunAux");//nombre wifi
|
strcpy(ssidWifi,"IdhunAux");//nombre wifi
|
||||||
strcpy(ideEsp,"Esp8266_Av_cocian");//idenitificador del esp (sera único)
|
strcpy(ideEsp,"Esp8266_Av_cocina");//idenitificador del esp (sera único)
|
||||||
|
|
||||||
ampli.set(D4,"casa/cocina/aux",0,1);
|
ampli.set(D4,"casa/cocina/alt",0,1);
|
||||||
altavoz.set(D3,"casa/cocina/alt",0,1);
|
altavoz.set(D3,"casa/cocina/blue",0,1);
|
||||||
radio.set(D5,"casa/cocina/arad",0);
|
radio.set(D5,"casa/cocina/arad",0);
|
||||||
poten.set("casa/cocina/volu",50, 0);
|
poten.set("casa/cocina/volu",50, 0);
|
||||||
pulso.set("casa/cocina/rad",1,1);
|
pulso.set("casa/cocina/rad",1,1);
|
||||||
|
|
@ -438,7 +461,77 @@ class ConfLamSalon: public DomoEspConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
class ConfHabitacionPrincipal: public DomoEspConfig
|
||||||
|
{
|
||||||
|
SensorDHT dht;
|
||||||
|
SensorDout radio;
|
||||||
|
public:
|
||||||
|
ConfHabitacionPrincipal()
|
||||||
|
{
|
||||||
|
strcpy(ssidWifi,"IdhunAux");//nombre wifi
|
||||||
|
strcpy(ideEsp,"Esp8266_HP");//idenitificador del esp (sera único)
|
||||||
|
dht.set(D4,"casa/habPrin");
|
||||||
|
radio.set(D0, "casa/habPrin/rad",0,0);
|
||||||
|
}
|
||||||
|
virtual void inicia(ISensorManager* man)
|
||||||
|
{
|
||||||
|
man->Add(&dht);
|
||||||
|
man->Add(&radio);
|
||||||
|
|
||||||
ConfigAltavozCocina ConfiguracionActual;
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
class ConfHabitacionNinas: public DomoEspConfig
|
||||||
|
{
|
||||||
|
SensorDHT dht;
|
||||||
|
SensorDout cartel;
|
||||||
|
SensorDin interruptor;
|
||||||
|
AutomatismoPulsador actuadorPul;
|
||||||
|
public:
|
||||||
|
ConfHabitacionNinas()
|
||||||
|
{
|
||||||
|
strcpy(ssidWifi,"IdhunAux");//nombre wifi
|
||||||
|
strcpy(ideEsp,"Esp8266_HN");//idenitificador del esp (sera único)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
int cociMov= vars->AddInternalVarInt( "casa/cocina/mov");
|
||||||
|
int cociLuz= vars->AddInternalVarInt( "casa/cocina/luz");
|
||||||
|
int cociLam= vars->AddInternalVarInt( "casa/cocina/lam");
|
||||||
|
int cociAuto= vars->AddInternalVarInt("casa/cocina/luzAuto");
|
||||||
|
*/
|
||||||
|
dht.set(D0,"casa/habNin");
|
||||||
|
cartel.set(D2, "casa/habNin/cartel",0,0);
|
||||||
|
interruptor.set( D1, "casa/habNin/inter", false);
|
||||||
|
}
|
||||||
|
virtual void inicia(ISensorManager* man)
|
||||||
|
{
|
||||||
|
man->Add(&dht);
|
||||||
|
man->Add(&cartel);
|
||||||
|
man->Add(&interruptor);
|
||||||
|
actuadorPul.inicia(man, &interruptor, &cartel);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
class ConfPruebaDHT: public DomoEspConfig
|
||||||
|
{
|
||||||
|
SensorDHT dht;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ConfPruebaDHT()
|
||||||
|
{
|
||||||
|
strcpy(ssidWifi,"IdhunAux");//nombre wifi
|
||||||
|
strcpy(ideEsp,"Esp8266_dht");//idenitificador del esp (sera único)
|
||||||
|
dht.set(D0, "casa/pruebas/dht");
|
||||||
|
}
|
||||||
|
virtual void inicia(ISensorManager* man)
|
||||||
|
{
|
||||||
|
man->Add(&dht);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
PruebaConexiones ConfiguracionActual;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Loading…
Reference in New Issue