From 66877a54b1fe1b8e8e53f57ff1307edc745d7e6f Mon Sep 17 00:00:00 2001 From: nilo Date: Sat, 31 Oct 2020 12:39:34 +0100 Subject: [PATCH 1/2] add led --- ebk_co2ampel/ebk_co2ampel.ino | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ebk_co2ampel/ebk_co2ampel.ino b/ebk_co2ampel/ebk_co2ampel.ino index a286c23..817a192 100644 --- a/ebk_co2ampel/ebk_co2ampel.ino +++ b/ebk_co2ampel/ebk_co2ampel.ino @@ -1,6 +1,9 @@ #include #include "MHZ19.h" #include "SSD1306Wire.h" + +#include + #define RX_PIN 16 #define TX_PIN 17 #define BAUDRATE 9600 @@ -10,6 +13,11 @@ MHZ19 myMHZ19; HardwareSerial mySerial(1); SSD1306Wire display(0x3c, 21, 22); +// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals. +// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, 4, NEO_GRB + NEO_KHZ800); + + unsigned long getDataTimer = 0; int lastvals[120]; int dheight; @@ -29,7 +37,9 @@ void setup() for (int x; x <= 119; x = x + 1) { lastvals[x] = -1; } - +pixels.begin(); // This initializes the NeoPixel library. +pixels.setPixelColor(0, pixels.Color(255,0,0)); // Moderately bright green color. +pixels.show(); } From 67f789336d1b2705dfab4d797a1471a2b0277d63 Mon Sep 17 00:00:00 2001 From: nilo Date: Sat, 31 Oct 2020 16:06:36 +0100 Subject: [PATCH 2/2] move to platformio ide --- .gitignore | 5 ++ .vscode/extensions.json | 7 +++ README.md | 29 +--------- ebk_co2ampel/ebk_co2ampel.ino | 82 --------------------------- include/README | 39 +++++++++++++ lib/README | 46 ++++++++++++++++ platformio.ini | 19 +++++++ src/ebk_co2ampel.cpp | 101 ++++++++++++++++++++++++++++++++++ test/README | 11 ++++ 9 files changed, 229 insertions(+), 110 deletions(-) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json delete mode 100644 ebk_co2ampel/ebk_co2ampel.ino create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/ebk_co2ampel.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..0f0d740 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/README.md b/README.md index 1f5743b..a6e0ede 100644 --- a/README.md +++ b/README.md @@ -40,34 +40,7 @@ Eine RGB-LED zeigt rot, gelb oder grün, je nach Messwert. ## Flashen -Wir benutzen bisher die Arduino IDE. - -### ESP32 Boarddefinitionen - -Falls noch nicht geschehen, müssen die Boarddefinitionen für den ESP32 installiert werden. Hierzu im Menü: Datei -> Voreinstellungen. Im Fenster dann bei "Zusätzliche Boardverwalter-URLs" diese URL eintragen: - -``` -https://dl.espressif.com/dl/package_esp32_index.json -``` - -Sollte dort schon etwas anderes drin stehen, mit einem Komma getrennt dazuschreiben. - -Danach im Menü: Werkzeuge -> Board -> Boardverwalter nach "ESP32" suchen, und installieren. - -### Libraries - -Die benötigten Libraries installieren wir über die Bibliotheksverwaltung der Arduino IDE (Menü: Sketch -> Bibliothek einbinden -> Bibliotheken verwalten…), und zwar: - -* [MH-Z19](https://github.com/crisap94/MHZ19) für das Auslesen des Sensors -* [ESP8266 and ESP32 OLED driver for SSD1306 displays](https://github.com/ThingPulse/esp8266-oled-ssd1306) für die Ansteuerung des Displays - -### Flashen - -* Als Board muss "ESP32 Dev Module" ausgewählt sein. - -* Mit Strg+U kompilieren und auf den ESP laden. - - +Wir benutzen die PlatformIO IDE. Dort kann das Projekt geöffnet und mit Klick auf "Upload" compiliert und auf den ESP32 geladen werden. ## Wiring diff --git a/ebk_co2ampel/ebk_co2ampel.ino b/ebk_co2ampel/ebk_co2ampel.ino deleted file mode 100644 index 817a192..0000000 --- a/ebk_co2ampel/ebk_co2ampel.ino +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include "MHZ19.h" -#include "SSD1306Wire.h" - -#include - -#define RX_PIN 16 -#define TX_PIN 17 -#define BAUDRATE 9600 - -MHZ19 myMHZ19; - -HardwareSerial mySerial(1); -SSD1306Wire display(0x3c, 21, 22); - -// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals. -// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest -Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, 4, NEO_GRB + NEO_KHZ800); - - -unsigned long getDataTimer = 0; -int lastvals[120]; -int dheight; - -void setup() -{ - Serial.begin(9600); - mySerial.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN); - myMHZ19.begin(mySerial); - display.init(); - display.setContrast(255); - delay(1000); - display.clear(); - dheight = display.getHeight(); - - myMHZ19.autoCalibration(); - for (int x; x <= 119; x = x + 1) { - lastvals[x] = -1; - } -pixels.begin(); // This initializes the NeoPixel library. -pixels.setPixelColor(0, pixels.Color(255,0,0)); // Moderately bright green color. -pixels.show(); -} - - -int calc_vpos_for_co2(int co2val, int display_height) { - return display_height - int((float(display_height) / 3000) * co2val); -} - - - -void loop() -{ - if (millis() - getDataTimer >= 5000) - { - int CO2; - CO2 = myMHZ19.getCO2(); - for (int x = 1; x <= 119; x = x + 1) { - lastvals[x - 1] = lastvals[x]; - } - lastvals[119] = CO2; - display.clear(); - for (int h = 1; h < 120; h = h + 1) { - int curval = lastvals[h]; - if (curval > 0) { - int vpos = calc_vpos_for_co2(lastvals[h], dheight); - int vpos_last = calc_vpos_for_co2(lastvals[h - 1], dheight); - display.drawLine(h - 1, vpos_last, h, vpos); - - } - } - - - display.setLogBuffer(5, 30); - display.println(CO2); - display.drawLogBuffer(0, 0); - display.display(); - Serial.print("CO2 (ppm): "); - Serial.println(CO2); - getDataTimer = millis(); - } -} diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..4964913 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino +upload_port = /dev/ttyUSB0 +lib_deps = + adafruit/Adafruit NeoPixel@^1.6.0 + wifwaf/MH-Z19@^1.5.2 + squix78/ESP8266_SSD1306@^4.1.0 diff --git a/src/ebk_co2ampel.cpp b/src/ebk_co2ampel.cpp new file mode 100644 index 0000000..6e18ef8 --- /dev/null +++ b/src/ebk_co2ampel.cpp @@ -0,0 +1,101 @@ +#include +#include "MHZ19.h" +#include "SSD1306Wire.h" +#include + +// Maximum CO² levels for green and yellow, everything above is considered red. +#define GREEN_CO2 800 +#define YELLOW_CO2 1500 + +// Measurement interval in miliseconds +#define INTERVAL 10000 + +// Pins for MH-Z19 +#define RX_PIN 16 +#define TX_PIN 17 + +// Pins for SD1306 +#define SDA_PIN 21 +#define SCL_PIN 22 + +// Pin for LED +#define LED_PIN 4 + +MHZ19 myMHZ19; +HardwareSerial mySerial(1); +SSD1306Wire display(0x3c, SDA_PIN, SCL_PIN); +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, LED_PIN, NEO_RGB + NEO_KHZ400); + +unsigned long getDataTimer = 0; +int lastvals[120]; +int dheight; + +void setup() { + Serial.begin(9600); + mySerial.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); + myMHZ19.begin(mySerial); + display.init(); + display.setContrast(255); + delay(1000); + display.clear(); + dheight = display.getHeight(); + myMHZ19.autoCalibration(); + // Fill array of last measurements with -1 + for (int x = 0; x <= 119; x = x + 1) { + lastvals[x] = -1; + } + pixels.begin(); + pixels.setPixelColor(0, 30,0,0); + pixels.show(); +} + +int calc_vpos_for_co2(int co2val, int display_height) { + return display_height - int((float(display_height) / 3000) * co2val); +} + +void set_led_color(int co2) { + if (co2 < GREEN_CO2) { + // Green + pixels.setPixelColor(0, 30,0,0); + } else if (co2 < YELLOW_CO2) { + // Yellow + pixels.setPixelColor(0, 40,40, 0); + } else { + // Red + pixels.setPixelColor(0, 0,90,0); + } + pixels.show(); +} + +void loop() { + if (millis() - getDataTimer >= INTERVAL) { + // Get new CO² value. + int CO2 = myMHZ19.getCO2(); + // Shift entries in array back one position. + for (int x = 1; x <= 119; x = x + 1) { + lastvals[x - 1] = lastvals[x]; + } + // Add new measurement at the end. + lastvals[119] = CO2; + // Clear display and redraw whole graph. + display.clear(); + for (int h = 1; h < 120; h = h + 1) { + int curval = lastvals[h]; + if (curval > 0) { + int vpos = calc_vpos_for_co2(lastvals[h], dheight); + int vpos_last = calc_vpos_for_co2(lastvals[h - 1], dheight); + display.drawLine(h - 1, vpos_last, h, vpos); + } + } + // Set LED color and print value on display + set_led_color(CO2); + display.setLogBuffer(1, 30); + display.println(CO2); + display.drawLogBuffer(0, 0); + display.display(); + // Debug output + Serial.print("CO2 (ppm): "); + Serial.println(CO2); + getDataTimer = millis(); + } +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html