Advertisement
manhoosbilli1

esp-32 sensor values print to serial monitor

Nov 22nd, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. //This code will print sensor values to the serial monitor in arduino
  2.  
  3. #include <HTTPClient.h>
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6. #include <Arduino.h>
  7. #include <WiFi.h>
  8. #include "DHT.h"
  9. const char* ssid = "Ayaz";
  10. const char* password = "Ayaz1234";
  11. const char server[] = "https://kakaexperiments.000webhostapp.com";
  12. #define DHTPIN 32
  13. #define oneWireBus  33
  14. #define DHTTYPE DHT22
  15. #define moistPin  35
  16. #define LDRPin  34
  17. String postData;
  18. String postVariable = "temp=";
  19. int LDRValue = 0;
  20. int moistVal;
  21. int moisture;
  22. int light;
  23. float t;
  24. String values;
  25. unsigned long currentMillis;
  26. unsigned long previousMillis;
  27. const int interval = 2500;
  28. DHT dht(DHTPIN, DHTTYPE);
  29. OneWire oneWire(oneWireBus);
  30. DallasTemperature sensors(&oneWire);
  31.  
  32. void setup() {
  33.   // Start the Serial Monitor
  34.   Serial.begin(115200);
  35.   sensors.begin();  //starts ds18b20
  36.   dht.begin();   //give 2 second delay to all sensors
  37. }
  38.  
  39. void loop() {
  40.   getSensorValues();
  41.   delay(2000);  //Send a request every 10 seconds
  42. }
  43.  
  44.  
  45. String readBattery() {
  46.   uint8_t percentage = 100;
  47.   float voltage = analogRead(35) / 4096.0 * 7.23;      // LOLIN D32 (no voltage divider need already fitted to board.or NODEMCU ESP32 with 100K+100K voltage divider
  48.   //float voltage = analogRead(39) / 4096.0 * 7.23;    // NODEMCU ESP32 with 100K+100K voltage divider added
  49.   //float voltage = analogRead(A0) / 4096.0 * 4.24;    // Wemos / Lolin D1 Mini 100K series resistor added
  50.   //float voltage = analogRead(A0) / 4096.0 * 5.00;    // Ardunio UNO, no voltage divider required
  51.   Serial.println("Voltage = " + String(voltage));
  52.   percentage = 2808.3808 * pow(voltage, 4) - 43560.9157 * pow(voltage, 3) + 252848.5888 * pow(voltage, 2) - 650767.4615 * voltage + 626532.5703;
  53.   if (voltage > 4.19) percentage = 100;
  54.   else if (voltage <= 3.50) percentage = 0;
  55.   return String(percentage) + "%";
  56. }
  57.  
  58. void getSensorValues() {
  59.   sensors.requestTemperatures();
  60.   float soilTemp = sensors.getTempCByIndex(0);
  61.   float h = dht.readHumidity();
  62.   t = dht.readTemperature();
  63.   if (isnan(h) || isnan(t)) {
  64.     Serial.println(F("Failed to read from DHT sensor!")); //print to server
  65.     return;
  66.   }
  67.   LDRValue = analogRead(LDRPin); // read the value from the LDR
  68.   light = LDRValue;
  69.   moistVal = analogRead(moistPin);
  70.   moisture = moistVal;
  71.   Serial.println("soilTemp: " + String(soilTemp));
  72.   Serial.println("AirTemp: " + String(t));
  73.   Serial.println("Humidity: " +String(h));
  74.   Serial.println("Light Intensity: " + String(light));
  75.   Serial.println("Moisture: " + String(moisture));  
  76.   Serial.println("**************");
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement