I am working on making a pool controller for one of my classes right now. I have an Arduino Uno with the official arduino ethernet shield attached to it. I have several temperature sensors and relays attached to the arduino, and am hosting an HTML website displaying them. I was able to get the temps and relay status to display on the website as shown below and automatically refresh.
However, I am having trouble adding in website control. I need to have buttons on the website for the user to be able to turn the relays on/off. I was able to implement a test code that only contains the buttons and it works well. However, when trying to combine them my website keeps crashing. I'm assuming it's some sort of error with the interaction of the buttons being pressed and the refreshing of the website to display the pool data. I attached the code with it all combined, it's a bit lengthy but the code for the website stuff starts around line 170.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); //The LCD address and size.
#define ONE_WIRE_BUS 6 //pin for sensor
#define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
// Setup oneWire
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Setup relays
const int pushButton[] = { 2, 3, 4 }; // define push button inputs
const int relayPin[] = { 9, 8, 7 }; // output pins where 3 relays will be connected
String relayNames[] = { "CH1", "CH2", "CH3" }; // Just put name for 3 relays
int pushed[] = { 0, 0, 0 }; // status of each buttons
int relayStatus[] = { HIGH, HIGH, HIGH }; // initial status of relay
String relayState[] = { "OFF", "OFF", "OFF" };
String relayNumber[] = { "1", "2", "3" };
String readString;
float calibration_value = 21.34 + 0.7;
unsigned long int avgValue; //Store the average value of the sensor feedback
int buf[10], temp;
// Website Setup
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
//this is the mac address of the physical ethernet shield, edited for arduino forum
};
// this is the IP address that the server is accessible at
IPAddress ip(11, 11, 11, 11);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup(void) {
for (int i = 0; i < 3; i++) {
pinMode(pushButton[i], INPUT_PULLUP);
pinMode(relayPin[i], OUTPUT);
digitalWrite(relayPin[i], HIGH); // initial relay status to be OFF
}
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Initialize temp sensor
sensors.begin();
// Initialize pH Sensor
pinMode(SensorPin, INPUT);
// Initialize LCD and display welcome message
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Pool Controller");
lcd.setCursor(0, 1);
lcd.print("by CC, BJ, NG");
delay(2000);
lcd.clear();
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
// Main loop
void loop(void) {
Serial.println("==");
// Send the command to get temperatures
// Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
// Serial.println("DONE");
// Get temperature data of each sensor
float tempC1 = sensors.getTempCByIndex(0); // Water sensor
float tempC2 = sensors.getTempCByIndex(1); // Air sensor
float tempF1 = DallasTemperature::toFahrenheit(tempC1); // Water in Fahrenheit
float tempF2 = DallasTemperature::toFahrenheit(tempC2); // Air in Fahrenheit
// pH sensor
for (int i = 0; i < 10; i++) { //Get 10 sample value from the sensor for smooth the value
buf[i] = analogRead(SensorPin);
delay(10);
}
for (int i = 0; i < 9; i++) { //sort the analog from small to large
for (int j = i + 1; j < 10; j++) {
if (buf[i] > buf[j]) {
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
}
avgValue = 0;
for (int i = 2; i < 8; i++) avgValue += buf[i]; //take the average value of 6 center sample
float phValue = (float)avgValue * 5.0 / 1024 / 6; //convert the analog into millivolt
phValue = -5.70 * phValue + calibration_value; //convert the millivolt into pH value
// end of pH sensor section
// this for loop controls the relays
for (int i = 0; i < 3; i++) {
int val = digitalRead(pushButton[i]);
if (val == HIGH && relayStatus[i] == LOW) {
pushed[i] = 1 - pushed[i];
delay(100);
}
relayStatus[i] = val; // status of each relay
// If pushed status is HIGH, turn relay on
if (pushed[i] == HIGH) {
digitalWrite(relayPin[i], LOW);
relayState[i] = "ON";
} else if (pushed[i] == LOW) {
digitalWrite(relayPin[i], HIGH);
relayState[i] = "OFF";
}
}
// start the ethernet client
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
while (client.connected()) {
if (client.available()) {
char c = client.read();
//Serial.write(c);
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n') {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<HTML>");
client.println("<HEAD> <meta http-equiv='refresh' content='1'/> <title>PoolController</title>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Pool Equipment Control</H1>");
client.print("<input type=submit value=HeaterON style=width:100px;height:45px onClick=location.href='/?on2;'>");
client.print("<input type=submit value=HeaterOFF style=width:100px;height:45px onClick=location.href='/?off3;'>");
client.println("<br />");
client.print("<input type=submit value=PumpON style=width:100px;height:45px onClick=location.href='/?on4;'>");
client.print("<input type=submit value=PumpOFF style=width:100px;height:45px onClick=location.href='/?off5;'>");
client.println("<br />");
client.print(" <input type=submit value='ALL OFF' style=width:100px;height:45px onClick=location.href='/?off35;'>");
client.println("<br />");
client.println("<H2>Pool Data</H2>");
client.println("<br />");
// upload temperature data
client.print("Water Temp = ");
client.print(tempF1);
client.print(" F<br />");
client.print("Air Temp = ");
client.print(tempF2);
client.print(" F<br />");
//upload pH sensor readings
client.print("Pool water = ");
client.print(phValue);
client.print(" pH<br />");
client.print("Pool Pump is ");
client.print(relayState[0]);
client.println("<br />");
client.print("Pool Heater is ");
client.print(relayState[1]);
client.println("<br />");
client.print("Relay #3 is ");
client.print(relayState[2]);
client.println("<br />");
client.println("<br />");
client.println("</BODY>");
// end document
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
if (readString.indexOf('2') > 0) //checks for 2
{
digitalWrite(relayPin[2], LOW); //turn pin8 ON
}
if (readString.indexOf('3') > 0) //checks for 3
{
digitalWrite(relayPin[2], HIGH); // turn pin8 OFF
}
if (readString.indexOf('4') > 0) //checks for 2
{
digitalWrite(relayPin[1], LOW); //turn pin 9 ON
}
if (readString.indexOf('5') > 0) //checks for 3
{
digitalWrite(relayPin[1], HIGH); // turn pin 9 OFF
}
readString = "";
Serial.println("Server Running");
}
}
}
}
// Check if reading was successful
if (tempC1 != DEVICE_DISCONNECTED_C && tempC2 != DEVICE_DISCONNECTED_C) {
// Displaying water temp
lcd.setCursor(0, 0);
lcd.print("Water: ");
lcd.setCursor(9, 0);
lcd.print(tempF1);
lcd.print((char)223);
lcd.print(" F");
// Displaying air temp
lcd.setCursor(0, 1);
lcd.print("Air: ");
lcd.setCursor(9, 1);
lcd.print(tempF2);
lcd.print((char)223);
lcd.print(" F");
} else {
Serial.println("Error: Could not read temperature data");
}
// Displaying pH Value
lcd.setCursor(0, 2);
lcd.print("pH:");
lcd.setCursor(9, 2);
lcd.print(phValue);
// Serial.println("==");
}