Commit 4274a0fb authored by Indiana Brown's avatar Indiana Brown

Removed original debug message. Commented lots of stuff. Cleaned up old code....

Removed original debug message. Commented lots of stuff. Cleaned up old code. Created new method, takeReading(), for taking daily readings. Set up device to function as it should.
parent de93f367
......@@ -7,9 +7,11 @@
int sInput1;
int sInput2;
int sInput3;
int vccPin = 12;
int average;
int debugID = 1;
//Averages variables
int Avg1, Avg2, Avg3, Avg4;
int dailyReading;
//Connectivity variables
char ssid[] = "BTHub5-MKS7";
......@@ -20,103 +22,152 @@ WiFiClient client;
//ThingSpeak variables
unsigned long myChannelNumber = 640257;
const char * myWriteAPIKey = "BRO3JQU737EE4WN9";
int timer = 24;
//Setup
void setup() {
//Setup (runs once)
void setup() {
//Serial monitor setup
Serial.begin(9600);
Serial.println("Program starting.");
Serial.println(" ");
pinMode(vccPin, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(10, LOW);
//check for the presence of the shield:
//Checks for a WiFi shield and halts the program if one is not detected
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
Serial.println("There were problems detecting the WiFi sheild. Check connection.");
while (true);
}
//Checks if the firmware version is up-to-date
String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}
Serial.println("Update the firmware of the WiFi shield to avoid unexpected issues.");
}
ThingSpeak.begin(client);
}
//Loop
//Loop (runs continuously)
void loop() {
connectToWPA();
delay(2000);
Serial.println("Reading data.");
sInput1 = analogRead(A0);
sInput2 = analogRead(A1);
sInput3 = analogRead(A2);
average = ((sInput1 + sInput2 + sInput3)/3);
Serial.println("Attempting to send data");
//int x = ThingSpeak.writeField(myChannelNumber, 1, average, myWriteAPIKey);
ThingSpeak.setField(1, average);
ThingSpeak.setField(2, timer);
takeReadings();
dailyReading = ((Avg1 + Avg2 + Avg3 + Avg4)/4);
//Print for debug
Serial.print("Daily reading is: ");
Serial.print(dailyReading);
Serial.println("Attempting to send data.");
//Attempt to write the data to ThingSpeak
ThingSpeak.setField(3, dailyReading);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
//x is the return code for HTTP. 200 means that the data was successfully transmitted.
if(x == 200){
Serial.println("Channel update successful.");
Serial.print("Moisture value sent: ");
Serial.println(average);
Serial.print("Timer value sent: ");
Serial.println(timer);
Serial.println(dailyReading);
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
//15 mins delay
delay(900000);
debug();
}
//debug() takes and prints the current readings for each of the three sensors and then prints their average reading.
//It is used for debugging only.
void debug() {
digitalWrite(vccPin, HIGH);
sInput1 = analogRead(A0);
sInput2 = analogRead(A1);
sInput3 = analogRead(A2);
average = ((sInput1 + sInput2 + sInput3)/3);
Serial.print("Debug ID: ");
Serial.println(debugID);
debugID++;
Serial.println(" ");
Serial.print("Input 1: ");
Serial.println(sInput1);
Serial.print("Input 2: ");
Serial.println(sInput2);
Serial.print("Input 3: ");
Serial.println(sInput3);
Serial.println(" ");
Serial.print("Average reading: ");
Serial.println(average);
Serial.println(" ");
Serial.println(" ");
//digitalWrite(vccPin, LOW);
delay(5000);
}
//Attempts to connect to the Internet.
void connectToWPA() {
//Checks if device is already connected to the Internet. If it is not, it attempts a connection. Otherwise it moves on.
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while(WiFi.status() != WL_CONNECTED){
digitalWrite(10, LOW);
WiFi.begin(ssid, pass);
Serial.print(".");
Serial.print(". . .");
delay(5000);
}
Serial.println("\nConnected.");
digitalWrite(10, HIGH);
}
}
//Takes four averages (once every six hours) for the purpose of reporting the daily reading.
void takeReadings(){
//First average
sInput1 = analogRead(A0);
sInput2 = analogRead(A1);
sInput3 = analogRead(A2);
Avg1 = ((sInput1 + sInput2 + sInput3)/3);
//Delay 6 hours
delay(21600000);
//Second average
sInput1 = analogRead(A0);
sInput2 = analogRead(A1);
sInput3 = analogRead(A2);
Avg2 = ((sInput1 + sInput2 + sInput3)/3);
//Delay 6 hours
delay(21600000);
//Third average
sInput1 = analogRead(A0);
sInput2 = analogRead(A1);
sInput3 = analogRead(A2);
Avg3 = ((sInput1 + sInput2 + sInput3)/3);
//Delay 6 hours
delay(21600000);
//Fourth average
sInput1 = analogRead(A0);
sInput2 = analogRead(A1);
sInput3 = analogRead(A2);
Avg4 = ((sInput1 + sInput2 + sInput3)/3);
//Delay 6 hours
delay(21600000);
}
//Debug method to demonstrate that my program works.
void debug(){
//First average
sInput1 = analogRead(A0);
sInput2 = analogRead(A1);
sInput3 = analogRead(A2);
Avg1 = ((sInput1 + sInput2 + sInput3)/3);
Serial.print("First average: ");
Serial.println(Avg1);
//Delay 10 seconds
delay(10000);
//Second average
sInput1 = analogRead(A0);
sInput2 = analogRead(A1);
sInput3 = analogRead(A2);
Avg2 = ((sInput1 + sInput2 + sInput3)/3);
Serial.print("Second average: ");
Serial.println(Avg2);
//Delay 10 seconds
delay(10000);
//Third average
sInput1 = analogRead(A0);
sInput2 = analogRead(A1);
sInput3 = analogRead(A2);
Avg3 = ((sInput1 + sInput2 + sInput3)/3);
Serial.print("Third average: ");
Serial.println(Avg3);
//Delay 10 seconds
delay(10000);
//Fourth average
sInput1 = analogRead(A0);
sInput2 = analogRead(A1);
sInput3 = analogRead(A2);
Avg4 = ((sInput1 + sInput2 + sInput3)/3);
Serial.print("Fourth average: ");
Serial.println(Avg4);
//Delay 10 seconds
delay(10000);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment