add main.ino

add code
This commit is contained in:
rintyuu 2025-01-12 21:50:45 -08:00
commit f3391967bf

69
main.ino Normal file
View file

@ -0,0 +1,69 @@
// PLEASE BE VERY SURE TO EDIT YOUR PINS ON LINES 12 AND 14
// PLEASE BE VERY SURE TO EDIT YOUR PINS ON LINES 12 AND 14
// PLEASE BE VERY SURE TO EDIT YOUR PINS ON LINES 12 AND 14
// PLEASE BE VERY SURE TO EDIT YOUR PINS ON LINES 12 AND 14
// PLEASE BE VERY SURE TO EDIT YOUR PINS ON LINES 12 AND 14
// PLEASE BE VERY SURE TO EDIT YOUR PINS ON LINES 12 AND 14
#include <ArduinoJson.h>
#include <DHT.h>
#include <U8g2lib.h>
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ GPIO_NUM_9, /* data=*/ GPIO_NUM_8);
#define DHTPIN GPIO_NUM_3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
u8g2.begin();
u8g2.clearBuffer();
Serial.begin(115200);
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.drawStr(0, 0, "Initializing...");
u8g2.sendBuffer();
delay(1000);
dht.begin();
u8g2.clearBuffer();
u8g2.drawStr(0, 0, "Ready!");
u8g2.sendBuffer();
delay(1000);
}
void loop() {
float indoorTemp = dht.readTemperature(true);
if (isnan(indoorTemp)) {
Serial.println("Failed to read from DHT sensor!");
indoorTemp = 0;
}
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_logisoso32_tf);
char tempBuffer[10];
snprintf(tempBuffer, sizeof(tempBuffer), "%.1f", indoorTemp);
uint8_t width = u8g2.getStrWidth(tempBuffer);
uint8_t x = (128 - width) / 2;
uint8_t y = 40;
u8g2.setCursor(x, y);
u8g2.print(tempBuffer);
u8g2.setFont(u8g2_font_6x10_tf);
uint8_t textWidth = u8g2.getStrWidth("Fahrenheit");
u8g2.setCursor((128 - textWidth) / 2, y + 20);
u8g2.print("Fahrenheit");
u8g2.sendBuffer();
Serial.print("Indoor Temp: ");
Serial.println(indoorTemp);
delay(15000);
}