Code simplification

This commit is contained in:
Mathieu Maret 2016-06-01 00:36:14 +02:00
parent 31e9c04e82
commit 26d3deee0e
5 changed files with 22 additions and 27 deletions

View File

@ -14,8 +14,8 @@ int BMP180Setup(int sda, int scl);
bool BMP180IsConnected();
#else //CONFIG_ENABLE_BMP80
int BMP180GetTemperature(double &t){return 0;};
int BMP180GetTempAndPressure(double &t, double &p){return 0;};
int BMP180GetTemperature(double &t){return -1;};
int BMP180GetTempAndPressure(double &t, double &p){return -1;};
int BMP180Setup(int , int ){SKETCH_DEBUG_PRINTLN("BMP180 is disabled at build time"); return 0;};
bool BMP180IsConnected(){return false;};
#endif

View File

@ -3,6 +3,9 @@
int BMP180Setup(int sda, int scl) {
//Use BMP fork at https://github.com/mmaret/BMP180_Breakout_Arduino_Library/archive/master.zip
bmp180Connected = bmp180.begin(sda, scl);
if (!bmp180Connected){
SKETCH_DEBUG_PRINTLN("Cannot connect to BMP180");
}
return bmp180Connected;
}
@ -12,6 +15,8 @@ bool BMP180IsConnected() {
int BMP180GetTemperature(double &t) {
char status;
if(!BMP180IsConnected())
return -1;
status = bmp180.startTemperature();
if (status != 0)
{

View File

@ -259,34 +259,25 @@ void loop() {
ArduinoOTA.handle();
} else {
server.handleClient();
if (MqttIsConfigured())
MqttCheckSubscription();
MqttCheckSubscription();
delay(CONFIG_WEB_DELAY_MS);
nbCycle++;
if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS) {
#ifdef CONFIG_ENABLE_BMP180
if (BMP180IsConnected() && BMP180GetTempAndPressure(temp, pressure) == 0) {
if (BMP180GetTempAndPressure(temp, pressure) == 0) {
SKETCH_DEBUG_PRINT("Current T°C ");
SKETCH_DEBUG_PRINT(temp);
SKETCH_DEBUG_PRINT(" Pressure mB ");
SKETCH_DEBUG_PRINTLN(pressure);
if (MqttIsConfigured())
MqttPublish(temp, pressure);
} else {
SKETCH_DEBUG_PRINTLN("Cannot get T°C");
MqttPublish(temp, pressure);
}
#endif
#ifdef CONFIG_ENABLE_DHT
if (DHTIsConnected() && DHTGetTempAndHumidity(dhtTemp, dhtHumidity) == 0) {
if (DHTGetTempAndHumidity(dhtTemp, dhtHumidity) == 0) {
SKETCH_DEBUG_PRINT("Current T°C ");
SKETCH_DEBUG_PRINT(dhtTemp);
SKETCH_DEBUG_PRINT(" Humidity ");
SKETCH_DEBUG_PRINTLN(dhtHumidity);
if (MqttIsConfigured())
MqttDhtPublish(dhtTemp, dhtHumidity);
MqttDhtPublish(dhtTemp, dhtHumidity);
}
#endif
nbCycle = 0;
}
}

View File

@ -9,6 +9,6 @@ bool DHTIsConnected();
#else //CONFIG_ENABLE_DHT
int DHTSetup(int pin){SKETCH_DEBUG_PRINTLN("DHT is disabled at build time"); return 0};
int DHTGetTempAndHumidity(float &t, float &h){return 0;};
int DHTGetTempAndHumidity(float &t, float &h){return -1;};
bool DHTIsConnected(){return false;};
#endif

View File

@ -7,18 +7,17 @@ int DHTSetup(int pin){
}
int DHTGetTempAndHumidity(float &t, float &h){
int ret = 0;
if(!DHTIsConnected())
goto err;
t = dht->readTemperature();
h = dht->readHumidity();
if(isnan(t)){
t = 0;
ret = -1;
}
if(isnan(h)){
h = 0;
ret = -1;
}
return ret;
if(isnan(t) || isnan(h))
goto err;
return 0;
err:
t=0;
h=0;
return -1;
}
bool DHTIsConnected(){