Advertisement
manhoosbilli1

esp-32 post to website

Nov 22nd, 2019
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <Arduino.h>           //don't need to add this one...already included in arduino
  2. #include <WiFi.h>
  3. #include <HTTPClient.h>
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6. #include "DHT.h"
  7.  
  8. #define DHTPIN 32
  9. #define oneWireBus  33
  10. #define DHTTYPE DHT22
  11. #define moistPin  35
  12. #define LDRPin  34
  13. DHT dht(DHTPIN, DHTTYPE);
  14. OneWire oneWire(oneWireBus);
  15. DallasTemperature sensors(&oneWire);
  16. const char* ssid = "SSID";
  17. const char* password = "password";
  18. float soilTemperature, h, t, lightIntensity, soilMoisture;
  19. void setup() {
  20.   // put your setup code here, to run once:
  21.   Serial.begin(115200);
  22.   sensors.begin();  //starts ds18b20
  23.   dht.begin();   //give 2 second delay to all sensors
  24.   WiFi.begin(ssid, password);
  25.   while (WiFi.status() != WL_CONNECTED) {
  26.     delay(500);
  27.     Serial.print(".");
  28.   }
  29.   Serial.println("");
  30.   Serial.println("WiFi connected");
  31.   Serial.println("IP Address");
  32.   Serial.println(WiFi.localIP());
  33.  
  34. }
  35.  
  36.  
  37. void loop() {
  38.   HTTPClient http;
  39.   http.begin("https://finalyearprojectnuml.000webhostapp.com/fyp.php");
  40.   http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  41.  
  42.  
  43.   String Data = "humidity= " + String(h);
  44.   Data += "&air_temp= " + String(t);
  45.   Data += "&humidity= " + String(h);
  46.   Data += "&temp=" + String(soilTemperature);                                      //change the order of data if you want. just keep the format similar
  47.   Data += "&soil_moisture=" + String(soilMoisture);
  48.   Data += "&light_intensity=" + String(lightIntensity);
  49.  
  50.  
  51.   int httpResponseCode = http.POST(Data);
  52.   if (httpResponseCode > 0) {
  53.     String response = http.getString();                //once you upload the code open serial monitor to show response code sent from website
  54.     Serial.println(httpResponseCode);
  55.     Serial.println(response);
  56.   }
  57.   else {
  58.     Serial.print("Error on sending post");
  59.     Serial.println(httpResponseCode);
  60.   }
  61.   http.end();
  62.   delay(2000);
  63.  
  64. }
  65.  
  66.  
  67.  
  68. void getSensorValues() {
  69.   sensors.requestTemperatures();
  70.   float soilTemp = sensors.getTempCByIndex(0);
  71.   float h = dht.readHumidity();
  72.   t = dht.readTemperature();
  73.   if (isnan(h) || isnan(t)) {
  74.     Serial.println(F("Failed to read from DHT sensor!")); //print to server
  75.     return;
  76.   }
  77.   lightIntensity = analogRead(LDRPin); // read the value from the LDR
  78.   soilMoisture = analogRead(moistPin);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement