-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp-poster-with-averaging-b.ino
251 lines (215 loc) · 9.18 KB
/
esp-poster-with-averaging-b.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
esp-poster-with-averaging-b.ino
This module is to be flashed to a ESP32 module/developmental board located on the
sensing device on panel A via Arduino IDE (preferred). Its purpose is to control the
MCU onboard the sensing device on panel B, providing the following functionalities:
(1) collecting data from each sensing module onboard the AIO PV Sensor,
(2) converting that data when necessary (unless converted through HW), and
(3) averaging collected data, and transmitting data to a PHP file stored on the
collector node.
Created by UCF Senior Design Spring 2022 - Summer 2022 Group 6
Released on June 30th, 2022
*/
#include <HTTPClient.h>
#include <WiFi.h>
#include <max6675.h>
#include <Adafruit_MCP3008.h>
// Number of sample readings to average
#define SAMPLES 10
// Sensor pins
#define THERMO_DO 19
#define THERMO_CS 5
#define THERMO_CLK 18
#define PYRA_SCK 14
#define PYRA_MOSI 13
#define PYRA_MISO 12
#define PYRA_CS 15
#define VOLT 35
#define AMP 34
// Network credentials
String ap_ssid = "OUC_AIOPV_AP";
String ap_password = "solarpi314";
// Domain website/IP address
int ipFix = 0; // Counter variable for last digit of IP address (LAN website)
// PHP file where collected data is posted to
String serverNameFix = "http://192.168.4.0/post-esp-data-b.php"; /* Last digit of IP
address starts at 0
as a base case
(until it reaches the
correct IP address)
*/
// API key value used to ensure only this device can post data to the database
// acts as an embedded password
String apiKeyValue = "uc7LkiKar7";
// Sensor meta data
String sensorName = "AIO PV Sensor B";
String sensorLocation = "Solar Panel B";
// Thermocouple
MAX6675 thermocouple(THERMO_CLK, THERMO_CS, THERMO_DO); /* Instantiating MAX6675 (ADC)
object with the ESP32 pin
numbers associated with
thermocouple's CLK, CS, and
DO pins
*/
int rawCelsiusTemperature[SAMPLES] = {}; // Buffer array used for averaging Celsius temperature values
int celsiusTemperatureSum = 0; // Accumulator variable used for averaging Celsius temperature values
float averageRawCelsiusTemperature = 0; // Average Celsius temperature value
float outputCelsiusTemperature = 0; // Same as averageRawCelsiusTemperature
int rawFahrenheitTemperature[SAMPLES] = {}; // Buffer array used for averaging Fahrenheit temperature values
int fahrenheitTemperatureSum = 0; // Accumulator variable used for averaging Fahrenheit temperature values
float averageRawFahrenheitTemperature = 0; // Average Fahrenheit temperature value
float outputFahrenheitTemperature = 0; // Same as averageRawFahrenheitTemperature
// Pyranometer
Adafruit_MCP3008 adc; // Instantiating MCP3008 object
float conv = (3.3 / 1024); // Conversion factor for 10-bit ADC (MCP3008)
float pcf = 5000; // Conversion factors of 1000 for V to mV and 5 for mV to W/m^2
int rawIrradiance[SAMPLES] = {}; // Buffer array used for averaging irradiance values
int irradianceSum = 0; // Accumulator variable used for averaging irradiance values
float averageRawIrradiance = 0; // Average irradiance value
float outputIrradiance = 0; // Same as above
// Voltage sensor
int analogVolt[SAMPLES] = {}; // Buffer array used for averaging analog voltage values
int voltSum = 0; // Accumulator variable used for averaging analog voltage values
float averageAnalogVolt = 0; // Average analog voltage value
float digitalVolt = 0; // Average digital voltage value
// Current sensor
int analogAmp[SAMPLES] = {}; // Buffer array used for averaging analog current values
int ampSum = 0; // Accumulator variable used for averaging analog current values
float averageAnalogAmp = 0; // Average analog current value
float digitalAmp = 0; // Average digital current value
// Setup Wi-Fi connection and pyranometer's ADC
void setup()
{
Serial.begin(115200); // Open a serial port at 115200 baud rate
WiFi.begin(ap_ssid, ap_password); // Connect to a Wi-Fi network with provided credentials
Serial.println("Connecting..."); // Print 'Connecting...' status to serial port
// Print '.' every 500 milliseconds until connected to desired Wi-Fi network
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(""); // Newline
// Print IP address of Wi-Fi network to serial port
Serial.print("Connected to WiFi network with IP address: ");
Serial.println(WiFi.localIP());
// Initiate pyranometer's ADC with the ESP32 pin numbers associated with pyranometer's SCK, MOSI, MISO, and CS pins
adc.begin(PYRA_SCK, PYRA_MOSI, PYRA_MISO, PYRA_CS);
}
void loop()
{
// If connected to Wi-Fi network
if(WiFi.status() == WL_CONNECTED)
{
// Instantiating HTTP client object and beginning connection to LAN website
HTTPClient http;
http.begin(serverNameFix);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Thermocouple sensing
// Outputs the average Celsius temperature value collected over SAMPLES samples
for (int i; i < SAMPLES; i++)
{
rawCelsiusTemperature[i] = thermocouple.readCelsius();
celsiusTemperatureSum += rawCelsiusTemperature[i];
}
averageRawCelsiusTemperature = celsiusTemperatureSum / SAMPLES;
outputCelsiusTemperature = averageRawCelsiusTemperature;
// Outputs the average Fahrenheit temperature value collected over SAMPLES samples
for (int i; i < SAMPLES; i++)
{
rawFahrenheitTemperature[i] = thermocouple.readFahrenheit();
fahrenheitTemperatureSum += rawFahrenheitTemperature[i];
}
averageRawFahrenheitTemperature = fahrenheitTemperatureSum / SAMPLES;
outputFahrenheitTemperature = averageRawFahrenheitTemperature;
// Pyranometer sensing
// Outputs the average irradiance value collected over SAMPLES samples
for (int i = 0; i < SAMPLES; i++)
{
rawIrradiance[i] = adc.readADCDifference(1) * conv * pcf;
irradianceSum += rawIrradiance[i];
}
averageRawIrradiance = irradianceSum / SAMPLES;
outputIrradiance = averageRawIrradiance;
// Voltage sensing
// Outputs the average voltage value collected over SAMPLES samples
for (int i = 0; i < SAMPLES; i++)
{
analogVolt[i] = analogRead(VOLT);
voltSum += analogVolt[i];
}
averageAnalogVolt = voltSum / SAMPLES;
digitalVolt = 22.05 * (0.16 + (averageAnalogVolt * 3.3 / 4095));
// Current sensing
// Outputs the average current value collected over SAMPLES samples
for (int i = 0; i < SAMPLES; i++)
{
analogAmp[i] = analogRead(AMP);
ampSum += analogAmp[i];
}
averageAnalogAmp = ampSum / SAMPLES;
digitalAmp = ((((averageAnalogAmp * 3.3) / 4095) + 0.102) / 0.173);
// Concatenate all collected values into a string to be passed to web server via HTTP
// If thermocouple is disconnected
if (outputCelsiusTemperature < 0)
{
String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName + "&location=" + sensorLocation + "&value1=" + String("NaN") + "&value2=" + String("NaN") + "&value3=" + String(outputIrradiance) + "&value4=" + String(digitalVolt) + "&value5=" + String(digitalAmp);
}
// If thermocouple is connected
else
{
String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName + "&location=" + sensorLocation + "&value1=" + String(outputCelsiusTemperature) + "&value2=" + String(outputFahrenheitTemperature) + "&value3=" + String(outputIrradiance) + "&value4=" + String(digitalVolt) + "&value5=" + String(digitalAmp);
}
// Print HTTP post request to serial port
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData); // HTTP response code
// Print valid HTTP response code to serial port
if (httpResponseCode>0)
{
Serial.print("HTTP Response Code: ");
Serial.println(httpResponseCode); // Will be 200 if valid
}
// Print invalid HTTP response code to serial port
// Increment the counter variable for the last digit of the IP address and update the website URL
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.print("IP address fix count: ");
Serial.println(ipFix);
ipFix += 1;
serverNameFix = "http://192.168.4." + String(ipFix) + "/post-esp-data-b.php";
// Reset ipFix variable to 0 if it reaches an IP address digit's physical maximum of 255
if (ipFix > 255)
{
ipFix = 0;
}
}
// Stop HTTP client
http.end();
// Reset variables used for averaging
celsiusTemperatureSum = 0;
averageRawCelsiusTemperature = 0;
outputCelsiusTemperature = 0;
fahrenheitTemperatureSum = 0;
averageRawFahrenheitTemperature = 0;
outputFahrenheitTemperature = 0;
irradianceSum = 0;
averageRawIrradiance = 0;
outputIrradiance = 0;
voltSum = 0;
averageAnalogVolt = 0;
digitalVolt = 0;
ampSum = 0;
averageAnalogAmp = 0;
digitalAmp = 0;
}
// Print Wi-Fi connection status if not connected to Wi-Fi network
else
{
Serial.println("Wi-Fi Disconnected");
}
// Wait 1 second
delay(1000);
}