Get pressure mesure from BMP180

This commit is contained in:
Mathieu Maret 2016-03-16 00:54:13 +01:00
parent 4f50d6f175
commit ff178ce940
4 changed files with 36 additions and 20 deletions

View File

@ -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;
}

View File

@ -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();
}

View File

@ -1,6 +1,7 @@
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1><br/>"
"Current temperature " + String(temp, 2) + "C<br/>"
"Current pressure " + String(pressure, 2) + "mB<br/>"
"MQTT Status :" + (MQTT_isConnected()? "Connected":"Disconnected") +"<br/>"
"<a href=\"/setup\">Setup</a><br/>"
"<a href=\"/otamode\">OTA mode</a><br/>"

View File

@ -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");
}