279 lines
7.4 KiB
C++
Executable File
279 lines
7.4 KiB
C++
Executable File
#include <ESP8266WiFi.h>
|
|
|
|
#include <Adafruit_BMP085.h>
|
|
#include <Wire.h>
|
|
#include "DHT.h"
|
|
|
|
// DHT
|
|
#define DHTPIN A0 // what pin we're connected to
|
|
#define DHTTYPE DHT11 // DHT 11
|
|
// Initialize DHT sensor for normal 16mhz Arduino
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
|
|
// Baromètre
|
|
// ################# Barometre ####
|
|
Adafruit_BMP085 bmp;
|
|
// #####################
|
|
|
|
float temperature = 0.0;
|
|
float pressure = 0.0;
|
|
float pression = 0.0;
|
|
float presiune = 0.0;
|
|
float humidite = 0.0;
|
|
|
|
const unsigned long activation = 111269;
|
|
const unsigned long idTemp=1969;
|
|
const unsigned long idPressure=2069;
|
|
const unsigned long idPression=2169;
|
|
const unsigned long idLum=2269;
|
|
const unsigned long idHum=2369;
|
|
const unsigned long desactivation = 962111;
|
|
const unsigned int delai = 11;
|
|
|
|
// WIFI
|
|
const char* ssid = "Livebox-37cc"; // Le nom de votre réseau Wifi
|
|
const char* password = "8A6060920A8A86896F770F2C47";
|
|
|
|
int ledPin = 2; // GPIO2
|
|
WiFiServer server(80);
|
|
|
|
|
|
#define DEBUG true
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
delay(10);
|
|
|
|
pinMode(ledPin, OUTPUT);
|
|
digitalWrite(ledPin, LOW);
|
|
|
|
// Connect to WiFi network
|
|
Serial.println();
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.println("WiFi connected");
|
|
|
|
// Start the server
|
|
server.begin();
|
|
Serial.println("Server started");
|
|
|
|
// Print the IP address
|
|
Serial.print("Use this URL to connect: ");
|
|
Serial.print("http://");
|
|
Serial.print(WiFi.localIP());
|
|
Serial.println("/");
|
|
|
|
Wire.begin(4, 5);
|
|
|
|
}
|
|
|
|
void loop() {
|
|
// Check if a client has connected
|
|
WiFiClient client = server.available();
|
|
if (!client) {
|
|
return;
|
|
}
|
|
|
|
// Wait until the client sends some data
|
|
Serial.println("new client");
|
|
while(!client.available()){
|
|
delay(1);
|
|
}
|
|
|
|
// Read the first line of the request
|
|
String request = client.readStringUntil('\r');
|
|
Serial.println(request);
|
|
client.flush();
|
|
|
|
// Match the request
|
|
|
|
int value = LOW;
|
|
if (request.indexOf("/LED=ON") != -1) {
|
|
digitalWrite(ledPin, HIGH);
|
|
value = HIGH;
|
|
}
|
|
if (request.indexOf("/LED=OFF") != -1) {
|
|
digitalWrite(ledPin, LOW);
|
|
value = LOW;
|
|
}
|
|
|
|
// Set ledPin according to the request
|
|
//digitalWrite(ledPin, value);
|
|
|
|
// Return the response
|
|
client.println("HTTP/1.1 200 OK");
|
|
client.println("Content-Type: text/html");
|
|
client.println(""); // do not forget this one
|
|
client.println("<!DOCTYPE HTML>");
|
|
client.println("<html>");
|
|
|
|
client.print("Led pin is now: ");
|
|
|
|
if(value == HIGH) {
|
|
client.print("On");
|
|
} else {
|
|
client.print("Off");
|
|
}
|
|
|
|
/////////////////////////////
|
|
//
|
|
////////////////////////////
|
|
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
|
|
delay(10); // wait for a second
|
|
digitalWrite(13, LOW);
|
|
|
|
barometre();
|
|
doDHT();
|
|
|
|
#ifdef DEBUG
|
|
Serial.print("Send");
|
|
#endif
|
|
|
|
myMessageSend(idTemp,temperature * 100);
|
|
myMessageSend(idPressure,pressure * 10);
|
|
myMessageSend(idHum,humidite);
|
|
myMessageSend(idPression,pression * 10);
|
|
|
|
// LUX
|
|
// R=K*L^-gamma
|
|
// R étant la résistance pour un niveau d'éclairement L.
|
|
int lum = analogRead(1);
|
|
|
|
int lux = (1000.0 * lum / 1024.0);
|
|
myMessageSend(idLum,lux);
|
|
|
|
|
|
client.println("<TABLE>");
|
|
|
|
client.println("<TR><TD>Click <a href=\"/LED=ON\">here</a> turn the LED on pin 2 ON</TD></TR>");
|
|
client.println("<TR><TD>Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 2 OFF</TD></TR>");
|
|
|
|
client.print("<TR><TD>Temperature=");client.print(temperature);client.print("</TD></TR>");
|
|
client.print("<TR><TD>Pressure =");client.print(pressure);client.print("</TD></TR>");
|
|
client.print("<TR><TD>Humidite =");client.print(humidite);client.print("</TD></TR>");
|
|
client.print("<TR><TD>Pression =");client.print(pression);client.print("</TD></TR>");
|
|
client.print("<TR><TD>Luminosite =");client.print(lux);client.print("</TD></TR>");
|
|
|
|
client.println("</TABLE></html>");
|
|
|
|
delay(1);
|
|
Serial.println("Client disonnected");
|
|
Serial.println("");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
// Commande pour barometre humidité température
|
|
// Virtual Device
|
|
// http://192.168.0.10:8080/json.htm?type=command¶m=udevice&idx=160&nvalue=0&svalue=23.3;50;2;1024.20;1024&battery=89
|
|
|
|
void doDHT() {
|
|
// Reading temperature or humidity takes about 250 milliseconds!
|
|
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
|
float h = dht.readHumidity();
|
|
humidite = h;
|
|
|
|
// Read temperature as Celsius
|
|
float t = dht.readTemperature();
|
|
// Read temperature as Fahrenheit
|
|
float f = dht.readTemperature(true);
|
|
|
|
// Check if any reads failed and exit early (to try again).
|
|
if (isnan(h) || isnan(t) || isnan(f)) {
|
|
// Serial.println("Failed to read from DHT sensor!");
|
|
return;
|
|
} else {
|
|
// Compute heat index
|
|
// Must send in temp in Fahrenheit!
|
|
float hi = dht.computeHeatIndex(f, h);
|
|
#ifdef DEBUG
|
|
Serial.print("Humidity: ");
|
|
Serial.print(h);
|
|
Serial.print(" %\t");
|
|
Serial.print("Temperature: ");
|
|
Serial.print(t);
|
|
Serial.print(" *C ");
|
|
Serial.print(f);
|
|
Serial.print(" *F\t");
|
|
Serial.print("Heat index: ");
|
|
Serial.print(hi);
|
|
Serial.println(" *F");
|
|
#endif
|
|
}
|
|
}
|
|
|
|
void barometre() {
|
|
/* See Example: TypeA_WithDIPSwitches */
|
|
// mySwitch.switchOn("00001", "10000");
|
|
// delay(1000);
|
|
// BMP
|
|
if (bmp.begin()) {
|
|
temperature = bmp.readTemperature();
|
|
pressure= bmp.readPressure() / 100.0;
|
|
pression = pressure / 101.325;
|
|
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(19) / 101.325;
|
|
presiune = presiune * 0.760;
|
|
#ifdef DEBUG
|
|
Serial.print("Temperature="); Serial.println(temperature);
|
|
Serial.print("pressure="); Serial.println(pressure);
|
|
Serial.print("pression="); Serial.println(pression);
|
|
#endif
|
|
|
|
}
|
|
}
|
|
|
|
void myMessageSend(long id, long value) {
|
|
|
|
#ifdef DEBUG
|
|
Serial.print("Send id="); Serial.print(id);
|
|
Serial.print(" value="); Serial.println(value);
|
|
#endif
|
|
|
|
|
|
|
|
//delay(5000);
|
|
//delayMicroseconds(TWOTIME*8);
|
|
}
|
|
//--------------------------------------------------------------------------------------------------
|
|
// Read current supply voltage
|
|
//--------------------------------------------------------------------------------------------------
|
|
// long readVcc() {
|
|
// bitClear(PRR, PRADC); ADCSRA |= bit(ADEN); // Enable the ADC
|
|
// long result;
|
|
// // Read 1.1V reference against Vcc
|
|
// #if defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
|
// ADMUX = _BV(MUX5) | _BV(MUX0); // For ATtiny84
|
|
// #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
|
// ADMUX = _BV(MUX3) | _BV(MUX2);
|
|
// #else
|
|
// ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // For ATmega328
|
|
// #endif
|
|
// // ADCSRB = 0;
|
|
//
|
|
// delay(2); // Wait for Vref to settle
|
|
// ADCSRA |= _BV(ADSC); // Convert
|
|
// while (bit_is_set(ADCSRA,ADSC));
|
|
// result = ADCL;
|
|
// result |= ADCH<<8;
|
|
// result = 1126400L / result; // Back-calculate Vcc in mV
|
|
// // ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC); // Disable the ADC to save power
|
|
//
|
|
// // analogReference(DEFAULT);
|
|
//
|
|
// return result; // Vcc in millivolts
|
|
//}
|