Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <ESPAsyncWebServer.h>
- #include <HTTPClient.h>
- #include <Adafruit_BMP085.h>
- #include <DHT.h>
- #include <DHT_U.h>
- #include <FS.h>
- #include "SPIFFS.h"
- HTTPClient http;
- Adafruit_BMP085 bmp;
- unsigned long last_time = 0;
- unsigned long timer_delay = 10000;
- //here we use 4 of ESP32 to read data
- #define DHTPIN 18
- //our sensor is DHT11 type
- #define DHTTYPE DHT11
- //create an instance of DHT sensor
- DHT dht(DHTPIN, DHTTYPE);
- const char *ssid = "const";
- const char *password = "const";
- const char *serverName = "const";
- // Keep this API Key value to be compatible with the PHP code provided in the project page.
- // If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
- String apiKeyValue = "xxx";
- AsyncWebServer server(80);
- void setup()
- {
- //----------------------------------------------------Serial
- Serial.begin(115200);
- Serial.println("\n");
- //----------------------------------------------------GPIO
- //----------------------------------------------------SPIFFS
- if (!SPIFFS.begin())
- {
- Serial.println("Erreur SPIFFS...");
- return;
- }
- File root = SPIFFS.open("/");
- File file = root.openNextFile();
- while (file)
- {
- Serial.print("File: ");
- Serial.println(file.name());
- file.close();
- file = root.openNextFile();
- }
- //----------------------------------------------------WIFI
- WiFi.begin(ssid, password);
- Serial.print("Tentative de connexion...");
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(100);
- }
- Serial.println("\n");
- Serial.println("Connexion etablie!");
- Serial.print("Adresse IP: ");
- Serial.println(WiFi.localIP());
- //Serial.println("DHT11 sensor!");
- //call begin to start sensor
- //dht.begin();
- //use the functions which are supplied by library.
- //float humidite = dht.readHumidity();
- // Read temperature as Celsius (the default)
- //float temperature = dht.readTemperature();
- // Check if any reads failed and exit early (to try again).
- // if (isnan(humidite) || isnan(temperature)) {
- // Serial.println("Failed to read from DHT sensor!");
- // return;
- // }
- dht.begin();
- //----------------------------------------------------SERVER
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
- {
- request->send(SPIFFS, "/index.html", "text/html");
- });
- server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request)
- {
- request->send(SPIFFS, "/style.css", "text/css");
- });
- server.on("/script.js", HTTP_GET, [](AsyncWebServerRequest *request)
- {
- request->send(SPIFFS, "/script.js", "text/javascript");
- });
- server.on("/script.js", HTTP_GET, [](AsyncWebServerRequest *request)
- {
- request->send(SPIFFS, "/jquery-3.4.1.min.js", "text/javascript");
- });
- server.on("/lireTemperature", HTTP_GET, [](AsyncWebServerRequest *request)
- {
- double val_temp = bmp.readTemperature();
- String Temperature = String(val_temp);
- request->send(200, "text/plain", Temperature);
- });
- server.on("/lireTemperatureDHT", HTTP_GET, [](AsyncWebServerRequest *request)
- {
- double val_tempDHT = dht.readTemperature();
- String TemperatureDHT = String(val_tempDHT);
- request->send(200, "text/plain", TemperatureDHT);
- });
- server.on("/lirePression", HTTP_GET, [](AsyncWebServerRequest *request)
- {
- int val_pression = bmp.readPressure() / 100;
- String Pression = String(val_pression);
- request->send(200, "text/plain", Pression);
- });
- server.on("/lireAltitude", HTTP_GET, [](AsyncWebServerRequest *request)
- {
- int val_altitude = bmp.readAltitude(103200);
- String Altitude = String(val_altitude);
- request->send(200, "text/plain", Altitude);
- });
- server.on("/lireHumidite", HTTP_GET, [](AsyncWebServerRequest *request)
- {
- int val_humidite = dht.readHumidity();
- String Humidite = String(val_humidite);
- request->send(200, "text/plain", Humidite);
- });
- server.begin();
- Serial.println("Serveur actif!");
- if (!bmp.begin())
- {
- Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
- while (1) {}
- }
- }
- void loop()
- {
- //Send an HTTP POST request every 10 seconds
- if ((millis() - last_time) > timer_delay)
- {
- if (WiFi.status() == WL_CONNECTED)
- {
- http.begin(serverName);
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- //Data to send with HTTP POST
- String httpRequestData = "api_key=" + apiKeyValue + "&humidity=" + dht.readHumidity()
- +
- "&temperature=" + dht.readTemperature() + "";
- // Send HTTP POST request
- int httpResponseCode = http.POST(httpRequestData);
- //http.addHeader("Content-Type", "application/json");
- // JSON data to send with HTTP POST
- //String httpRequestData = "{\"api_key\":\"" + my_Api_Key + "\",\"field1\":\"" + String(random(50)) + "\"}";
- // Send HTTP POST request
- // int httpResponseCode = http.POST(httpRequestData);
- Serial.print("HTTP Response code is: ");
- Serial.println(httpResponseCode);
- http.end();
- }
- else
- {
- Serial.println("WiFi is Disconnected!");
- }
- }
- last_time = millis();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement