sensor schema, irrigation project
This commit is contained in:
47
irrigation/src/WebContent.h
Normal file
47
irrigation/src/WebContent.h
Normal file
@@ -0,0 +1,47 @@
|
||||
// HTML web page
|
||||
const char index_html[] = R"rawliteral(
|
||||
<!DOCTYPE HTML><html>
|
||||
<head>
|
||||
<title>Greenhousino Irrigation System</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<style>
|
||||
body { font-family: Arial; text-align: center; margin:0px auto; padding-top: 30px;}
|
||||
.button {
|
||||
padding: 10px 20px;
|
||||
font-size: 24px;
|
||||
text-align: center;
|
||||
outline: none;
|
||||
color: #fff;
|
||||
background-color: #2f4468;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
box-shadow: 0 6px #999;
|
||||
cursor: pointer;
|
||||
-webkit-touch-callout: none;
|
||||
-webkit-user-select: none;
|
||||
-khtml-user-select: none;
|
||||
-moz-user-select: none;
|
||||
-ms-user-select: none;
|
||||
user-select: none;
|
||||
-webkit-tap-highlight-color: rgba(0,0,0,0);
|
||||
}
|
||||
.button:hover {background-color: #1f2e45}
|
||||
.button:active {
|
||||
background-color: #1f2e45;
|
||||
box-shadow: 0 4px #666;
|
||||
transform: translateY(2px);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Greenhousino Irrigation System</h1>
|
||||
<button class="button" onmousedown="toggleCheckbox('on');" ontouchstart="toggleCheckbox('on');" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">PUMP!</button>
|
||||
<script>
|
||||
function toggleCheckbox(x) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", "/" + x, true);
|
||||
xhr.send();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>)rawliteral";
|
||||
101
irrigation/src/main.cpp
Normal file
101
irrigation/src/main.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
||||
|
||||
//needed for library
|
||||
#include <DNSServer.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include "WiFiManager.h" //https://github.com/tzapu/WiFiManager
|
||||
|
||||
#include "WebContent.h"
|
||||
|
||||
#define RelaisPin D4 // Relais
|
||||
#define SwitchPin D1 // Active Low, Switch between D1 and GND
|
||||
|
||||
#define R_ON 0// active Low = 0, otherwise 1
|
||||
#define R_OFF 1// active Low = 1, otherwise 0
|
||||
|
||||
ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
|
||||
|
||||
void handleRoot(); // function prototypes for HTTP handlers
|
||||
void handleOn();
|
||||
void handleOff();
|
||||
void handleNotFound();
|
||||
|
||||
void configModeCallback(WiFiManager *myWiFiManager)
|
||||
{
|
||||
Serial.println("Entered config mode");
|
||||
Serial.println(WiFi.softAPIP());
|
||||
//if you used auto generated SSID, print it
|
||||
Serial.println(myWiFiManager->getConfigPortalSSID());
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(9600);
|
||||
|
||||
pinMode(RelaisPin, OUTPUT);
|
||||
pinMode(SwitchPin, INPUT_PULLUP);
|
||||
digitalWrite(RelaisPin, R_OFF);
|
||||
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wifiManager;
|
||||
//reset settings - for testing
|
||||
//wifiManager.resetSettings();
|
||||
|
||||
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
|
||||
wifiManager.setAPCallback(configModeCallback);
|
||||
|
||||
//fetches ssid and pass and tries to connect
|
||||
//if it does not connect it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
if (!wifiManager.autoConnect())
|
||||
{
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
ESP.reset();
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
|
||||
server.on("/", handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
|
||||
server.on("/on", handleOn); // Call the 'handleRoot' function when a client requests URI "/"
|
||||
server.on("/off", handleOff); // Call the 'handleRoot' function when a client requests URI "/"
|
||||
|
||||
|
||||
server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
|
||||
|
||||
server.begin(); // Actually start the server
|
||||
Serial.println("HTTP server started");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
server.handleClient(); // Listen for HTTP requests from clients
|
||||
}
|
||||
|
||||
void handleRoot()
|
||||
{
|
||||
server.send(200, "text/html", index_html);
|
||||
}
|
||||
|
||||
void handleOn()
|
||||
{
|
||||
digitalWrite(RelaisPin, R_ON);
|
||||
server.send(200, "text/plain", "on");
|
||||
|
||||
}
|
||||
|
||||
void handleOff()
|
||||
{
|
||||
digitalWrite(RelaisPin, R_OFF);
|
||||
server.send(200, "text/plain", "off");
|
||||
}
|
||||
|
||||
void handleNotFound()
|
||||
{
|
||||
server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
|
||||
}
|
||||
Reference in New Issue
Block a user