part 2:
void loop() {
Serial.println("Sleeping...");
delay(1000);
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306, user, password)) {
delay(5000);
Serial.println("Running a query");
sensorValueVout1 = digitalRead(8);
sensorValueVout2 = digitalRead(9);
// Counter of HIGH and LOW for Vout1 (2.5 um particles):
if (sensorValueVout1 == HIGH) {
counterVout1HighNP = counterVout1HighNP +1;
counterVout1LowYP = counterVout1LowYP +0;
}
else {
counterVout1HighNP = counterVout1HighNP +0;
counterVout1LowYP = counterVout1LowYP +1;
}
// Counter of HIGH for Vout2 (1.0 um particles):
if (sensorValueVout2 == HIGH) {
counterVout2HighNP = counterVout2HighNP +1;
counterVout2LowYP = counterVout2LowYP +0;
}
else {
counterVout2HighNP = counterVout2HighNP +0;
counterVout2LowYP = counterVout2LowYP +1;
}
dataTable1 = sensorValueVout1 + c2 + sensorValueVout2 + c2 + counterVout1HighNP + c2 + counterVout1LowYP + c2 + counterVout2HighNP + c2 + counterVout2LowYP + c2;
Serial.print(dataTable1);
inputQueryTable1 ();
// Calculation of LOW RATIO:
if (id%20 == 0) {
delay(5000);
Vout1LowRatio = (float(counterVout1LowYP)/20)*100; //otherwise the result is an int
Vout2LowRatio = float((float(counterVout2LowYP)/20)*100);
// LOW RATIO vs. CONCENTRATION Vout1:
// ----------------------------------
// Upper Curve Vout1 (particles = 2.5 um)
if (Vout1LowRatio <= 10) {
particleA_Vout1_upper = float(0.0089*Vout1LowRatio - 0.0089);
}
else if (Vout1LowRatio > 10) {
particleA_Vout1_upper = float(0.125*Vout1LowRatio - 0.45);
}
// AVG Curve Vout1 (particles = 2.5 um)
if (Vout1LowRatio >= 8) {
particleA_Vout1_average = float(0.149925*Vout1LowRatio - 0.4);
}
else if (2 < Vout1LowRatio < 8) {
particleA_Vout1_average = float(0.1*Vout1LowRatio);
}
else if (Vout1LowRatio <= 2) {
particleA_Vout1_average = float(0.125*Vout1LowRatio - 0.05);
}
// Lower Curve Vout1 (particles = 2.5 um)
if (Vout1LowRatio <= 10) {
particleA_Vout1_lower = float(0.125*Vout1LowRatio + 0.1);
}
else if (Vout1LowRatio > 10) {
particleA_Vout1_lower = float(0.167*Vout1LowRatio - 0.1333);
}
// LOW RATIO vs. CONCENTRATION Vout2:
// ----------------------------------
// Upper Curve Vout2 (particles = 1.0 um)
if (Vout2LowRatio <= 10) {
particleA_Vout2_upper = float(0.0089*Vout2LowRatio - 0.0089);
}
else if (Vout2LowRatio > 10) {
particleA_Vout2_upper = float(0.125*Vout2LowRatio - 0.45);
}
// AVG Curve Vout2 (particles = 1.0 um)
if (Vout2LowRatio >= 8) {
particleA_Vout2_average = float(0.149925*Vout2LowRatio - 0.4);
}
else if (2 < Vout2LowRatio < 8) {
particleA_Vout2_average = float(0.1*Vout2LowRatio);
}
else if (Vout2LowRatio <= 2) {
particleA_Vout2_average = float(0.125*Vout2LowRatio - 0.05);
}
// Lower Curve Vout2 (particles = 1.0 um)
if (Vout2LowRatio <= 10) {
particleA_Vout2_lower = float(0.125*Vout2LowRatio + 0.1);
}
else if (Vout2LowRatio > 10) {
particleA_Vout2_lower = float(0.167*Vout2LowRatio - 0.1333);
}
// LOW RATIO vs. PARTICLE Vout1:
// -----------------------------
// Upper Curve Vout1 (particles = 2.5 um)
if (Vout1LowRatio <= 25) {
particleB_Vout1_upper = float(512.82*Vout1LowRatio - 320.5128);
}
else if (Vout1LowRatio > 25) {
particleB_Vout1_upper = float(2500*Vout1LowRatio - 50000);
}
// AVG Curve Vout1 (particles = 2.5 um)
if (Vout1LowRatio <= 20) {
particleB_Vout1_average = float(625*Vout1LowRatio);
}
else if (Vout1LowRatio > 20) {
particleB_Vout1_average = float(1136.3636*Vout1LowRatio - 10227.2727);
}
// Lower Curve Vout1 (particles = 2.5 um)
particleB_Vout1_lower = float(781.25*Vout1LowRatio + 546.875);
// LOW RATIO vs. PARTICLE Vout2:
// -----------------------------
// Upper Curve Vout2 (particles = 1.0 um)
if (Vout2LowRatio <= 25) {
particleB_Vout2_upper = float(512.82*Vout2LowRatio - 320.5128);
}
else if (Vout2LowRatio > 25) {
particleB_Vout2_upper = float(2500*Vout2LowRatio - 50000);
}
// AVG Curve Vout2 (particles = 1.0 um)
if (Vout2LowRatio <= 20) {
particleB_Vout2_average = float(625*Vout2LowRatio);
}
else if (Vout2LowRatio > 20) {
particleB_Vout2_average = float(1136.3636*Vout2LowRatio - 10227.2727);
}
// Lower Curve Vout2 (particles = 1.0 um)
particleB_Vout2_lower = float(781.25*Vout2LowRatio + 546.875);
// String dataTable2
dataTable2 = Vout1LowRatio + c2 + Vout2LowRatio + c2 + particleA_Vout1_upper + c2 + particleA_Vout1_average + c2 + particleA_Vout1_lower + c2 + particleA_Vout2_upper + c2 + particleA_Vout2_average + c2 + particleA_Vout2_lower + c2 + particleB_Vout1_upper + c2 + particleB_Vout1_average + c2 + particleB_Vout1_lower + c2 + particleB_Vout2_upper + c2 + particleB_Vout2_average + c2 + particleB_Vout2_lower;
// Print in Serial Monitor
Serial.print(dataTable2);
// Give the MySQL query action:
inputQueryTable2 ();
// Reset the counters:
counterVout1HighNP = 0;
counterVout1LowYP = 0;
counterVout2HighNP = 0;
counterVout2LowYP = 0;
// Update the counter id30:
id20 = id20 + 20;
}
id++;
Serial.println("Recording data.");
delay(5000);
}
else {
num_fails++;
Serial.println("Connect failed!");
if (num_fails == MAX_FAILED_CONNECTS) {
Serial.println("Ok, that's it. I'm outta here. Rebooting...");
soft_reset();
}
}
client.flush();
client.stop();
}
//************************************************************************************//
void inputQueryTable1 () {
String InsertSQL_String_A = QueryTable1 + c1 + id + c1 + c2 + c1 + sensorValueVout1 + c1 + c2 + c1 + sensorValueVout2 + c1 + c2 + c1 + counterVout1HighNP + c1 + c2 + c1 + counterVout1LowYP + c1 + c2 + c1 + counterVout2HighNP + c1 + c2 + c1 + counterVout2LowYP + c1 + c3;
int length_InsertSQL_String_A = InsertSQL_String_A.length() + 1;
char InsertSQL_A [length_InsertSQL_String_A];
InsertSQL_String_A.toCharArray(InsertSQL_A,length_InsertSQL_String_A);
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); // Initiate the query class instance
cur_mem->execute(InsertSQL_A); // Execute the query
delete cur_mem;
}
//************************************************************************************//
void inputQueryTable2 () {
String InsertSQL_String_B = QueryTable2 + c1 + id20 + c1 + c2 + c1 + Vout1LowRatio + c1 + c2 + c1 + Vout2LowRatio + c1 + c2 + c1 + particleA_Vout1_upper + c1 + c2 + c1 + particleA_Vout1_average + c1 + c2 + c1 + particleA_Vout1_lower + c1 + c2 + c1 + particleA_Vout2_upper + c1 + c2 + c1 + particleA_Vout2_average + c1 + c2 + c1 + particleA_Vout2_lower + c1 + c2 + c1 + particleB_Vout1_upper + c1 + c2 + c1 + particleB_Vout1_average + c1 + c2 + c1 + particleB_Vout1_lower + c1 + c2 + c1 + particleB_Vout2_upper + c1 + c2 + c1 + particleB_Vout2_average + c1 + c2 + c1 + particleB_Vout2_lower + c1 + c3;
int length_InsertSQL_String_B = InsertSQL_String_B.length() + 1;
char InsertSQL_B [length_InsertSQL_String_B];
InsertSQL_String_B.toCharArray(InsertSQL_B,length_InsertSQL_String_B);
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
cur_mem->execute(InsertSQL_B);
delete cur_mem;
}
//************************************************************************************//
void soft_reset() {
asm volatile("jmp 0");
}