Skip to content

Commit 855bcd7

Browse files
committed
first commit
0 parents  commit 855bcd7

File tree

7 files changed

+219
-0
lines changed

7 files changed

+219
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch

.vscode/extensions.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
],
7+
"unwantedRecommendations": [
8+
"ms-vscode.cpptools-extension-pack"
9+
]
10+
}

include/README

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

lib/README

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

platformio.ini

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:esp32dev]
12+
platform = espressif32
13+
board = esp32dev
14+
framework = arduino
15+
lib_deps =
16+
wifwaf/MH-Z19@^1.5.4
17+
bblanchon/ArduinoJson@^7.0.4
18+
monitor_speed = 9600

src/main.cpp

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <Arduino.h>
2+
#include "MHZ19.h"
3+
#include <HardwareSerial.h>
4+
#include <WiFi.h>
5+
#include <ArduinoJson.h>
6+
#include <HTTPClient.h>
7+
#include <esp_wifi.h>
8+
#include <vector>
9+
10+
#define BAUDRATE 9600 // Device to MH-Z19 Serial baudrate (should not be changed)
11+
const char* ssid = "Wifi SSID"; // WiFi SSID
12+
const char* password = "WIFI PASSWORD"; // WiFi Password
13+
const char* api_url = "https://backend.thinger.io/v3/users/..."; // Thinger.io host
14+
const String token = "Bearer ABCABCABC";
15+
16+
MHZ19 myMHZ19; // Constructor for library
17+
HardwareSerial mySerial(2);
18+
WiFiClientSecure client;
19+
20+
std::vector<String> offlineData;
21+
22+
unsigned long getDataTimer = 0;
23+
24+
void setup()
25+
{
26+
Serial.begin(9600); // Device to serial monitor feedback
27+
28+
mySerial.begin(BAUDRATE);
29+
myMHZ19.begin(mySerial); // *Serial(Stream) refence must be passed to library begin().
30+
myMHZ19.autoCalibration(); // Turn auto calibration ON (OFF autoCalibration(false))
31+
32+
WiFi.mode(WIFI_STA);
33+
WiFi.begin(ssid, password);
34+
while (WiFi.status() != WL_CONNECTED) {
35+
Serial.print('.');
36+
delay(1000);
37+
}
38+
Serial.println(WiFi.localIP());
39+
client.setInsecure();
40+
}
41+
42+
void sendData(String data) {
43+
HTTPClient http;
44+
http.begin(client, api_url);
45+
http.addHeader("Content-Type", "application/json;charset=UTF-8");
46+
http.addHeader("Authorization", token);
47+
int httpCode = http.POST(data);
48+
if (httpCode != 200 && httpCode > 0 ) {
49+
String payload = http.getString();
50+
Serial.println(httpCode);
51+
Serial.println(payload);
52+
}
53+
else {
54+
Serial.println(http.errorToString(httpCode).c_str());
55+
}
56+
http.end();
57+
}
58+
59+
void loop()
60+
{
61+
if (millis() - getDataTimer >= 61000)
62+
{
63+
JsonDocument doc;
64+
String data;
65+
66+
doc["co2"] = myMHZ19.getCO2();
67+
doc["temperature"] = (int)myMHZ19.getTemperature();
68+
doc["startup_time"] = millis() / 1000;
69+
serializeJson(doc, data);
70+
if (WiFi.status() == WL_CONNECTED)
71+
{
72+
// if (offlineData.size() > 0) {
73+
// for (auto& d : offlineData) {
74+
// sleep(0.1);
75+
// sendData(d);
76+
// }
77+
// offlineData.clear();
78+
// }
79+
// Send the current data
80+
sendData(data);
81+
} else {
82+
// If WiFi is not connected, store the data for later
83+
// if (offlineData.size() > 800) {
84+
// offlineData.erase(offlineData.begin());
85+
// }
86+
offlineData.push_back(data);
87+
WiFi.begin(ssid, password);
88+
}
89+
}
90+
}

test/README

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
This directory is intended for PlatformIO Test Runner and project tests.
3+
4+
Unit Testing is a software testing method by which individual units of
5+
source code, sets of one or more MCU program modules together with associated
6+
control data, usage procedures, and operating procedures, are tested to
7+
determine whether they are fit for use. Unit testing finds problems early
8+
in the development cycle.
9+
10+
More information about PlatformIO Unit Testing:
11+
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html

0 commit comments

Comments
 (0)