From ff178ce940dcbe762e1d4252b8198d0fcd31215e Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Wed, 16 Mar 2016 00:54:13 +0100 Subject: [PATCH] Get pressure mesure from BMP180 --- WifiControlSensor/BMP180.ino | 24 ++++++++++++++++++------ WifiControlSensor/MQTT.ino | 12 ++++++------ WifiControlSensor/WebServer.ino | 1 + WifiControlSensor/WifiControlSensor.ino | 19 +++++++++++-------- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/WifiControlSensor/BMP180.ino b/WifiControlSensor/BMP180.ino index abe3ea0..078e496 100644 --- a/WifiControlSensor/BMP180.ino +++ b/WifiControlSensor/BMP180.ino @@ -1,16 +1,28 @@ int getTemperature(double &t) { char status; - status = pressure.startTemperature(); + status = bmp180.startTemperature(); if (status != 0) { // Wait for the measurement to complete: delay(status); - status = pressure.getTemperature(t); + status = bmp180.getTemperature(t); if (status != 0) return 0; - else - return -1; - } else { - return -1; } + return -1; +} + +int getTempAndPressure(double &t, double &p) { + if (getTemperature(t) == 0) { + char status; + status = bmp180.startPressure(3); + if (status != 0) + { + delay(status); + status = bmp180.getPressure(p, t); + if (status != 0) + return 0; + } + } + return -1; } diff --git a/WifiControlSensor/MQTT.ino b/WifiControlSensor/MQTT.ino index 150fd49..15521c8 100644 --- a/WifiControlSensor/MQTT.ino +++ b/WifiControlSensor/MQTT.ino @@ -2,31 +2,31 @@ WiFiClient client; Adafruit_MQTT_Client *mqtt; Adafruit_MQTT_Publish *mqtt_temp; +Adafruit_MQTT_Publish *mqtt_pressure; const char TEMPERATURE_FEED[] = "/feeds/temperature"; +const char PRESSURE_FEED[] = "/feeds/pressure"; boolean mqttIsConfigured; -int publishMQTT(double temp) { +int publishMQTT(double temp, double pressure) { if (MQTT_connect() == 0) { Serial.println("publishing !"); mqtt_temp->publish(temp); + mqtt_pressure->publish(pressure); } } int setupMQTT(char *server, char *user, char *passwd, int port) { - - if(server == ""){ - Serial.println("Mqtt Server not configured"); - } // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd); mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED); + mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, PRESSURE_FEED); return 0; } -int MQTT_isConnected(){ +int MQTT_isConnected() { return mqtt->connected(); } diff --git a/WifiControlSensor/WebServer.ino b/WifiControlSensor/WebServer.ino index 6a6c84f..83b57a4 100644 --- a/WifiControlSensor/WebServer.ino +++ b/WifiControlSensor/WebServer.ino @@ -1,6 +1,7 @@ void handleRoot() { server.send(200, "text/html", "

You are connected


" "Current temperature " + String(temp, 2) + "C
" + "Current pressure " + String(pressure, 2) + "mB
" "MQTT Status :" + (MQTT_isConnected()? "Connected":"Disconnected") +"
" "Setup
" "OTA mode
" diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index e637607..9b19a7e 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -32,7 +32,7 @@ char eeprom[EEPROM_SIZE]; #define WEB_DELAY_MS 100 -#define SAMPLING_PERIODE_MS 6000 +#define SAMPLING_PERIODE_MS 60000 /* I2C pin used*/ #define SDA 2 @@ -42,14 +42,14 @@ char eeprom[EEPROM_SIZE]; #define BOOTMODE_NORMAL 1 #define BOOTMODE_OTA 2 -double temp; +double temp, pressure; uint8_t mode; /* Set these to your desired credentials. */ const char *ssid = "ESPConfigurator"; ESP8266WebServer server(80); -SFE_BMP180 pressure; +SFE_BMP180 bmp180; /* WebServer decl*/ void handleRoot(); @@ -67,12 +67,13 @@ void readEEPROM(uint8_t &bootMode, char **ssid, char **password, char **host, ch /* BMP180 decl */ int getTemperature(double &t); +int getTempAndPressure(double &t, double &p); /* MQTT decl */ int MQTT_connect(); int MQTT_isConnected(); int setupMQTT(char *server, char *user, char *passwd, int port); -int publishMQTT(double temp); +int publishMQTT(double temp, double pressure); void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) { IPAddress myIP; @@ -193,7 +194,7 @@ void setup() { if (mode == BOOTMODE_OTA) { setupOTA(); } else { - if (pressure.begin(SDA, SCL)) + if (bmp180.begin(SDA, SCL)) Serial.println("BMP180 init success"); setupWebServer(mode); } @@ -208,10 +209,12 @@ void loop() { delay(WEB_DELAY_MS); nbCycle++; if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) { - if (getTemperature(temp) == 0) { + if (getTempAndPressure(temp, pressure) == 0) { Serial.print("Current T°C "); - Serial.println(temp); - publishMQTT(temp); + Serial.print(temp); + Serial.print( " Pressure mB "); + Serial.println(pressure); + publishMQTT(temp, pressure); } else { Serial.println("Cannot get T°C"); }