ioT-Sensor/sensor.ino

173 lines
4.5 KiB
C++

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#include "SparkFunCCS811.h"
#include <AM2302-Sensor.h>
// 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();
}
}