Hi everyone,
I am making something for fun at home, and I'm hitting a bit of a wall trying to handle web server stuff. I am using a Mega2560 as the server with an Ethernet 2 Shield. I have it connected to a relay board that operates buttons on a window unit air conditioner. I also have a temp sensor tied to the Mega just for observation. I have gotten fairly far (made sure the temp sensor reads and works...made sure that my operation of the relay board is correct and it functions), but I'm not able to get variable changes on HTML button presses.
I may be completely approaching this wrong as I am not usually doing web apps, but I would appreciate any and all guidance I could get to make this work. If I can just get one button to work, I can complete the rest of the code, I'm sure. I will post my code below for all who may be interested.
#include <SPI.h>
#include <Ethernet.h>
#include "SHT31.h"
uint32_t start;
uint32_t stop;
String readString;
// Temp sensor declaration:
SHT31 sht;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip = {192, 168, 1, 101};
// 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() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("A/C WebServer");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield not found");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable not connected or web client closed");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
// assign pins to relay control
pinMode(30,OUTPUT);
pinMode(32,OUTPUT);
pinMode(34,OUTPUT);
pinMode(36,OUTPUT);
pinMode(38,OUTPUT);
pinMode(40,OUTPUT);
pinMode(42,OUTPUT);
pinMode(44,OUTPUT);
// assign pins to detect LEDs
pinMode (31,INPUT);
pinMode (33,INPUT);
pinMode (35,INPUT);
pinMode (37,INPUT);
pinMode (39,INPUT);
pinMode (41,INPUT);
// temp sensor I2C address
sht.begin(0x44);
//clock poll speed for I2C
Wire.setClock(100000);
uint16_t stat = sht.readStatus();
}
void loop()
{
sht.read();
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100) {
readString += c;
}
if (readString.indexOf("power_toggle") > 0) {
digitalWrite(50,HIGH);
delay(50);
digitalWrite(50,LOW);
delay(50);
}
// 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' && currentLineIsBlank) {
Serial.println(readString);
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<form action=\"http://192.168.1.101/\" method=\"get\" id=\"form1\">");
client.println("<label for=\"shelterTemp\">Current Shelter Temp: </label>");
// output the value of the temp sensor
client.println(sht.getTemperature()*1.8+32, 1);
// temp degree symbol and units (Fahrenheit)
client.println("\xB0""F<br>");
//client.println("<button name=power_toggle type=submit value=on>Power</button>");
client.println("<button name=\"power_toggle\" type=\"submit\" form=\"form1\" value=\"on\">Power</button>");
client.println("</form>");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}