Advertisement
Guest User

Untitled

a guest
May 6th, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include <LiquidCrystal_I2C.h>
  2. #include <SPI.h>
  3. #include <SD.h>
  4. #include <DHT.h>
  5. #include <Wire.h>
  6. //Pressure Sensor
  7. #include <Adafruit_BMP085.h>
  8. #include <WiFi101.h>
  9. #include "arduino_secrets.h"
  10. //modified library
  11. #include <NTPClient.h>
  12. #include <WiFiUdp.h>
  13.  
  14. #define DHTPIN 8
  15. #define DHTTYPE DHT22
  16. #define SHUMSENSOR A0
  17.  
  18. DHT dht(DHTPIN, DHTTYPE);
  19. LiquidCrystal_I2C lcd(0x3F, 20, 4);
  20. Adafruit_BMP085 bmp;
  21. WiFiClient Client;
  22.  
  23. const int chipSelect = 4;
  24. int status = WL_IDLE_STATUS;
  25.  
  26. WiFiUDP ntpUDP;
  27. NTPClient timeClient(ntpUDP);
  28.  
  29. void setup() {
  30.   bmp.begin();
  31.   dht.begin();
  32.   Serial.begin(115200);
  33.   lcd.init();
  34.   lcd.backlight();
  35.   analogReadResolution(12);
  36.   while(status != WL_CONNECTED) {
  37.     Serial.print("Verbinde zu SSID: ");
  38.     Serial.println(ssid);
  39.     status = WiFi.begin(ssid, pass);
  40.     delay(10000);
  41.   }
  42.   Serial.println("Verbunden!");
  43.  
  44.   timeClient.begin();
  45.   // Set offset time in seconds to adjust for your timezone, for example:
  46.   // GMT +1 = 3600
  47.   // GMT +8 = 28800
  48.   // GMT -1 = -3600
  49.   // GMT 0 = 0
  50.   timeClient.setTimeOffset(7200);
  51.  
  52.   while (!Serial);
  53.     Serial.print("Initializing SD card...");
  54.   if (!SD.begin(chipSelect)) {
  55.     Serial.println("initialization failed. Things to check:");
  56.     Serial.println("1. is a card inserted?");
  57.     Serial.println("2. is your wiring correct?");
  58.     Serial.println("3. did you change the chipSelect pin to match your shield or module?");
  59.     Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
  60.     while (true);
  61.   }
  62.   Serial.println("initialization done.");
  63. }
  64.  
  65. void loop() {
  66.   delay(2000);
  67.   //functions for LCD
  68.   readTempHum();
  69.   readPress();
  70.   readSHum();
  71.   writeSD();
  72.  
  73.   while(!timeClient.update()) {
  74.     timeClient.forceUpdate();
  75.   }
  76.  
  77.   Serial.print(timeClient.getFormattedDate());
  78.   Serial.print(", ");
  79.   Serial.print(timeClient.getFormattedTime());
  80.   Serial.print(", ");
  81.   Serial.print(dht.readTemperature(), 1);
  82.   Serial.print(" °C, ");
  83.   Serial.print(dht.readHumidity(), 1);
  84.   Serial.print(" hPa, ");
  85.   Serial.println(analogRead(SHUMSENSOR));
  86. }
  87.  
  88. void readTempHum() {
  89.   lcd.setCursor(0, 0);
  90.   lcd.print("Temperatur: ");
  91.   lcd.setCursor(0, 1);
  92.   lcd.print("Luftfeuchte: ");
  93.   lcd.setCursor(13, 0);
  94.   lcd.print(dht.readTemperature(), 1);
  95.   lcd.setCursor(18, 0);
  96.   lcd.write(0xDF);
  97.   lcd.print("C");
  98.   lcd.setCursor(13, 1);
  99.   lcd.print(dht.readHumidity(), 1);
  100.   lcd.setCursor(17, 1);
  101.   lcd.print("%");
  102. }
  103.  
  104. void readPress() {
  105.   lcd.setCursor(0, 2);
  106.   lcd.print("Luftdruck: ");
  107.   lcd.setCursor(13, 2);
  108.   lcd.print(bmp.readPressure() / 100);
  109.   lcd.print(" hPa");
  110. }
  111.  
  112. void readSHum() {
  113.   lcd.setCursor(0, 3);
  114.   lcd.print("Bodenfeuchte: ");
  115.   lcd.setCursor(14, 3);
  116.   int bodenFeuchte = analogRead(SHUMSENSOR);
  117.   lcd.print(bodenFeuchte);
  118. }
  119.  
  120. void writeSD() {
  121.   File dataFile = SD.open("datalog.txt", FILE_WRITE);
  122.   // if the file is available, write to it:
  123.   if (dataFile) {
  124.     dataFile.print(timeClient.getFormattedDate());
  125.     dataFile.print(", ");
  126.     dataFile.print(timeClient.getFormattedTime());
  127.     dataFile.print(", ");
  128.     dataFile.print(dht.readTemperature(), 1);
  129.     dataFile.print(" °C, ");
  130.     dataFile.print(dht.readHumidity(), 1);
  131.     dataFile.print(" hPa, ");
  132.     dataFile.println(analogRead(SHUMSENSOR));
  133.     dataFile.close();
  134.   }
  135.     // if the file isn't open, pop up an error:
  136.   else {
  137.     Serial.println("error opening datalog.txt");
  138.   }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement