From: Noah Tomkins Date: Wed, 19 Aug 2026 04:13:52 +0000 (-0400) Subject: IOT JSON for BME280 X-Git-Url: https://git.tnoah.ca/?a=commitdiff_plain;h=60333967e73e9a24c1744da329910a205b946d87;p=garden.git IOT JSON for BME280 --- diff --git a/.gitignore b/.gitignore index 89cc49c..4117624 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch +src/secrets.h \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 6869843..2049f4b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,7 +8,14 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -[env:wemos_d1_mini32] -platform = espressif32 -board = wemos_d1_mini32 +[env:d1_mini_lite] +platform = espressif8266 +board = d1_mini_lite framework = arduino +monitor_speed = 115200 +lib_deps = + bblanchon/ArduinoJson@^7.2.2 + esp32async/ESPAsyncWebServer@^3.12.0 + esp32async/ESPAsyncTCP@^2.0.0 + adafruit/Adafruit BME280 Library@^2.3.0 + adafruit/Adafruit Unified Sensor@^1.1.15 diff --git a/src/garden.c b/src/garden.c deleted file mode 100644 index e20cfc7..0000000 --- a/src/garden.c +++ /dev/null @@ -1,9 +0,0 @@ -void setup() { - // put your setup code here, to run once: - -} - -void loop() { - // put your main code here, to run repeatedly: - -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index cb9fbba..37f2a2e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,88 @@ #include -// put function declarations here: -int myFunction(int, int); +#include +#include +#include + +#include +#include +#include + +#include "AsyncJson.h" +#include "ArduinoJson.h" + +#include "main.h" +#include "secrets.h" + +void connectToWiFi(); +void setupWebServer(); +void setupBME280(); + +WiFiClient wifiClient; +AsyncWebServer server(80); + +Adafruit_BME280 bme; + +void connectToWiFi() { + WiFi.begin(ssid, pass); + + Serial.print("Connecting"); + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + Serial.println(); + + Serial.print("Connected, IP address: "); + Serial.println(WiFi.localIP()); +} + +void setupWebServer() { + server.on("/api/sensors", HTTP_GET, [] (AsyncWebServerRequest *request) { + + AsyncResponseStream *response = request->beginResponseStream("application/json"); + JsonDocument doc; + + doc["temperature"] = bme.readTemperature(); + doc["pressure"] = bme.readPressure() / 1000; + doc["humidity"] = bme.readHumidity(); + doc["altitude"] = bme.readAltitude(SEALEVELPRESSURE_HPA); + + serializeJson(doc, *response); + request->send(response); + + }); + + server.begin(); +} + +void setupBME280() { + bool status; + + status = bme.begin(0x76); + for (int i = 10; i < 10; i++) { + if (status) continue; + delay(100); + Serial.print("Could not connect to BME, retrying"); + } + + Serial.print("Connected to BME280!"); +} void setup() { - // put your setup code here, to run once: - int result = myFunction(2, 3); + Serial.begin(115200); + Serial.println(); + + connectToWiFi(); + setupWebServer(); + setupBME280(); + + pinMode(SOAKER, OUTPUT); } void loop() { - // put your main code here, to run repeatedly: + + delay(1000); } -// put function definitions here: -int myFunction(int x, int y) { - return x + y; -} \ No newline at end of file diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..84b49c5 --- /dev/null +++ b/src/main.h @@ -0,0 +1,8 @@ +// Soaker Hose Solenoid +#define SOAKER D8 + +// BME280 I2C +#define BME_SCL D1 +#define BME_SDA D2 + +#define SEALEVELPRESSURE_HPA (1013.25) \ No newline at end of file diff --git a/src/secrets.h.example b/src/secrets.h.example new file mode 100644 index 0000000..90a1225 --- /dev/null +++ b/src/secrets.h.example @@ -0,0 +1,3 @@ +// WiFi settings +const char *ssid = "ssid"; +const char *pass = "password"; \ No newline at end of file