diff options
author | Torben Egmose <torben.egmose@gmail.com> | 2021-02-07 22:03:36 +0100 |
---|---|---|
committer | Torben Egmose <torben.egmose@gmail.com> | 2021-02-07 22:03:36 +0100 |
commit | 44b1d4f73eee8a3a6f1423223eec9693c82ff883 (patch) | |
tree | 213918ba193518c8243b3320e13376bafe5d3d90 |
init commit
-rw-r--r-- | .gitignore | 5 | ||||
-rw-r--r-- | .vscode/extensions.json | 7 | ||||
-rw-r--r-- | include/README | 39 | ||||
-rw-r--r-- | lib/README | 46 | ||||
-rw-r--r-- | platformio.ini | 30 | ||||
-rw-r--r-- | src/main.cpp | 359 | ||||
-rw-r--r-- | test/README | 11 |
7 files changed, 497 insertions, 0 deletions
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/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 <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 diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..c4f0d94 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,30 @@ +; 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:esp01_1m] +platform = espressif8266 +board = esp01_1m +framework = arduino +board_build.flash_mode = dout + +lib_deps = + LiquidCrystal_I2C + WifWaf/MH-Z19 + adafruit/Adafruit Unified Sensor + adafruit/Adafruit AHTX0 + tzapu/WiFiManager + PubSubClient + +;upload_port = /dev/ttyUSB0 + +upload_protocol = espota +upload_port = 172.19.1.209 +upload_flags= -P 36286 + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..04b765b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,359 @@ +#include <Arduino.h> + +#include <Wire.h> +#include <ESP8266WiFi.h> +#include <LiquidCrystal_I2C.h> +#include <MHZ19.h> +#include <Adafruit_Sensor.h> +#include <Adafruit_AHTX0.h> + +#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager +WiFiManager wm; + +#include <PubSubClient.h> +const char* debug = "debug"; +const char* server = "172.19.1.10"; +const char* s_will = "died"; +const char* s_ready = "ready"; +const char* s_sens = "zero MHZ"; +WiFiClient espClient; +PubSubClient client(espClient); +char co[28]; +char tmp[28]; +char hum[28]; +char sta[21]; +uint8_t MAC_array[6]; +char MAC_char[21]; +char buffer[60]; +bool pubsub = false; + +MHZ19 myMHZ19; +Adafruit_AHTX0 aht; // x38 +LiquidCrystal_I2C lcd(0x27, 20, 4); + +#define DEGREE_SYMBOL 0xDF //degree symbol from LCD ROM +byte user_value1[8] ={ + B11111, + B11111, + B00000, + B00000, + B00000, + B00000, + B00000, + B00000 +}; +byte user_value2[8] ={ + B00000, + B00000, + B00000, + B00000, + B00000, + B00000, + B11111, + B11111 +}; +byte user_value3[8] ={ + B11111, + B11111, + B00000, + B00000, + B00000, + B00000, + B11111, + B11111 +}; +byte user_value4[8] ={ + B11111, + B11111, + B11111, + B11111, + B11111, + B11111, + B11111, + B11111 +}; +byte user_value5[8] ={ + B00000, + B00000, + B00011, + B00011, + B00011, + B00011, + B00000, + B00000 +}; +byte user_value6[8] ={ + B00000, + B00000, + B11000, + B11000, + B11000, + B11000, + B00000, + B00000 +}; + +byte plus_minus_icon[8] = {0x00, 0x04, 0x0E, 0x04, 0x00, 0x0E, 0x00, 0x00}; + +const uint8_t numbers[][6] = { + { + 3, 0, 3, + 3, 1, 3 + }, + { + 0, 3, 160, + 1, 3, 1 + }, + { + 2, 2, 3, + 3, 1, 1 + }, + { + 2, 2, 3, + 1, 1, 3 + }, + { + 3, 1, 3, + 160, 160, 3 + }, + { + 3, 2, 2, + 1, 1, 3 + }, + { + 3, 2, 2, + 3, 1, 3 + }, + { + 0, 0, 3, + 160, 160, 3 + }, + { + 3, 2, 3, + 3, 1, 3 + }, + { + 3, 2, 3, + 1, 1, 3 + }, + { + 160, 160, 160, + 160, 160, 160 + } +}; + +void myprint(int x, const uint8_t num[6]) { + yield(); + lcd.setCursor(x,0); + yield(); + lcd.write(num[0]); yield(); lcd.write(num[1]); yield(); lcd.write(num[2]); + yield(); + lcd.setCursor(x,1); + yield(); + lcd.write(num[3]); yield(); lcd.write(num[4]); yield(); lcd.write(num[5]); +} + +void numprint(int num) { + if(num >= 10000) { + myprint(0, numbers[num / 10000 % 10]); + } else { + myprint(0, numbers[10]); + } + + if(num >= 1000) { + myprint(4, numbers[num / 1000 % 10]); + } else { + myprint(4, numbers[10]); + } + + if(num >= 100) { + myprint(8, numbers[num / 100 % 10]); + } else { + myprint(8, numbers[10]); + } + + if(num >= 10) { + myprint(12, numbers[num / 10 % 10]); + } else { + myprint(12, numbers[10]); + } + + if(num >= 0) { + myprint(16, numbers[num / 1 % 10]); + } else { + myprint(16, numbers[10]); + } +} + +void callback(char* topic, byte* payload, unsigned int length) { + if(topic[14] == 's') { + if((char)payload[0] == 'r' && (char)payload[1] == 'e' && (char)payload[2] == 's' && (char)payload[3] == 'e' && (char)payload[4] == 't') + { + lcd.setCursor(0,1); + lcd.print(F("reset")); + WiFi.forceSleepBegin(); + ESP.wdtFeed(); + ESP.restart(); + while(true) + { + yield(); + } + } + + if((char)payload[0] == 'c' && (char)payload[1] == 'a' && (char)payload[2] == 'l' && (char)payload[3] == 'i' && (char)payload[4] == 'b') + { + myMHZ19.calibrate(); + client.publish(sta,s_sens); + } + } +} + +unsigned long tick = 0; +int sec = 20; +bool sensor = false; + +void setup() { + + Serial.begin(9600); + WiFi.mode(WIFI_STA); + + unsigned int i; + WiFi.macAddress(MAC_array); + for (i = 0; i < sizeof(MAC_array); ++i){ + sprintf(MAC_char,"%s%02x",MAC_char,MAC_array[i]); + } + sprintf(co,"/%s/co2",MAC_char); + sprintf(sta,"/%s/status",MAC_char); + sprintf(tmp,"/%s/temperature",MAC_char); + sprintf(hum,"/%s/humidity",MAC_char); + client.setServer(server, 1883); + client.setCallback(callback); + wm.setConfigPortalBlocking(false); + wm.setDebugOutput(false); + if(wm.autoConnect("CO2sensor")){ + pubsub = true; + client.connect(MAC_char, sta, 0, false, s_will); + client.publish(debug,MAC_char); + client.subscribe(sta,1); + + client.publish(sta,__FILE__); + IPAddress ip = WiFi.localIP(); + sprintf(buffer,"%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); + client.publish(sta,buffer); + client.publish(sta,s_ready); + } + + Wire.begin(0,2); // 00-sda,02-sdl + + lcd.init(); + lcd.noBacklight(); + lcd.clear(); + lcd.backlight(); + + lcd.print(F("CO2 monitor starting")); + + myMHZ19.begin(Serial); + myMHZ19.setRange(2000); + myMHZ19.autoCalibration(false); + + lcd.setCursor(0,1); + lcd.print(F("CO2 sensor started")); + + sensor = aht.begin(); + + lcd.setCursor(0,2); + lcd.print(F("aht sensor started")); + + lcd.createChar(0, user_value1); + lcd.createChar(1, user_value2); + lcd.createChar(2, user_value3); + lcd.createChar(3, user_value4); + lcd.createChar(4, user_value5); + lcd.createChar(5, user_value6); + lcd.createChar(6, plus_minus_icon); + + lcd.setCursor(0,3); + lcd.print(F("lcd font loaded")); + + ESP.wdtEnable(WDTO_8S); + tick = millis(); +} + + +bool clear = true; +int value; +sensors_event_t humidity, temp; + +void loop() { + + ESP.wdtFeed(); + + wm.process(); + + pubsub = WiFi.isConnected(); + + if(pubsub) { + ESP.wdtFeed(); + yield(); + + if(client.connected()) { + client.loop(); + } else { + client.connect(MAC_char, sta, 0, false, s_will); + client.subscribe(sta,1); + client.publish(sta,__FILE__); + IPAddress ip = WiFi.localIP(); + sprintf(buffer,"%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); + client.publish(sta,buffer); + } + } + + if(millis() - tick >= 1000) { + tick = millis(); + + sec--; + if(sec<0) sec = 5; + + if(sec == 5) { + if(clear) { + lcd.clear(); + clear = false; + } + + value = myMHZ19.getCO2(); + numprint(value); + if(pubsub) { + sprintf(buffer,"%d", (int) value); + client.publish(co,buffer); + } + + + if(sensor) { + aht.getEvent(&humidity, &temp); + if(pubsub) { + dtostrf(temp.temperature,4,2,buffer); + client.publish(tmp,buffer); + + sprintf(buffer,"%d", (int) humidity.relative_humidity); + client.publish(hum,buffer); + } + + lcd.setCursor(0,2); + lcd.print(temp.temperature); + lcd.print(F(" ")); + lcd.write(6); + lcd.print(F("0.3")); + lcd.write(DEGREE_SYMBOL); + lcd.print(F("C ")); + + lcd.setCursor(0,3); + lcd.print((int) humidity.relative_humidity); + lcd.print(F(" ")); + lcd.write(6); + lcd.print(F("2% ")); + } + } + } + + delay(100); +} 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 |