Advertisement
pseud0_

Untitled

Jan 19th, 2022
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.03 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <ESPAsyncWebServer.h>
  3. #include <HTTPClient.h>
  4. #include <Adafruit_BMP085.h>
  5. #include <DHT.h>
  6. #include <DHT_U.h>
  7. #include <FS.h>
  8. #include "SPIFFS.h"
  9.  
  10. HTTPClient http;
  11. Adafruit_BMP085 bmp;
  12. unsigned long last_time = 0;
  13. unsigned long timer_delay = 10000;
  14.  
  15. //here we use 4 of ESP32 to read data
  16. #define DHTPIN 18
  17. //our sensor is DHT11 type
  18. #define DHTTYPE DHT11
  19. //create an instance of DHT sensor
  20. DHT dht(DHTPIN, DHTTYPE);
  21.  
  22. const char *ssid = "const";
  23. const char *password = "const";
  24. const char *serverName = "const";
  25.  
  26. // Keep this API Key value to be compatible with the PHP code provided in the project page.
  27. // If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
  28. String apiKeyValue = "xxx";
  29. AsyncWebServer server(80);
  30.  
  31. void setup()
  32. {
  33.     //----------------------------------------------------Serial
  34.     Serial.begin(115200);
  35.     Serial.println("\n");
  36.     //----------------------------------------------------GPIO
  37.     //----------------------------------------------------SPIFFS
  38.     if (!SPIFFS.begin())
  39.     {
  40.         Serial.println("Erreur SPIFFS...");
  41.         return;
  42.     }
  43.     File root = SPIFFS.open("/");
  44.     File file = root.openNextFile();
  45.     while (file)
  46.     {
  47.         Serial.print("File: ");
  48.         Serial.println(file.name());
  49.         file.close();
  50.         file = root.openNextFile();
  51.     }
  52.     //----------------------------------------------------WIFI
  53.     WiFi.begin(ssid, password);
  54.     Serial.print("Tentative de connexion...");
  55.     while (WiFi.status() != WL_CONNECTED)
  56.     {
  57.         Serial.print(".");
  58.         delay(100);
  59.     }
  60.     Serial.println("\n");
  61.     Serial.println("Connexion etablie!");
  62.     Serial.print("Adresse IP: ");
  63.     Serial.println(WiFi.localIP());
  64.     //Serial.println("DHT11 sensor!");
  65.     //call begin to start sensor
  66.     //dht.begin();
  67.     //use the functions which are supplied by library.
  68.     //float humidite = dht.readHumidity();
  69.     // Read temperature as Celsius (the default)
  70.     //float temperature = dht.readTemperature();
  71.     // Check if any reads failed and exit early (to try again).
  72.     // if (isnan(humidite) || isnan(temperature)) {
  73.     //   Serial.println("Failed to read from DHT sensor!");
  74.     // return;
  75.     // }
  76.     dht.begin();
  77.     //----------------------------------------------------SERVER
  78.     server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
  79.         {
  80.             request->send(SPIFFS, "/index.html", "text/html");
  81.     });
  82.     server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request)
  83.         {
  84.             request->send(SPIFFS, "/style.css", "text/css");
  85.     });
  86.     server.on("/script.js", HTTP_GET, [](AsyncWebServerRequest *request)
  87.         {
  88.             request->send(SPIFFS, "/script.js", "text/javascript");
  89.     });
  90.     server.on("/script.js", HTTP_GET, [](AsyncWebServerRequest *request)
  91.         {
  92.             request->send(SPIFFS, "/jquery-3.4.1.min.js", "text/javascript");
  93.     });
  94.     server.on("/lireTemperature", HTTP_GET, [](AsyncWebServerRequest *request)
  95.         {
  96.             double val_temp = bmp.readTemperature();
  97.             String Temperature = String(val_temp);
  98.             request->send(200, "text/plain", Temperature);
  99.     });
  100.     server.on("/lireTemperatureDHT", HTTP_GET, [](AsyncWebServerRequest *request)
  101.         {
  102.             double val_tempDHT = dht.readTemperature();
  103.             String TemperatureDHT = String(val_tempDHT);
  104.             request->send(200, "text/plain", TemperatureDHT);
  105.     });
  106.     server.on("/lirePression", HTTP_GET, [](AsyncWebServerRequest *request)
  107.         {
  108.             int val_pression = bmp.readPressure() / 100;
  109.             String Pression = String(val_pression);
  110.             request->send(200, "text/plain", Pression);
  111.     });
  112.     server.on("/lireAltitude", HTTP_GET, [](AsyncWebServerRequest *request)
  113.         {
  114.             int val_altitude = bmp.readAltitude(103200);
  115.             String Altitude = String(val_altitude);
  116.             request->send(200, "text/plain", Altitude);
  117.     });
  118.     server.on("/lireHumidite", HTTP_GET, [](AsyncWebServerRequest *request)
  119.         {
  120.             int val_humidite = dht.readHumidity();
  121.             String Humidite = String(val_humidite);
  122.             request->send(200, "text/plain", Humidite);
  123.     });
  124.     server.begin();
  125.     Serial.println("Serveur actif!");
  126.     if (!bmp.begin())
  127.     {
  128.         Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
  129.         while (1) {}
  130.     }
  131. }
  132. void loop()
  133. {
  134.     //Send an HTTP POST request every 10 seconds
  135.     if ((millis() - last_time) > timer_delay)
  136.     {
  137.         if (WiFi.status() == WL_CONNECTED)
  138.         {
  139.             http.begin(serverName);
  140.             http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  141.             //Data to send with HTTP POST
  142.             String httpRequestData = "api_key=" + apiKeyValue + "&humidity=" + dht.readHumidity()
  143.                 +
  144.                 "&temperature=" + dht.readTemperature() + "";
  145.             // Send HTTP POST request
  146.             int httpResponseCode = http.POST(httpRequestData);
  147.             //http.addHeader("Content-Type", "application/json");
  148.             // JSON data to send with HTTP POST
  149.             //String httpRequestData = "{\"api_key\":\"" + my_Api_Key + "\",\"field1\":\"" + String(random(50)) + "\"}";          
  150.             // Send HTTP POST request
  151.             // int httpResponseCode = http.POST(httpRequestData);
  152.             Serial.print("HTTP Response code is: ");
  153.             Serial.println(httpResponseCode);
  154.             http.end();
  155.         }
  156.         else
  157.         {
  158.             Serial.println("WiFi is Disconnected!");
  159.         }
  160.     }
  161.     last_time = millis();
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement