Advertisement
Tywais

Weather Station

Jun 27th, 2025 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 KB | Source Code | 0 0
  1. /*
  2. ADDED BLYNK - GOOD JUNE 26,2025
  3. */
  4. #define BLYNK_TEMPLATE_ID "xxxxxxxxxxx"
  5. #define BLYNK_TEMPLATE_NAME "Weather"
  6. #define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxx"
  7. char ssid[] = "xxxxxxxxxx";
  8. char pass[] = "xxxxxxxxxx";
  9. char auth[] = BLYNK_AUTH_TOKEN;
  10.  
  11. #include <Adafruit_GFX.h>
  12. #include <Adafruit_ST7735.h>
  13. #include <SPI.h>
  14. #include <Wire.h>
  15. #include <Adafruit_Sensor.h>
  16. #include <Adafruit_BME280.h>
  17. #include <WiFi.h>
  18. #include <WiFiClient.h>
  19. #include <BlynkSimpleEsp32.h>  //IMPORTANT TO USE ESP32 VERSION
  20. #include <Adafruit_ADS1X15.h>
  21. Adafruit_ADS1115 ads;
  22.  
  23. BlynkTimer timer;
  24. #define BLYNK_PRINT Serial
  25. #include <Fonts/Seven_Segment8pt7b.h>
  26.  
  27. // TFT pin configuration
  28. #define TFT_CS     5
  29. #define TFT_RST    3
  30. #define TFT_DC     4
  31.  
  32. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  33. #define SCK_PIN 20  
  34. #define MOSI_PIN 19
  35. #define MISO_PIN 18
  36.  
  37. // I2C pins for ESP32-C6
  38. #define I2C_SDA    22
  39. #define I2C_SCL    21
  40. const int ADS_I2C_ADDR  = 0x48;
  41.  
  42. // BME280 I2C
  43. #define SEALEVELPRESSURE_HPA (1013.25)
  44. Adafruit_BME280 bme; // I2C interface
  45. int sda = 22;  //I2C
  46. int scl = 21;
  47.  
  48. // Label colors
  49. #define ST77XX_GOLD 0xF631  //custom color
  50.  
  51. uint16_t colors[] = {
  52.   ST77XX_YELLOW,  // Temperature
  53.   ST77XX_GREEN,   // Humidity
  54.   ST77XX_CYAN,    // Heat Index
  55.   ST77XX_GOLD    // Pressure
  56. };
  57.  
  58. // Label names
  59. const char* smallLabels[] = { "Temperature", "Humidity", "Heat Index", "Pressure" };
  60.  
  61. // Values to be displayed
  62. String values[4];
  63. float temp;
  64. float humid;
  65. float pressure;
  66. float hi;
  67. float voltage;
  68.  
  69. void setup() {
  70.  
  71.  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  72.  timer.setInterval(1000L, sendSensor);
  73.  SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SCK_PIN);  //Initialize SPI manually for custom pins
  74.  Wire.begin(sda, scl);    // Initialize I2C manually (optional for custom pins)
  75.  Serial.begin(115200);
  76.  
  77.  ads.begin(ADS_I2C_ADDR);
  78.  ads.setGain(GAIN_TWOTHIRDS); // Gain = 0 => ±6.144V range (default)
  79.  ads.setDataRate(RATE_ADS1115_32SPS);
  80.  
  81.   // Initialize BME280 over I2C
  82.   if (!bme.begin(0x76, &Wire)) {
  83.     Serial.println("Could not find BME280 sensor!");
  84.     while (1);
  85.   }
  86.  
  87.   // Initialize TFT
  88.   tft.initR(INITR_GREENTAB);
  89.   tft.setRotation(0);
  90.   tft.fillScreen(ST77XX_BLACK);
  91. }
  92.  
  93. void loop() {
  94.   Blynk.run();
  95.   timer.run();
  96.  
  97.   // Read BME280 values
  98.   temp = bme.readTemperature();
  99.   humid = bme.readHumidity();
  100.   pressure = bme.readPressure() / 100.0; // hPa
  101.  
  102.   // Calculate Heat Index in Celsius
  103.     hi = computeHeatIndex(temp, humid, false);
  104.  
  105.   // Store values as strings
  106.   values[0] = String(temp, 1) + "C";
  107.   values[1] = String(humid, 1) + "%";
  108.   values[2] = String(hi, 1) + "C";
  109.   values[3] = String(pressure, 1) + "hPa";
  110.  
  111.   // Update display
  112.   tft.fillScreen(ST77XX_BLACK);
  113.   drawLabels();
  114.   delay(1000);
  115. }
  116.  
  117. void drawLabels() {
  118.   int boxWidth = 100;
  119.   int boxHeight = 32;
  120.   int radius = 4;
  121.   int spacingY = 8;
  122.   int startX = (tft.width() - boxWidth) / 2;
  123.  
  124.   for (int i = 0; i < 4; i++) {
  125.     int y = i * (boxHeight + spacingY) + 5;
  126.  
  127.     // Draw box
  128.     tft.drawRoundRect(startX, y, boxWidth, boxHeight, radius, colors[i]);
  129.  
  130.     // Big label (value)
  131.     tft.setFont(&Seven_Segment8pt7b);
  132.     tft.setTextColor(colors[i]);
  133.     int16_t x1, y1;
  134.     uint16_t w, h;
  135.     tft.getTextBounds(values[i], 0, 0, &x1, &y1, &w, &h);
  136.     int textX = startX + (boxWidth - w) / 2;
  137.     int textY = y + 14;
  138.     tft.setCursor(textX, textY);
  139.     tft.print(values[i]);
  140.  
  141.     // Small label (description)
  142.     tft.setFont();
  143.     tft.setTextColor(ST77XX_WHITE);
  144.     tft.setTextSize(1);
  145.     tft.getTextBounds(smallLabels[i], 0, 0, &x1, &y1, &w, &h);
  146.     textX = startX + (boxWidth - w) / 2;
  147.     textY = y + boxHeight - h - 2;
  148.     tft.setCursor(textX, textY);
  149.     tft.print(smallLabels[i]);
  150.   }
  151. }
  152.  
  153. // Heat Index Calculation
  154. float computeHeatIndex(float temperature, float humidity, bool isFahrenheit) {
  155.   if (!isFahrenheit) {
  156.     temperature = temperature * 1.8 + 32;
  157.   }
  158.  
  159.   hi = -42.379 +
  160.              2.04901523 * temperature +
  161.              10.14333127 * humidity +
  162.              -0.22475541 * temperature * humidity +
  163.              -0.00683783 * temperature * temperature +
  164.              -0.05481717 * humidity * humidity +
  165.              0.00122874 * temperature * temperature * humidity +
  166.              0.00085282 * temperature * humidity * humidity +
  167.              -0.00000199 * temperature * temperature * humidity * humidity;
  168.  
  169.   if ((humidity < 13) && (temperature >= 80.0) && (temperature <= 112.0)) {
  170.     hi -= ((13.0 - humidity) * 0.25) *
  171.           sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);
  172.   } else if ((humidity > 85.0) && (temperature >= 80.0) && (temperature <= 87.0)) {
  173.     hi += ((humidity - 85.0) * 0.1) *
  174.           ((87.0 - temperature) * 0.2);
  175.   }
  176.  
  177.   if (!isFahrenheit) {
  178.     hi = (hi - 32) * 0.5556;
  179.   }
  180.  
  181.   return hi;
  182. }
  183.  
  184. void sendSensor()
  185. {
  186.   readADC();
  187.   Blynk.virtualWrite(V0, temp);
  188.   Blynk.virtualWrite(V1, humid);
  189.   Blynk.virtualWrite(V2, hi);
  190.   Blynk.virtualWrite(V3, pressure);
  191.   Blynk.virtualWrite(V4,voltage);
  192. }
  193.  
  194. void readADC() {
  195.   int16_t raw = ads.readADC_SingleEnded(0);
  196.   voltage = raw *0.0001875; //6.144/2^15 = 0.1875mV per bit
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement