I have 3 sensors (air temp, soil moisture, water level). They are all connected to the 5V as show in the picture below.
Now here's the code:
#include <SPI.h>
#include <Ethernet.h>
/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare Variables )-----*/
//#define Relay_1 2 // Arduino Digital I/O pin number
//#define Relay_2 3
#define Relay_3 4
#define Relay_4 5
#define Relay_5 6
const float minOutsideAirTemp = 60;
const float maxOutsideAirTemp = 78;
const float maxPH = 7.5;
const float minPH = 6;
const float minWaterTemp = 0;
const float maxWaterTemp = 0;
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(XXX,XXX,X,XX); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
String HTTP_req; // stores the HTTP request
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
//pinMode(13, INPUT); // switch is attached to Arduino pin 13
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// AJAX request for switch state
if (HTTP_req.indexOf("ajax_switch") > -1) {
// read switch state and send appropriate paragraph text
GetSwitchState(client);
}
else { // HTTP request for web page
// send web page - contains JavaScript with AJAX calls
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Aquaponics System Status</title>");
client.println("<script>");
client.println("function GetSwitchState() {");
client.println("nocache = \"&nocache=\"\
+ Math.random() * 1000000;");
client.println("var request = new XMLHttpRequest();");
client.println("request.onreadystatechange = function() {");
client.println("if (this.readyState == 4) {");
client.println("if (this.status == 200) {");
client.println("if (this.responseText != null) {");
client.println("document.getElementById(\"switch_txt\")\
.innerHTML = this.responseText;");
client.println("}}}}");
client.println(
"request.open(\"GET\", \"ajax_switch\" + nocache, true);");
client.println("request.send(null);");
client.println("setTimeout('GetSwitchState()', 1000);");
client.println("}");
client.println("</script>");
client.println("</head>");
client.println("<body onload=\"GetSwitchState()\">");
client.println("<h1>Arduino Aquaponics System Status</h1>");
client.println("<h2>Sensors</h2>");
client.println("<p id=\"switch_txt\">Loading data...</p>");
//client.println("<h2>Camera</h2>");
client.println("</body>");
client.println("</html>");
}
// display received HTTP request on serial port
Serial.print(HTTP_req);
HTTP_req = ""; // finished with request, empty string
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
// send the state of the switch to the web browser
void GetSwitchState(EthernetClient cl)
{
int airTemp;
airTemp = analogRead(0);
// Convert ADC reading to voltage
float voltage = (airTemp/1024.0 * 5.0);
Serial.print("Volts: ");
Serial.print(voltage);
Serial.print("\n");
// Convert the voltage to temperature in degrees celsius
float temperature = ((voltage - 0.5) * 100);
Serial.print("Temperature C: ");
Serial.print(temperature);
Serial.print("\n");
// Convert celsius into farenheight
float temperatureF = ( ( (temperature * 9) / 5) + 32);
int phLevel;
int waterLevel;
waterLevel = analogRead(1);
int soilMoisture;
soilMoisture = analogRead(2);
cl.println("<p>");
cl.print("Air Temperature: ");
cl.print(temperatureF);
if ((temperatureF >= minOutsideAirTemp) && (temperatureF <= maxOutsideAirTemp)) {
cl.print(" <span style='color:green;'>JUST RIGHT</span>");
} else if (temperatureF > maxOutsideAirTemp) {
cl.print(" <span style='color:red;'>TOO HOT</span>");
} else if (temperatureF < maxOutsideAirTemp) {
cl.print(" <span style='color:blue;'>TOO COLD</span>");
}
cl.println("</p>");
delay(100);
/*
cl.print("<p>");
cl.print("Fish Tank Water Temperature: ");
cl.print(soilMoisture);
cl.print("</p>");
*/
//delay(100);
/*
cl.println("<p>");
cl.print("pH Level: ");
cl.print(phLevel);
cl.println("</p>");
*/
//delay(100);
cl.println("<p>");
cl.print("Fish Tank Water Level: ");
cl.print(waterLevel);
cl.println("</p>");
delay(100);
cl.println("<p>");
cl.print("Grow Bed Moisture Level: ");
cl.print(soilMoisture);
cl.println("</p>");
delay(100);
cl.println("<h2>Relays</h2>");
cl.println("<p>");
cl.print("Fan 1: ");
if (Relay_3 == HIGH) {
cl.print("ON");
} else {
cl.print("OFF");
}
cl.println("</p>");
cl.println("<p>");
cl.print("Fan 2: ");
if (Relay_4 == HIGH) {
cl.print("ON");
} else {
cl.print("OFF");
}
cl.println("</p>");
cl.println("<p>");
cl.print("Fan 3: ");
if (Relay_4 == HIGH) {
cl.print("ON");
} else {
cl.print("OFF");
}
cl.println("</p>");
cl.println("<p>");
cl.print("Garden Light: ");
if (Relay_5 == HIGH) {
cl.print("ON");
} else {
cl.print("OFF");
}
cl.println("</p>");
}
For some reason, when I add more code inside GetSwitchState() and then run it, the Arduino will suddenly stop at a random point in the sketch.
Is this a power issue?
Do I need to add resistors to the sensors?
The sketch is only 16,908 bytes..