From dc9027678361f91dde79ed484fa82d04f8acf6e3 Mon Sep 17 00:00:00 2001 From: Julian Kiedaisch Date: Tue, 4 Mar 2025 12:12:19 +0000 Subject: [PATCH] server.ino added --- sensor.ino | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 sensor.ino diff --git a/sensor.ino b/sensor.ino new file mode 100644 index 0000000..6859b44 --- /dev/null +++ b/sensor.ino @@ -0,0 +1,173 @@ +#include +#include +#include +#include +#include +#include +#include +#include "SparkFunCCS811.h" +#include + +// Set WiFi credentials +#define WIFI_SSID "mdg-sensor" +#define WIFI_PASS "" // wird von Lehrkraft ausgefuellt + +const char* sender = "NWP-SensorX"; // muss eindeutiger Name sein +const char* serverName = "https://sensor.hub.mdg-hamburg.de/send"; + +#define SEALEVELPRESSURE_HPA (1013.25) +#define BMP_SCK 0//14 +#define BMP_MISO 12 +#define BMP_MOSI 2//0 +#define BMP_CS 14//2 + +#define CCS811_ADDR 0x5A + + +Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK); + +CCS811 airSensor(CCS811_ADDR); + +float temperature; +float pressure; +float temperature_2; +float humidity; + +uint co2; +uint tvoc; + +constexpr unsigned int SENSOR_PIN {13U}; + +AM2302::AM2302_Sensor am2302{SENSOR_PIN}; + + +unsigned long lastTime = 0; +unsigned long timerDelay = 60000; // send readings timer + +void getSensorReadings(){ + + temperature = bmp.readTemperature(); + pressure = bmp.readPressure() / 100.0F; + auto status = am2302.read(); + temperature_2 = am2302.get_Temperature(); + humidity = am2302.get_Humidity(); + + if (airSensor.dataAvailable()) + { + airSensor.readAlgorithmResults(); + co2 = airSensor.getCO2(); + tvoc = airSensor.getTVOC(); + } + airSensor.setEnvironmentalData(humidity, temperature); +} + + +void setup() { + Serial.begin(115200); + + // Begin WiFi + WiFi.begin(WIFI_SSID, WIFI_PASS); + + // Connecting to WiFi... + Serial.print("Connecting to "); + Serial.print(WIFI_SSID); + while (WiFi.status() != WL_CONNECTED) + { + delay(100); + Serial.print("."); + } + + // Connected to WiFi + Serial.println(); + Serial.println("Connected!"); + Serial.print("IP address for network "); + Serial.print(WIFI_SSID); + Serial.print(" : "); + Serial.println(WiFi.localIP()); + + bool status; + // default settings + // (you can also pass in a Wire library object like &Wire2) + status = bmp.begin(); + if (!status) { + Serial.println("Could not detect a BMP280 sensor, Fix wiring Connections!"); + while (1); + } + + if (am2302.begin()) { + // this delay is needed to receive valid data, + // when the loop directly read again + delay(3000); + } + else { + while (true) { + Serial.println("Error: am2302 sensor check. => Please check sensor connection!"); + delay(10000); + } + } + + Wire.begin(); + if (airSensor.begin() == false) + { + Serial.print("CCS811 error. Please check wiring. Freezing..."); + while (1) + ; + } +} + +void loop() { + if ((millis() - lastTime) > timerDelay) { + //timeClient.update(); + //Get a time structure + //time_t epochTime = timeClient.getEpochTime(); + //struct tm *ptm = gmtime ((time_t *)&epochTime); + + //int monthDay = ptm->tm_mday; + //int currentMonth = ptm->tm_mon+1; + //int currentYear = ptm->tm_year+1900; + //String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay); + //Serial.print("Current date: "); + //Serial.println(currentDate); + //String formattedTime = timeClient.getFormattedTime(); + //Serial.print("Formatted Time: "); + //Serial.println(formattedTime); + getSensorReadings(); + Serial.printf("Temperature = %.2f ºC \n", temperature); + Serial.printf("Temperature2 = %.2f ºC \n", temperature_2); + Serial.printf("Pressure = %.2f hPa \n", pressure); + Serial.printf("Humidity = %.2f \n", humidity); + Serial.printf("CO2 = %d \n", co2); + Serial.printf("TVOC = %d \n", tvoc); + Serial.println(); + + if(WiFi.status()== WL_CONNECTED){ + WiFiClientSecure client; + client.setInsecure(); + HTTPClient http; + // Your Domain name with URL path or IP address with path + http.begin(client, serverName); + // Specify content-type header + http.addHeader("Content-Type", "application/json"); + // Data to send with HTTP POST + String httpRequestData = "{\"sender\":\"" + String(sender) + "\",\"data\":[" + String(temperature) + "," + String(temperature_2) + "," + String(pressure) + "," + String(humidity) + "," + String(co2) + "," + String(tvoc) + "]}"; + // Send HTTP POST request + Serial.println(httpRequestData); + int httpResponseCode = http.POST(httpRequestData); + + Serial.print("HTTP Response code: "); + Serial.println(httpResponseCode); + + // Free resources + http.end(); + } + else { + Serial.println("WiFi Disconnected"); + WiFi.begin(WIFI_SSID, WIFI_PASS); + } + + + lastTime = millis(); + + + } +} \ No newline at end of file