Аз съм боднал едно nodemcu на село да си пускам помпата да поливам райграса... кода иска малко оптимизиране но работи без грешка, ако на някой му трябва да си го ползва със здраве!
Код: Избери всички
#include <ESP8266WiFi.h> //nodecmu
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(23509); //web server port
#include <Simplepush.h> //phone notifications
#include "Countimer.h" //timer library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //check your I2C LCD address, disply pins: D4(GPIO2)-SDA, D3(GPIO0)-SCL
Simplepush simple;
Countimer mainTimer;
Countimer dryRunTimer;
byte clockIcon[8] = {
0b00000,
0b11111,
0b11111,
0b01110,
0b00100,
0b01110,
0b11111,
0b11111
};
int pump = D1; // pump pin
int led = D2; // button led indication pin
int button = D5; // manual button pin
int sensor = D6; // water flow sensor YF-S201
boolean timerOff = false;
boolean isStarted = false;
boolean manualMode = false; // operating mode variable
boolean state = LOW; // variable for first status (app mode)
boolean lastState;
boolean stateManual = LOW; // variable for first status (manual mode)
boolean sendOnceManualMode = false; // send notifications on manual mode
boolean water; // output water
unsigned long revs;
int freq;
String header; // app response data
char key[] = "your key"; // Simplepush simple.send("HuxgBB", "title", "message", "event");
char token[] = "passkey!"; //andriod app token
const char* ssid = "your netwrk name"; // Name of WiFi Network
const char* password = "your nework pass"; // Password of WiFi Network
void setup() {
Wire.begin(2, 0); //SDA, SCL
lcd.init();
lcd.backlight();
lcd.createChar(0, clockIcon);
mainTimer.setCounter(0, 30, 0, mainTimer.COUNT_DOWN, mainTimerComplete);//max time to run the pump 30 min
dryRunTimer.setCounter(0, 0, 10, dryRunTimer.COUNT_DOWN, NoWaterComplete);//max time to run the pump dry 10sec
mainTimer.setInterval(print_time, 1000);//printing max time to run on LCD
dryRunTimer.setInterval(print_time, 1000);//printing dry run timer on LCD
pinMode(button, INPUT);// initialize the pushbutton pin as an input:
pinMode(sensor, INPUT);// initialize the sensor pin as an input:
pinMode(pump, OUTPUT); // initialize the pump pin as an output:
pinMode(led, OUTPUT); // initialize the pushbutton led as an output:
WiFi.begin(ssid, password);// connect to WiFi
delay(3000);
lcd.setCursor(0, 0);
lcd.print("System starting!");
lcd.setCursor(0, 1);
lcd.print("Please wait... ");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Initialising ");
lcd.setCursor(0, 1);
lcd.print("connection ");
delay(2000);
lcd.clear();
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
lcd.setCursor(0, 0);
lcd.print("Connecting to ");
lcd.setCursor(0, 1);
lcd.print(ssid );
delay(500);
i++;
if (i == 10) {
break;
}
}
lcd.clear();
if (WiFi.status() == WL_CONNECTED) {
simple.send(key, "Connected", "to WiFi network", "Pump");
lcd.setCursor(0, 0);
lcd.print("Connected");
lcd.setCursor(0, 1);
lcd.print("IP: ");
lcd.setCursor(4, 1);
lcd.print(WiFi.localIP());
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("All OK! System");
lcd.setCursor(0, 1);
lcd.print("is now running");
}
else {
lcd.setCursor(0, 0);
lcd.print("WiFi Status:");
lcd.setCursor(0, 1);
lcd.print("Failed!");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System running");
lcd.setCursor(0, 1);
lcd.print("without network");
}
delay(2000);
lcd.clear();
server.on("/", handleRoot);
server.begin();
}//setup ends here
void handleRoot() {
if (server.hasArg("token") && server.hasArg("action") ) {
if (server.arg("token") == token && server.arg("action") == "getStatus") {
header = "HTTP/1.1 200 OK\r\n";
header += "Server: Hapachi/2.2.15 (Hubuntu Linux OS)\r\n";
header += "Content-Type: application/json\r\n\r\n";
header += "{\"Pump\":\"";
header += (state == HIGH || stateManual == HIGH) ? "On" : "Off";
header += "\",\"Remaining time\":\"";
if (stateManual == HIGH) {
header += "Until manual off!";
}
else if (state == HIGH) {
header += mainTimer.getCurrentTime();
}
else {
header += "None";
}
header += "\",\"Water leaking\":\"";
header += (water == true) ? "Yes" : "No";
header += "\"}";
}
if (server.arg("token") == token && server.arg("action") == "toggle") {
if (manualMode == true ) {
stateManual = LOW;
simple.send(key, "Pump is off", "Manual mode is now off, you are good to go!", "Pump");
}
else {
state = !state;
if (state == LOW) {
simple.send(key, "Pump is off", "You are good to go!", "Pump");
}
}
header = "HTTP/1.1 200 OK\r\n";
header += "Server: Hapachi/2.2.15 (Hubuntu Linux OS)\r\n";
header += "Content-Type: application/json\r\n\r\n";
header += "{\"Pump\":\"";
header += (state == HIGH || stateManual == HIGH) ? "On" : "Off";
header += "\",\"Remaining time\":\"";
header += (state == HIGH) ? mainTimer.getCurrentTime() : "None";
header += "\",\"Water leaking\":\"";
header += "Check later";
header += "\"}";
}
}
else {
header = "HTTP/1.1 301 OK\r\nLocation: " + server.header("Referer"); +"\r\nCache-Control: no-cache\r\n\r\n";
}
server.sendContent(header);
}
void mainTimerComplete() {// do when max running time elapsed
timerOff = false;
isStarted = false;
mainTimer.stop();
dryRunTimer.stop();
digitalWrite(pump, LOW);
digitalWrite(led, LOW);
state = !state;
lcd.setCursor(0, 1);
lcd.print("Time ran out ");
simple.send(key, "Pump is off", "Time ran out", "Pump");
delay(4000);
}
void NoWaterComplete() {// do when dry running timer elapsed
timerOff = false;
isStarted = false;
mainTimer.stop();
dryRunTimer.stop();
digitalWrite(pump, LOW);
digitalWrite(led, LOW);
state = !state;
lcd.setCursor(0, 1);
lcd.print("Stopped no water");
simple.send(key, "Pump is off!", "Stopped no water", "Pump");
delay(4000);
}
void print_time() {}
void loop() {
server.handleClient();
mainTimer.run();
dryRunTimer.run();
//manual mode start
int reading = digitalRead(button);
if ( reading == LOW && lastState == HIGH ) {
delay (10); // solve the switch bounce
if (digitalRead(button) == LOW) {
stateManual = !stateManual;
}
}
lastState = reading;
if (stateManual == HIGH ) {
manualMode = true;
if (state == HIGH) {
state = LOW;
manualMode = false;
stateManual = LOW;
}
else {
digitalWrite(pump, HIGH);
digitalWrite(led, HIGH);
lcd.setCursor(0, 0);
lcd.print("Manual mode ");
lcd.setCursor(0, 1);
lcd.print("Stop when done! ");
if (sendOnceManualMode == false) {
simple.send(key, "Pump is on!", "Will remain on until manual stop only!", "Pump");//send it once
sendOnceManualMode = true;
}
}
}
if (stateManual == LOW ) {
manualMode = false;
if (sendOnceManualMode == true) {
simple.send(key, "Pump is off!", "Stopped manualy!", "Pump");//send it on manual stop
}
sendOnceManualMode = false;
}//manual mode end
//app mode start
if (state == LOW && manualMode == false) {
digitalWrite(pump, LOW);
digitalWrite(led, LOW);
timerOff = false;
isStarted = false;
mainTimer.stop();
dryRunTimer.stop();
lcd.setCursor(0, 0);
lcd.print("System is ready ");
lcd.setCursor(0, 1);
lcd.print("to start! ");
}
if (state == HIGH && manualMode == false) {
digitalWrite(pump, HIGH);
digitalWrite(led, HIGH);
lcd.setCursor(0, 0);
lcd.print("Switching off...");
lcd.setCursor(0, 1);
if (timerOff == false) {
mainTimer.start();
timerOff = true;
simple.send(key, "Pump is on!", "Will remain on until manual stop or after 30 min.", "Pump");
}
//dry timer for sensor start
revs = pulseIn(sensor, HIGH);
if (revs > 0) {
freq = 1000000 / revs;
if (freq > 0 && freq < 1000) {
water = true;
}
}
else {
water = false;
}
lcd.print(char(0));
lcd.print(" ");
if (water == false) {
if (isStarted == false) {
dryRunTimer.start();
isStarted == true;
}
lcd.print(dryRunTimer.getCurrentTime());
}
if (water == true) {//restart sensor timer
dryRunTimer.restart();
isStarted = false;
lcd.print(mainTimer.getCurrentTime());
}
lcd.setCursor(10, 1);
lcd.print(" left ");
//dry timer for sensor end
}//app mode end
}