-
Notifications
You must be signed in to change notification settings - Fork 0
/
IoTFridge.ino
148 lines (125 loc) · 3.24 KB
/
IoTFridge.ino
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "config.h"
#include "fan_frames.h"
#define OLED_RESET 0 // GPIO0
Adafruit_SSD1306 display(OLED_RESET);
// Mapping NodeMCU Ports to Arduino GPIO Pins
// Allows use of NodeMCU Port nomenclature in config.h
#define A0 0
#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
unsigned long time_now = 0;
bool fan_on;
const int ThermistorPowerPin = THERMISTOR_POWER;
const int ThermistorReadPin = THERMISTOR_READ;
const int ThermistorDelay = THERMISTOR_DELAY;
const double Vcc = VCC;
const float r1 = R1;
const float c1 = C1;
const float c2 = C2;
const float c3 = C3;
float Temp;
float LowerTemp = LOWER_TEMP;
float UpperTemp = UPPER_TEMP;
int xx=22;
int yy=0;
int tt=200;
double get_thermistor_reading() {
// returns thermistor value in F
int Vo;
float logR2, R2, T;
digitalWrite(ThermistorPowerPin, HIGH);
Vo = analogRead(ThermistorReadPin);
//R2 = (R1 * Vo) / (Vcc - Vo);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
T = T - 273.15;
T = (T * 9.0)/ 5.0 + 32.0;
digitalWrite(ThermistorPowerPin, LOW);
return T;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ThermistorPowerPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(0,0);
}
void loop() {
// put your main code here, to run repeatedly:
if(millis() - time_now > ThermistorDelay) {
time_now = millis();
Temp = get_thermistor_reading();
Serial.print("Reading temp: ");
Serial.print(Temp);
Serial.print("\n");
}
if (Temp > UpperTemp) {
// If temp is greater than upper bound, activate cooling
digitalWrite(LED_BUILTIN, LOW);
fan_on = true;
}
if (Temp < LowerTemp) {
// If temp is less than lower bound, deactivate cooling
digitalWrite(LED_BUILTIN, HIGH);
fan_on = false;
}
if (fan_on) {
display.clearDisplay();
display.drawBitmap(xx, yy,frame0,24,24, 1);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,24);
display.print("T: ");
display.print(Temp);
display.print("F\n");
display.display();
delay(tt);
display.clearDisplay();
display.drawBitmap(xx, yy,frame1,24,24, 1);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,24);
display.print("T: ");
display.print(Temp);
display.print("F\n");
display.display();
delay(tt);
display.clearDisplay();
display.drawBitmap(xx, yy,frame2,24,24, 1);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,24);
display.print("T: ");
display.print(Temp);
display.print("F\n");
display.display();
delay(tt);
} else {
display.clearDisplay();
display.drawBitmap(xx, yy,frame0,24,24, 1);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,24);
display.print("T: ");
display.print(Temp);
display.print("F\n");
display.display();
delay(tt);
}
}