From 7cca5486ef474f817cbe792d9d4b9f26c3c5571f Mon Sep 17 00:00:00 2001
From: Moritz Perschke <moritz.perschke@uibk.ac.at>
Date: Mon, 25 Jul 2022 16:45:16 +0200
Subject: [PATCH] airsensor readout added

---
 .gitignore                                 |  2 +-
 client/client/lib/I2C/I2C.cpp              | 26 ++++++++++++
 client/client/lib/I2C/I2C.hpp              |  8 ++++
 client/client/lib/I2C/pinout.hpp           |  2 +
 client/client/lib/espnow/src/espnow.hpp    | 13 ++++++
 client/client/platformio.ini               |  3 +-
 client/client/src/main.cpp                 | 18 ++++++++-
 code-snippets/espnow/espNowTest/lib/README | 46 ++++++++++++++++++++++
 8 files changed, 115 insertions(+), 3 deletions(-)
 create mode 100644 client/client/lib/I2C/I2C.cpp
 create mode 100644 client/client/lib/I2C/I2C.hpp
 create mode 100644 client/client/lib/I2C/pinout.hpp
 create mode 100644 client/client/lib/espnow/src/espnow.hpp
 create mode 100644 code-snippets/espnow/espNowTest/lib/README

diff --git a/.gitignore b/.gitignore
index 88fc45a..23b4089 100644
--- a/.gitignore
+++ b/.gitignore
@@ -14,7 +14,7 @@ dist/
 downloads/
 eggs/
 .eggs/
-lib/
+#lib/  // commented this out as to not always force add /lib in platformio project
 lib64/
 parts/
 sdist/
diff --git a/client/client/lib/I2C/I2C.cpp b/client/client/lib/I2C/I2C.cpp
new file mode 100644
index 0000000..e293d48
--- /dev/null
+++ b/client/client/lib/I2C/I2C.cpp
@@ -0,0 +1,26 @@
+#include <Wire.h>
+#include <SparkFun_SCD30_Arduino_Library.h>
+#include "pinout.hpp"
+#include "I2C.hpp"
+
+SCD30 airSensor;
+
+int i2c_setup(){
+    Wire.begin(I2C_SDA, I2C_SCL);
+    if(!airSensor.begin()){
+        return 1;
+    }
+    return 0;
+}
+
+i2c_readout get_i2c_data(){
+    if(airSensor.dataAvailable()){
+        i2c_readout result;
+        result.co2 = airSensor.getCO2();
+        result.temp = airSensor.getTemperature();
+        result.humidity = airSensor.getHumidity();
+        return result;
+    }
+    i2c_readout empty; // bad, remove this
+    return empty;
+}
\ No newline at end of file
diff --git a/client/client/lib/I2C/I2C.hpp b/client/client/lib/I2C/I2C.hpp
new file mode 100644
index 0000000..1c767d1
--- /dev/null
+++ b/client/client/lib/I2C/I2C.hpp
@@ -0,0 +1,8 @@
+typedef struct i2c_readout{
+    float co2;
+    float temp;
+    float humidity;
+} i2c_readout;
+
+int i2c_setup();
+i2c_readout get_i2c_data();
diff --git a/client/client/lib/I2C/pinout.hpp b/client/client/lib/I2C/pinout.hpp
new file mode 100644
index 0000000..7385cac
--- /dev/null
+++ b/client/client/lib/I2C/pinout.hpp
@@ -0,0 +1,2 @@
+#define I2C_SDA 18
+#define I2C_SCL 19
diff --git a/client/client/lib/espnow/src/espnow.hpp b/client/client/lib/espnow/src/espnow.hpp
new file mode 100644
index 0000000..a32e7c9
--- /dev/null
+++ b/client/client/lib/espnow/src/espnow.hpp
@@ -0,0 +1,13 @@
+#include <string>
+#define NUM_SENSORS 10
+
+// I originally wanted to define the mac addresses here, but i got a "multiple definition" error?
+typedef struct dataMessage{
+    std::string identifiers[NUM_SENSORS];
+    float values[NUM_SENSORS];
+}dataMessage;
+
+int send_data(dataMessage data);
+
+int espnow_setup();
+
diff --git a/client/client/platformio.ini b/client/client/platformio.ini
index ee50833..aeb9e53 100644
--- a/client/client/platformio.ini
+++ b/client/client/platformio.ini
@@ -12,4 +12,5 @@
 platform = espressif32
 board = esp32-c3-devkitm-1
 framework = arduino
-monitor_speed = 115200
\ No newline at end of file
+monitor_speed = 115200
+lib_deps = sparkfun/SparkFun SCD30 Arduino Library@^1.0.18
diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp
index 91da067..e0d73da 100644
--- a/client/client/src/main.cpp
+++ b/client/client/src/main.cpp
@@ -1,5 +1,6 @@
 #include <Arduino.h>
 #include "espnow.hpp"
+#include "I2C.hpp"
 
 void setup() {
  
@@ -7,9 +8,24 @@ void setup() {
   Serial.begin(115200);
   while(!Serial);
 
-  int result = espnow_setup();
+  espnow_setup();
+  i2c_setup();
 }
 
 void loop() {
   // put your main code here, to run repeatedly:
+  i2c_readout data = get_i2c_data();
+  dataMessage message;
+  message.identifiers[0] = "co2";
+  message.identifiers[1] = "temp";
+  message.identifiers[2] = "humid";
+
+  message.values[0] = data.co2;
+  message.values[1] = data.co2;
+  message.values[2] = data.humidity;
+
+  send_data(message);
+  Serial.println("Message sent");
+
+  delay(3000);
 }
\ No newline at end of file
diff --git a/code-snippets/espnow/espNowTest/lib/README b/code-snippets/espnow/espNowTest/lib/README
new file mode 100644
index 0000000..6debab1
--- /dev/null
+++ b/code-snippets/espnow/espNowTest/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
-- 
GitLab