Merge pull request #1 from Eigenbaukombinat/neopixel

accidentially pushed to a wrong branch...
This commit is contained in:
Daniel Havlik 2020-10-31 16:07:31 +01:00 committed by GitHub
commit d41815aeb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 229 additions and 100 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

7
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}

View File

@ -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

View File

@ -1,72 +0,0 @@
#include <Arduino.h>
#include "MHZ19.h"
#include "SSD1306Wire.h"
#define RX_PIN 16
#define TX_PIN 17
#define BAUDRATE 9600
MHZ19 myMHZ19;
HardwareSerial mySerial(1);
SSD1306Wire display(0x3c, 21, 22);
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;
}
}
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();
}
}

39
include/README Normal file
View File

@ -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

46
lib/README Normal file
View File

@ -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 <Foo.h>
#include <Bar.h>
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

19
platformio.ini Normal file
View File

@ -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

101
src/ebk_co2ampel.cpp Normal file
View File

@ -0,0 +1,101 @@
#include <Arduino.h>
#include "MHZ19.h"
#include "SSD1306Wire.h"
#include <Adafruit_NeoPixel.h>
// 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();
}
}

11
test/README Normal file
View File

@ -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