0% found this document useful (0 votes)
17 views

Programme

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Programme

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <ESP8266WebServer.

h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include "SSD1306Wire.h"
#include <SPI.h>
#include <WiFiManager.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define TRIGGER_PIN 0
#define SEALEVELPRESSURE_HPA (1016.8)

Adafruit_BME280 bme;
SSD1306Wire display(0x3c, SDA, SCL);
unsigned long delayTime;
float humidite;
float temperature ;
float pression;
float altitude ;
const char *ssid="Nathlebo";
const char *password="syl202122";
IPAddress IP(192,168,17,12);
IPAddress gateway(192,168,17,1);
IPAddress subnet(255,255,255,0);
ESP8266WebServer ServerWeb(80);

WiFiManager wm;
bool portalRunning = false;

const char index_html[] PROGMEM = R"=====(


<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Commande LED</title>
<script>
function refresh() {
fetch('/data')
.then(response => response.json())
.then(data => {
document.getElementById('ledStatus').innerText = data.ledStatus;
document.getElementById('temperature').innerText = data.temperature;
document.getElementById('humidite').innerText = data.humidity;
document.getElementById('altitude').innerText = data.altitude;
document.getElementById('pression').innerText = data.pressure;
})
.catch(error => console.error('Error fetching data:', error));
}

window.onload = function() {
refresh();
setInterval(refresh, 500);
};
</script>
</head>
<body>
<h1>Etat de la LED</h1>
<h2 id="ledStatus">%LED%</h2>
<h1>Commande de la LED</h1>
<a href="/SwitchLedOn">
<button>Allumer la LED</button>
</a>
<a href="/SwitchLedOff">
<button>Eteindre la LED</button>
</a>
<h1>Température</h1>
<h2 id="temperature">%temperature%</h2>
<h1>Humidité</h1>
<h2 id="humidite">%humidite%</h2>
<h1>Altitude</h1>
<h2 id="altitude">%altitude%</h2>
<h1>Pression</h1>
<h2 id="pression">%pression%</h2>
</body>
</html>
)=====";

void SwitchLedOn() {
digitalWrite(5,HIGH);
handleRoot();
}

void SwitchLedOff() {
digitalWrite(5,LOW);
handleRoot();
}

void handleRoot() {
String temp(reinterpret_cast<const __FlashStringHelper *>(index_html));
if(digitalRead(5)==HIGH)
temp.replace("%LED%","LED Allumée");
else
temp.replace("%LED%","LED Eteinte");
temp.replace("%temperature%",String(temperature) + "°C");
temp.replace("%humidite%",String(humidite) + "%");
temp.replace("%altitude%",String(altitude) + "m");
temp.replace("%pression%",String(pression) + "Pa");
ServerWeb.send(200,"text/html",temp);
}

void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
while(!wm.autoConnect(ssid,password))
{
Serial.println(".");
}
display.init();
display.flipScreenVertically();
display.clear();
while(!Serial); // time to get serial running
Serial.println(F("BME280 test"));

unsigned status;

status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring,
address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or
BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}

Serial.println("-- Default Test --");


delayTime = 1000;

Serial.println();

pinMode(5,OUTPUT);
WiFi.softAPConfig(IP,gateway,subnet);
WiFi.softAP(ssid,password);

ServerWeb.on("/SwitchLedOn",SwitchLedOn);
ServerWeb.on("/SwitchLedOff",SwitchLedOff);
ServerWeb.on("/",handleRoot);
ServerWeb.on("/data", handleData);
ServerWeb.on("index.html",handleRoot);

ServerWeb.begin();
}

void handleData() {
String jsonData = "{";
jsonData += "\"ledStatus\": \"" + String(digitalRead(5) == HIGH ? "Allumée" :
"Eteinte") + "\",";
jsonData += "\"temperature\": \"" + String(bme.readTemperature()) + "°C\",";
jsonData += "\"humidity\": \"" + String(bme.readHumidity()) + "%\",";
jsonData += "\"altitude\": \"" + String(bme.readAltitude(SEALEVELPRESSURE_HPA))
+ " m\",";
jsonData += "\"pressure\": \"" + String(bme.readPressure()) + " Pa\"";
jsonData += "}";

ServerWeb.send(200, "application/json", jsonData);


}
void loop() {
display.clear();
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, "Temperature : ");
display.drawString(74, 0, String(bme.readTemperature()));
display.drawString(0, 10, "Humidite : ");
display.drawString(50, 10, String(bme.readHumidity()));
display.drawString(0, 20, "Pression : ");
display.drawString(50, 20, String(bme.readPressure()));
display.drawString(0, 30, "Altitude : ");
display.drawString(50, 30, String(bme.readAltitude(SEALEVELPRESSURE_HPA)));
display.display();

delay(500);

if(analogRead(A0)> 1000)
{
wm.resetSettings();
ESP.restart();
}

humidite = bme.readHumidity();
temperature = bme.readTemperature();
pression = bme.readPressure();
altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);

ServerWeb.handleClient();
}

You might also like