Commit 5d5e6a82 authored by Indiana Brown's avatar Indiana Brown

Added digital power pin (vccPin). Removed needless library (SPI.h).

parent b6cbc041
//Libraries
#include "ThingSpeak.h"
#include <WiFi.h>
#include <SPI.h>
//Program variables
int sInput1;
int sInput2;
int sInput3;
//Averages variables
int Avg1, Avg2, Avg3, Avg4;
int dailyReading;
//Connectivity variables
char ssid[] = "BTHub5-MKS7";
char pass[] = "42d6789b8d";
int status = WL_IDLE_STATUS;
WiFiClient client;
//ThingSpeak variables
unsigned long myChannelNumber = 640257;
const char * myWriteAPIKey = "BRO3JQU737EE4WN9";
//Setup (runs once)
void setup() {
//Serial monitor setup
Serial.begin(9600);
Serial.println("Program starting.");
Serial.println(" ");
//Checks for a WiFi shield and halts the program if one is not detected
if (WiFi.status() == WL_NO_SHIELD) {
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("Update the firmware of the WiFi shield to avoid unexpected issues.");
}
ThingSpeak.begin(client);
}
//Loop (runs continuously)
void loop() {
connectToWPA();
Serial.println("Reading data.");
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(dailyReading);
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}
//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(". . .");
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