/*for use with IDE 1.0https://forum.pjrc.com/threads/68066-New-lwIP-based-Ethernet-library-for-Teensy-4-1?p=299978&viewfull=1#post299978
* open serial monitor to see what the arduino receives and the server address
* requires DHCP
* for hardware: Sprinkler.kicad_pro or bare teensy 4.1
* use the \ slash to escape the " in the html
*/
#include <QNEthernet.h>
//for hardware: Sprinkler Rev- 2022/05/25
#define LEDPIN13 13
#define FVEGPIN 10
#define FLWRPIN 12
#define RGRGPIN 25
#define RWALLPIN 28
using namespace qindesign::network;
EthernetServer server(80); //server port
constexpr uint32_t kDHCPTimeout = 10000; // 10 seconds
constexpr uint16_t kServerPort = 80; //443 for TLS
constexpr int kMessageSize = 255; // Pretend the protocol specifies 10 bytes
IPAddress ip;
String readString;
boolean gledon=false;
boolean gfvegon=false;
boolean gflwron=false;
boolean grgrgon=false;
boolean gwallon=false;
int theline=0;
uint8_t mac[6];
void setup(){
pinMode(LEDPIN13, OUTPUT); //pin selected to control
pinMode(FVEGPIN, OUTPUT); //pin selected to control
pinMode(FLWRPIN, OUTPUT); //pin selected to control
pinMode(RGRGPIN, OUTPUT); //pin selected to control
pinMode(RWALLPIN, OUTPUT); //pin selected to control
digitalWrite(LEDPIN13, gledon); //turn off when starting up
digitalWrite(FVEGPIN, gfvegon); //turn off when starting up
digitalWrite(FLWRPIN, gflwron); //turn off when starting up
digitalWrite(RGRGPIN, grgrgon); //turn off when starting up
digitalWrite(RWALLPIN, gwallon); //turn off when starting up
//enable serial data print
Serial.begin(9600);
while (!Serial && millis()<5000) {
; // wait for serial port to connect or 5 seconds
}
//Serial.print(CrashReport);
stdPrint = &Serial; // Make printf work (a QNEthernet feature)
Serial.println("QsprinklerServerMinimal"); // so I can keep track of what is loaded
Ethernet.macAddress(mac);
Serial.printf("\nMAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
if(mac[5]==0xf3){ //host name stuff
Ethernet.setHostname("GPSD_Box");
}else if(mac[5]==0xb6){
Ethernet.setHostname("bare-Teensy");
} //will use default 'teensy-lwip'
printf("Starting Ethernet with DHCP...\n");
if (!Ethernet.begin()) {
printf("Failed to start Ethernet\n");
//return;
}
if (!Ethernet.waitForLocalIP(kDHCPTimeout)) {
printf("Failed to get IP address from DHCP\n");
gledon=false;
} else {
ip = Ethernet.subnetMask();
printf(" Subnet mask = %u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]);
ip = Ethernet.gatewayIP();
printf(" Gateway = %u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]);
ip = Ethernet.dnsServerIP();
printf(" DNS = %u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]);
// Start the server
ip = Ethernet.localIP();
printf("Listening for clients on '%s'. Copy this to your browser: http://%u.%u.%u.%u \n\n", Ethernet.hostname().c_str(), ip[0], ip[1], ip[2], ip[3]);
server.begin();
gledon=true;
}
digitalWrite(LEDPIN13, gledon); //turn off when starting up
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c); //uncomment to see in serial monitor
}
//if HTTP request has ended
if (c == '\n') {
theline++;
Serial.println(theline); //separate responses with a line #
Serial.print(readString); // \n already included
//set the status of items before sending the HTML in case this is a response to previous change
//this will prevent having to update again as in formServer.ino
if(readString.indexOf("LED+ON") >0){
gledon=true;
digitalWrite(LEDPIN13, gledon);
}else if(readString.indexOf("LED+OFF") >0){
gledon=false;
digitalWrite(LEDPIN13, gledon);
}else if(readString.indexOf("BLE+ON") >0){
gfvegon=true;
digitalWrite(FVEGPIN, gfvegon);
}else if(readString.indexOf("BLE+OFF") >0){
gfvegon=false;
digitalWrite(FVEGPIN, gfvegon);
}else if(readString.indexOf("WER+ON") >0){
gflwron=true;
digitalWrite(FLWRPIN, gflwron);
}else if(readString.indexOf("WER+OFF") >0){
gflwron=false;
digitalWrite(FLWRPIN, gflwron);
}else if(readString.indexOf("AGE+ON") >0){
grgrgon=true;
digitalWrite(RGRGPIN, grgrgon);
}else if(readString.indexOf("AGE+OFF") >0){
grgrgon=false;
digitalWrite(RGRGPIN, grgrgon);
}else if(readString.indexOf("WALL+ON") >0){
gwallon=true;
digitalWrite(FVEGPIN, gwallon); //RWALLPIN
}else if(readString.indexOf("WALL+OFF") >0){
gwallon=false;
digitalWrite(FVEGPIN, gwallon); //RWALLPIN
}else{
Serial.println("Didn't see on or off");
}
//now output HTML data header
/////////////////////
client.writeFully("HTTP/1.1 200 OK\n");
client.writeFully("Content-Type: text/html\n");
client.writeFully("\n");
client.writeFully("<HTML>");
client.writeFully("<HEAD>");
client.writeFully("<TITLE>Sprinklers</TITLE>");
//client.println("<meta http-equiv=\"refresh\" content=\"3\" />"); //this is lame with updates every 3 seconds
client.writeFully("</HEAD>");
client.writeFully("<BODY>");
client.writeFully("<font size=\"10\">");
client.writeFully("<H1>sprinklerServer</H1>");
//client.printf("<FORM ACTION=\"http://%u.%u.%u.%u:80\" method=get >", ip[0], ip[1], ip[2], ip[3]);
char buff[60];
sprintf(buff, "<FORM ACTION=\"http://%u.%u.%u.%u:80\" method=get >", ip[0], ip[1], ip[2], ip[3]);
//String ths=buff; //to check the bubber length
//Serial.printf("\nths.length() is = %d\n", ths.length());
client.writeFully(buff);
if(gledon){
client.writeFully("The LED is ON");
client.writeFully("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"TURN LED OFF\" style=\"font-size: 70px\">");
}else{
client.writeFully("The LED is OFF");
client.writeFully("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"TURN LED ON\" style=\"font-size: 70px\">");
}
client.writeFully("<BR>");
if(gfvegon){
client.writeFully("The front vegetable sprinkler is ON");
client.writeFully("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"TURN VEGETABLE OFF\" style=\"font-size: 70px\">");
}else{
client.writeFully("The front vegetable sprinkler is OFF");
client.writeFully("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"TURN VEGETABLE ON\" style=\"font-size: 70px\">");
}
client.writeFully("<BR>");
if(gflwron){
client.writeFully("The front flower sprinkler is ON");
client.writeFully("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"TURN FLOWER OFF\" style=\"font-size: 70px\">");
}else{
client.writeFully("The front flower sprinkler is OFF");
client.writeFully("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"TURN FLOWER ON\" style=\"font-size: 70px\">");
}
client.writeFully("<BR>");
if(grgrgon){
client.writeFully("The rear sprinkler by garage is ON");
client.writeFully("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"TURN BY GARAGE OFF\" style=\"font-size: 70px\">");
}else{
client.writeFully("The rear sprinkler by garage is OFF");
client.writeFully("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"TURN BY GARAGE ON\" style=\"font-size: 70px\">");
}
client.writeFully("<BR>");
if(gwallon){
client.writeFully("The rear sprinkler by wall is ON");
client.writeFully("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"TURN BY WALL OFF\" style=\"font-size: 70px\">");
}else{
client.writeFully("The rear sprinkler by wall is OFF");
client.writeFully("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"TURN BY WALL ON\" style=\"font-size: 70px\">");
}
client.writeFully("<BR>");
client.writeFully("</FORM>");
//client.println("Changes immediately, but page <BR> update requires another press");
//client.printf("file: %s", __FILE__ ); //this works but prints to internet: /Users/michaelrunyan/Doc... not good
//client.println("<BR><BR>");
//client.printf("temperature: %0.2f ºC", tempmonGetTemp());
client.writeFully("<BR>");
client.writeFully("</font>");
client.writeFully("</BODY>");
client.writeFully("</HTML>");
client.flush();
delay(1);
//stopping client
client.stop();
//clearing string for next read
readString="";
}
}
}
}
}