Advertisement
manhoosbilli1

updated wifi esp32 code

Nov 16th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3. #include <Arduino.h>
  4. #include <WiFi.h>
  5. #include <HTTPClient.h>
  6. #include "DHT.h"
  7. const char* ssid = "Shoaib";
  8. const char* password = "A01234567890";
  9. #define DHTPIN 32
  10. #define oneWireBus  33
  11. #define DHTTYPE DHT22
  12. #define moistPin  35
  13. #define LDRPin  34
  14. int LDRValue = 0;
  15. int light;
  16. String values;
  17. unsigned long currentMillis;
  18. unsigned long previousMillis;
  19. const int interval = 2500;
  20. DHT dht(DHTPIN, DHTTYPE);
  21. OneWire oneWire(oneWireBus);
  22. DallasTemperature sensors(&oneWire);
  23.  
  24. void setup() {
  25.   // Start the Serial Monitor
  26.   Serial.begin(115200);
  27.   sensors.begin();  //starts ds18b20
  28.   dht.begin();   //give 2 second delay to all sensors
  29. }
  30.  
  31.  
  32. void loop() {
  33.   //get values and send to website only once and then go deep sleep
  34.   currentMillis = millis();
  35.   if (currentMillis - previousMillis > interval) {
  36.     getSensorValues();
  37.     //readBattery();   not configured yet
  38.     delay(1);
  39.     connectWifi();
  40.     delay(100);
  41.     postValues();
  42.     delay(100);
  43.     disconnectWifi();
  44.     delay(100);
  45.     previousMillis = currentMillis;
  46.   }
  47. }
  48.  
  49. String readBattery() {
  50.   uint8_t percentage = 100;
  51.   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
  52.   //float voltage = analogRead(39) / 4096.0 * 7.23;    // NODEMCU ESP32 with 100K+100K voltage divider added
  53.   //float voltage = analogRead(A0) / 4096.0 * 4.24;    // Wemos / Lolin D1 Mini 100K series resistor added
  54.   //float voltage = analogRead(A0) / 4096.0 * 5.00;    // Ardunio UNO, no voltage divider required
  55.   Serial.println("Voltage = " + String(voltage));
  56.   percentage = 2808.3808 * pow(voltage, 4) - 43560.9157 * pow(voltage, 3) + 252848.5888 * pow(voltage, 2) - 650767.4615 * voltage + 626532.5703;
  57.   if (voltage > 4.19) percentage = 100;
  58.   else if (voltage <= 3.50) percentage = 0;
  59.   return String(percentage) + "%";
  60. }
  61.  
  62. void getSensorValues() {
  63.   sensors.requestTemperatures();
  64.   float soilTemp = sensors.getTempCByIndex(0);
  65.   float h = dht.readHumidity();
  66.   values += "soilTemp=" + String(h);
  67.   float t = dht.readTemperature();
  68.   if (isnan(h) || isnan(t)) {
  69.     Serial.println(F("Failed to read from DHT sensor!")); //print to server
  70.     return;
  71.   }
  72.   LDRValue = analogRead(LDRPin); // read the value from the LDR
  73.   light = map(ldrValue, 0, 4095, 0, 100); //map light value as percentage
  74.   moistVal = analogRead(moistPin);
  75.   moisture = map(moistVal, 0, 4095, 100, 0);
  76. }
  77.  
  78.  
  79. void postValues() {
  80.   HTTPClient http;
  81.   http.begin("https://finalyearprojectnuml.000webhostapp.com/fyp.php");
  82.   http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  83.   int httpResponseCode = http.POST(String(values));
  84.   if (httpResponseCode > 0) {
  85.     String response = http.getString();
  86.     Serial.println(httpResponseCode);
  87.     Serial.println(response);
  88.   }
  89.   else {
  90.     Serial.print("Error on sending post");
  91.     Serial.println(httpResponseCode);
  92.   }
  93.   http.end();
  94. }
  95.  
  96.  
  97. void connectWifi() {
  98.   WiFi.begin(ssid, password);
  99.   while (WiFi.status() != WL_CONNECTED) {
  100.     delay(500);
  101.     Serial.print(".");
  102.   }
  103.   Serial.println("");
  104.   Serial.println("WiFi connected");
  105.   Serial.println("IP Address");
  106.   Serial.println(WiFi.localIP());
  107. }
  108.  
  109.  
  110. void disconnectWifi(){
  111.   WiFi.disconnect();
  112.   while(WiFi.status() == WL_CONNECTED){
  113.     delay(500);
  114.     Serial.println("Wifi failed to disconnect");
  115.   }
  116.   else
  117.   Serial.println("Wifi disconnected succesfully");
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement