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(); bool BMP180IsConnected();
#else //CONFIG_ENABLE_BMP80 #else //CONFIG_ENABLE_BMP80
int BMP180GetTemperature(double &t){return 0;}; int BMP180GetTemperature(double &t){return -1;};
int BMP180GetTempAndPressure(double &t, double &p){return 0;}; int BMP180GetTempAndPressure(double &t, double &p){return -1;};
int BMP180Setup(int , int ){SKETCH_DEBUG_PRINTLN("BMP180 is disabled at build time"); return 0;}; int BMP180Setup(int , int ){SKETCH_DEBUG_PRINTLN("BMP180 is disabled at build time"); return 0;};
bool BMP180IsConnected(){return false;}; bool BMP180IsConnected(){return false;};
#endif #endif

View File

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

View File

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

View File

@ -9,6 +9,6 @@ bool DHTIsConnected();
#else //CONFIG_ENABLE_DHT #else //CONFIG_ENABLE_DHT
int DHTSetup(int pin){SKETCH_DEBUG_PRINTLN("DHT is disabled at build time"); return 0}; 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;}; bool DHTIsConnected(){return false;};
#endif #endif

View File

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