summaryrefslogtreecommitdiff
path: root/Machine Learning/Weather-Prediction-Device-main/Deployment_code/weather_prediction_model.ino
blob: 6cbb87a5b7ac851449ba25d0d6f9292e7a0acb22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "DecisionTree.h"
Eloquent::ML::Port::DecisionTree weatherClassifier;
#include <math.h>
#include "DHT.h"

// Variable Declaration
float t;
float h;
float hic;
int prediction;

#define DHTPIN 4     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.print("Welcome!");
  delay(10);

  dht.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
  // Read humidity
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Celsius (the default)
  float hic = calculate_heat_index(t,h);



  // Print values of temperature, humidity and heat index to serial monitor
  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C  Heat index: "));
  Serial.print(hic);
  Serial.println(F("°C"));

  float input[3] = {t,h,hic};
  // Giving input values to predict Function
  int prediction = weatherClassifier.predict(input);

  // Checking Condition of weather
  Serial.print("Prediction: ");
  // Serial.print(prediction);
  if (prediction == 0){
    Serial.println("Foggy \U0001F32B");
  }
  else if(prediction == 1){
    Serial.println("Rainy \U0001F327");
  }
  else if(prediction == 2){
    Serial.println("Cloudy \u2601️");
  }
  else if(prediction == 3){
    Serial.println("Sunny \u2600");
  }

  Serial.println();

  delay(1000);
}

double calculate_heat_index(double temperature, double humidity) {
    // Coefficients for the heat index formula
    double c1 = -42.379;
    double c2 = 2.04901523;
    double c3 = 10.14333127;
    double c4 = -0.22475541;
    double c5 = -6.83783e-3;
    double c6 = -5.481717e-2;
    double c7 = 1.22874e-3;
    double c8 = 8.5282e-4;
    double c9 = -1.99e-6;

    // Calculate the heat index
    double heat_index = (c1 + (c2 * temperature) + (c3 * humidity) + (c4 * temperature * humidity) +
                         (c5 * pow(temperature, 2)) + (c6 * pow(humidity, 2)) +
                         (c7 * pow(temperature, 2) * humidity) + (c8 * temperature * pow(humidity, 2)) +
                         (c9 * pow(temperature, 2) * pow(humidity, 2)));

    // Adjustments for specific conditions
    if (humidity < 13 && (80 <= temperature && temperature <= 112)) {
        double adjustment = ((13 - humidity) / 4.0) * sqrt((17 - fabs(temperature - 95.0)) / 17);
        heat_index -= adjustment;
    } else if (humidity > 85 && (80 <= temperature && temperature <= 87)) {
        double adjustment = ((humidity - 85) / 10.0) * ((87 - temperature) / 5.0);
        heat_index += adjustment;
    }

    return heat_index;
}