Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ADDED BLYNK - GOOD JUNE 26,2025
- */
- #define BLYNK_TEMPLATE_ID "xxxxxxxxxxx"
- #define BLYNK_TEMPLATE_NAME "Weather"
- #define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxx"
- char ssid[] = "xxxxxxxxxx";
- char pass[] = "xxxxxxxxxx";
- char auth[] = BLYNK_AUTH_TOKEN;
- #include <Adafruit_GFX.h>
- #include <Adafruit_ST7735.h>
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BME280.h>
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <BlynkSimpleEsp32.h> //IMPORTANT TO USE ESP32 VERSION
- #include <Adafruit_ADS1X15.h>
- Adafruit_ADS1115 ads;
- BlynkTimer timer;
- #define BLYNK_PRINT Serial
- #include <Fonts/Seven_Segment8pt7b.h>
- // TFT pin configuration
- #define TFT_CS 5
- #define TFT_RST 3
- #define TFT_DC 4
- Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
- #define SCK_PIN 20
- #define MOSI_PIN 19
- #define MISO_PIN 18
- // I2C pins for ESP32-C6
- #define I2C_SDA 22
- #define I2C_SCL 21
- const int ADS_I2C_ADDR = 0x48;
- // BME280 I2C
- #define SEALEVELPRESSURE_HPA (1013.25)
- Adafruit_BME280 bme; // I2C interface
- int sda = 22; //I2C
- int scl = 21;
- // Label colors
- #define ST77XX_GOLD 0xF631 //custom color
- uint16_t colors[] = {
- ST77XX_YELLOW, // Temperature
- ST77XX_GREEN, // Humidity
- ST77XX_CYAN, // Heat Index
- ST77XX_GOLD // Pressure
- };
- // Label names
- const char* smallLabels[] = { "Temperature", "Humidity", "Heat Index", "Pressure" };
- // Values to be displayed
- String values[4];
- float temp;
- float humid;
- float pressure;
- float hi;
- float voltage;
- void setup() {
- Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
- timer.setInterval(1000L, sendSensor);
- SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SCK_PIN); //Initialize SPI manually for custom pins
- Wire.begin(sda, scl); // Initialize I2C manually (optional for custom pins)
- Serial.begin(115200);
- ads.begin(ADS_I2C_ADDR);
- ads.setGain(GAIN_TWOTHIRDS); // Gain = 0 => ±6.144V range (default)
- ads.setDataRate(RATE_ADS1115_32SPS);
- // Initialize BME280 over I2C
- if (!bme.begin(0x76, &Wire)) {
- Serial.println("Could not find BME280 sensor!");
- while (1);
- }
- // Initialize TFT
- tft.initR(INITR_GREENTAB);
- tft.setRotation(0);
- tft.fillScreen(ST77XX_BLACK);
- }
- void loop() {
- Blynk.run();
- timer.run();
- // Read BME280 values
- temp = bme.readTemperature();
- humid = bme.readHumidity();
- pressure = bme.readPressure() / 100.0; // hPa
- // Calculate Heat Index in Celsius
- hi = computeHeatIndex(temp, humid, false);
- // Store values as strings
- values[0] = String(temp, 1) + "C";
- values[1] = String(humid, 1) + "%";
- values[2] = String(hi, 1) + "C";
- values[3] = String(pressure, 1) + "hPa";
- // Update display
- tft.fillScreen(ST77XX_BLACK);
- drawLabels();
- delay(1000);
- }
- void drawLabels() {
- int boxWidth = 100;
- int boxHeight = 32;
- int radius = 4;
- int spacingY = 8;
- int startX = (tft.width() - boxWidth) / 2;
- for (int i = 0; i < 4; i++) {
- int y = i * (boxHeight + spacingY) + 5;
- // Draw box
- tft.drawRoundRect(startX, y, boxWidth, boxHeight, radius, colors[i]);
- // Big label (value)
- tft.setFont(&Seven_Segment8pt7b);
- tft.setTextColor(colors[i]);
- int16_t x1, y1;
- uint16_t w, h;
- tft.getTextBounds(values[i], 0, 0, &x1, &y1, &w, &h);
- int textX = startX + (boxWidth - w) / 2;
- int textY = y + 14;
- tft.setCursor(textX, textY);
- tft.print(values[i]);
- // Small label (description)
- tft.setFont();
- tft.setTextColor(ST77XX_WHITE);
- tft.setTextSize(1);
- tft.getTextBounds(smallLabels[i], 0, 0, &x1, &y1, &w, &h);
- textX = startX + (boxWidth - w) / 2;
- textY = y + boxHeight - h - 2;
- tft.setCursor(textX, textY);
- tft.print(smallLabels[i]);
- }
- }
- // Heat Index Calculation
- float computeHeatIndex(float temperature, float humidity, bool isFahrenheit) {
- if (!isFahrenheit) {
- temperature = temperature * 1.8 + 32;
- }
- hi = -42.379 +
- 2.04901523 * temperature +
- 10.14333127 * humidity +
- -0.22475541 * temperature * humidity +
- -0.00683783 * temperature * temperature +
- -0.05481717 * humidity * humidity +
- 0.00122874 * temperature * temperature * humidity +
- 0.00085282 * temperature * humidity * humidity +
- -0.00000199 * temperature * temperature * humidity * humidity;
- if ((humidity < 13) && (temperature >= 80.0) && (temperature <= 112.0)) {
- hi -= ((13.0 - humidity) * 0.25) *
- sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);
- } else if ((humidity > 85.0) && (temperature >= 80.0) && (temperature <= 87.0)) {
- hi += ((humidity - 85.0) * 0.1) *
- ((87.0 - temperature) * 0.2);
- }
- if (!isFahrenheit) {
- hi = (hi - 32) * 0.5556;
- }
- return hi;
- }
- void sendSensor()
- {
- readADC();
- Blynk.virtualWrite(V0, temp);
- Blynk.virtualWrite(V1, humid);
- Blynk.virtualWrite(V2, hi);
- Blynk.virtualWrite(V3, pressure);
- Blynk.virtualWrite(V4,voltage);
- }
- void readADC() {
- int16_t raw = ads.readADC_SingleEnded(0);
- voltage = raw *0.0001875; //6.144/2^15 = 0.1875mV per bit
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement