Commit 2ab520d5 authored by zak.evans's avatar zak.evans

Add new file

parent 78f97898
#include <SPI.h>
#include <WiFiNINA.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
char ssid[] = "Enterwifiname";
char pass[] = "Enterpassword";
int keyIndex = 0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
const char* thingSpeakApiKey = "MRIHLKXFMUDD5V0K";
const char* thingSpeakAddress = "api.thingspeak.com";
const int thingSpeakPort = 80;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
//This was the first setup for the system which powers on and connects...
//...the LCD to display "Irrigation ssytem is on".
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
delay(1000);
lcd.setCursor(0, 0);
lcd.print("IRRIGATION");
lcd.setCursor(0, 1);
lcd.print("SYSTEM IS ON ");
lcd.print("");
delay(3000);
lcd.clear();
// WiFi setup
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
server.begin();
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() { //Loop to make the water pump turn on once the soil moisture is low from...void
//...reading Analog Pin (A0) on the Arduino.
int value = analogRead(A0);
Serial.println(value);
if (value > 950) {
digitalWrite(2, LOW);
lcd.setCursor(0, 0);
lcd.print("Water Pump is ON ");
} else {
digitalWrite(2, HIGH);
lcd.setCursor(0, 0);
lcd.print("Water Pump is OFF");
}
//Programs the soil mositure sensor to be at a Low, Mid or High state at a certain value.
if (value < 300) {
lcd.setCursor(0, 1);
lcd.print("Moisture : HIGH");
} else if (value > 300 && value < 950) {
lcd.setCursor(0, 1);
lcd.print("Moisture : MID ");
} else if (value > 950) {
lcd.setCursor(0, 1);
lcd.print("Moisture : LOW ");
}
uploadToThingSpeak(value);
}
void uploadToThingSpeak(int moistureValue) {
WiFiClient client;
if (client.connect(thingSpeakAddress, thingSpeakPort)) {
String url = "/update?api_key=";
url += thingSpeakApiKey;
url += "&field1=";
url += String(moistureValue);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + thingSpeakAddress + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println("Data sent to ThingSpeak!");
} else {
Serial.println("Failed to connect to ThingSpeak");
}
client.stop();
}
\ No newline at end of file
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