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 @@ ...@@ -7,9 +7,11 @@
int sInput1; int sInput1;
int sInput2; int sInput2;
int sInput3; int sInput3;
int vccPin = 12;
int average;
int debugID = 1; //Averages variables
int Avg1, Avg2, Avg3, Avg4;
int dailyReading;
//Connectivity variables //Connectivity variables
char ssid[] = "BTHub5-MKS7"; char ssid[] = "BTHub5-MKS7";
...@@ -20,103 +22,152 @@ WiFiClient client; ...@@ -20,103 +22,152 @@ WiFiClient client;
//ThingSpeak variables //ThingSpeak variables
unsigned long myChannelNumber = 640257; unsigned long myChannelNumber = 640257;
const char * myWriteAPIKey = "BRO3JQU737EE4WN9"; const char * myWriteAPIKey = "BRO3JQU737EE4WN9";
int timer = 24;
//Setup
void setup() { //Setup (runs once)
void setup() {
//Serial monitor setup
Serial.begin(9600); Serial.begin(9600);
Serial.println("Program starting."); Serial.println("Program starting.");
Serial.println(" "); Serial.println(" ");
pinMode(vccPin, OUTPUT);
pinMode(10, OUTPUT); //Checks for a WiFi shield and halts the program if one is not detected
digitalWrite(10, LOW);
//check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) { if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); Serial.println("There were problems detecting the WiFi sheild. Check connection.");
// don't continue:
while (true); while (true);
} }
//Checks if the firmware version is up-to-date
String fv = WiFi.firmwareVersion(); String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") { 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); ThingSpeak.begin(client);
} }
//Loop //Loop (runs continuously)
void loop() { void loop() {
connectToWPA(); connectToWPA();
delay(2000);
Serial.println("Reading data."); Serial.println("Reading data.");
sInput1 = analogRead(A0); takeReadings();
sInput2 = analogRead(A1); dailyReading = ((Avg1 + Avg2 + Avg3 + Avg4)/4);
sInput3 = analogRead(A2); //Print for debug
average = ((sInput1 + sInput2 + sInput3)/3); Serial.print("Daily reading is: ");
Serial.println("Attempting to send data"); Serial.print(dailyReading);
//int x = ThingSpeak.writeField(myChannelNumber, 1, average, myWriteAPIKey); Serial.println("Attempting to send data.");
//Attempt to write the data to ThingSpeak
ThingSpeak.setField(1, average); ThingSpeak.setField(3, dailyReading);
ThingSpeak.setField(2, timer);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
//x is the return code for HTTP. 200 means that the data was successfully transmitted. //x is the return code for HTTP. 200 means that the data was successfully transmitted.
if(x == 200){ if(x == 200){
Serial.println("Channel update successful."); Serial.println("Channel update successful.");
Serial.print("Moisture value sent: "); Serial.print("Moisture value sent: ");
Serial.println(average); Serial.println(dailyReading);
Serial.print("Timer value sent: ");
Serial.println(timer);
} }
else{ else{
Serial.println("Problem updating channel. HTTP error code " + String(x)); 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() { 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){ if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: "); Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid); Serial.println(ssid);
while(WiFi.status() != WL_CONNECTED){ while(WiFi.status() != WL_CONNECTED){
digitalWrite(10, LOW); digitalWrite(10, LOW);
WiFi.begin(ssid, pass); WiFi.begin(ssid, pass);
Serial.print("."); Serial.print(". . .");
delay(5000); delay(5000);
} }
Serial.println("\nConnected."); Serial.println("\nConnected.");
digitalWrite(10, HIGH); 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