Blogs

My First Post
Sat Feb 04 2023

After I have upgraded Astro to use the latest Astro version 2.0.0. a

Read more
My Second Post
Thu Aug 03 2023

Testing out code

int fun() {
    cout << "hello";
}
Read more
Monitor Filament Moisture
Mon Apr 17 2023

Explanation about the code

// Basic Controller for wifi, mdns and ota
void setup() {
    Serial.begin(115200);
    dht.begin();
    // Wire.begin();
    // light_meter.begin(light_meter.CONTINUOUS_HIGH_RES_MODE_2);

    // begin_webserver();

#ifdef DEBUG
    controller.enable_wifi_station(SSID, PASSWORD);
#else
    controller.enable_wifi_ap(SSID, PASSWORD);
#endif

    if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
        Serial.println(F("SSD1306 allocation failed"));
        for (;;)
            ;
    }

    display.display();
    delay(2000);
}

First, we will have to initialize a Serial IO with baudarate of 115200. Besides that, we have to initialize our DHT. To allow our device to communicate with any other client. We have to first initialize our our as a wifi station that will connect to our home wifi by calling controller.enable_wifi_station(SSID, PASSWORD) function with SSID and PASSWORD argument. After that, we will need to initialize our OLED display with display.begin(SSD1306_SWITCHCAPVCC, 0x3C). Finally we will clear our display by calling display.display().

Loop Function

void loop() {
    delay(1000);
    sensors_event_t event;

    dht.temperature().getEvent(&event);
    float temperature = event.temperature;
    dht.humidity().getEvent(&event);
    float humidity = event.relative_humidity;
    // printf("%.3f\n", temperature);

    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 0);
    display.printf("Temperature: %.2f°C", temperature);
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 10);
    display.printf("Humidity: %.2f%", humidity);
    display.display();
    Serial.println(F("STARTED"));

    controller.handler_ota();
    // server.handleClient();
}

We delay our looping by at least 1000ms. After that, we read the temperature and humidity then store them in float temperature and float humidity. We will then write out our values in OLED.

Constant header

#include <Arduino.h>
// #include <BH1750.h>
#include <DHT_U.h>
// #include <WebServer.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>

#define DEBUG

#ifdef DEBUG
const char *SSID = "";
const char *PASSWORD = "";
#else
const char *SSID = "";
const char *PASSWORD = "";
#endif
const byte DHTPIN = 4;
const byte DHTTYPE = DHT11;
const byte BH1750ADDR = 0x23;
const int PORT = 9105;
const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 64;
const int OLED_RESET = -1;

Here is a place your store your constant.

Read more