Skip to content

Instantly share code, notes, and snippets.

@adrianhopebailie
Created March 18, 2021 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianhopebailie/da669b6a6ea3119dfc2e9862c42eb05b to your computer and use it in GitHub Desktop.
Save adrianhopebailie/da669b6a6ea3119dfc2e9862c42eb05b to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#define SENSOR A0
#define DIGI_SENSOR D5
#define PUMPOUT D6
/* Set these to your desired credentials. */
const char *ssid = ""; //Enter your WIFI ssid
const char *password = ""; //Enter your WIFI password
ESP8266WebServer server(80);
int PUMP_STATE = 0;
void handleRoot() {
String pumpOutput = (PUMP_STATE > 0) ? "true" : "false";
String digitalOutput = (digitalRead(DIGI_SENSOR) > 0) ? "true" : "false";
String analogOutput = String(analogRead(SENSOR));
String body = "{\"analog\":" + analogOutput + ",\"digital\":" + digitalOutput + ",\"pumping\":" + pumpOutput + "}";
server.send(200, "application/json", body);
}
void handlePumpOn() {
Serial.println("Pump: ON");
PUMP_STATE = 1;
digitalWrite(PUMPOUT, true);
handleRoot();
}
void handlePumpOff() {
Serial.println("Pump: OFF");
PUMP_STATE = 0;
digitalWrite(PUMPOUT, false);
handleRoot();
}
void setup() {
pinMode(SENSOR, INPUT);
pinMode(DIGI_SENSOR, INPUT);
pinMode(PUMPOUT, OUTPUT);
delay(3000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.on ( "/", handleRoot );
server.on ("/pump-on", handlePumpOn );
server.on ("/pump-off", handlePumpOff );
server.begin();
Serial.println ( "HTTP server started" );
}
void loop() {
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment