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 keyWifi[32];//key wifi a conectar
|
||||
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 velocidadPortSerie;
|
||||
|
|
@ -19,6 +21,8 @@ class DomoEspConfig
|
|||
|
||||
DomoEspConfig()
|
||||
{
|
||||
conexionPorApi=false;
|
||||
keyhost[0]=0;
|
||||
velocidadPortSerie=115200;
|
||||
strcpy(ssidWifi,"Idhun");//nombre wifi
|
||||
strcpy(keyWifi,"Ardileorca1234.");//key wifi
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ DomoEspManager::DomoEspManager()
|
|||
tiempo=0;
|
||||
suscrito=false;
|
||||
SetTimeRefres(15);
|
||||
tNoConexion.set(300);
|
||||
tNomqtt.set(300);
|
||||
}
|
||||
void DomoEspManager::SetTimeRefres(int seg)
|
||||
{
|
||||
|
|
@ -20,7 +22,7 @@ void DomoEspManager::inicia(DomoEspConfig* cnf)
|
|||
conf=cnf;
|
||||
#ifdef DEBUG_PS
|
||||
Serial.begin(conf->velocidadPortSerie);
|
||||
delay(100);
|
||||
delay(1000);
|
||||
Serial.println("");
|
||||
Serial.println("Iniciando");
|
||||
#endif
|
||||
|
|
@ -31,7 +33,14 @@ void DomoEspManager::inicia(DomoEspConfig* cnf)
|
|||
#ifdef DEBUG_PS
|
||||
Serial.println("Inicia Mqtt");
|
||||
#endif
|
||||
mqtt.inicia(&clienteMqtt,conf->ideEsp, conf->hostMQTT, conf->portMQTT, this);
|
||||
if(conf->conexionPorApi)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
mqtt.inicia(&clienteMqtt,conf->ideEsp, conf->hostMQTT, conf->portMQTT, this);
|
||||
//loguea------------
|
||||
MqttSend("casa/log", conf->ideEsp);
|
||||
|
||||
#ifdef DEBUG_PS
|
||||
Serial.println("Configura Sensores");
|
||||
|
|
@ -49,17 +58,24 @@ void DomoEspManager::inicia(DomoEspConfig* cnf)
|
|||
void DomoEspManager::loop()
|
||||
{
|
||||
if(!wifi.loop())
|
||||
{
|
||||
{
|
||||
suscrito=false;
|
||||
return;
|
||||
}
|
||||
|
||||
if(!wifi.conecta())
|
||||
{
|
||||
if(tNoConexion.onTimer())
|
||||
ESP.reset();
|
||||
return;
|
||||
}
|
||||
}
|
||||
tNoConexion.inicia();
|
||||
if(!mqtt.loop())
|
||||
{
|
||||
suscrito=false;
|
||||
return;
|
||||
if(tNomqtt.onTimer())
|
||||
ESP.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
tNomqtt.inicia();
|
||||
if(!suscrito)
|
||||
{
|
||||
for(int i=0; i<n; i++)
|
||||
|
|
@ -71,7 +87,16 @@ void DomoEspManager::loop()
|
|||
int tiempoAux=0;
|
||||
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;
|
||||
tiempoAux=tiempo+1;
|
||||
#ifdef DEBUG_PS
|
||||
|
|
@ -137,11 +162,22 @@ void DomoEspManager::SubscribeMqtt(PubSubClient *client_mqtt)
|
|||
|
||||
void DomoEspManager::MqttSend(char* topic, char* payload)
|
||||
{
|
||||
clienteMqtt.publish(topic, payload);
|
||||
if(conf->conexionPorApi)
|
||||
api.send(topic, payload);
|
||||
else
|
||||
{
|
||||
if(suscrito);
|
||||
clienteMqtt.publish(topic, payload);
|
||||
}
|
||||
}
|
||||
void DomoEspManager::MqttSubs(char* topic)
|
||||
{
|
||||
clienteMqtt.subscribe(topic);
|
||||
if(conf->conexionPorApi)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
clienteMqtt.subscribe(topic);
|
||||
}
|
||||
|
||||
void DomoEspManager::Add(DomoEspSensorReceiver* sensor)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#include <PubSubClient.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "utiles.h"
|
||||
#include "DomoEspApiConexion.h"
|
||||
class DomoEspConfig;
|
||||
class IDomoEspLisener
|
||||
{
|
||||
|
|
@ -21,13 +22,15 @@ class DomoEspManager: public MqttReceiver, public IMqttManager, public ISensorMa
|
|||
WiFiClient espClient;
|
||||
PubSubClient clienteMqtt;
|
||||
MqttManager mqtt;
|
||||
DomoEspApiConexion api;
|
||||
|
||||
DomoEspSensorReceiver* sensores[MAXSENS];
|
||||
int n;
|
||||
int tiempo;
|
||||
bool suscrito;
|
||||
Ctimer timer;
|
||||
|
||||
Ctimer tNoConexion;
|
||||
Ctimer tNomqtt;
|
||||
public:
|
||||
|
||||
DomoEspManager();
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ class SensorDHT: public DomoEspSensorReceiver
|
|||
void set(uint8_t _pin, char* topic_id)
|
||||
{
|
||||
pin=_pin;
|
||||
t=h=0;
|
||||
t=h=9.99998;
|
||||
strcpy(topic, topic_id);
|
||||
}
|
||||
virtual void procesa(IMqttManager * man, int tiempo)
|
||||
|
|
@ -43,8 +43,17 @@ class SensorDHT: public DomoEspSensorReceiver
|
|||
}
|
||||
else
|
||||
{
|
||||
t=ta;
|
||||
h=ha;
|
||||
/*if(t==h && t==9.99998)
|
||||
{*/
|
||||
t=ta;
|
||||
h=ha;
|
||||
/*}
|
||||
else
|
||||
{
|
||||
t=(t+ta)/2;
|
||||
h=(h+ha)/2;
|
||||
}*/
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -168,9 +168,9 @@ bool WifiManager::loop()
|
|||
return true;
|
||||
}
|
||||
#ifdef DEBUG_PS
|
||||
Serial.println("Fallo de conexion, se reinicia arduino");
|
||||
Serial.println("Fallo de conexion wifi");
|
||||
#endif
|
||||
ESP.reset();
|
||||
//ESP.reset();
|
||||
return false;
|
||||
}
|
||||
//**************************************************************************************************************************************************
|
||||
|
|
@ -207,9 +207,9 @@ bool MqttManager::loop()
|
|||
|
||||
}
|
||||
#ifdef DEBUG_PS
|
||||
Serial.println("Fallo de conexion, se reinicia arduino");
|
||||
Serial.println("Fallo de conexion a mqtt broker");
|
||||
#endif
|
||||
ESP.reset();
|
||||
//ESP.reset();
|
||||
return false;
|
||||
}
|
||||
void MqttManager::subscribe_mqtt()
|
||||
|
|
|
|||
|
|
@ -7,6 +7,29 @@
|
|||
|
||||
|
||||
//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
|
||||
{
|
||||
SensorRF presencia;
|
||||
|
|
@ -391,10 +414,10 @@ class ConfigAltavozCocina: public DomoEspConfig
|
|||
{
|
||||
|
||||
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);
|
||||
altavoz.set(D3,"casa/cocina/alt",0,1);
|
||||
ampli.set(D4,"casa/cocina/alt",0,1);
|
||||
altavoz.set(D3,"casa/cocina/blue",0,1);
|
||||
radio.set(D5,"casa/cocina/arad",0);
|
||||
poten.set("casa/cocina/volu",50, 0);
|
||||
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
|
||||
Loading…
Reference in New Issue