commit 84f9ee4c9039b8d1b63039d8d2d6285649228f4b Author: hajamieli Date: Thu Dec 19 00:29:57 2024 +0200 funky stuff diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..92001b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +secrets.h \ No newline at end of file diff --git a/proto.ino b/proto.ino new file mode 100644 index 0000000..7bd6259 --- /dev/null +++ b/proto.ino @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include "ScioSense_ENS160.h" +#include "secrets.h" + +#define SCREEN_WIDTH 128 // OLED display width, in pixels +#define SCREEN_HEIGHT 32 // OLED display height, in pixels +#define OLED_RESET -1 +#define SCREEN_ADDRESS 0x3C + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); +ScioSense_ENS160 ens160(ENS160_I2CADDR_1); +DHT11 dht11(2); +Adafruit_AHTX0 aht; + +WiFiClient net; +MQTTClient client; + +int ATHtempC; +int ATHhumidity; + +unsigned long timestamp = 0; +unsigned long uploadts = 0; + +void connect() { + Serial.print("checking wifi..."); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(1000); + } + + Serial.print("\nconnecting..."); + while (!client.connect(HOSTNAME, MQTT_USER, MQTT_PASS)) { + Serial.print("."); + delay(1000); + } + + Serial.println("\nconnected!"); + //client.subscribe("/hello"); +} + +void sensors(){ + Serial.print("ENS160..."); + dht11.setDelay(1000); + ens160.begin(); + delay(1000); + + Serial.println(ens160.available() ? "done." : "failed!"); + if (ens160.available()) { + Serial.print("\tRev: "); Serial.print(ens160.getMajorRev()); + Serial.print("."); Serial.print(ens160.getMinorRev()); + Serial.print("."); Serial.println(ens160.getBuild()); + + Serial.print("\tStandard mode "); + Serial.println(ens160.setMode(ENS160_OPMODE_STD) ? "done." : "failed!" ); + } + + if (! aht.begin()) { + Serial.println("Could not find AHT? Check wiring"); + while (1) delay(10); + } + Serial.println("AHT10 or AHT20 found"); + + if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { // Address 0x3D for 128x64 + Serial.println("SSD1306 allocation failed"); + for(;;); + } +} + +void writeToScreen(String &text) { + display.clearDisplay(); + display.setCursor(0, 3); + display.println(text); + display.display(); +} + +/* +void messageReceived(String &topic, String &payload) { + Serial.println("incoming: " + topic + " - " + payload); +} +*/ + +void setup() { + Serial.begin(115200); + while (!Serial) {} + + WiFi.setHostname(HOSTNAME); + WiFi.begin(WIFI_SSID, WIFI_PASS); + client.begin(MQTT_URL, MQTT_PORT, net); + //client.onMessage(messageReceived); + + connect(); + sensors(); + + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(WHITE); + display.display(); +} + +void loop() { + client.loop(); + delay(1000); + if (!client.connected()) { + connect(); + } + + if (millis() - timestamp > 2500) { + timestamp = millis(); + sensors_event_t humidity1, temp; + aht.getEvent(&humidity1, &temp); + + ATHtempC = (temp.temperature); + ATHhumidity = (humidity1.relative_humidity); + + int temperature = 0; + int humidity = 0; + int result = dht11.readTemperatureHumidity(temperature, humidity); + + if (result == 0 && ens160.available()) { + ens160.set_envdata(temperature, ATHhumidity); + ens160.measure(true); + ens160.measureRaw(true); + + Serial.print("Temperature: ");Serial.print(temperature);Serial.print("°C\tHumidity: ");Serial.print(ATHhumidity);Serial.print("% "); + Serial.print("AQI: ");Serial.print(ens160.getAQI());Serial.print(" "); + Serial.print("TVOC: ");Serial.print(ens160.getTVOC());Serial.print("ppb "); + Serial.print("eCO2: ");Serial.print(ens160.geteCO2());Serial.println("ppm "); + + writeToScreen("Temp: " + String(temperature) + "C H: " + String(ATHhumidity) + "%\nAQI: " + ens160.getAQI()); + if (millis() - uploadts > 30000) { + client.publish("envi/temparature", String(temperature)); + client.publish("envi/humidity", String(ATHhumidity)); + client.publish("envi/aqi", String(ens160.getAQI())); + client.publish("envi/tvoc", String(ens160.getTVOC())); + client.publish("envi/eco", String(ens160.geteCO2())); + uploadts = millis(); + } + } + + if(result != 0 || !ens160.available()) { + client.publish("envi/error", "sensor error"); + Serial.println(DHT11::getErrorString(result)); + } + } +}