Dateien nach "Res" hochladen
This commit is contained in:
166
Res/ESP8266-Sensor.ino
Normal file
166
Res/ESP8266-Sensor.ino
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
#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>
|
||||||
|
|
||||||
|
//WLAN-Daten
|
||||||
|
#define WIFI_SSID "mdg-sensor"
|
||||||
|
#define WIFI_PASS "" //Ausgefüllt durch Lehrkraft
|
||||||
|
|
||||||
|
const char* sender = ""; //Sensorname - bitte eingeben, muss eindeutig sein (zwei Sensoren dürfen nicht den selben Namen haben)
|
||||||
|
const char* serverName = "https://sensor.hub.mdg-hamburg.de/send";
|
||||||
|
|
||||||
|
#define SEALEVELPRESSURE_HPA (1013.25)
|
||||||
|
#define BMP_SCK 0
|
||||||
|
#define BMP_MISO 12
|
||||||
|
#define BMP_MOSI 2
|
||||||
|
#define BMP_CS 14
|
||||||
|
|
||||||
|
#define CCS811_ADDR 0x5A
|
||||||
|
|
||||||
|
#define TEMT6000 A0
|
||||||
|
|
||||||
|
Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
|
||||||
|
|
||||||
|
CCS811 airSensor(CCS811_ADDR);
|
||||||
|
|
||||||
|
float temperature;
|
||||||
|
float pressure;
|
||||||
|
float temperature_2;
|
||||||
|
float humidity;
|
||||||
|
float light;
|
||||||
|
float adcv;
|
||||||
|
uint co2;
|
||||||
|
uint tvoc;
|
||||||
|
|
||||||
|
constexpr unsigned int SENSOR_PIN {13U};
|
||||||
|
|
||||||
|
AM2302::AM2302_Sensor am2302{SENSOR_PIN};
|
||||||
|
|
||||||
|
|
||||||
|
unsigned long lastTime = 0;
|
||||||
|
unsigned long timerDelay = 60000; //Ausleserate der Sensoren
|
||||||
|
|
||||||
|
void getSensorReadings(){
|
||||||
|
//Daten auslesen
|
||||||
|
temperature = bmp.readTemperature(); //Temperatur #2
|
||||||
|
pressure = bmp.readPressure() / 100.0F; //Druck
|
||||||
|
auto status = am2302.read();
|
||||||
|
temperature_2 = am2302.get_Temperature(); //Temperatur #1
|
||||||
|
humidity = am2302.get_Humidity(); //Luftfeuchte
|
||||||
|
adcv = analogRead(TEMT6000) * (3.3 / 1023); //Licht messen (analogRead)
|
||||||
|
light = adcv * 500; //Licht in lx (Lux) umwandeln
|
||||||
|
|
||||||
|
if (airSensor.dataAvailable())
|
||||||
|
{
|
||||||
|
airSensor.readAlgorithmResults();
|
||||||
|
co2 = airSensor.getCO2(); //ECO2 messen
|
||||||
|
tvoc = airSensor.getTVOC(); //TVOC messen
|
||||||
|
}
|
||||||
|
airSensor.setEnvironmentalData(humidity, temperature);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(TEMT6000, INPUT);
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
//WLAN-Verbindung aufbauen
|
||||||
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||||
|
|
||||||
|
//Mit WLAN verbinden
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.print(WIFI_SSID);
|
||||||
|
while (WiFi.status() != WL_CONNECTED)
|
||||||
|
{
|
||||||
|
delay(100);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wenn mit WLAN verbunden
|
||||||
|
Serial.println();
|
||||||
|
Serial.println("Connected!");
|
||||||
|
Serial.print("IP address for network ");
|
||||||
|
Serial.print(WIFI_SSID);
|
||||||
|
Serial.print(" : ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
bool status;
|
||||||
|
|
||||||
|
status = bmp.begin();
|
||||||
|
if (!status) {
|
||||||
|
Serial.println("Could not detect a BMP280 sensor, Fix wiring Connections!");
|
||||||
|
while (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (am2302.begin()) {
|
||||||
|
//Benötigt um richtige Daten zu erhalten
|
||||||
|
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("ERROR: CCS811 error. Please check wiring. Freezing...");
|
||||||
|
while (1)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if ((millis() - lastTime) > timerDelay) {
|
||||||
|
//Wenn der sensor seit mehr als einer Minute keine Messung gemacht hat
|
||||||
|
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);
|
||||||
|
if(TEMT6000ERR == true) {
|
||||||
|
Serial.print("NAN / Error reading TEMT6000 value");
|
||||||
|
}else{
|
||||||
|
Serial.printf("Light = %.2f \n", light);
|
||||||
|
}
|
||||||
|
Serial.printf("CO2 = %d \n", co2);
|
||||||
|
Serial.printf("TVOC = %d \n", tvoc);
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
|
if(WiFi.status()== WL_CONNECTED){
|
||||||
|
//Wenn eine WLAN-Verbindung besteht
|
||||||
|
WiFiClientSecure client;
|
||||||
|
client.setInsecure();
|
||||||
|
HTTPClient http;
|
||||||
|
|
||||||
|
http.begin(client, serverName);
|
||||||
|
|
||||||
|
//Daten senden
|
||||||
|
http.addHeader("Content-Type", "application/json");
|
||||||
|
String httpRequestData = "{\"sender\":\"" + String(sender) + "\",\"data\":[" + String(temperature) + "," + String(temperature_2) + "," + String(pressure) + "," + String(humidity) + "," + String(light) + "," + String(co2) + "," + String(tvoc) + "]}";
|
||||||
|
Serial.println(httpRequestData);
|
||||||
|
int httpResponseCode = http.POST(httpRequestData);
|
||||||
|
|
||||||
|
Serial.print("HTTP Response code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
|
||||||
|
http.end();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//Wenn kein WLAN verbunden ist
|
||||||
|
Serial.println("WiFi Disconnected");
|
||||||
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastTime = millis();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user