Get pressure mesure from BMP180
This commit is contained in:
parent
4f50d6f175
commit
ff178ce940
@ -1,16 +1,28 @@
|
|||||||
int getTemperature(double &t) {
|
int getTemperature(double &t) {
|
||||||
char status;
|
char status;
|
||||||
status = pressure.startTemperature();
|
status = bmp180.startTemperature();
|
||||||
if (status != 0)
|
if (status != 0)
|
||||||
{
|
{
|
||||||
// Wait for the measurement to complete:
|
// Wait for the measurement to complete:
|
||||||
delay(status);
|
delay(status);
|
||||||
status = pressure.getTemperature(t);
|
status = bmp180.getTemperature(t);
|
||||||
if (status != 0)
|
if (status != 0)
|
||||||
return 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;
|
||||||
}
|
}
|
||||||
|
@ -2,31 +2,31 @@
|
|||||||
WiFiClient client;
|
WiFiClient client;
|
||||||
Adafruit_MQTT_Client *mqtt;
|
Adafruit_MQTT_Client *mqtt;
|
||||||
Adafruit_MQTT_Publish *mqtt_temp;
|
Adafruit_MQTT_Publish *mqtt_temp;
|
||||||
|
Adafruit_MQTT_Publish *mqtt_pressure;
|
||||||
|
|
||||||
const char TEMPERATURE_FEED[] = "/feeds/temperature";
|
const char TEMPERATURE_FEED[] = "/feeds/temperature";
|
||||||
|
const char PRESSURE_FEED[] = "/feeds/pressure";
|
||||||
|
|
||||||
boolean mqttIsConfigured;
|
boolean mqttIsConfigured;
|
||||||
|
|
||||||
int publishMQTT(double temp) {
|
int publishMQTT(double temp, double pressure) {
|
||||||
if (MQTT_connect() == 0) {
|
if (MQTT_connect() == 0) {
|
||||||
Serial.println("publishing !");
|
Serial.println("publishing !");
|
||||||
mqtt_temp->publish(temp);
|
mqtt_temp->publish(temp);
|
||||||
|
mqtt_pressure->publish(pressure);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int setupMQTT(char *server, char *user, char *passwd, int port) {
|
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.
|
// 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 = new Adafruit_MQTT_Client(&client, server, port, user, passwd);
|
||||||
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED);
|
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED);
|
||||||
|
mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, PRESSURE_FEED);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MQTT_isConnected(){
|
int MQTT_isConnected() {
|
||||||
return mqtt->connected();
|
return mqtt->connected();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
void handleRoot() {
|
void handleRoot() {
|
||||||
server.send(200, "text/html", "<h1>You are connected</h1><br/>"
|
server.send(200, "text/html", "<h1>You are connected</h1><br/>"
|
||||||
"Current temperature " + String(temp, 2) + "C<br/>"
|
"Current temperature " + String(temp, 2) + "C<br/>"
|
||||||
|
"Current pressure " + String(pressure, 2) + "mB<br/>"
|
||||||
"MQTT Status :" + (MQTT_isConnected()? "Connected":"Disconnected") +"<br/>"
|
"MQTT Status :" + (MQTT_isConnected()? "Connected":"Disconnected") +"<br/>"
|
||||||
"<a href=\"/setup\">Setup</a><br/>"
|
"<a href=\"/setup\">Setup</a><br/>"
|
||||||
"<a href=\"/otamode\">OTA mode</a><br/>"
|
"<a href=\"/otamode\">OTA mode</a><br/>"
|
||||||
|
@ -32,7 +32,7 @@
|
|||||||
char eeprom[EEPROM_SIZE];
|
char eeprom[EEPROM_SIZE];
|
||||||
|
|
||||||
#define WEB_DELAY_MS 100
|
#define WEB_DELAY_MS 100
|
||||||
#define SAMPLING_PERIODE_MS 6000
|
#define SAMPLING_PERIODE_MS 60000
|
||||||
|
|
||||||
/* I2C pin used*/
|
/* I2C pin used*/
|
||||||
#define SDA 2
|
#define SDA 2
|
||||||
@ -42,14 +42,14 @@ char eeprom[EEPROM_SIZE];
|
|||||||
#define BOOTMODE_NORMAL 1
|
#define BOOTMODE_NORMAL 1
|
||||||
#define BOOTMODE_OTA 2
|
#define BOOTMODE_OTA 2
|
||||||
|
|
||||||
double temp;
|
double temp, pressure;
|
||||||
uint8_t mode;
|
uint8_t mode;
|
||||||
|
|
||||||
/* Set these to your desired credentials. */
|
/* Set these to your desired credentials. */
|
||||||
const char *ssid = "ESPConfigurator";
|
const char *ssid = "ESPConfigurator";
|
||||||
|
|
||||||
ESP8266WebServer server(80);
|
ESP8266WebServer server(80);
|
||||||
SFE_BMP180 pressure;
|
SFE_BMP180 bmp180;
|
||||||
|
|
||||||
/* WebServer decl*/
|
/* WebServer decl*/
|
||||||
void handleRoot();
|
void handleRoot();
|
||||||
@ -67,12 +67,13 @@ void readEEPROM(uint8_t &bootMode, char **ssid, char **password, char **host, ch
|
|||||||
|
|
||||||
/* BMP180 decl */
|
/* BMP180 decl */
|
||||||
int getTemperature(double &t);
|
int getTemperature(double &t);
|
||||||
|
int getTempAndPressure(double &t, double &p);
|
||||||
|
|
||||||
/* MQTT decl */
|
/* MQTT decl */
|
||||||
int MQTT_connect();
|
int MQTT_connect();
|
||||||
int MQTT_isConnected();
|
int MQTT_isConnected();
|
||||||
int setupMQTT(char *server, char *user, char *passwd, int port);
|
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) {
|
void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
|
||||||
IPAddress myIP;
|
IPAddress myIP;
|
||||||
@ -193,7 +194,7 @@ void setup() {
|
|||||||
if (mode == BOOTMODE_OTA) {
|
if (mode == BOOTMODE_OTA) {
|
||||||
setupOTA();
|
setupOTA();
|
||||||
} else {
|
} else {
|
||||||
if (pressure.begin(SDA, SCL))
|
if (bmp180.begin(SDA, SCL))
|
||||||
Serial.println("BMP180 init success");
|
Serial.println("BMP180 init success");
|
||||||
setupWebServer(mode);
|
setupWebServer(mode);
|
||||||
}
|
}
|
||||||
@ -208,10 +209,12 @@ void loop() {
|
|||||||
delay(WEB_DELAY_MS);
|
delay(WEB_DELAY_MS);
|
||||||
nbCycle++;
|
nbCycle++;
|
||||||
if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) {
|
if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) {
|
||||||
if (getTemperature(temp) == 0) {
|
if (getTempAndPressure(temp, pressure) == 0) {
|
||||||
Serial.print("Current T°C ");
|
Serial.print("Current T°C ");
|
||||||
Serial.println(temp);
|
Serial.print(temp);
|
||||||
publishMQTT(temp);
|
Serial.print( " Pressure mB ");
|
||||||
|
Serial.println(pressure);
|
||||||
|
publishMQTT(temp, pressure);
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Cannot get T°C");
|
Serial.println("Cannot get T°C");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user