Added new sketch and edit Readme

This commit is contained in:
Joachim Hummel 2023-03-02 21:50:08 +00:00
parent 2880def38b
commit a444f87744
2 changed files with 301 additions and 3 deletions

View File

@ -0,0 +1,298 @@
/* Disclaimer IoT-Werkstatt CC 4.0 BY NC SA
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For Ardublock see the
GNU General Public License for more details. */
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SparkFun_SCD30_Arduino_Library.h>
#include <Wire.h>
#include <bsec.h>
#include <Ticker.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
#include <Adafruit_NeoPixel.h>
String matrixausgabe_text = " "; // Ausgabetext als globale Variable
volatile int matrixausgabe_index = 0;// aktuelle Position in Matrix
IPAddress myOwnIP; // ownIP for mDNS
//-------------- definition mqtt-object ueber WiFi
WiFiClient espClient;
PubSubClient mqttclient(espClient);
//--------- list of mqtt callback functions
#define MAX_MQTT_SUB 10 // maximal 10 subscriptions erlaubt
typedef void (*mqtthandle) (byte*,unsigned int);
typedef struct { // Typdeklaration Callback
String topic; // mqtt-topic
mqtthandle fun; // callback function
}
subscribe_type;
subscribe_type mqtt_sub[MAX_MQTT_SUB];
int mqtt_sub_count=0;
String MQTT_Rx_Payload = "" ;
//--------- mqtt callback function
void mqttcallback(char* to, byte* pay, unsigned int len) {
String topic = String(to);
String payload = String((char*)pay);
MQTT_Rx_Payload=payload.substring(0,len);
Serial.println("\ncallback topic:" + topic + ", payload:" + MQTT_Rx_Payload);
for (int i=0;i<mqtt_sub_count;i++) { // durchsuche alle subscriptions, bis topic passt
if (topic==mqtt_sub[i].topic)
mqtt_sub[i].fun(pay,len); // Aufruf der richtigen callback-Funktion
}
}
//------------ reconnect mqtt-client
void mqttreconnect() { // Loop until we're reconnected
if (!mqttclient.connected()) {
while (!mqttclient.connected()) {
Serial.print("Attempting MQTT connection...");
if (mqttclient.connect("IoTHackathon" , "xxxxx", "xxxxxx" )) {
Serial.println("connected");
for (int i=0;i<mqtt_sub_count;i++) { // subscribe topic
mqttclient.subscribe(mqtt_sub[i].topic.c_str());
Serial.println("\nsubscribe");
Serial.print(mqtt_sub[i].topic);
}
}
else {
Serial.print("failed, rc=");
Serial.print(mqttclient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
else {
mqttclient.loop();
}
}
int CO2 = 0 ;
//Reading CO2, humidity and temperature from the SCD30 By: Nathan Seidle SparkFun Electronics
//https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library
SCD30 airSensorSCD30; // Objekt SDC30 Umweltsensor
int TEMP = 0 ;
/*
Bosch BSEC Lib, https://github.com/BoschSensortec/BSEC-Arduino-library
The BSEC software is only available for download or use after accepting the software license agreement.
By using this library, you have agreed to the terms of the license agreement:
https://ae-bst.resource.bosch.com/media/_tech/media/bsec/2017-07-17_ClickThrough_License_Terms_Environmentalib_SW_CLEAN.pdf */
Bsec iaqSensor; // Create an object of the class Bsec
Ticker Bsec_Ticker; // schedule cyclic update via Ticker
const uint8_t bsec_config_iaq[] = {
#include "config/generic_33v_3s_28d_2d_iaq_50_200/bsec_iaq.txt"
};
// ------------------------ Helper functions Bosch Bsec - Lib
void checkIaqSensorStatus(void)
{
String output;
if (iaqSensor.status != BSEC_OK) {
if (iaqSensor.status < BSEC_OK) {
output = "BSEC error code : " + String(iaqSensor.status);
for (;;) {
Serial.println(output);
delay(500);
} // Halt in case of failure
}
else {
output = "BSEC warning code : " + String(iaqSensor.status);
Serial.println(output);
}
}
if (iaqSensor.bme680Status != BME680_OK) {
if (iaqSensor.bme680Status < BME680_OK) {
output = "BME680 error code : " + String(iaqSensor.bme680Status);
for (;;){
Serial.println(output);
delay(500);
} // Halt in case of failure
}
else {
output = "BME680 warning code : " + String(iaqSensor.bme680Status);
Serial.println(output);
}
}
}
// Housekeeping: scheduled update using ticker-lib
void iaqSensor_Housekeeping(){ // get new data
iaqSensor.run();
}
int LUFTDRUCK = 0 ;
int FEUCHTE = 0 ;
// Adafruit Feather 7 Segment https://learn.adafruit.com/adafruit-7-segment-led-featherwings/overview
Adafruit_7segment matrix7Seg = Adafruit_7segment(); // 7Segment Feather
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(2,13,NEO_GRBW + NEO_KHZ800);
void setup(){ // Einmalige Initialisierung
Serial.begin(115200);
//----------------------------------MQTT-Client
mqttclient.setServer("mqtt.unixweb.eu", 1883);
mqttclient.setCallback(mqttcallback);
Wire.begin(); // ---- Initialisiere den I2C-Bus
if (Wire.status() != I2C_OK) Serial.println("Something wrong with I2C");
if (airSensorSCD30.begin() == false) {
Serial.println("The SCD30 did not respond. Please check wiring.");
while(1) {
yield();
delay(1);
}
}
airSensorSCD30.setAutoSelfCalibration(false); // Sensirion no auto calibration
airSensorSCD30.setMeasurementInterval(2); // CO2-Messung alle 2 s
iaqSensor.begin(BME680_I2C_ADDR_PRIMARY, Wire);
String output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix);
Serial.println(output);
iaqSensor.setConfig(bsec_config_iaq);
checkIaqSensorStatus();
bsec_virtual_sensor_t sensorList[10] = {
BSEC_OUTPUT_RAW_TEMPERATURE,
BSEC_OUTPUT_RAW_PRESSURE,
BSEC_OUTPUT_RAW_HUMIDITY,
BSEC_OUTPUT_RAW_GAS,
BSEC_OUTPUT_IAQ,
BSEC_OUTPUT_STATIC_IAQ,
BSEC_OUTPUT_CO2_EQUIVALENT,
BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
};
iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
checkIaqSensorStatus();
iaqSensor_Housekeeping();
Bsec_Ticker.attach_ms(3000, iaqSensor_Housekeeping);
Serial.println();
matrix7Seg.begin(0x70); // ---- Initialisiere 7Segment Matrix
matrix7Seg.clear(); //
matrix7Seg.writeDisplay();
pixels.begin();//-------------- Initialisierung Neopixel
delay(1);
pixels.show();
pixels.setPixelColor(0,0,0,0,0); // alle aus
pixels.setPixelColor(1,0,0,0,0);
pixels.show(); // und anzeigen
//------------ WLAN initialisieren
WiFi.disconnect();
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
delay(100);
Serial.print ("\nWLAN connect to:");
Serial.print("WLAN-24");
WiFi.begin("WLAN-24","111111111");
while (WiFi.status() != WL_CONNECTED) { // Warte bis Verbindung steht
delay(500);
Serial.print(".");
};
Serial.println ("\nconnected, meine IP:"+ WiFi.localIP().toString());
matrixausgabe_text = " Meine IP:" + WiFi.localIP().toString();
myOwnIP = WiFi.localIP();
matrixausgabe_index=0;
Wire.setClock(100000L); // 100 kHz SCD30
Wire.setClockStretchLimit(200000L);// CO2-SCD30
}
void loop() { // Kontinuierliche Wiederholung
CO2 = airSensorSCD30.getCO2() ;
TEMP = iaqSensor.temperature ;
LUFTDRUCK = (iaqSensor.pressure/100.) ;
FEUCHTE = iaqSensor.humidity ;
mqttreconnect();
{
String pay=String(String(CO2));
mqttclient.publish("Octopus/CO2",pay.c_str());
Serial.print("\nmqtt publish: ");
Serial.print(pay);
};
mqttreconnect();
{
String pay=String(String(TEMP));
mqttclient.publish("Octopus/Temp",pay.c_str());
Serial.print("\nmqtt publish: ");
Serial.print(pay);
};
mqttreconnect();
{
String pay=String(String(LUFTDRUCK));
mqttclient.publish("Octopus/Luftdruck",pay.c_str());
Serial.print("\nmqtt publish: ");
Serial.print(pay);
};
mqttreconnect();
{
String pay=String(String(FEUCHTE));
mqttclient.publish("Octopus/Feuchte",pay.c_str());
Serial.print("\nmqtt publish: ");
Serial.print(pay);
};
Serial.print("CO2:"+String(String(CO2)));
Serial.println();
matrix7Seg.print(TEMP);
matrix7Seg.writeDigitNum(0,1,true);
matrix7Seg.writeDisplay();
delay( 2000 );
matrix7Seg.print(LUFTDRUCK);
matrix7Seg.writeDigitNum(0,2,true);
matrix7Seg.writeDisplay();
delay( 2000 );
matrix7Seg.print(FEUCHTE);
matrix7Seg.writeDigitNum(0,3,true);
matrix7Seg.writeDisplay();
delay( 2000 );
matrix7Seg.print(CO2);
matrix7Seg.writeDisplay();
if (( ( CO2 ) < ( 1000 ) ))
{
Serial.print("green");
Serial.println();
pixels.setPixelColor(0,0,30,0,0);
pixels.show();
pixels.setPixelColor(1,0,30,0,0);
pixels.show();
}
else
{
if (( ( CO2 ) < ( 1400 ) ))
{
pixels.setPixelColor(0,30,30,0,0);
pixels.show();
pixels.setPixelColor(1,30,30,0,0);
pixels.show();
Serial.print("yellow");
Serial.println();
}
else
{
pixels.setPixelColor(0,40,0,0,0);
pixels.show();
pixels.setPixelColor(1,40,0,0,0);
pixels.show();
Serial.print("red");
Serial.println();
}
}
delay( 6000 );
} //end loop

View File

@ -23,10 +23,10 @@
* https://www.umwelt-campus.de/forschung/projekte/iot-werkstatt/ideen-zur-corona-krise * https://www.umwelt-campus.de/forschung/projekte/iot-werkstatt/ideen-zur-corona-krise
* WICHTIG Kalibrierung des Sensor https://www.youtube.com/watch?v=od_XsBE6hU4 * WICHTIG Kalibrierung des Sensor https://www.youtube.com/watch?v=od_XsBE6hU4
![Screenshot](https://git.unixweb.net/jhummel/CO2-Ampel-Octopus/raw/branch/master/images/Die-CO2-Ampel.jpg) ![Screenshot](https://git.unixweb.net/unixweb/CO2-Ampel-Octopus/raw/branch/master/images/Die-CO2-Ampel.jpg)
![Screenshot](https://git.unixweb.net/jhummel/CO2-Ampel-Octopus/raw/branch/master/images/SCD30-CO2-Ampel.jpg) ![Screenshot](https://git.unixweb.net/unixweb/CO2-Ampel-Octopus/raw/branch/master/images/SCD30-CO2-Ampel.jpg)
![Screenshot](https://git.unixweb.net/jhummel/CO2-Ampel-Octopus/raw/branch/master/images/CO2-Ampel-mit-NodeRED-Dashboard.png) ![Screenshot](https://git.unixweb.net/unixweb/CO2-Ampel-Octopus/raw/branch/master/images/CO2-Ampel-mit-NodeRED-Dashboard.png)