Create

Χρησιμοποιήσαμε ένα ESP 8266, καλώδια και ένα ΒΜΕ680. Μέσω WiFi τα δεδομένα μεταφέρονται στην Διαδικτυακή σελίδα μας https://iot.labrakis.com/stempowering-youth-karditsa/, όπου και είναι διαθέσιμα σε όλους.
Τα υλικά που χρησιμοποιήσαμε:
Nodemcu-lua-based-esp8266
bme680-environmental-sensor-sen0248
power-supply-5v-2a-usb-plug
στεγανό κουτί
Wire jumper καλώδια
breadboard
O Κώδικας:
#include
const char *ssid = "";
const char *pass = "";
const char server[] = "";
#include
#include
#include
#include
#include "Adafruit_BME680.h"
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME680 bme; // I2C
float hum_weighting = 0.25; // so hum effect is 25% of the total air quality score
float gas_weighting = 0.75; // so gas effect is 75% of the total air quality score
float hum_score, gas_score;
float gas_reference = 250000;
float hum_reference = 40;
int getgasreference_count = 0;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println(F("BME680 test"));
Wire.begin();
if (!bme.begin()) {
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
} else Serial.println("Found a sensor");
bme.setTemperatureOversampling(BME680_OS_2X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_2X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320°C for 150 ms
GetGasReference();
}
void loop() {
String ADCData, station, getData, Link, payload;
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println("°C");
Serial.print(" Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print(" Humidity = ");
Serial.print(bme.readHumidity());
Serial.println("%");
Serial.print(" Gas = ");
Serial.print(bme.readGas());
Serial.println("R\n");
float current_humidity = bme.readHumidity();
if (current_humidity >= 38 && current_humidity <= 42)
hum_score = 0.25*100; // Humidity +/-5% around optimum
else
{ //sub-optimal
if (current_humidity < 38)
hum_score = 0.25/hum_reference*current_humidity*100;
else
{
hum_score = ((-0.25/(100-hum_reference)*current_humidity)+0.416666)*100;
}
}
float gas_lower_limit = 5000; // Bad air quality limit
float gas_upper_limit = 50000;
{
if (gas_reference > gas_upper_limit)
gas_reference = gas_upper_limit;
}
if (gas_reference < gas_lower_limit) gas_reference = gas_lower_limit;
gas_score = (0.75/(gas_upper_limit-gas_lower_limit)*gas_reference -(gas_lower_limit*(0.75/(gas_upper_limit-gas_lower_limit))))*100;
float air_quality_score = hum_score + gas_score;
Serial.println("Air Quality = "+String(air_quality_score,1)+"% derived from 25% of Humidity reading and 75% of Gas reading - 100% is good quality air");
Serial.println("Humidity element was : "+String(hum_score/100)+" of 0.25");
Serial.println(" Gas element was : "+String(gas_score/100)+" of 0.75");
if (bme.readGas() < 120000) Serial.println("***** Poor air quality *****");
Serial.println("----------");
if ((getgasreference_count++)%10==0) GetGasReference();
String IAQs=CalculateIAQ(air_quality_score);
Serial.print(IAQs);
Serial.print(" -- ");
float IAQ=(100-air_quality_score)*5;
Serial.println(IAQ);
Serial.println("------------------------------------------------");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
HTTPClient http; //Declare object of class HTTPClient
float t = bme.readTemperature();
getData = "?sensor=kard0Temp&value=" + String(t); //Note "?" added at front
Link = "http://iot.labrakis.com/upload.php" + getData;
Serial.println(Link);
http.begin(Link); //Specify request destination
int httpCode = http.GET(); //Send the request
payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
float h = bme.readHumidity();
getData = "?sensor=kard0Hum&value=" + String(h); //Note "?" added at front
Link = "http://iot.labrakis.com/upload.php" + getData;
Serial.println(Link);
http.begin(Link); //Specify request destination
httpCode = http.GET(); //Send the request
payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
float p = bme.readPressure();
getData = "?sensor=kard0Pres&value=" + String(p/100); //Note "?" added at front
Link = "http://iot.labrakis.com/upload.php" + getData;
Serial.println(Link);
http.begin(Link); //Specify request destination
httpCode = http.GET(); //Send the request
payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
float a = air_quality_score;
getData = "?sensor=kard0Air&value=" + String(IAQ,2); //Note "?" added at front
Link = "http://iot.labrakis.com/upload.php" + getData;
Serial.println(Link);
http.begin(Link); //Specify request destination
httpCode = http.GET(); //Send the request
payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
delay(55000);
}
void GetGasReference(){
// Now run the sensor for a burn-in period, then use combination of relative humidity and gas resistance to estimate indoor air quality as a percentage.
Serial.println("Getting a new gas reference value");
int readings = 10;
for (int i = 1; i <= readings; i++){ // read gas for 10 x 0.150mS = 1.5secs
gas_reference += bme.readGas();
}
gas_reference = gas_reference / readings;
}
String CalculateIAQ(float score){
String IAQ_text = "Air quality is ";
score = (100-score)*5;
if (score >= 301) IAQ_text += "Hazardous";
else if (score >= 201 && score <= 300 ) IAQ_text += "Very Unhealthy";
else if (score >= 176 && score <= 200 ) IAQ_text += "Unhealthy";
else if (score >= 151 && score <= 175 ) IAQ_text += "Unhealthy for Sensitive Groups";
else if (score >= 51 && score <= 150 ) IAQ_text += "Moderate";
else if (score >= 00 && score <= 50 ) IAQ_text += "Good";
return IAQ_text;
}
WiFiClient client;