Created
June 7, 2019 15:54
-
-
Save Redth/b4b5684063d94fa23b91120e7aa5b5ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266mDNS.h> // Include the mDNS library | |
const char* ssid = "JJAGD"; | |
const char* password = ""; | |
// Assign output variables to GPIO pins | |
const int pinHeater = 4; | |
const int pinPump1 = 5; | |
const int pinPump2 = 12; | |
const int pinPump3 = 14; | |
// Set web server port number to 80 | |
WiFiServer server(80); | |
// Variable to store the HTTP request | |
String header; | |
int heaterState = 0; | |
int pumpState = 0; | |
double tempState = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(pinHeater, OUTPUT); | |
pinMode(pinPump1, OUTPUT); | |
pinMode(pinPump2, OUTPUT); | |
pinMode(pinPump3, OUTPUT); | |
// Relays are closed HIGH | |
digitalWrite(pinHeater, HIGH); | |
digitalWrite(pinPump1, HIGH); | |
digitalWrite(pinPump2, HIGH); | |
digitalWrite(pinPump3, HIGH); | |
// Connect to Wi-Fi network with SSID and password | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
// Print local IP address and start web server | |
Serial.println(""); | |
Serial.println("WiFi connected."); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
// Begin mDNS so we can discover the device on the network from the client | |
if (MDNS.begin("poolduino")) { | |
MDNS.addService("http", "tcp", 80); | |
} | |
server.begin(); | |
} | |
// HTTP Paths | |
// /heater/0 | |
// /heater/1 | |
// /pump/0 | |
// /pump/1 | |
// /pump/2 | |
// /pump/3 | |
// JSON Response | |
// { "heater" : 0, "pump" : 0, "temp" : 82.1 } | |
void loop() { | |
// Update mDNS in case something changes (Like our IP) | |
MDNS.update(); | |
WiFiClient client = server.available(); // Listen for incoming clients | |
if (client) { // If a new client connects, | |
Serial.println("New Client."); // print a message out in the serial port | |
String currentLine = ""; // make a String to hold incoming data from the client | |
while (client.connected()) { // loop while the client's connected | |
if (client.available()) { // if there's bytes to read from the client, | |
char c = client.read(); // read a byte, then | |
Serial.write(c); // print it out the serial monitor | |
header += c; | |
if (c == '\n') { // if the byte is a newline character | |
// if the current line is blank, you got two newline characters in a row. | |
// that's the end of the client HTTP request, so send a response: | |
if (currentLine.length() == 0) { | |
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) | |
// and a content-type so the client knows what's coming, then a blank line: | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-type: application/json"); | |
client.println("Connection: close"); | |
client.println(); | |
bool heaterChanged = false; | |
bool pumpChanged = false; | |
// turns the GPIOs on and off | |
if (header.indexOf("GET /heater/0") >= 0) { | |
heaterState = 0; | |
heaterChanged = true; | |
} else if (header.indexOf("GET /heater/1") >= 0) { | |
heaterState = 1; | |
heaterChanged = true; | |
} else if (header.indexOf("GET /pump/0") >= 0) { | |
pumpState = 0; | |
pumpChanged = true; | |
} else if (header.indexOf("GET /pump/1") >= 0) { | |
pumpState = 1; | |
pumpChanged = true; | |
} else if (header.indexOf("GET /pump/2") >= 0) { | |
pumpState = 2; | |
pumpChanged = true; | |
} else if (header.indexOf("GET /pump/3") >= 0) { | |
pumpState = 3; | |
pumpChanged = true; | |
} | |
if (heaterChanged) { | |
Serial.println("Heater Changed to: " + heaterState); | |
digitalWrite(pinHeater, heaterState == 1 ? LOW : HIGH); | |
} | |
if (pumpChanged) { | |
Serial.println("Pump Changed to: " + pumpState); | |
// First make sure all pump relays are off | |
digitalWrite(pinPump1, HIGH); | |
delay(250); | |
digitalWrite(pinPump2, HIGH); | |
delay(250); | |
digitalWrite(pinPump3, HIGH); | |
delay(250); | |
// Now turn the correct one on: | |
if (pumpState == 1) { | |
digitalWrite(pinPump1, LOW); | |
} else if (pumpState == 2) { | |
digitalWrite(pinPump2, LOW); | |
} else if (pumpState == 3) { | |
digitalWrite(pinPump3, LOW); | |
} | |
} | |
// Print back JSON to the client | |
client.println("{ \"heater\" : " + heaterState + ", \"pump\" : " + pumpState + ", \"temp\" : " + tempState + " } "); | |
// The HTTP response ends with another blank line | |
client.println(); | |
// Break out of the while loop | |
break; | |
} else { // if you got a newline, then clear currentLine | |
currentLine = ""; | |
} | |
} else if (c != '\r') { // if you got anything else but a carriage return character, | |
currentLine += c; // add it to the end of the currentLine | |
} | |
} | |
} | |
// Clear the header variable | |
header = ""; | |
// Close the connection | |
client.stop(); | |
Serial.println("Client disconnected."); | |
Serial.println(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment