Commit de93f367 authored by Indiana Brown's avatar Indiana Brown

Initial commit

parents
Pipeline #399 canceled with stages
//Libraries
#include "ThingSpeak.h"
#include <WiFi.h>
#include <SPI.h>
//Program variables
int sInput1;
int sInput2;
int sInput3;
int vccPin = 12;
int average;
int debugID = 1;
//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";
int timer = 24;
//Setup
void 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:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}
ThingSpeak.begin(client);
}
//Loop
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);
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);
}
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);
}
void connectToWPA() {
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);
}
}
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