first commit
This commit is contained in:
248
ESP8266_DOMOTICZ_BMP/ESP8266_DOMOTICZ_BMP.ino
Executable file
248
ESP8266_DOMOTICZ_BMP/ESP8266_DOMOTICZ_BMP.ino
Executable file
@@ -0,0 +1,248 @@
|
||||
//extern "C" {
|
||||
//#include "user_interface.h"
|
||||
//}
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_BMP085.h>
|
||||
//#include <SFE_BMP180.h>
|
||||
|
||||
//BMP
|
||||
//static int default_sda_pin = 0;
|
||||
//static int default_scl_pin = 2;
|
||||
// You will need to create an SFE_BMP180 object, here called "pressure":
|
||||
#define ALTITUDE 19.0 // Altitude of SparkFun's HQ in Boulder, CO. in meters
|
||||
// ################# Barometre ####
|
||||
Adafruit_BMP085 bmp;
|
||||
//SFE_BMP180 bmp;
|
||||
// #####################
|
||||
|
||||
const char* ssid = "Livebox-37cc";
|
||||
const char* password = "8A6060920A8A86896F770F2C47";
|
||||
const int sleepTime = 60; //Power down WiFi for 6 seconds
|
||||
|
||||
|
||||
// int status = WL_IDLE_STATUS; // the Wifi radio's status
|
||||
WiFiClient client;
|
||||
|
||||
// domoticz
|
||||
const char * domoticz_server = "192.168.1.3"; //Domoticz port
|
||||
int port = 81; //Domoticz port
|
||||
int idx = 668; //IDX for this virtual sensor, found in Setup -> Devices
|
||||
double humidity = 0;
|
||||
double temp = 0;
|
||||
double pression = 0;
|
||||
double pressure = 0;
|
||||
double presiune = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
//Wire.pins(4, 5);
|
||||
//Wire.begin(4, 5);
|
||||
// Wire.pins(0, 2);
|
||||
Wire.begin(); //D2, D3);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
initWifi();
|
||||
barometre();
|
||||
printInfo();
|
||||
sleepWifi();
|
||||
}
|
||||
|
||||
//void barometre()
|
||||
//{
|
||||
// char status;
|
||||
// double p0, a;
|
||||
//
|
||||
// // Loop here getting pressure readings every 10 seconds.
|
||||
//
|
||||
// // If you want sea-level-compensated pressure, as used in weather reports,
|
||||
// // you will need to know the altitude at which your measurements are taken.
|
||||
// // We're using a constant called ALTITUDE in this sketch:
|
||||
//
|
||||
// Serial.println();
|
||||
// Serial.print("provided altitude: ");
|
||||
// Serial.print(ALTITUDE, 0);
|
||||
// Serial.print(" meters, ");
|
||||
// Serial.print(ALTITUDE * 3.28084, 0);
|
||||
// Serial.println(" feet");
|
||||
//
|
||||
// // If you want to measure altitude, and not pressure, you will instead need
|
||||
// // to provide a known baseline pressure. This is shown at the end of the sketch.
|
||||
//
|
||||
// // You must first get a temperature measurement to perform a pressure reading.
|
||||
//
|
||||
// // Start a temperature measurement:
|
||||
// // If request is successful, the number of ms to wait is returned.
|
||||
// // If request is unsuccessful, 0 is returned.
|
||||
//
|
||||
// status = bmp.startTemperature();
|
||||
// if (status != 0)
|
||||
// {
|
||||
// // Wait for the measurement to complete:
|
||||
// delay(status);
|
||||
//
|
||||
// // Retrieve the completed temperature measurement:
|
||||
// // Note that the measurement is stored in the variable T.
|
||||
// // Function returns 1 if successful, 0 if failure.
|
||||
//
|
||||
// status = bmp.getTemperature(temp);
|
||||
// if (status != 0)
|
||||
// {
|
||||
// // Print out the measurement:
|
||||
// Serial.print("temperature: ");
|
||||
// Serial.print(temp, 2);
|
||||
// Serial.print(" deg C, ");
|
||||
// Serial.print((9.0 / 5.0) * temp + 32.0, 2);
|
||||
// Serial.println(" deg F");
|
||||
//
|
||||
// // Start a pressure measurement:
|
||||
// // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
|
||||
// // If request is successful, the number of ms to wait is returned.
|
||||
// // If request is unsuccessful, 0 is returned.
|
||||
//
|
||||
// status = bmp.startPressure(3);
|
||||
// if (status != 0)
|
||||
// {
|
||||
// // Wait for the measurement to complete:
|
||||
// delay(status);
|
||||
//
|
||||
// // Retrieve the completed pressure measurement:
|
||||
// // Note that the measurement is stored in the variable P.
|
||||
// // Note also that the function requires the previous temperature measurement (T).
|
||||
// // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
|
||||
// // Function returns 1 if successful, 0 if failure.
|
||||
//
|
||||
// status = bmp.getPressure(pression, temp);
|
||||
// if (status != 0)
|
||||
// {
|
||||
// // Print out the measurement:
|
||||
// Serial.print("absolute pressure: ");
|
||||
// Serial.print(pression, 2);
|
||||
// Serial.print(" mb, ");
|
||||
// Serial.print(pression * 0.0295333727, 2);
|
||||
// Serial.println(" inHg");
|
||||
//
|
||||
// // The pressure sensor returns abolute pressure, which varies with altitude.
|
||||
// // To remove the effects of altitude, use the sealevel function and your current altitude.
|
||||
// // This number is commonly used in weather reports.
|
||||
// // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
|
||||
// // Result: p0 = sea-level compensated pressure in mb
|
||||
//
|
||||
// p0 = bmp.sealevel(pression, ALTITUDE); // we're at 1655 meters (Boulder, CO)
|
||||
// Serial.print("relative (sea-level) pressure: ");
|
||||
// Serial.print(p0, 2);
|
||||
// Serial.print(" mb, ");
|
||||
// Serial.print(p0 * 0.0295333727, 2);
|
||||
// Serial.println(" inHg");
|
||||
//
|
||||
// // On the other hand, if you want to determine your altitude from the pressure reading,
|
||||
// // use the altitude function along with a baseline pressure (sea-level or other).
|
||||
// // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb.
|
||||
// // Result: a = altitude in m.
|
||||
//
|
||||
// a = bmp.altitude(pression, p0);
|
||||
// Serial.print("computed altitude: ");
|
||||
// Serial.print(a, 0);
|
||||
// Serial.print(" meters, ");
|
||||
// Serial.print(a * 3.28084, 0);
|
||||
// Serial.println(" feet");
|
||||
// }
|
||||
// else Serial.println("error retrieving pressure measurement\n");
|
||||
// }
|
||||
// else Serial.println("error starting pressure measurement\n");
|
||||
// }
|
||||
// else Serial.println("error retrieving temperature measurement\n");
|
||||
// }
|
||||
// else Serial.println("error starting temperature measurement\n");
|
||||
//}
|
||||
|
||||
void sleepWifi()
|
||||
{
|
||||
//Time to let the WiFi go to sleep!
|
||||
Serial.println("Sleeping...");
|
||||
WiFi.disconnect();
|
||||
WiFi.mode(WIFI_OFF);
|
||||
WiFi.forceSleepBegin(sleepTime * 1000000L); //In uS. Must be same length as your delay
|
||||
delay(sleepTime * 1000); //Hang out at 15mA for 6 seconds
|
||||
WiFi.mode(WIFI_STA);
|
||||
Serial.println("Awake!");
|
||||
}
|
||||
void initWifi()
|
||||
{
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.begin(ssid, password); //Connect to local Wifi
|
||||
|
||||
Serial.println();
|
||||
Serial.print("Connecting to WiFi");
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
Serial.print(".");
|
||||
delay(500);
|
||||
}
|
||||
Serial.println("WiFi Connected!");
|
||||
}
|
||||
|
||||
void printInfo()
|
||||
{
|
||||
// Domoticz format /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
|
||||
|
||||
if (client.connect(domoticz_server, port) && temp > 0) {
|
||||
|
||||
// /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT;BAR;BAR_FOR
|
||||
//domoticz:8080/json.htm?type=command¶m=udevice&idx=659&nvalue=0&svalue=16.00;50;50;1007;760
|
||||
client.print("GET /json.htm?type=command¶m=udevice&idx=");
|
||||
client.print(idx);
|
||||
client.print("&nvalue=0&svalue=");
|
||||
client.print(temp);
|
||||
client.print(";");
|
||||
client.print(humidity);
|
||||
client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
|
||||
client.print(";");
|
||||
client.print(humidity);
|
||||
|
||||
client.print(";");
|
||||
client.print(pressure);
|
||||
|
||||
client.print(";");
|
||||
client.print(pression);
|
||||
client.println(" HTTP/1.1");
|
||||
client.print("Host: ");
|
||||
client.print(domoticz_server);
|
||||
client.print(":");
|
||||
client.println(port);
|
||||
client.println("User-Agent: Arduino-ethernet");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
|
||||
client.stop();
|
||||
}
|
||||
}
|
||||
|
||||
void barometre() {
|
||||
/* See Example: TypeA_WithDIPSwitches */
|
||||
// mySwitch.switchOn("00001", "10000");
|
||||
// delay(1000);
|
||||
// BMP
|
||||
if (bmp.begin()) {
|
||||
delay(1000);
|
||||
temp = bmp.readTemperature();
|
||||
pressure = bmp.readPressure() / 100.0;
|
||||
Serial.println(pressure);
|
||||
pression = pressure / 101.325;
|
||||
Serial.println(pression);
|
||||
pression = pression * 0.760 * 100;
|
||||
// http://en.wikipedia.org/wiki/Atmospheric_pressure#Mean_sea_level_pressure
|
||||
// Serial.print("Presiure la nivelul marii (calculata) = ");
|
||||
presiune = bmp.readSealevelPressure(ALTITUDE) / 101.325;
|
||||
Serial.println(presiune);
|
||||
presiune = presiune * 0.760;
|
||||
Serial.print("Temperature="); Serial.println(temp);
|
||||
Serial.print("pressure="); Serial.println(pressure);
|
||||
Serial.print("pression="); Serial.println(pression);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user